Skip to content

Package: FeatureSegmentConverter

FeatureSegmentConverter

nameinstructionbranchcomplexitylinemethod
FeatureSegmentConverter()
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: 10 C: 21
68%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 2 C: 6
75%
M: 0 C: 1
100%
convertToListProperty(VDomainModelReferenceSegment, EClass, EditingDomain)
M: 0 C: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
convertToValueProperty(VDomainModelReferenceSegment, EClass, EditingDomain)
M: 0 C: 31
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
findStructuralFeature(EClass, VFeatureDomainModelReferenceSegment)
M: 0 C: 28
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 10
100%
M: 0 C: 1
100%
getSetting(VDomainModelReferenceSegment, EObject)
M: 29 C: 22
43%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 5 C: 6
55%
M: 0 C: 1
100%
isApplicable(VDomainModelReferenceSegment)
M: 0 C: 8
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$0(String, EClass)
M: 0 C: 4
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
lambda$2(VFeatureDomainModelReferenceSegment, EClass)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 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.featurepath;
15:
16: import java.util.Objects;
17: import java.util.Optional;
18:
19: import org.eclipse.emf.databinding.edit.EMFEditProperties;
20: import org.eclipse.emf.databinding.edit.IEMFEditListProperty;
21: import org.eclipse.emf.databinding.edit.IEMFEditValueProperty;
22: import org.eclipse.emf.ecore.EClass;
23: import org.eclipse.emf.ecore.EObject;
24: import org.eclipse.emf.ecore.EReference;
25: import org.eclipse.emf.ecore.EStructuralFeature;
26: import org.eclipse.emf.ecore.EStructuralFeature.Setting;
27: import org.eclipse.emf.ecore.InternalEObject;
28: import org.eclipse.emf.ecp.common.spi.EMFUtils;
29: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
30: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
31: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
32: import org.eclipse.emf.edit.domain.EditingDomain;
33: import org.eclipse.emfforms.internal.core.services.databinding.SegmentConverterListResultImpl;
34: import org.eclipse.emfforms.internal.core.services.databinding.SegmentConverterValueResultImpl;
35: import org.eclipse.emfforms.spi.core.services.databinding.DatabindingFailedException;
36: import org.eclipse.emfforms.spi.core.services.databinding.emf.DomainModelReferenceSegmentConverterEMF;
37: import org.eclipse.emfforms.spi.core.services.databinding.emf.SegmentConverterListResultEMF;
38: import org.eclipse.emfforms.spi.core.services.databinding.emf.SegmentConverterValueResultEMF;
39: import org.osgi.service.component.annotations.Component;
40:
41: /**
42: * A {@link DomainModelReferenceSegmentConverterEMF} for {@link VFeatureDomainModelReferenceSegment
43: * VFeatureDomainModelReferenceSegments}.
44: *
45: * @author Lucas Koehler
46: *
47: */
48: @Component(name = "FeatureSegmentConverter")
49: @SuppressWarnings("restriction")
50: public class FeatureSegmentConverter implements DomainModelReferenceSegmentConverterEMF {
51:
52:         @Override
53:         public double isApplicable(VDomainModelReferenceSegment segment) {
54:•                if (VFeatureDomainModelReferenceSegment.class.isInstance(segment)) {
55:                         return 1d;
56:                 }
57:                 return NOT_APPLICABLE;
58:         }
59:
60:         @Override
61:         public SegmentConverterValueResultEMF convertToValueProperty(VDomainModelReferenceSegment segment,
62:                 EClass segmentRoot,
63:                 EditingDomain editingDomain) throws DatabindingFailedException {
64:                 final VFeatureDomainModelReferenceSegment featureSegment = checkAndConvertSegment(segment);
65:
66:                 final EStructuralFeature structuralFeature = findStructuralFeature(segmentRoot, featureSegment);
67:                 final IEMFEditValueProperty valueProperty = EMFEditProperties.value(editingDomain, structuralFeature);
68:•                if (EReference.class.isInstance(structuralFeature)) {
69:                         return new SegmentConverterValueResultImpl(valueProperty,
70:                                 ((EReference) structuralFeature).getEReferenceType());
71:                 }
72:                 return new SegmentConverterValueResultImpl(valueProperty, null);
73:         }
74:
75:         @Override
76:         public SegmentConverterListResultEMF convertToListProperty(VDomainModelReferenceSegment segment, EClass segmentRoot,
77:                 EditingDomain editingDomain) throws DatabindingFailedException {
78:                 final VFeatureDomainModelReferenceSegment featureSegment = checkAndConvertSegment(segment);
79:
80:                 final EStructuralFeature structuralFeature = findStructuralFeature(segmentRoot, featureSegment);
81:
82:                 final IEMFEditListProperty listProperty = EMFEditProperties.list(editingDomain, structuralFeature);
83:•                if (EReference.class.isInstance(structuralFeature)) {
84:                         return new SegmentConverterListResultImpl(listProperty,
85:                                 ((EReference) structuralFeature).getEReferenceType());
86:                 }
87:                 return new SegmentConverterListResultImpl(listProperty, null);
88:         }
89:
90:         @Override
91:         public Setting getSetting(VDomainModelReferenceSegment segment, EObject eObject) throws DatabindingFailedException {
92:                 final VFeatureDomainModelReferenceSegment featureSegment = checkAndConvertSegment(segment);
93:
94:                 final EStructuralFeature structuralFeature = eObject.eClass()
95:                         .getEStructuralFeature(featureSegment.getDomainModelFeature());
96:•                if (structuralFeature == null) {
97:                         throw new DatabindingFailedException(String.format(
98:                                 "The given EOject does not contain the segment's feature. The segment was %1$s. The EObject was %2$s.", //$NON-NLS-1$
99:                                 segment, eObject));
100:                 }
101:•                if (structuralFeature.getEType() == null) {
102:                         throw new DatabindingFailedException(
103:                                 String.format("The eType of the feature %1$s is null.", structuralFeature.getName())); //$NON-NLS-1$
104:                 }
105:
106:                 return InternalEObject.class.cast(eObject).eSetting(structuralFeature);
107:         }
108:
109:         private EStructuralFeature findStructuralFeature(EClass segmentRoot, VFeatureDomainModelReferenceSegment segment)
110:                 throws DatabindingFailedException {
111:                 final String featureName = segment.getDomainModelFeature();
112:                 EStructuralFeature structuralFeature = segmentRoot.getEStructuralFeature(featureName);
113:•                if (structuralFeature == null) {
114:                         // If the feature is not present in the segment's root EClass, try to find it in one of its sub classes
115:                         final Optional<EStructuralFeature> feature = EMFUtils.getSubClasses(segmentRoot).stream()
116:                                 .map(c -> c.getEStructuralFeature(featureName))
117:                                 .filter(Objects::nonNull)
118:                                 .findFirst();
119:
120:                         structuralFeature = feature.orElseThrow(
121:                                 () -> new DatabindingFailedException(String.format(
122:                                         "The segment's feature could not be resolved for the given EClass. The segment was %1$s. The EClass was %2$s.", //$NON-NLS-1$
123:                                         segment, segmentRoot)));
124:                 }
125:                 return structuralFeature;
126:         }
127:
128:         private VFeatureDomainModelReferenceSegment checkAndConvertSegment(VDomainModelReferenceSegment segment)
129:                 throws DatabindingFailedException {
130:                 Assert.create(segment).notNull();
131:                 Assert.create(segment).ofClass(VFeatureDomainModelReferenceSegment.class);
132:
133:                 final VFeatureDomainModelReferenceSegment featureSegment = (VFeatureDomainModelReferenceSegment) segment;
134:
135:•                if (featureSegment.getDomainModelFeature() == null) {
136:                         throw new DatabindingFailedException("The segment's domain model feature must not be null."); //$NON-NLS-1$
137:                 }
138:•                if (featureSegment.getDomainModelFeature().isEmpty()) {
139:                         throw new DatabindingFailedException("The segment's domain model feature must not be an empty string."); //$NON-NLS-1$
140:                 }
141:
142:                 return featureSegment;
143:         }
144: }