Skip to content

Package: EMFFormsMappingDMRExpander

EMFFormsMappingDMRExpander

nameinstructionbranchcomplexitylinemethod
EMFFormsMappingDMRExpander()
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%
checkMapType(EStructuralFeature)
M: 25 C: 50
67%
M: 7 C: 9
56%
M: 7 C: 2
22%
M: 10 C: 12
55%
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: 5 C: 92
95%
M: 2 C: 4
67%
M: 2 C: 2
50%
M: 2 C: 21
91%
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.mapping;
15:
16: import org.eclipse.emf.common.util.EMap;
17: import org.eclipse.emf.ecore.EClass;
18: import org.eclipse.emf.ecore.EObject;
19: import org.eclipse.emf.ecore.EReference;
20: import org.eclipse.emf.ecore.EStructuralFeature;
21: import org.eclipse.emf.ecore.util.EcoreUtil;
22: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
23: import org.eclipse.emf.ecp.view.spi.mappingdmr.model.VMappingDomainModelReference;
24: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
25: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
26: import org.eclipse.emf.ecp.view.spi.model.VViewFactory;
27: import org.eclipse.emfforms.spi.common.report.AbstractReport;
28: import org.eclipse.emfforms.spi.common.report.ReportService;
29: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander;
30: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDomainExpander;
31: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsExpandingFailedException;
32: import org.osgi.framework.BundleContext;
33: import org.osgi.framework.ServiceReference;
34: import org.osgi.service.component.annotations.Activate;
35: import org.osgi.service.component.annotations.Component;
36: import org.osgi.service.component.annotations.Deactivate;
37: import org.osgi.service.component.annotations.Reference;
38:
39: /**
40: * An {@link EMFFormsDMRExpander} for expanding {@link VMappingDomainModelReference VMappingDomainModelReferences}.
41: *
42: * @author Lucas Koehler
43: *
44: */
45: @Component(name = "EMFFormsMappingDMRExpander")
46: public class EMFFormsMappingDMRExpander implements EMFFormsDMRExpander {
47:
48:         private ReportService reportService;
49:         private EMFFormsDomainExpander domainExpander;
50:         private BundleContext bundleContext;
51:         private ServiceReference<EMFFormsDomainExpander> eMFFormsDomainExpanderServiceReference;
52:
53:         /**
54:          * Called by the framework to set the {@link ReportService}.
55:          *
56:          * @param reportService The {@link ReportService}
57:          */
58:         @Reference(unbind = "-")
59:         protected void setReportService(ReportService reportService) {
60:                 this.reportService = reportService;
61:
62:         }
63:
64:         /**
65:          * Called by the framework when the component gets activated.
66:          *
67:          * @param bundleContext The {@link BundleContext}
68:          */
69:         @Activate
70:         protected void activate(BundleContext bundleContext) {
71:                 this.bundleContext = bundleContext;
72:         }
73:
74:         /**
75:          * Called by the framework when the component gets deactivated.
76:          *
77:          * @param bundleContext The {@link BundleContext}
78:          */
79:         @Deactivate
80:         protected void deactivate(BundleContext bundleContext) {
81:•                if (eMFFormsDomainExpanderServiceReference != null) {
82:                         bundleContext.ungetService(eMFFormsDomainExpanderServiceReference);
83:                         domainExpander = null;
84:                 }
85:         }
86:
87:         private EMFFormsDomainExpander getEMFFormsDomainExpander() {
88:•                if (domainExpander == null) {
89:                         eMFFormsDomainExpanderServiceReference = bundleContext.getServiceReference(EMFFormsDomainExpander.class);
90:•                        if (eMFFormsDomainExpanderServiceReference == null) {
91:                                 throw new IllegalStateException("No EMFFormsDomainExpander available!"); //$NON-NLS-1$
92:                         }
93:                         domainExpander = bundleContext.getService(eMFFormsDomainExpanderServiceReference);
94:                 }
95:                 return domainExpander;
96:         }
97:
98:         /**
99:          * Helper method for tests. This is quite ugly!
100:          *
101:          * @param domainExpander The EMFFormsDomainExpander to use
102:          */
103:         void setEMFFormsDomainExpander(EMFFormsDomainExpander domainExpander) {
104:                 this.domainExpander = domainExpander;
105:         }
106:
107:         /**
108:          * {@inheritDoc}
109:          *
110:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander#prepareDomainObject(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
111:          * org.eclipse.emf.ecore.EObject)
112:          */
113:         @Override
114:         public void prepareDomainObject(VDomainModelReference domainModelReference, EObject domainObject)
115:                 throws EMFFormsExpandingFailedException {
116:
117:                 Assert.create(domainModelReference).notNull();
118:                 Assert.create(domainObject).notNull();
119:                 Assert.create(domainModelReference).ofClass(VMappingDomainModelReference.class);
120:
121:                 final VMappingDomainModelReference mappingReference = (VMappingDomainModelReference) domainModelReference;
122:
123:•                if (mappingReference.getDomainModelEFeature() == null) {
124:                         throw new EMFFormsExpandingFailedException(
125:                                 "The mapping domain model reference's domain model e feature must not be null."); //$NON-NLS-1$
126:                 }
127:                 checkMapType(mappingReference.getDomainModelEFeature());
128:
129:                 // expand first part of the mapping dmr. A new reference needs to be created because we only want to expand the
130:                 // first part of the mapping dmr in this step.
131:                 final VFeaturePathDomainModelReference firstReference = VViewFactory.eINSTANCE
132:                         .createFeaturePathDomainModelReference();
133:                 firstReference.setDomainModelEFeature(mappingReference.getDomainModelEFeature());
134:                 firstReference.getDomainModelEReferencePath().addAll(mappingReference.getDomainModelEReferencePath());
135:                 getEMFFormsDomainExpander().prepareDomainObject(firstReference, domainObject);
136:
137:                 // get the EObject that contains the map
138:                 EObject eObject = domainObject;
139:•                for (final EReference eRef : firstReference.getDomainModelEReferencePath()) {
140:                         eObject = (EObject) domainObject.eGet(eRef);
141:                 }
142:
143:                 // Need unchecked conversion because EObject#eGet always returns an Object
144:                 @SuppressWarnings("unchecked")
145:                 final EMap<EClass, EObject> map = (EMap<EClass, EObject>) eObject.eGet(mappingReference
146:                         .getDomainModelEFeature());
147:•                if (!map.containsKey(mappingReference.getMappedClass())) {
148:                         map.put(mappingReference.getMappedClass(), EcoreUtil.create(mappingReference.getMappedClass()));
149:                 }
150:                 final EObject mappedEObject = map.get(mappingReference.getMappedClass());
151:
152:                 // expand second part of the mapping dmr
153:                 getEMFFormsDomainExpander().prepareDomainObject(mappingReference.getDomainModelReference(), mappedEObject);
154:         }
155:
156:         /**
157:          * {@inheritDoc}
158:          *
159:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander#isApplicable(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference)
160:          */
161:         @Override
162:         public double isApplicable(VDomainModelReference domainModelReference) {
163:•                if (domainModelReference == null) {
164:                         reportService.report(new AbstractReport("Warning: The given domain model reference was null.")); //$NON-NLS-1$
165:                         return NOT_APPLICABLE;
166:                 }
167:•                if (VMappingDomainModelReference.class.isInstance(domainModelReference)) {
168:                         return 5d;
169:                 }
170:                 return NOT_APPLICABLE;
171:         }
172:
173:         /**
174:          * Checks whether the given structural feature references a proper map.
175:          *
176:          * @param structuralFeature The feature to check
177:          * @throws EMFFormsExpandingFailedException if the structural feature doesn't reference a proper map.
178:          */
179:         private void checkMapType(EStructuralFeature structuralFeature) throws EMFFormsExpandingFailedException {
180:•                if (!structuralFeature.getEType().getInstanceClassName().equals("java.util.Map$Entry")) { //$NON-NLS-1$
181:                         throw new EMFFormsExpandingFailedException(
182:                                 "The VMappingDomainModelReference's domainModelEFeature must reference a map."); //$NON-NLS-1$
183:                 }
184:•                if (structuralFeature.getLowerBound() != 0 || structuralFeature.getUpperBound() != -1) {
185:                         throw new EMFFormsExpandingFailedException(
186:                                 "The VMappingDomainModelReference's domainModelEFeature must reference a map."); //$NON-NLS-1$
187:                 }
188:
189:                 final EClass eClass = (EClass) structuralFeature.getEType();
190:                 final EStructuralFeature keyFeature = eClass.getEStructuralFeature("key"); //$NON-NLS-1$
191:                 final EStructuralFeature valueFeature = eClass.getEStructuralFeature("value"); //$NON-NLS-1$
192:•                if (keyFeature == null || valueFeature == null) {
193:                         throw new EMFFormsExpandingFailedException(
194:                                 "The VMappingDomainModelReference's domainModelEFeature must reference a map."); //$NON-NLS-1$
195:                 }
196:•                if (!EReference.class.isInstance(valueFeature)) {
197:                         throw new EMFFormsExpandingFailedException(
198:                                 "The values of the map referenced by the VMappingDomainModelReference's domainModelEFeature must be referenced EObjects."); //$NON-NLS-1$
199:                 }
200:•                if (!EReference.class.isInstance(keyFeature)) {
201:                         throw new EMFFormsExpandingFailedException(
202:                                 "The keys of the map referenced by the VMappingDomainModelReference's domainModelEFeature must be referenced EClasses."); //$NON-NLS-1$
203:                 }
204:•                if (!EClass.class.isAssignableFrom(((EReference) keyFeature).getEReferenceType().getInstanceClass())) {
205:                         throw new EMFFormsExpandingFailedException(
206:                                 "The keys of the map referenced by the VMappingDomainModelReference's domainModelEFeature must be referenced EClasses."); //$NON-NLS-1$
207:                 }
208:         }
209:
210: }