Skip to content

Package: IndexSegmentExpander

IndexSegmentExpander

nameinstructionbranchcomplexitylinemethod
IndexSegmentExpander()
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%
checkReferenceInstantiable(VDomainModelReferenceSegment, EReference)
M: 0 C: 21
100%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 0 C: 5
100%
M: 0 C: 1
100%
isApplicable(VDomainModelReferenceSegment)
M: 0 C: 22
100%
M: 0 C: 4
100%
M: 0 C: 3
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
needsToExpandLastSegment()
M: 2 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
prepareDomainObject(VDomainModelReferenceSegment, EObject)
M: 13 C: 126
91%
M: 1 C: 11
92%
M: 1 C: 6
86%
M: 3 C: 30
91%
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.index;
15:
16: import java.util.Optional;
17:
18: import org.eclipse.core.runtime.IStatus;
19: import org.eclipse.emf.common.util.EList;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecore.EReference;
22: import org.eclipse.emf.ecore.EStructuralFeature;
23: import org.eclipse.emf.ecore.util.EcoreUtil;
24: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
25: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReferenceSegment;
26: import org.eclipse.emfforms.spi.common.report.AbstractReport;
27: import org.eclipse.emfforms.spi.common.report.ReportService;
28: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRSegmentExpander;
29: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsExpandingFailedException;
30: import org.eclipse.emfforms.spi.view.indexsegment.model.VIndexDomainModelReferenceSegment;
31: import org.osgi.service.component.annotations.Component;
32: import org.osgi.service.component.annotations.Reference;
33:
34: /**
35: * A {@link EMFFormsDMRSegmentExpander} for {@link VIndexDomainModelReferenceSegment
36: * VIndexDomainModelReferenceSegments}.
37: *
38: * @author Lucas Koehler
39: *
40: */
41: @Component(name = "IndexSegmentExpander")
42: public class IndexSegmentExpander implements EMFFormsDMRSegmentExpander {
43:         private ReportService reportService;
44:
45:         /**
46:          * Called by the framework to set the {@link ReportService}.
47:          *
48:          * @param reportService The {@link ReportService}
49:          */
50:         @Reference(unbind = "-")
51:         protected void setReportService(ReportService reportService) {
52:                 this.reportService = reportService;
53:
54:         }
55:
56:         @Override
57:         @SuppressWarnings("unchecked")
58:         public Optional<EObject> prepareDomainObject(VDomainModelReferenceSegment segment, EObject domainObject)
59:                 throws EMFFormsExpandingFailedException {
60:                 Assert.create(segment).notNull();
61:                 Assert.create(domainObject).notNull();
62:                 Assert.create(segment).ofClass(VIndexDomainModelReferenceSegment.class);
63:
64:                 final VIndexDomainModelReferenceSegment indexSegment = (VIndexDomainModelReferenceSegment) segment;
65:                 final EStructuralFeature structuralFeature = domainObject.eClass()
66:                         .getEStructuralFeature(indexSegment.getDomainModelFeature());
67:
68:•                if (structuralFeature == null) {
69:                         throw new EMFFormsExpandingFailedException(
70:                                 String.format("The given domain object does not contain the segment's feature. " //$NON-NLS-1$
71:                                         + "The segment was %1$s. The domain object was %2$s.", segment, domainObject)); //$NON-NLS-1$
72:                 }
73:•                if (!EReference.class.isInstance(structuralFeature)) {
74:                         throw new EMFFormsExpandingFailedException(
75:                                 String.format("The feature described by the given segment must be an EReference. " //$NON-NLS-1$
76:                                         + "The segment was %1$s.", segment)); //$NON-NLS-1$
77:                 }
78:
79:                 final EReference reference = (EReference) structuralFeature;
80:
81:•                if (!reference.isMany()) {
82:                         throw new EMFFormsExpandingFailedException(
83:                                 String.format("The EReference described by the given index segment must be a multi reference. " //$NON-NLS-1$
84:                                         + "The segment was %1$s.", segment)); //$NON-NLS-1$
85:                 }
86:
87:                 final int index = indexSegment.getIndex();
88:                 final EList<EObject> list = (EList<EObject>) domainObject.eGet(reference);
89:•                if (list.size() <= index) {
90:                         checkReferenceInstantiable(segment, reference);
91:
92:                         // When index == list.size(), one element needs to be created as the index starts at 0
93:                         final int iterations = index - list.size() + 1;
94:•                        for (int i = 0; i < iterations; i++) {
95:                                 final EObject newElement = EcoreUtil.create(reference.getEReferenceType());
96:                                 list.add(newElement);
97:                         }
98:•                } else if (list.get(index) == null) {
99:                         // The list is big enough but the element at the index must not be null
100:                         checkReferenceInstantiable(segment, reference);
101:
102:                         final EObject newElement = EcoreUtil.create(reference.getEReferenceType());
103:                         list.set(index, newElement);
104:                 }
105:                 final EObject target = list.get(indexSegment.getIndex());
106:                 return Optional.ofNullable(target);
107:         }
108:
109:         /**
110:          * Checks that the type of the given {@link EReference} is instantiable.
111:          *
112:          * @param segment The source segment (only for the exception message)
113:          * @param reference The {@link EReference} to check
114:          * @throws EMFFormsExpandingFailedException If the given {@link EReference} is not instantiable
115:          */
116:         private void checkReferenceInstantiable(VDomainModelReferenceSegment segment, final EReference reference)
117:                 throws EMFFormsExpandingFailedException {
118:•                if (reference.getEReferenceType().isAbstract() || reference.getEReferenceType().isInterface()) {
119:                         throw new EMFFormsExpandingFailedException(String.format(
120:                                 "The reference type of the segment's feature is either abstract or an interface. " //$NON-NLS-1$
121:                                         + "Therefore, no instance can be created. The segment was %1$s.", //$NON-NLS-1$
122:                                 segment));
123:                 }
124:         }
125:
126:         @Override
127:         public double isApplicable(VDomainModelReferenceSegment segment) {
128:•                if (segment == null) {
129:                         reportService
130:                                 .report(new AbstractReport("The given domain model reference segment was null.", IStatus.WARNING)); //$NON-NLS-1$
131:                         return NOT_APPLICABLE;
132:                 }
133:•                if (VIndexDomainModelReferenceSegment.class.isInstance(segment)) {
134:                         return 5d;
135:                 }
136:                 return NOT_APPLICABLE;
137:         }
138:
139:         @Override
140:         public boolean needsToExpandLastSegment() {
141:                 return false;
142:         }
143:
144: }