Skip to content

Package: EMFFormsCustomDMRExpander

EMFFormsCustomDMRExpander

nameinstructionbranchcomplexitylinemethod
EMFFormsCustomDMRExpander()
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%
activate(BundleContext)
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%
deactivate(BundleContext)
M: 0 C: 12
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
getEMFFormsDomainExpander()
M: 5 C: 23
82%
M: 1 C: 3
75%
M: 1 C: 2
67%
M: 1 C: 5
83%
M: 0 C: 1
100%
isApplicable(VDomainModelReference)
M: 0 C: 9
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
loadObject(String, String)
M: 26 C: 22
46%
M: 2 C: 2
50%
M: 2 C: 1
33%
M: 8 C: 7
47%
M: 0 C: 1
100%
prepareDomainObject(VDomainModelReference, EObject)
M: 5 C: 83
94%
M: 2 C: 8
80%
M: 2 C: 4
67%
M: 2 C: 21
91%
M: 0 C: 1
100%
setEMFFormsDomainExpander(EMFFormsDomainExpander)
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%

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: * Eugen Neufeld - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.view.spi.custom.model.impl;
15:
16: import java.util.LinkedHashSet;
17: import java.util.Set;
18:
19: import org.eclipse.core.runtime.Platform;
20: import org.eclipse.emf.ecore.EObject;
21: import org.eclipse.emf.ecp.common.spi.asserts.Assert;
22: import org.eclipse.emf.ecp.view.spi.custom.model.ECPHardcodedReferences;
23: import org.eclipse.emf.ecp.view.spi.custom.model.VCustomDomainModelReference;
24: import org.eclipse.emf.ecp.view.spi.model.VDomainModelReference;
25: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander;
26: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDomainExpander;
27: import org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsExpandingFailedException;
28: import org.eclipse.emfforms.spi.localization.LocalizationServiceHelper;
29: import org.osgi.framework.Bundle;
30: import org.osgi.framework.BundleContext;
31: import org.osgi.framework.ServiceReference;
32: import org.osgi.service.component.annotations.Activate;
33: import org.osgi.service.component.annotations.Component;
34: import org.osgi.service.component.annotations.Deactivate;
35:
36: /**
37: * DMR Expander for VCustomDMR implementation of {@link EMFFormsDMRExpander}.
38: *
39: * @author Eugen Neufeld
40: * @since 1.8
41: *
42: */
43: @Component(name = "EMFFormsCustomDMRExpander", service = EMFFormsDMRExpander.class)
44: public class EMFFormsCustomDMRExpander implements EMFFormsDMRExpander {
45:
46:         private EMFFormsDomainExpander domainExpander;
47:         private BundleContext bundleContext;
48:         private ServiceReference<EMFFormsDomainExpander> eMFFormsDomainExpanderServiceReference;
49:
50:         /**
51:          * Called by the framework when the component gets activated.
52:          *
53:          * @param bundleContext The {@link BundleContext}
54:          */
55:         @Activate
56:         protected void activate(BundleContext bundleContext) {
57:                 this.bundleContext = bundleContext;
58:         }
59:
60:         /**
61:          * Called by the framework when the component gets deactivated.
62:          *
63:          * @param bundleContext The {@link BundleContext}
64:          */
65:         @Deactivate
66:         protected void deactivate(BundleContext bundleContext) {
67:•                if (eMFFormsDomainExpanderServiceReference != null) {
68:                         bundleContext.ungetService(eMFFormsDomainExpanderServiceReference);
69:                         domainExpander = null;
70:                 }
71:         }
72:
73:         private EMFFormsDomainExpander getEMFFormsDomainExpander() {
74:•                if (domainExpander == null) {
75:                         eMFFormsDomainExpanderServiceReference = bundleContext.getServiceReference(EMFFormsDomainExpander.class);
76:•                        if (eMFFormsDomainExpanderServiceReference == null) {
77:                                 throw new IllegalStateException("No EMFFormsDomainExpander available!"); //$NON-NLS-1$
78:                         }
79:                         domainExpander = bundleContext.getService(eMFFormsDomainExpanderServiceReference);
80:                 }
81:                 return domainExpander;
82:         }
83:
84:         /**
85:          * Helper method for tests. This is quite ugly!
86:          *
87:          * @param domainExpander The EMFFormsDomainExpander to use
88:          */
89:         void setEMFFormsDomainExpander(EMFFormsDomainExpander domainExpander) {
90:                 this.domainExpander = domainExpander;
91:         }
92:
93:         /**
94:          * {@inheritDoc}
95:          *
96:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander#prepareDomainObject(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference,
97:          * org.eclipse.emf.ecore.EObject)
98:          */
99:         @Override
100:         public void prepareDomainObject(VDomainModelReference domainModelReference, EObject domainObject)
101:                 throws EMFFormsExpandingFailedException {
102:                 Assert.create(domainModelReference).notNull();
103:                 Assert.create(domainObject).notNull();
104:                 Assert.create(domainModelReference).ofClass(VCustomDomainModelReference.class);
105:
106:                 final VCustomDomainModelReference customReference = VCustomDomainModelReference.class
107:                         .cast(domainModelReference);
108:
109:•                if (customReference.getBundleName() == null || customReference.getClassName() == null) {
110:                         throw new EMFFormsExpandingFailedException(
111:                                 "The custom domain model reference's bundle name and classname must not be null."); //$NON-NLS-1$
112:                 }
113:                 final ECPHardcodedReferences customControl = loadObject(customReference.getBundleName(),
114:                         customReference.getClassName());
115:•                if (customControl == null) {
116:                         throw new EMFFormsExpandingFailedException(
117:                                 "The custom domain model cannot be loaded."); //$NON-NLS-1$
118:                 }
119:•                if (!customReference.isControlChecked()) {
120:                         // read stuff from control
121:                         final Set<VDomainModelReference> controlReferences = new LinkedHashSet<VDomainModelReference>();
122:                         controlReferences.addAll(customControl.getNeededDomainModelReferences());
123:                         controlReferences.addAll(customReference.getDomainModelReferences());
124:                         customReference.getDomainModelReferences().clear();
125:                         customReference.getDomainModelReferences().addAll(controlReferences);
126:                         customReference.setControlChecked(true);
127:                 }
128:                 // resolve references from control
129:•                for (final VDomainModelReference dmr : customReference.getDomainModelReferences()) {
130:                         getEMFFormsDomainExpander().prepareDomainObject(dmr, domainObject);
131:                 }
132:         }
133:
134:         private static ECPHardcodedReferences loadObject(String bundleName, String clazz) {
135:                 final Bundle bundle = Platform.getBundle(bundleName);
136:•                if (bundle == null) {
137:                         new ClassNotFoundException(String.format(LocalizationServiceHelper.getString(
138:                                 VCustomDomainModelReferenceImpl.class, "BundleNotFound_ExceptionMessage"), clazz, bundleName)); //$NON-NLS-1$
139:                         return null;
140:                 }
141:                 try {
142:                         final Class<?> loadClass = bundle.loadClass(clazz);
143:•                        if (!ECPHardcodedReferences.class.isAssignableFrom(loadClass)) {
144:                                 return null;
145:                         }
146:                         return ECPHardcodedReferences.class.cast(loadClass.newInstance());
147:                 } catch (final ClassNotFoundException ex) {
148:                         return null;
149:                 } catch (final InstantiationException ex) {
150:                         return null;
151:                 } catch (final IllegalAccessException ex) {
152:                         return null;
153:                 }
154:
155:         }
156:
157:         /**
158:          * {@inheritDoc}
159:          *
160:          * @see org.eclipse.emfforms.spi.core.services.domainexpander.EMFFormsDMRExpander#isApplicable(org.eclipse.emf.ecp.view.spi.model.VDomainModelReference)
161:          */
162:         @Override
163:         public double isApplicable(VDomainModelReference domainModelReference) {
164:•                if (VCustomDomainModelReference.class.isInstance(domainModelReference)) {
165:                         return 5;
166:                 }
167:                 return NOT_APPLICABLE;
168:         }
169:
170: }