Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / input / InputsValuesMergingBusinessLogic.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.merge.input;
22
23 import java.util.List;
24 import java.util.Map;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.openecomp.sdc.be.dao.utils.MapUtil;
28 import org.openecomp.sdc.be.model.InputDefinition;
29
30 @org.springframework.stereotype.Component
31 public class InputsValuesMergingBusinessLogic {
32
33     /**
34      * Merge old inputs values into the updated inputs
35      */
36     public void mergeComponentInputs(List<InputDefinition> oldInputs, List<InputDefinition> inputsToMerge) {
37         Map<String, InputDefinition> oldInputsByName =  MapUtil.toMap(oldInputs, InputDefinition::getName);
38         Map<String, InputDefinition> inputsToMergeByName = MapUtil.toMap(inputsToMerge, InputDefinition::getName);
39         mergeComponentInputs(oldInputsByName, inputsToMergeByName);
40     }
41     
42     /**
43      * Merge old inputs values into the updated inputs
44      * An input value is merged if the input previous version had a user defined value and its value is empty in current version
45      * @param oldInputs the currently persisted inputs mapped by their names
46      * @param updatedInputs the currently being update inputs mapped by their names
47      */
48     public void mergeComponentInputs(Map<String, InputDefinition> oldInputs, Map<String, InputDefinition> updatedInputs) {
49         updatedInputs.forEach((inputName, input) -> mergeInputsValues(oldInputs.get(inputName), input));
50     }
51
52     private void mergeInputsValues(InputDefinition oldInput, InputDefinition updatedInput) {
53         if (shouldMergeOldValue(oldInput, updatedInput)) {
54             updatedInput.setDefaultValue(oldInput.getDefaultValue());
55         }
56     }
57
58     private boolean shouldMergeOldValue(InputDefinition oldInput, InputDefinition newInput) {
59         return isNonEmptyDefaultValue(oldInput) && isEmptyDefaultValue(newInput) && isSameType(oldInput, newInput);
60     }
61
62     private boolean isSameType(InputDefinition oldInput, InputDefinition updatedInput) {
63         return oldInput.typeEquals(updatedInput);
64     }
65
66     private boolean isEmptyDefaultValue(InputDefinition input) {
67         return input != null && StringUtils.isEmpty(input.getDefaultValue());
68     }
69
70     private boolean isNonEmptyDefaultValue(InputDefinition input) {
71         return input != null && !isEmptyDefaultValue(input);
72     }
73
74
75 }