Skip to content

Package: EMFFormsIndexDMRExpander

EMFFormsIndexDMRExpander

nameinstructionbranchcomplexitylinemethod
EMFFormsIndexDMRExpander()
M: 0 C: 3
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
activate(BundleContext)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
checkListType(EStructuralFeature)
M: 5 C: 13
72%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 2 C: 5
71%
M: 0 C: 1
100%
deactivate(BundleContext)
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getEMFFormsDomainExpander()
M: 5 C: 23
82%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
isApplicable(VDomainModelReference)
M: 0 C: 21
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 6
100%
M: 0 C: 1
100%
prepareDomainObject(VDomainModelReference, EObject)
M: 16 C: 130
89%
M: 4 C: 10
71%
M: 4 C: 4
50%
M: 5 C: 32
86%
M: 0 C: 1
100%
setEMFFormsDatabinding(EMFFormsDatabinding)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setEMFFormsDomainExpander(EMFFormsDomainExpander)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
setReportService(ReportService)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2015 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.internal.core.services.domainexpander.index;
15:
16: import java.util.List;
17:
18: import org.eclipse.core.databinding.property.value.IValueProperty;
19: import org.eclipse.emf.ecore.EClass;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.EReference;
22: import org.eclipse.emf.ecore.EStructuralFeature;
23: import org.eclipse.emf.ecore.util.EcoreUtil;
24: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
25: import org.eclipse.emf.ecp.view.spi.indexdmr.model.VIndexDomainModelReference;
26: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
27: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
28: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
29: import org.eclipse.emfforms.spi.common.report.AbstractReport;
30: import org.eclipse.emfforms.spi.common.report.ReportService;
31: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
32: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
33: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander;
34: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDomainExpander;
35: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsExpandingFailedException;
36: import org.osgi.framework.BundleContext;
37: import org.osgi.framework.ServiceReference;
38: import org.osgi.service.component.annotations.Activate;
39: import org.osgi.service.component.annotations.Component;
40: import org.osgi.service.component.annotations.Deactivate;
41: import org.osgi.service.component.annotations.Reference;
42:
43: /**
44: * An {@link EMFFormsDMRExpander} for {@link VIndexDomainModelReference VIndexDomainModelReferences}.
45: *
46: * @author Lucas Koehler
47: *
48: */
49: @Component(name = "EMFFormsIndexDMRExpander")
50: public class EMFFormsIndexDMRExpander implements EMFFormsDMRExpander {
51:
52:         private ReportService reportService;
53:         private EMFFormsDomainExpander domainExpander;
54:         private EMFFormsDatabinding databindingService;
55:         private BundleContext bundleContext;
56:         private ServiceReference<EMFFormsDomainExpander> eMFFormsDomainExpanderServiceReference;
57:
58:         /**
59:          * Called by the framework to set the {@link ReportService}.
60:          *
61:          * @param reportService The {@link ReportService}
62:          */
63:         @Reference(unbind = "-")
64:         protected void setReportService(ReportService reportService) {
65:                 this.reportService = reportService;
66:
67:         }
68:
69:         /**
70:          * Called by the framework when the component gets activated.
71:          *
72:          * @param bundleContext The {@link BundleContext}
73:          */
74:         @Activate
75:         protected void activate(BundleContext bundleContext) {
76:                 this.bundleContext = bundleContext;
77:         }
78:
79:         /**
80:          * Called by the framework when the component gets deactivated.
81:          *
82:          * @param bundleContext The {@link BundleContext}
83:          */
84:         @Deactivate
85:         protected void deactivate(BundleContext bundleContext) {
86:•                if (eMFFormsDomainExpanderServiceReference != null) {
87:                         bundleContext.ungetService(eMFFormsDomainExpanderServiceReference);
88:                         domainExpander = null;
89:                 }
90:         }
91:
92:         private EMFFormsDomainExpander getEMFFormsDomainExpander() {
93:•                if (domainExpander == null) {
94:                         eMFFormsDomainExpanderServiceReference = bundleContext.getServiceReference(EMFFormsDomainExpander.class);
95:•                        if (eMFFormsDomainExpanderServiceReference == null) {
96:                                 throw new IllegalStateException("No EMFFormsDomainExpander available!"); //$NON-NLS-1$
97:                         }
98:                         domainExpander = bundleContext.getService(eMFFormsDomainExpanderServiceReference);
99:                 }
100:                 return domainExpander;
101:         }
102:
103:         /**
104:          * Helper method for tests. This is quite ugly!
105:          *
106:          * @param domainExpander The EMFFormsDomainExpander to use
107:          */
108:         void setEMFFormsDomainExpander(EMFFormsDomainExpander domainExpander) {
109:                 this.domainExpander = domainExpander;
110:         }
111:
112:         /**
113:          * Called by the framework to set the {@link EMFFormsDatabinding}.
114:          *
115:          * @param emfFormsDatabinding The {@link EMFFormsDatabinding}
116:          */
117:         @Reference(unbind = "-")
118:         protected void setEMFFormsDatabinding(EMFFormsDatabinding emfFormsDatabinding) {
119:                 databindingService = emfFormsDatabinding;
120:         }
121:
122:         /**
123:          * {@inheritDoc}
124:          *
125:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander#prepareDomainObject(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
126:          * org.eclipse.emf.ecore.EObject)
127:          */
128:         @Override
129:         public void prepareDomainObject(VDomainModelReference domainModelReference, EObject domainObject)
130:                 throws EMFFormsExpandingFailedException {
131:                 Assert.create(domainModelReference).notNull();
132:                 Assert.create(domainObject).notNull();
133:                 Assert.create(domainModelReference).ofClass(VIndexDomainModelReference.class);
134:
135:                 final VIndexDomainModelReference indexReference = VIndexDomainModelReference.class.cast(domainModelReference);
136:                 final VDomainModelReference targetDMR = indexReference.getTargetDMR();
137:
138:                 VDomainModelReference prefixDMR;
139:•                if (indexReference.getPrefixDMR() != null) {
140:                         prefixDMR = indexReference.getPrefixDMR();
141:                 } else {
142:•                        if (indexReference.getDomainModelEFeature() == null) {
143:                                 throw new EMFFormsExpandingFailedException(
144:                                         "The index domain model reference's domain model e feature must not be null when the reference's prefix dmr is null."); //$NON-NLS-1$
145:                         }
146:                         final VFeaturePathDomainModelReference prefixFeatureDMR = VViewFactory.eINSTANCE
147:                                 .createFeaturePathDomainModelReference();
148:                         prefixFeatureDMR.setDomainModelEFeature(indexReference.getDomainModelEFeature());
149:                         prefixFeatureDMR.getDomainModelEReferencePath().addAll(indexReference.getDomainModelEReferencePath());
150:                         prefixDMR = prefixFeatureDMR;
151:                 }
152:
153:                 IValueProperty valueProperty;
154:                 try {
155:                         valueProperty = databindingService.getValueProperty(prefixDMR, domainObject);
156:                 } catch (final DatabindingFailedException ex) {
157:                         throw new EMFFormsExpandingFailedException(
158:                                 "Domain Expansion failed due to a failed databinding: " + ex.getMessage()); //$NON-NLS-1$
159:                 }
160:
161:                 final EStructuralFeature listEStructuralFeature = (EStructuralFeature) valueProperty.getValueType();
162:
163:                 checkListType(listEStructuralFeature);
164:                 getEMFFormsDomainExpander().prepareDomainObject(prefixDMR, domainObject);
165:
166:                 // type of the list items
167:                 final EClass featureEClass = EClass.class.cast(listEStructuralFeature.getEType());
168:
169:                 // need unchecked type cast because IValueProperty#getValue always returns an Object.
170:                 @SuppressWarnings("unchecked")
171:                 final List<EObject> list = (List<EObject>) valueProperty.getValue(domainObject);
172:                 EObject targetDomainObject;
173:                 // fill up all indices of the list with new instances up to the index defined by the index dmr.
174:•                if (!featureEClass.isAbstract() && !featureEClass.isInterface()) {
175:•                        for (int i = 0; i <= indexReference.getIndex(); i++) {
176:•                                if (list.size() <= i) {
177:                                         list.add(EcoreUtil.create(featureEClass));
178:•                                } else if (list.get(i) == null) {
179:                                         list.set(i, EcoreUtil.create(featureEClass));
180:                                 }
181:                         }
182:                         targetDomainObject = list.get(indexReference.getIndex());
183:                 } else {
184:                         throw new EMFFormsExpandingFailedException(
185:                                 "Could not expand the VIndexDomainModelReference because the type of the prefix DMR's domain model e feature is either abstract or an interface. Hence, no new instances of this type can be created."); //$NON-NLS-1$
186:                 }
187:
188:                 getEMFFormsDomainExpander().prepareDomainObject(targetDMR, targetDomainObject);
189:         }
190:
191:         /**
192:          * {@inheritDoc}
193:          *
194:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander#isApplicable(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference)
195:          */
196:         @Override
197:         public double isApplicable(VDomainModelReference domainModelReference) {
198:•                if (domainModelReference == null) {
199:                         reportService.report(new AbstractReport("Warning: The given domain model reference was null.")); //$NON-NLS-1$
200:                         return NOT_APPLICABLE;
201:                 }
202:•                if (VIndexDomainModelReference.class.isInstance(domainModelReference)) {
203:                         return 5d;
204:                 }
205:                 return NOT_APPLICABLE;
206:         }
207:
208:         /**
209:          * Checks whether the given structural feature references a proper list.
210:          *
211:          * @param structuralFeature The feature to check
212:          * @throws EMFFormsExpandingFailedException if the structural feature doesn't reference a proper list.
213:          */
214:         private void checkListType(EStructuralFeature structuralFeature) throws EMFFormsExpandingFailedException {
215:•                if (!structuralFeature.isMany()) {
216:                         throw new EMFFormsExpandingFailedException(
217:                                 "The VIndexDomainModelReference's domainModelEFeature must reference a list."); //$NON-NLS-1$
218:                 }
219:•                if (!EReference.class.isInstance(structuralFeature)) {
220:                         throw new EMFFormsExpandingFailedException(
221:                                 "The VIndexDomainModelReference's domainModelEFeature must reference a list of EObjects."); //$NON-NLS-1$
222:                 }
223:         }
224: }