Catalog alignment
[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 org.apache.commons.lang.StringUtils;
24 import org.openecomp.sdc.be.dao.utils.MapUtil;
25 import org.openecomp.sdc.be.model.InputDefinition;
26
27 import java.util.List;
28 import java.util.Map;
29 @org.springframework.stereotype.Component
30 public class InputsValuesMergingBusinessLogic {
31
32     /**
33      * Merge old inputs values into the updated inputs
34      */
35     public void mergeComponentInputs(List<InputDefinition> oldInputs, List<InputDefinition> inputsToMerge) {
36         Map<String, InputDefinition> oldInputsByName =  MapUtil.toMap(oldInputs, InputDefinition::getName);
37         Map<String, InputDefinition> inputsToMergeByName = MapUtil.toMap(inputsToMerge, InputDefinition::getName);
38         mergeComponentInputs(oldInputsByName, inputsToMergeByName);
39     }
40     
41     /**
42      * Merge old inputs values into the updated inputs
43      * An input value is merged if the input previous version had a user defined value and its value is empty in current version
44      * @param oldInputs the currently persisted inputs mapped by their names
45      * @param updatedInputs the currently being update inputs mapped by their names
46      */
47     public void mergeComponentInputs(Map<String, InputDefinition> oldInputs, Map<String, InputDefinition> updatedInputs) {
48         updatedInputs.forEach((inputName, input) -> mergeInputsValues(oldInputs.get(inputName), input));
49     }
50
51     private void mergeInputsValues(InputDefinition oldInput, InputDefinition updatedInput) {
52         if (shouldMergeOldValue(oldInput, updatedInput)) {
53             updatedInput.setDefaultValue(oldInput.getDefaultValue());
54         }
55     }
56
57     private boolean shouldMergeOldValue(InputDefinition oldInput, InputDefinition newInput) {
58         return isNonEmptyDefaultValue(oldInput) && isEmptyDefaultValue(newInput) && isSameType(oldInput, newInput);
59     }
60
61     private boolean isSameType(InputDefinition oldInput, InputDefinition updatedInput) {
62         return oldInput.typeEquals(updatedInput);
63     }
64
65     private boolean isEmptyDefaultValue(InputDefinition input) {
66         return input != null && StringUtils.isEmpty(input.getDefaultValue());
67     }
68
69     private boolean isNonEmptyDefaultValue(InputDefinition input) {
70         return input != null && !isEmptyDefaultValue(input);
71     }
72
73
74 }