|
| 1 | +/* |
| 2 | + * Copyright 2015-2017 Hewlett Packard Enterprise Development LP. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.github.cafdataprocessing.utilities.initialization; |
| 17 | + |
| 18 | +import com.github.cafdataprocessing.utilities.initialization.jsonobjects.ActionJson; |
| 19 | +import com.github.cafdataprocessing.utilities.initialization.jsonobjects.MergeMode; |
| 20 | +import com.github.cafdataprocessing.utilities.initialization.jsonobjects.ProcessingRuleJson; |
| 21 | +import com.github.cafdataprocessing.utilities.initialization.jsonobjects.WorkflowJson; |
| 22 | +import com.github.cafdataprocessing.utilities.initialization.jsonobjects.conditions.ConditionAdditionalJson; |
| 23 | +import com.github.cafdataprocessing.utilities.initialization.jsonobjects.conditions.ConditionJson; |
| 24 | +import com.google.common.base.Strings; |
| 25 | +import org.apache.commons.lang.NotImplementedException; |
| 26 | + |
| 27 | +import java.util.LinkedHashMap; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Objects; |
| 30 | +import java.util.Optional; |
| 31 | + |
| 32 | +/** |
| 33 | + * Class responsible for merging of two workflow objects - a base one and other one with overrides and / or extensions. |
| 34 | + */ |
| 35 | +public class WorkflowCombiner |
| 36 | +{ |
| 37 | + /** |
| 38 | + * Combines (merges) two workflow objects. |
| 39 | + * Merging is based on the name attributes of subtypes. For example, if a {@link ProcessingRuleJson} has |
| 40 | + * a name "Metadata Processing" and the {@code overlayWorkflow} also has a {@link ProcessingRuleJson} with the same |
| 41 | + * name ("Metadata Processing"), properties of the processing rule in the overlay will be merged into the original |
| 42 | + * workflow. Merging will either extend the original workflow or will override values in the original object. |
| 43 | + * If a particular value is not set in the original workflow, it will be added. If it exists - it will be overridden. |
| 44 | + * There is an option to force complete replacement behaviour. {@link ProcessingRuleJson} and {@link ActionJson} have |
| 45 | + * {@link MergeMode} property. If this property is set to {@code REPLACE}, values in matched (by name) in |
| 46 | + * Processing Rule or Action will replace values in the {@code targetOriginalWorkflow}. |
| 47 | + * {@link MergeMode} value for both objects defaults to {@code MERGE}. |
| 48 | + * |
| 49 | + * @param targetOriginalWorkflow a base workflow to which apply the overrides and / or extensions |
| 50 | + * @param overlayWorkflow a workflow object with overrides and / or extensions |
| 51 | + */ |
| 52 | + public static void combineWorkflows(WorkflowJson targetOriginalWorkflow, WorkflowJson overlayWorkflow) |
| 53 | + { |
| 54 | + Objects.requireNonNull(targetOriginalWorkflow); |
| 55 | + if (overlayWorkflow == null) { |
| 56 | + return; |
| 57 | + } |
| 58 | + for (ProcessingRuleJson overlayProcessingRule : overlayWorkflow.processingRules) { |
| 59 | + Optional<ProcessingRuleJson> baseWorkflowProcessingRule = targetOriginalWorkflow.processingRules.stream().filter(rule -> rule.name.equals(overlayProcessingRule.name)).findFirst(); |
| 60 | + if (baseWorkflowProcessingRule.isPresent()) { |
| 61 | + combineProcessingRule(baseWorkflowProcessingRule.get(), overlayProcessingRule); |
| 62 | + } |
| 63 | + else { |
| 64 | + targetOriginalWorkflow.processingRules.add(overlayProcessingRule); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private static void combineProcessingRule(ProcessingRuleJson processingRule, ProcessingRuleJson overlayProcessingRule) |
| 70 | + { |
| 71 | + if (overlayProcessingRule.mergeMode == MergeMode.REPLACE) { |
| 72 | + processingRule.enabled = overlayProcessingRule.enabled; |
| 73 | + processingRule.priority = overlayProcessingRule.priority; |
| 74 | + processingRule.description = overlayProcessingRule.description; |
| 75 | + processingRule.actions = overlayProcessingRule.actions; |
| 76 | + processingRule.ruleConditions = overlayProcessingRule.ruleConditions; |
| 77 | + } |
| 78 | + if (overlayProcessingRule.enabled != null) { |
| 79 | + processingRule.enabled = overlayProcessingRule.enabled; |
| 80 | + } |
| 81 | + if (overlayProcessingRule.priority != null) { |
| 82 | + processingRule.priority = overlayProcessingRule.priority; |
| 83 | + } |
| 84 | + if (!Strings.isNullOrEmpty(overlayProcessingRule.description)) { |
| 85 | + processingRule.description = overlayProcessingRule.description; |
| 86 | + } |
| 87 | + if (overlayProcessingRule.actions != null) { |
| 88 | + for (ActionJson overlayAction : overlayProcessingRule.actions) { |
| 89 | + Optional<ActionJson> actionJson = processingRule.actions.stream().filter(action -> action.name.equals(overlayAction.name)).findFirst(); |
| 90 | + if (actionJson.isPresent()) { |
| 91 | + combineAction(actionJson.get(), overlayAction); |
| 92 | + } |
| 93 | + else { |
| 94 | + processingRule.actions.add(overlayAction); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + if (overlayProcessingRule.ruleConditions != null) { |
| 99 | + if (processingRule.ruleConditions == null) { |
| 100 | + processingRule.ruleConditions = overlayProcessingRule.ruleConditions; |
| 101 | + } |
| 102 | + else { |
| 103 | + for (ConditionJson overlayActionCondition : overlayProcessingRule.ruleConditions) { |
| 104 | + Optional<ConditionJson> originalCondition = processingRule.ruleConditions.stream().filter(condition -> condition.name.equals(overlayActionCondition.name)).findFirst(); |
| 105 | + if (originalCondition.isPresent()) { |
| 106 | + combineCondition(originalCondition.get(), overlayActionCondition); |
| 107 | + } |
| 108 | + else { |
| 109 | + processingRule.ruleConditions.add(overlayActionCondition); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static void combineAction(ActionJson action, ActionJson overlayAction) |
| 117 | + { |
| 118 | + if (overlayAction.mergeMode == MergeMode.REPLACE) { |
| 119 | + action.order = overlayAction.order; |
| 120 | + action.typeId = overlayAction.typeId; |
| 121 | + action.typeName = overlayAction.typeName; |
| 122 | + action.description = overlayAction.description; |
| 123 | + action.settings = overlayAction.settings; |
| 124 | + action.actionConditions = overlayAction.actionConditions; |
| 125 | + return; |
| 126 | + } |
| 127 | + if (!Strings.isNullOrEmpty(overlayAction.name)) { |
| 128 | + action.name = overlayAction.name; |
| 129 | + } |
| 130 | + if (overlayAction.order != null) { |
| 131 | + action.order = overlayAction.order; |
| 132 | + } |
| 133 | + if (overlayAction.typeId != null) { |
| 134 | + action.typeId = overlayAction.typeId; |
| 135 | + } |
| 136 | + if (!Strings.isNullOrEmpty(overlayAction.typeName)) { |
| 137 | + action.typeName = overlayAction.typeName; |
| 138 | + } |
| 139 | + if (!Strings.isNullOrEmpty(overlayAction.description)) { |
| 140 | + action.description = overlayAction.description; |
| 141 | + } |
| 142 | + for (Map.Entry<String, Object> overlaySettingsEntry : overlayAction.settings.entrySet()) { |
| 143 | + action.settings.put(overlaySettingsEntry.getKey(), overlaySettingsEntry.getValue()); |
| 144 | + } |
| 145 | + |
| 146 | + if (overlayAction.actionConditions != null) { |
| 147 | + if (action.actionConditions == null) { |
| 148 | + action.actionConditions = overlayAction.actionConditions; |
| 149 | + } |
| 150 | + else { |
| 151 | + for (ConditionJson overlayActionCondition : overlayAction.actionConditions) { |
| 152 | + Optional<ConditionJson> originalCondition = action.actionConditions.stream().filter(condition -> condition.name.equals(overlayActionCondition.name)).findFirst(); |
| 153 | + if (originalCondition.isPresent()) { |
| 154 | + combineCondition(originalCondition.get(), overlayActionCondition); |
| 155 | + } |
| 156 | + else { |
| 157 | + action.actionConditions.add(overlayActionCondition); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + private static void combineCondition(ConditionJson condition, ConditionJson overlayCondition) |
| 167 | + { |
| 168 | + ConditionAdditionalJson overlayAdditional = overlayCondition.additional; |
| 169 | + if (condition.additional == null) { |
| 170 | + condition.additional = overlayAdditional; |
| 171 | + return; |
| 172 | + } |
| 173 | + if (overlayAdditional != null) { |
| 174 | + if (!Strings.isNullOrEmpty(overlayAdditional.notes)) { |
| 175 | + condition.additional.notes = overlayAdditional.notes; |
| 176 | + } |
| 177 | + if (!Strings.isNullOrEmpty(overlayAdditional.type)) { |
| 178 | + condition.additional.type = overlayAdditional.type; |
| 179 | + } |
| 180 | + if (overlayAdditional.order != null) { |
| 181 | + condition.additional.order = overlayAdditional.order; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | +} |
0 commit comments