Skip to content

Package: EMFFormsKeyAttributeDMRExpander

EMFFormsKeyAttributeDMRExpander

nameinstructionbranchcomplexitylinemethod
EMFFormsKeyAttributeDMRExpander()
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: 10 C: 8
44%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 4 C: 3
43%
M: 0 C: 1
100%
deactivate(BundleContext)
M: 0 C: 12
100%
M: 1 C: 1
50%
M: 1 C: 1
50%
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: 26 C: 92
78%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 4 C: 24
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-2016 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.keyattribute;
15:
16: import java.util.List;
17:
18: import org.eclipse.core.databinding.property.value.IValueProperty;
19: import org.eclipse.emf.ecore.EObject;
20: import org.eclipse.emf.ecore.EReference;
21: import org.eclipse.emf.ecore.EStructuralFeature;
22: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
23: import org.eclipse.emf.ecp.view.spi.keyattributedmr.model.VKeyAttributeDomainModelReference;
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.databinding.DatabindingFailedException;
30: import org.eclipse.emfforms.spi.core.services.databinding.EMFFormsDatabinding;
31: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander;
32: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDomainExpander;
33: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsExpandingFailedException;
34: import org.osgi.framework.BundleContext;
35: import org.osgi.framework.ServiceReference;
36: import org.osgi.service.component.annotations.Activate;
37: import org.osgi.service.component.annotations.Component;
38: import org.osgi.service.component.annotations.Deactivate;
39: import org.osgi.service.component.annotations.Reference;
40:
41: /**
42: * An {@link EMFFormsDMRExpander} for {@link VKeyAttributeDomainModelReference VKeyAttributeDomainModelReferences}.
43: *
44: * @author Lucas Koehler
45: *
46: */
47: @Component(name = "EMFFormsKeyAttributeDMRExpander")
48: public class EMFFormsKeyAttributeDMRExpander implements EMFFormsDMRExpander {
49:
50:         private ReportService reportService;
51:         private EMFFormsDomainExpander domainExpander;
52:         private EMFFormsDatabinding databindingService;
53:         private BundleContext bundleContext;
54:         private ServiceReference<EMFFormsDomainExpander> emfFormsDomainExpanderServiceReference;
55:
56:         /**
57:          * Called by the framework to set the {@link ReportService}.
58:          *
59:          * @param reportService The {@link ReportService}
60:          */
61:         @Reference(unbind = "-")
62:         protected void setReportService(ReportService reportService) {
63:                 this.reportService = reportService;
64:
65:         }
66:
67:         /**
68:          * Called by the framework when the component gets activated.
69:          *
70:          * @param bundleContext The {@link BundleContext}
71:          */
72:         @Activate
73:         protected void activate(BundleContext bundleContext) {
74:                 this.bundleContext = bundleContext;
75:         }
76:
77:         /**
78:          * Called by the framework when the component gets deactivated.
79:          *
80:          * @param bundleContext The {@link BundleContext}
81:          */
82:         @Deactivate
83:         protected void deactivate(BundleContext bundleContext) {
84:•                if (emfFormsDomainExpanderServiceReference != null) {
85:                         bundleContext.ungetService(emfFormsDomainExpanderServiceReference);
86:                         domainExpander = null;
87:                 }
88:         }
89:
90:         private EMFFormsDomainExpander getEMFFormsDomainExpander() {
91:•                if (domainExpander == null) {
92:                         emfFormsDomainExpanderServiceReference = bundleContext.getServiceReference(EMFFormsDomainExpander.class);
93:•                        if (emfFormsDomainExpanderServiceReference == null) {
94:                                 throw new IllegalStateException("No EMFFormsDomainExpander available!"); //$NON-NLS-1$
95:                         }
96:                         domainExpander = bundleContext.getService(emfFormsDomainExpanderServiceReference);
97:                 }
98:                 return domainExpander;
99:         }
100:
101:         /**
102:          * Helper method for tests. This is quite ugly!
103:          *
104:          * @param domainExpander The EMFFormsDomainExpander to use
105:          */
106:         void setEMFFormsDomainExpander(EMFFormsDomainExpander domainExpander) {
107:                 this.domainExpander = domainExpander;
108:         }
109:
110:         /**
111:          * Called by the framework to set the {@link EMFFormsDatabinding}.
112:          *
113:          * @param emfFormsDatabinding The {@link EMFFormsDatabinding}
114:          */
115:         @Reference(unbind = "-")
116:         protected void setEMFFormsDatabinding(EMFFormsDatabinding emfFormsDatabinding) {
117:                 databindingService = emfFormsDatabinding;
118:         }
119:
120:         /**
121:          * {@inheritDoc}
122:          *
123:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander#prepareDomainObject(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
124:          * org.eclipse.emf.ecore.EObject)
125:          */
126:         @Override
127:         public void prepareDomainObject(VDomainModelReference domainModelReference, EObject domainObject)
128:                 throws EMFFormsExpandingFailedException {
129:                 Assert.create(domainModelReference).notNull();
130:                 Assert.create(domainObject).notNull();
131:                 Assert.create(domainModelReference).ofClass(VKeyAttributeDomainModelReference.class);
132:
133:                 final VKeyAttributeDomainModelReference keyAttributeDMR = VKeyAttributeDomainModelReference.class
134:                         .cast(domainModelReference);
135:
136:                 // Create a new feature path dmr to only expand the feature path part of the key attribute dmr.
137:                 final VFeaturePathDomainModelReference featurePathDMR = VViewFactory.eINSTANCE
138:                         .createFeaturePathDomainModelReference();
139:                 featurePathDMR.setDomainModelEFeature(keyAttributeDMR.getDomainModelEFeature());
140:                 featurePathDMR.getDomainModelEReferencePath().addAll(keyAttributeDMR.getDomainModelEReferencePath());
141:
142:                 getEMFFormsDomainExpander().prepareDomainObject(featurePathDMR, domainObject);
143:
144:                 IValueProperty valueProperty;
145:                 try {
146:                         valueProperty = databindingService.getValueProperty(featurePathDMR, domainObject);
147:                 } catch (final DatabindingFailedException ex) {
148:                         throw new EMFFormsExpandingFailedException(
149:                                 "Domain Expansion failed due to a failed databinding: " + ex.getMessage()); //$NON-NLS-1$
150:                 }
151:
152:                 final EStructuralFeature listEStructuralFeature = (EStructuralFeature) valueProperty.getValueType();
153:
154:                 checkListType(listEStructuralFeature);
155:
156:                 // need unchecked type cast because IValueProperty#getValue always returns an Object.
157:                 @SuppressWarnings("unchecked")
158:                 final List<EObject> eObjectsList = (List<EObject>) valueProperty.getValue(domainObject);
159:
160:                 /*
161:                  * Iterate over all EObjects of the list referenced by the feature path DMR part of the key attribute DMR.
162:                  * Check for every EObject whether the key DMR references the key value defined by the key attribute DMR.
163:                  * If this is the case, expand the value DMR for this EObject.
164:                  */
165:•                for (final EObject targetDomainObject : eObjectsList) {
166:                         IValueProperty keyValueProperty;
167:                         try {
168:                                 keyValueProperty = databindingService.getValueProperty(keyAttributeDMR.getKeyDMR(),
169:                                         targetDomainObject);
170:                         } catch (final DatabindingFailedException ex) {
171:                                 throw new EMFFormsExpandingFailedException(
172:                                         "Domain Expansion failed due to an invalid key attribute dmr: " + ex.getMessage()); //$NON-NLS-1$
173:                         }
174:                         final Object potentialKey = keyValueProperty.getValue(targetDomainObject);
175:
176:                         final boolean foundKey = keyAttributeDMR.getKeyValue().equals(potentialKey);
177:•                        if (foundKey) {
178:                                 domainExpander.prepareDomainObject(keyAttributeDMR.getValueDMR(), targetDomainObject);
179:                         }
180:                 }
181:         }
182:
183:         /**
184:          * Checks whether the given structural feature references a list of {@link EObject EObjects}.
185:          *
186:          * @param structuralFeature The feature to check
187:          * @throws EMFFormsExpandingFailedException if the structural feature doesn't reference a list of {@link EObject
188:          * EObjects}.
189:          */
190:         private void checkListType(EStructuralFeature structuralFeature) throws EMFFormsExpandingFailedException {
191:•                if (!structuralFeature.isMany()) {
192:                         throw new EMFFormsExpandingFailedException(
193:                                 "The VKeyAttributeDomainModelReference's domainModelEFeature must reference a list."); //$NON-NLS-1$
194:                 }
195:•                if (!EReference.class.isInstance(structuralFeature)) {
196:                         throw new EMFFormsExpandingFailedException(
197:                                 "The VKeyAttributeDomainModelReference's domainModelEFeature must reference a list of EObjects."); //$NON-NLS-1$
198:                 }
199:         }
200:
201:         /**
202:          * {@inheritDoc}
203:          *
204:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander#isApplicable(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference)
205:          */
206:         @Override
207:         public double isApplicable(VDomainModelReference domainModelReference) {
208:•                if (domainModelReference == null) {
209:                         reportService.report(new AbstractReport("Warning: The given domain model reference was null.")); //$NON-NLS-1$
210:                         return NOT_APPLICABLE;
211:                 }
212:•                if (VKeyAttributeDomainModelReference.class.isInstance(domainModelReference)) {
213:                         return 5d;
214:                 }
215:                 return NOT_APPLICABLE;
216:         }
217:
218: }