Added oparent to sdc main
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / merge / property / DataDefinitionsValuesMergingBusinessLogic.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.property;
22
23 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
24 import org.openecomp.sdc.be.model.InputDefinition;
25 import org.springframework.stereotype.Component;
26
27 import java.util.Collections;
28 import java.util.List;
29
30 import static org.apache.commons.collections.CollectionUtils.isEmpty;
31 import static org.openecomp.sdc.be.components.merge.property.PropertyInstanceMergeDataBuilder.buildDataForMerging;
32
33 @Component
34 public class DataDefinitionsValuesMergingBusinessLogic  {
35
36     private PropertyDataValueMergeBusinessLogic propertyValueMergeBL;
37
38     public DataDefinitionsValuesMergingBusinessLogic(PropertyDataValueMergeBusinessLogic propertyValueMergeBL) {
39         this.propertyValueMergeBL = propertyValueMergeBL;
40     }
41
42     /**
43      * Merge previous version data definition values into the new version data definition.
44      * A data definition value is merged if it had a value in previous version and has no value in the current version.
45      * in case a property get input value has no corresponding input in the current version its value will not be merged
46      * @param oldInstanceDataDefinition the currently persisted instance data definitions
47      * @param oldInputs the previous version inputs
48      * @param updatedInstanceDataDefinition the currently being update instance data definitions
49      * @param newInputs the new version inputs
50      */
51     public <T extends PropertyDataDefinition> void mergeInstanceDataDefinitions(List<T> oldInstanceDataDefinition, List<InputDefinition> oldInputs, List<T> updatedInstanceDataDefinition, List<InputDefinition> newInputs) {
52         if (isEmpty(updatedInstanceDataDefinition) || isEmpty(oldInstanceDataDefinition)) {
53             return;
54         }
55         List<MergePropertyData> mergePropertyData = buildDataForMerging(oldInstanceDataDefinition, oldInputs, updatedInstanceDataDefinition, newInputs);
56         mergePropertyData.forEach(this::mergeInstanceDefinition);
57
58     }
59
60     public <T extends PropertyDataDefinition> void mergeInstanceDataDefinitions(List<T> oldInstanceDataDefinition,  List<T> updatedInstanceDataDefinition) {
61         List<InputDefinition> emptyInputsList = Collections.emptyList();
62         mergeInstanceDataDefinitions(oldInstanceDataDefinition, emptyInputsList, updatedInstanceDataDefinition, emptyInputsList);
63     }
64
65     private void mergeInstanceDefinition(MergePropertyData mergeData) {
66         if (isSameType(mergeData.getOldProp(), mergeData.getNewProp())) {
67             propertyValueMergeBL.mergePropertyValue(mergeData.getOldProp(), mergeData.getNewProp(), mergeData.getGetInputNamesToMerge());
68         }
69     }
70
71     private boolean isSameType(PropertyDataDefinition oldDataDefinition, PropertyDataDefinition updatedDataDefinition) {
72         return oldDataDefinition.typeEquals(updatedDataDefinition);
73     }
74
75 }