Skip to content

Package: FeaturePathDomainModelReferenceConverter

FeaturePathDomainModelReferenceConverter

nameinstructionbranchcomplexitylinemethod
FeaturePathDomainModelReferenceConverter()
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%
checkAndConvertDMR(VDomainModelReference)
M: 0 C: 93
100%
M: 0 C: 12
100%
M: 0 C: 7
100%
M: 0 C: 20
100%
M: 0 C: 1
100%
convertToListProperty(VDomainModelReference, EObject)
M: 0 C: 46
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 9
100%
M: 0 C: 1
100%
convertToValueProperty(VDomainModelReference, EClass, EditingDomain)
M: 0 C: 92
100%
M: 0 C: 8
100%
M: 0 C: 5
100%
M: 0 C: 17
100%
M: 0 C: 1
100%
convertToValueProperty(VDomainModelReference, EObject)
M: 0 C: 10
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
getEditingDomain(EObject)
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%
getSetting(VDomainModelReference, EObject)
M: 0 C: 166
100%
M: 0 C: 12
100%
M: 0 C: 7
100%
M: 0 C: 27
100%
M: 0 C: 1
100%
identify(ENamedElement)
M: 2 C: 99
98%
M: 2 C: 10
83%
M: 2 C: 5
71%
M: 1 C: 24
96%
M: 0 C: 1
100%
identify(EObject)
M: 2 C: 52
96%
M: 2 C: 6
75%
M: 2 C: 3
60%
M: 1 C: 14
93%
M: 0 C: 1
100%
isApplicable(VDomainModelReference)
M: 0 C: 14
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 5
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 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: * Christian W. Damus - bug 553224
14: ******************************************************************************/
15: package org.eclipse.emfforms.core.services.databinding.featurepath;
16:
17: import java.util.List;
18:
19: import org.eclipse.emf.common.util.URI;
20: import org.eclipse.emf.databinding.IEMFListProperty;
21: import org.eclipse.emf.databinding.IEMFValueProperty;
22: import org.eclipse.emf.databinding.edit.EMFEditProperties;
23: import org.eclipse.emf.ecore.EClass;
24: import org.eclipse.emf.ecore.ENamedElement;
25: import org.eclipse.emf.ecore.EObject;
26: import org.eclipse.emf.ecore.EPackage;
27: import org.eclipse.emf.ecore.EReference;
28: import org.eclipse.emf.ecore.EStructuralFeature;
29: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
30: import org.eclipse.emf.ecore.InternalEObject;
31: import org.eclipse.emf.ecore.resource.Resource;
32: import org.eclipse.emf.ecore.util.EcoreUtil;
33: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
34: import org.eclipse.emf.ecp.view.spi.model.VFeaturePathDomainModelReference;
35: import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
36: import org.eclipse.emf.edit.domain.EditingDomain;
37: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
38: import org.eclipse.emfforms.spi.core.services.databinding.emf.DomainModelReferenceConverterEMF;
39:
40: /**
41: * An implementation of {@link DomainModelReferenceConverterEMF} that converts {@link VFeaturePathDomainModelReference}
42: * s.
43: *
44: * @author Lucas Koehler
45: *
46: */
47: public class FeaturePathDomainModelReferenceConverter implements DomainModelReferenceConverterEMF {
48:
49:         @Override
50:         public double isApplicable(VDomainModelReference domainModelReference) {
51:•                if (domainModelReference == null) {
52:                         throw new IllegalArgumentException("The given VDomainModelReference must not be null."); //$NON-NLS-1$
53:                 }
54:•                if (domainModelReference instanceof VFeaturePathDomainModelReference) {
55:                         return 0;
56:                 }
57:                 return NOT_APPLICABLE;
58:         }
59:
60:         private VFeaturePathDomainModelReference checkAndConvertDMR(VDomainModelReference domainModelReference)
61:                 throws DatabindingFailedException {
62:•                if (domainModelReference == null) {
63:                         throw new IllegalArgumentException("The given VDomainModelReference must not be null."); //$NON-NLS-1$
64:                 }
65:•                if (!VFeaturePathDomainModelReference.class.isInstance(domainModelReference)) {
66:                         throw new IllegalArgumentException(
67:                                 String.format("DomainModelReference %1$s is not an instance of VFeaturePathDomainModelReference.", //$NON-NLS-1$
68:                                         identify(domainModelReference)));
69:                 }
70:
71:                 final VFeaturePathDomainModelReference featurePathReference = (VFeaturePathDomainModelReference) domainModelReference;
72:•                if (featurePathReference.getDomainModelEFeature() == null) {
73:                         throw new DatabindingFailedException(
74:                                 "The field domainModelEFeature of the given VFeaturePathDomainModelReference must not be null."); //$NON-NLS-1$
75:                 }
76:•                if (featurePathReference.getDomainModelEFeature().eIsProxy()) {
77:                         throw new DatabindingFailedException(
78:                                 String.format("The domainModelEFeature %1$s of the given DMR %2$s is a proxy.", //$NON-NLS-1$
79:                                         identify(featurePathReference.getDomainModelEFeature()), identify(featurePathReference)));
80:                 }
81:•                for (final EReference path : featurePathReference.getDomainModelEReferencePath()) {
82:•                        if (path.eIsProxy()) {
83:                                 throw new DatabindingFailedException(
84:                                         String.format("The path reference %1$s of the given DMR %2$s is a proxy.", //$NON-NLS-1$
85:                                                 identify(path), identify(featurePathReference)));
86:                         }
87:                 }
88:                 return featurePathReference;
89:         }
90:
91:         @Override
92:         public IEMFValueProperty convertToValueProperty(VDomainModelReference domainModelReference, EObject object)
93:                 throws DatabindingFailedException {
94:
95:                 final EditingDomain editingDomain = getEditingDomain(object);
96:                 return convertToValueProperty(domainModelReference, null, editingDomain);
97:         }
98:
99:         @Override
100:         public IEMFValueProperty convertToValueProperty(VDomainModelReference domainModelReference, EClass rootEClass,
101:                 EditingDomain editingDomain) throws DatabindingFailedException {
102:
103:                 final VFeaturePathDomainModelReference featurePathReference = checkAndConvertDMR(domainModelReference);
104:                 final List<EReference> referencePath = featurePathReference.getDomainModelEReferencePath();
105:•                if (referencePath.isEmpty()) {
106:                         return EMFEditProperties.value(editingDomain, featurePathReference.getDomainModelEFeature());
107:                 }
108:
109:•                if (referencePath.get(0).isMany()) {
110:                         throw new DatabindingFailedException(String.format(
111:                                 "The path is not fully resolved. The reference being resolved is not a single reference: %1$s. The DMR is %2$s.", //$NON-NLS-1$
112:                                 identify(referencePath.get(0)), identify(domainModelReference)));
113:                 }
114:                 IEMFValueProperty emfValueProperty = EMFEditProperties.value(editingDomain, referencePath.get(0));
115:•                for (int i = 1; i < referencePath.size(); i++) {
116:                         final EReference eReference = referencePath.get(i);
117:•                        if (eReference.isMany()) {
118:                                 throw new DatabindingFailedException(String.format(
119:                                         "The path is not fully resolved. The reference being resolved is not a single reference: %1$s. The DMR is %2$s.", //$NON-NLS-1$
120:                                         identify(eReference), identify(domainModelReference)));
121:                         }
122:                         emfValueProperty = emfValueProperty.value(eReference);
123:                 }
124:                 return emfValueProperty.value(featurePathReference.getDomainModelEFeature());
125:         }
126:
127:         private EditingDomain getEditingDomain(EObject object) throws DatabindingFailedException {
128:                 return AdapterFactoryEditingDomain.getEditingDomainFor(object);
129:         }
130:
131:         @Override
132:         public IEMFListProperty convertToListProperty(VDomainModelReference domainModelReference, EObject object)
133:                 throws DatabindingFailedException {
134:                 final VFeaturePathDomainModelReference featurePathReference = checkAndConvertDMR(domainModelReference);
135:
136:                 final List<EReference> referencePath = featurePathReference.getDomainModelEReferencePath();
137:                 final EditingDomain editingDomain = getEditingDomain(object);
138:•                if (referencePath.isEmpty()) {
139:                         return EMFEditProperties.list(editingDomain, featurePathReference.getDomainModelEFeature());
140:                 }
141:
142:                 IEMFValueProperty emfValueProperty = EMFEditProperties.value(editingDomain, referencePath.get(0));
143:•                for (int i = 1; i < referencePath.size(); i++) {
144:                         emfValueProperty = emfValueProperty.value(referencePath.get(i));
145:                 }
146:                 return emfValueProperty.list(featurePathReference.getDomainModelEFeature());
147:         }
148:
149:         @Override
150:         public Setting getSetting(VDomainModelReference domainModelReference, EObject object)
151:                 throws DatabindingFailedException {
152:                 final VFeaturePathDomainModelReference featurePathReference = checkAndConvertDMR(domainModelReference);
153:
154:                 EObject currentObject = object;
155:•                for (final EReference eReference : featurePathReference.getDomainModelEReferencePath()) {
156:•                        if (eReference.isMany()) {
157:                                 throw new DatabindingFailedException(String.format(
158:                                         "The path is not fully resolved. The reference being resolved is not a single reference: %1$s. The DMR is %2$s. Last resolved EObject is %3$s.", //$NON-NLS-1$
159:                                         identify(eReference), identify(domainModelReference), identify(currentObject)));
160:                         }
161:•                        if (currentObject.eClass().getFeatureID(eReference) == -1) {
162:                                 throw new DatabindingFailedException(String.format(
163:                                         "Given EClass %1$s has no such feature %2$s. The DMR is %3$s. Last resolved EObject is %4$s.", //$NON-NLS-1$
164:                                         identify(currentObject.eClass()), identify(eReference),
165:                                         identify(domainModelReference), identify(currentObject)));
166:                         }
167:                         final EObject nextObject = (EObject) currentObject.eGet(eReference);
168:•                        if (nextObject == null) {
169:                                 throw new DatabindingFailedException(String.format(
170:                                         "The path is not fully resolved. The DMR is %1$s. Last resolved EObject is %2$s. Reference being resolved is %3$s.", //$NON-NLS-1$
171:                                         identify(domainModelReference), identify(currentObject), identify(eReference)));
172:                         }
173:                         currentObject = nextObject;
174:                 }
175:                 final EStructuralFeature structuralFeature = featurePathReference.getDomainModelEFeature();
176:•                if (structuralFeature.getEType() == null) {
177:                         throw new DatabindingFailedException(
178:                                 String.format("The eType of the feature %1$s is null in DMR %2$s.", identify(structuralFeature), //$NON-NLS-1$
179:                                         identify(domainModelReference)));
180:                 }
181:•                if (currentObject.eClass().getEAllStructuralFeatures().contains(structuralFeature)) {
182:                         return InternalEObject.class.cast(currentObject).eSetting(structuralFeature);
183:                 }
184:                 throw new DatabindingFailedException(String.format("The resolved EObject %1$s doesn't have the feature %2$s.", //$NON-NLS-1$
185:                         identify(currentObject), identify(structuralFeature)));
186:         }
187:
188:         private static String identify(EObject eObject) {
189:•                if (eObject == null) {
190:                         return "<null>"; //$NON-NLS-1$
191:                 }
192:
193:                 final StringBuilder result = new StringBuilder();
194:
195:                 // First, a trimmed variant of the to-string representation (omit Java package)
196:                 result.append(eObject.toString());
197:                 final int hashCodePos = result.indexOf("@"); //$NON-NLS-1$
198:•                if (hashCodePos > 0) {
199:                         final int lastDotPos = result.lastIndexOf(".", hashCodePos); //$NON-NLS-1$
200:•                        if (lastDotPos > 0) {
201:                                 result.delete(0, lastDotPos + 1);
202:                         }
203:                 }
204:
205:                 final Resource resource = eObject.eResource();
206:•                if (resource != null) {
207:                         result.append('<');
208:                         result.append(EcoreUtil.getURI(eObject));
209:                         result.append('>');
210:                 }
211:
212:                 return result.toString();
213:         }
214:
215:         private static String identify(ENamedElement element) {
216:•                if (element == null) {
217:                         return "<null>"; //$NON-NLS-1$
218:                 }
219:
220:                 final StringBuilder result = new StringBuilder();
221:
222:•                if (element.eIsProxy()) {
223:                         // Infer as much as possible
224:                         final URI proxyURI = EcoreUtil.getURI(element);
225:
226:                         // Get the qualified ENamedElement name
227:                         String fragment = proxyURI.fragment();
228:•                        while (!fragment.isEmpty() && fragment.charAt(0) == '/') {
229:                                 fragment = fragment.substring(1);
230:                         }
231:                         result.append(fragment.replace('/', '.'));
232:
233:                         // Get the package namespace (or Ecore path)
234:                         result.append(" ("); //$NON-NLS-1$
235:                         result.append(proxyURI.trimFragment());
236:                         result.append(')');
237:                 } else {
238:                         result.append(element.getName());
239:
240:•                        for (EObject container = element.eContainer(); container instanceof ENamedElement; container = container
241:                                 .eContainer()) {
242:
243:                                 final ENamedElement parent = (ENamedElement) container;
244:
245:•                                if (parent instanceof EPackage) {
246:                                         result.append(" ("); //$NON-NLS-1$
247:                                         result.append(((EPackage) container).getNsURI());
248:                                         result.append(')');
249:                                 } else {
250:                                         result.insert(0, '.');
251:                                         result.insert(0, parent.getName());
252:                                 }
253:                         }
254:                 }
255:
256:                 return result.toString();
257:         }
258:
259: }