Skip to content

Package: FeatureSegmentExpander

FeatureSegmentExpander

nameinstructionbranchcomplexitylinemethod
FeatureSegmentExpander()
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%
isApplicable(VDomainModelReferenceSegment)
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%
needsToExpandLastSegment()
M: 0 C: 2
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
prepareDomainObject(VDomainModelReferenceSegment, EObject)
M: 0 C: 98
100%
M: 1 C: 9
90%
M: 1 C: 5
83%
M: 0 C: 25
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-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.Optional;
17:
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.model.VDomainModelReferenceSegment;
24: import org.eclipse.emf.ecp.view.spi.model.VFeatureDomainModelReferenceSegment;
25: import org.eclipse.emfforms.spi.common.report.AbstractReport;
26: import org.eclipse.emfforms.spi.common.report.ReportService;
27: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRSegmentExpander;
28: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsExpandingFailedException;
29: import org.osgi.service.component.annotations.Component;
30: import org.osgi.service.component.annotations.Reference;
31:
32: /**
33: * A {@link EMFFormsDMRSegmentExpander} for {@link VFeatureDomainModelReferenceSegment
34: * VFeatureDomainModelReferenceSegments}.
35: *
36: * @author Lucas Koehler
37: *
38: */
39: @Component(name = "FeatureSegmentExpander")
40: public class FeatureSegmentExpander implements EMFFormsDMRSegmentExpander {
41:
42:         private ReportService reportService;
43:
44:         /**
45:          * Called by the framework to set the {@link ReportService}.
46:          *
47:          * @param reportService The {@link ReportService}
48:          */
49:         @Reference(unbind = "-")
50:         protected void setReportService(ReportService reportService) {
51:                 this.reportService = reportService;
52:
53:         }
54:
55:         /**
56:          * {@inheritDoc}
57:          *
58:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRSegmentExpander#prepareDomainObject(org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment,
59:          * org.eclipse.emf.ecore.EObject)
60:          */
61:         @Override
62:         public Optional<EObject> prepareDomainObject(VDomainModelReferenceSegment segment, EObject domainObject)
63:                 throws EMFFormsExpandingFailedException {
64:                 Assert.create(segment).notNull();
65:                 Assert.create(domainObject).notNull();
66:                 Assert.create(segment).ofClass(VFeatureDomainModelReferenceSegment.class);
67:
68:                 final VFeatureDomainModelReferenceSegment featureSegment = (VFeatureDomainModelReferenceSegment) segment;
69:                 final EStructuralFeature structuralFeature = domainObject.eClass()
70:                         .getEStructuralFeature(featureSegment.getDomainModelFeature());
71:
72:•                if (structuralFeature == null) {
73:                         throw new EMFFormsExpandingFailedException(
74:                                 String.format("The given domain object does not contain the segment's feature. " //$NON-NLS-1$
75:                                         + "The segment was %1$s. The domain object was %2$s.", segment, domainObject)); //$NON-NLS-1$
76:                 }
77:•                if (!EReference.class.isInstance(structuralFeature)) {
78:                         throw new EMFFormsExpandingFailedException(
79:                                 String.format("The feature described by the given segment must be an EReference. " //$NON-NLS-1$
80:                                         + "The segment was %1$s.", segment)); //$NON-NLS-1$
81:                 }
82:
83:                 final EReference reference = (EReference) structuralFeature;
84:
85:                 /*
86:                  * If the reference's target already exists, we do not need to do anything.
87:                  * Otherwise we have to create a new instance.
88:                  */
89:                 EObject child = (EObject) domainObject.eGet(reference);
90:•                if (child == null) {
91:•                        if (!reference.getEReferenceType().isAbstract() && !reference.getEReferenceType().isInterface()) {
92:                                 child = EcoreUtil.create(reference.getEReferenceType());
93:                                 domainObject.eSet(reference, child);
94:                         } else {
95:                                 throw new EMFFormsExpandingFailedException(String.format(
96:                                         "The reference type of the segment's feature is either abstract or an interface. " //$NON-NLS-1$
97:                                                 + "Therefore, no instance can be created. The segment was %1$s.", //$NON-NLS-1$
98:                                         segment));
99:                         }
100:                 }
101:
102:                 return Optional.ofNullable(child);
103:         }
104:
105:         /**
106:          * {@inheritDoc}
107:          *
108:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRSegmentExpander#isApplicable(org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment)
109:          */
110:         @Override
111:         public double isApplicable(VDomainModelReferenceSegment segment) {
112:•                if (segment == null) {
113:                         reportService.report(new AbstractReport("Warning: The given domain model reference segment was null.")); //$NON-NLS-1$
114:                         return NOT_APPLICABLE;
115:                 }
116:•                if (VFeatureDomainModelReferenceSegment.class.isInstance(segment)) {
117:                         return 1d;
118:                 }
119:                 return NOT_APPLICABLE;
120:         }
121:
122:         /**
123:          * {@inheritDoc}
124:          *
125:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRSegmentExpander#needsToExpandLastSegment()
126:          */
127:         @Override
128:         public boolean needsToExpandLastSegment() {
129:                 return false;
130:         }
131:
132: }