2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.merge.input;
23 import java.util.List;
26 import org.apache.commons.lang.StringUtils;
27 import org.openecomp.sdc.be.dao.utils.MapUtil;
28 import org.openecomp.sdc.be.model.InputDefinition;
30 @org.springframework.stereotype.Component
31 public class InputsValuesMergingBusinessLogic {
34 * Merge old inputs values into the updated inputs
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);
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
48 public void mergeComponentInputs(Map<String, InputDefinition> oldInputs, Map<String, InputDefinition> updatedInputs) {
49 updatedInputs.forEach((inputName, input) -> mergeInputsValues(oldInputs.get(inputName), input));
52 private void mergeInputsValues(InputDefinition oldInput, InputDefinition updatedInput) {
53 if (shouldMergeOldValue(oldInput, updatedInput)) {
54 updatedInput.setDefaultValue(oldInput.getDefaultValue());
58 private boolean shouldMergeOldValue(InputDefinition oldInput, InputDefinition newInput) {
59 return isNonEmptyDefaultValue(oldInput) && isEmptyDefaultValue(newInput) && isSameType(oldInput, newInput);
62 private boolean isSameType(InputDefinition oldInput, InputDefinition updatedInput) {
63 return oldInput.typeEquals(updatedInput);
66 private boolean isEmptyDefaultValue(InputDefinition input) {
67 return input != null && StringUtils.isEmpty(input.getDefaultValue());
70 private boolean isNonEmptyDefaultValue(InputDefinition input) {
71 return input != null && !isEmptyDefaultValue(input);