Skip to content

Package: MergeHelper

MergeHelper

nameinstructionbranchcomplexitylinemethod
getExistingRule(EList)
M: 22 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%
merge(VRuleRepository)
M: 37 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 7 C: 0
0%
M: 1 C: 0
0%
mergeRules(VElement, VRuleEntry, Map)
M: 77 C: 0
0%
M: 8 C: 0
0%
M: 5 C: 0
0%
M: 19 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.emfforms.internal.rulerepository.tooling.merge;
15:
16: import java.util.LinkedHashMap;
17: import java.util.Map;
18:
19: import org.eclipse.emf.common.util.EList;
20: import org.eclipse.emf.ecore.util.EcoreUtil;
21: import org.eclipse.emf.ecp.view.spi.model.VAttachment;
22: import org.eclipse.emf.ecp.view.spi.model.VElement;
23: import org.eclipse.emf.ecp.view.spi.rule.model.AndCondition;
24: import org.eclipse.emf.ecp.view.spi.rule.model.OrCondition;
25: import org.eclipse.emf.ecp.view.spi.rule.model.Rule;
26: import org.eclipse.emf.ecp.view.spi.rule.model.RuleFactory;
27: import org.eclipse.emfforms.spi.rulerepository.model.MergeType;
28: import org.eclipse.emfforms.spi.rulerepository.model.VRuleEntry;
29: import org.eclipse.emfforms.spi.rulerepository.model.VRuleRepository;
30:
31: /**
32: * Helper class to merge the linked View model with the rule repository into a new view model.
33: *
34: * @author Eugen Neufeld
35: *
36: */
37: public final class MergeHelper {
38:
39:         private MergeHelper() {
40:         }
41:
42:         /**
43:          * Merges the {@link VRuleRepository} into the linked {@link org.eclipse.emf.ecp.view.spi.model.VView VView}.
44:          *
45:          * @param ruleRepository The {@link VRuleRepository} to merge from
46:          */
47:         public static void merge(VRuleRepository ruleRepository) {
48:                 final Map<VElement, OrCondition> initialMerged = new LinkedHashMap<VElement, OrCondition>();
49:•                for (final VRuleEntry ruleEntry : ruleRepository.getRuleEntries()) {
50:•                        for (final VElement vElement : ruleEntry.getElements()) {
51:•                                if (vElement.eIsProxy()) {
52:                                         // FIXME: log error
53:                                         continue;
54:                                 }
55:                                 mergeRules(vElement, ruleEntry, initialMerged);
56:                         }
57:                 }
58:
59:         }
60:
61:         private static void mergeRules(VElement vElement, VRuleEntry ruleEntry, Map<VElement, OrCondition> initialMerged) {
62:                 final Rule existingRule = getExistingRule(vElement.getAttachments());
63:                 final Rule newRule = EcoreUtil.copy(ruleEntry.getRule());
64:•                if (existingRule == null) {
65:                         vElement.getAttachments().add(newRule);
66:                         return;
67:                 }
68:
69:•                if (!initialMerged.containsKey(vElement)) {
70:                         // initial merge
71:                         final AndCondition and = RuleFactory.eINSTANCE.createAndCondition();
72:                         final OrCondition or = RuleFactory.eINSTANCE.createOrCondition();
73:                         and.getConditions().add(or);
74:                         or.getConditions().add(existingRule.getCondition());
75:                         existingRule.setCondition(and);
76:                         initialMerged.put(vElement, or);
77:                 }
78:•                if (ruleEntry.getMergeType() == MergeType.OR) {
79:                         final OrCondition or = initialMerged.get(vElement);
80:                         or.getConditions().add(newRule.getCondition());
81:•                } else if (ruleEntry.getMergeType() == MergeType.AND) {
82:                         final AndCondition and = (AndCondition) existingRule.getCondition();
83:                         and.getConditions().add(newRule.getCondition());
84:                 }
85:         }
86:
87:         private static Rule getExistingRule(EList<VAttachment> attachments) {
88:•                for (final VAttachment attachment : attachments) {
89:•                        if (Rule.class.isInstance(attachment)) {
90:                                 return Rule.class.cast(attachment);
91:                         }
92:                 }
93:                 return null;
94:         }
95:
96: }