Skip to content

Package: MappingSegmentConverter

MappingSegmentConverter

nameinstructionbranchcomplexitylinemethod
MappingSegmentConverter()
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%
checkAndConvertSegment(VDomainModelReferenceSegment)
M: 15 C: 24
62%
M: 3 C: 3
50%
M: 3 C: 1
25%
M: 3 C: 7
70%
M: 0 C: 1
100%
checkMapType(EStructuralFeature)
M: 15 C: 40
73%
M: 4 C: 6
60%
M: 4 C: 2
33%
M: 6 C: 11
65%
M: 0 C: 1
100%
checkStructuralFeature(EStructuralFeature)
M: 15 C: 26
63%
M: 4 C: 6
60%
M: 4 C: 2
33%
M: 6 C: 7
54%
M: 0 C: 1
100%
convertToListProperty(VDomainModelReferenceSegment, EClass, EditingDomain)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%
convertToValueProperty(VDomainModelReferenceSegment, EClass, EditingDomain)
M: 23 C: 50
68%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 4 C: 13
76%
M: 0 C: 1
100%
getSetting(VDomainModelReferenceSegment, EObject)
M: 16 C: 23
59%
M: 1 C: 1
50%
M: 1 C: 1
50%
M: 3 C: 6
67%
M: 0 C: 1
100%
isApplicable(VDomainModelReferenceSegment)
M: 0 C: 11
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
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.segments.mapping;
15:
16: import org.eclipse.emf.databinding.internal.EMFValuePropertyDecorator;
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.EStructuralFeature.Setting;
22: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
23: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
24: import org.eclipse.emf.edit.domain.EditingDomain;
25: import org.eclipse.emfforms.internal.core.services.databinding.SegmentConverterValueResultImpl;
26: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
27: import org.eclipse.emfforms.spi.core.services.databinding.emf.DomainModelReferenceSegmentConverterEMF;
28: import org.eclipse.emfforms.spi.core.services.databinding.emf.SegmentConverterListResultEMF;
29: import org.eclipse.emfforms.spi.core.services.databinding.emf.SegmentConverterValueResultEMF;
30: import org.eclipse.emfforms.spi.view.mappingsegment.model.VMappingDomainModelReferenceSegment;
31: import org.osgi.service.component.annotations.Component;
32:
33: /**
34: * Converts {@link VMappingDomainModelReferenceSegment VMappingDomainModelReferenceSegments} to value and list
35: * properties, and to {@link Setting settings}.
36: *
37: * @author Lucas Koehler
38: *
39: */
40: @SuppressWarnings("restriction")
41: @Component(name = "MappingSegmentConverter")
42: public class MappingSegmentConverter implements DomainModelReferenceSegmentConverterEMF {
43:
44:         @Override
45:         public double isApplicable(VDomainModelReferenceSegment segment) {
46:                 Assert.create(segment).notNull();
47:•                if (segment instanceof VMappingDomainModelReferenceSegment) {
48:                         return 10d;
49:                 }
50:                 return NOT_APPLICABLE;
51:         }
52:
53:         @Override
54:         public SegmentConverterValueResultEMF convertToValueProperty(VDomainModelReferenceSegment segment,
55:                 EClass segmentRoot, EditingDomain editingDomain) throws DatabindingFailedException {
56:
57:                 final VMappingDomainModelReferenceSegment mappingSegment = checkAndConvertSegment(segment);
58:
59:                 final EStructuralFeature structuralFeature = segmentRoot
60:                         .getEStructuralFeature(mappingSegment.getDomainModelFeature());
61:•                if (structuralFeature == null) {
62:                         throw new DatabindingFailedException(String.format(
63:                                 "The given EOject does not contain the segment's feature. The segment was %1$s. The EObject was %2$s.", //$NON-NLS-1$
64:                                 segment, segmentRoot));
65:                 }
66:                 checkMapType(structuralFeature);
67:
68:                 // no checks necessary as they are already done in checkMapType()
69:                 final EClass eClass = (EClass) structuralFeature.getEType();
70:                 final EReference valueReference = (EReference) eClass.getEStructuralFeature("value"); //$NON-NLS-1$
71:
72:                 final EMFMappingValueProperty mappingProperty = new EMFMappingValueProperty(editingDomain,
73:                         mappingSegment.getMappedClass(), structuralFeature);
74:                 final EMFValuePropertyDecorator resultProperty = new EMFValuePropertyDecorator(mappingProperty,
75:                         structuralFeature);
76:
77:•                if (valueReference.getEReferenceType().isSuperTypeOf(mappingSegment.getMappedClass())) {
78:                         return new SegmentConverterValueResultImpl(resultProperty, mappingSegment.getMappedClass());
79:                 }
80:                 return new SegmentConverterValueResultImpl(resultProperty, valueReference.getEReferenceType());
81:
82:         }
83:
84:         @Override
85:         public SegmentConverterListResultEMF convertToListProperty(VDomainModelReferenceSegment segment, EClass segmentRoot,
86:                 EditingDomain editingDomain) throws DatabindingFailedException {
87:                 throw new UnsupportedOperationException(
88:                         "A VMappingDomainModelReferenceSegment cannot be converted to a list property, only to a value property."); //$NON-NLS-1$
89:         }
90:
91:         @Override
92:         public Setting getSetting(VDomainModelReferenceSegment segment, EObject eObject) throws DatabindingFailedException {
93:
94:                 final VMappingDomainModelReferenceSegment mappingSegment = checkAndConvertSegment(segment);
95:
96:                 final EStructuralFeature structuralFeature = eObject.eClass()
97:                         .getEStructuralFeature(mappingSegment.getDomainModelFeature());
98:•                if (structuralFeature == null) {
99:                         throw new DatabindingFailedException(String.format(
100:                                 "The given EOject does not contain the segment's feature. The segment was %1$s. The EObject was %2$s.", //$NON-NLS-1$
101:                                 segment, eObject));
102:                 }
103:                 checkMapType(structuralFeature);
104:
105:                 return new MappedSetting(eObject, structuralFeature, mappingSegment.getMappedClass());
106:         }
107:
108:         /**
109:          * Checks whether the given segment is a valid {@link VMappingDomainModelReferenceSegment}. If yes, convert and
110:          * return it. If no, throw an exception.
111:          *
112:          * @param segment The segment to check and convert
113:          * @return The converted segment
114:          * @throws DatabindingFailedException If the given segment is not a valid mapping segment
115:          */
116:         private VMappingDomainModelReferenceSegment checkAndConvertSegment(VDomainModelReferenceSegment segment)
117:                 throws DatabindingFailedException {
118:                 Assert.create(segment).notNull();
119:                 Assert.create(segment).ofClass(VMappingDomainModelReferenceSegment.class);
120:
121:                 final VMappingDomainModelReferenceSegment mappingSegment = (VMappingDomainModelReferenceSegment) segment;
122:
123:•                if (mappingSegment.getDomainModelFeature() == null) {
124:                         throw new DatabindingFailedException("The segment's domain model feature must not be null."); //$NON-NLS-1$
125:                 }
126:•                if (mappingSegment.getDomainModelFeature().isEmpty()) {
127:                         throw new DatabindingFailedException("The segment's domain model feature must not be an empty string."); //$NON-NLS-1$
128:                 }
129:•                if (mappingSegment.getMappedClass() == null) {
130:                         throw new DatabindingFailedException("The mapping segment's mapped class must not be null."); //$NON-NLS-1$
131:                 }
132:
133:                 return mappingSegment;
134:         }
135:
136:         /**
137:          * Checks whether the given structural feature references a proper map.
138:          *
139:          * @param structuralFeature The feature to check
140:          * @throws IllegalMapTypeException if the structural feature doesn't reference a proper map.
141:          */
142:         private void checkMapType(EStructuralFeature structuralFeature) throws IllegalMapTypeException {
143:                 checkStructuralFeature(structuralFeature);
144:
145:                 final EClass eClass = (EClass) structuralFeature.getEType();
146:                 final EStructuralFeature keyFeature = eClass.getEStructuralFeature("key"); //$NON-NLS-1$
147:                 final EStructuralFeature valueFeature = eClass.getEStructuralFeature("value"); //$NON-NLS-1$
148:•                if (keyFeature == null || valueFeature == null) {
149:                         throw new IllegalMapTypeException(
150:                                 "The segment's structural feature must reference a map."); //$NON-NLS-1$
151:                 }
152:•                if (!EReference.class.isInstance(keyFeature)) {
153:                         throw new IllegalMapTypeException(
154:                                 "The keys of the map referenced by the segment's structural feature must be referenced EClasses."); //$NON-NLS-1$
155:                 }
156:•                if (!EClass.class.isAssignableFrom(((EReference) keyFeature).getEReferenceType().getInstanceClass())) {
157:                         throw new IllegalMapTypeException(
158:                                 "The keys of the map referenced by the segment's structural feature must be referenced EClasses."); //$NON-NLS-1$
159:                 }
160:•                if (!EReference.class.isInstance(valueFeature)) {
161:                         throw new IllegalMapTypeException(
162:                                 "The values of the map referenced by the segment's structural feature must be referenced EObjects."); //$NON-NLS-1$
163:                 }
164:         }
165:
166:         /**
167:          * Checks basic required properties of the given {@link EStructuralFeature}.
168:          *
169:          * @param structuralFeature The {@link EStructuralFeature} to check
170:          * @throws IllegalMapTypeException if something's wrong with the feature
171:          */
172:         private void checkStructuralFeature(EStructuralFeature structuralFeature) throws IllegalMapTypeException {
173:•                if (structuralFeature.getEType() == null) {
174:                         throw new IllegalMapTypeException(
175:                                 "The EType of the segment's structural feature was null."); //$NON-NLS-1$
176:                 }
177:•                if (structuralFeature.getEType().getInstanceClassName() == null) {
178:                         throw new IllegalMapTypeException(
179:                                 "The InstanceClassName of the segment's structural feature's EType was null."); //$NON-NLS-1$
180:                 }
181:•                if (!structuralFeature.getEType().getInstanceClassName().equals("java.util.Map$Entry")) { //$NON-NLS-1$
182:                         throw new IllegalMapTypeException(
183:                                 "The segment's structural feature must reference a map."); //$NON-NLS-1$
184:                 }
185:•                if (structuralFeature.getLowerBound() != 0 || structuralFeature.getUpperBound() != -1) {
186:                         throw new IllegalMapTypeException(
187:                                 "The segment's structural feature must reference a map."); //$NON-NLS-1$
188:                 }
189:         }
190: }