re base code
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / AnnotationBusinessLogic.java
1 package org.openecomp.sdc.be.components.impl;
2
3 import org.openecomp.sdc.be.components.validation.AnnotationValidator;
4 import org.openecomp.sdc.be.dao.utils.MapUtil;
5 import org.openecomp.sdc.be.datatypes.elements.Annotation;
6 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
7 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
8 import org.openecomp.sdc.be.model.InputDefinition;
9 import org.openecomp.sdc.be.model.PropertyDefinition;
10 import org.openecomp.sdc.be.model.operations.impl.AnnotationTypeOperations;
11 import org.springframework.stereotype.Component;
12
13 import java.util.*;
14
15 import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
16
17 @Component
18 public class AnnotationBusinessLogic {
19
20     private final AnnotationTypeOperations annotationTypeOperations;
21
22     private final AnnotationValidator annotationValidator;
23
24     public AnnotationBusinessLogic(AnnotationTypeOperations annotationTypeOperations,
25                                    AnnotationValidator annotationValidator){
26         this.annotationTypeOperations = annotationTypeOperations;
27         this.annotationValidator = annotationValidator;
28     }
29
30     public void validateAndMergeAnnotationsAndAssignToInput(Map<String, InputDefinition> inputs) {
31         if (!inputs.isEmpty()){
32             for (InputDefinition input : inputs.values()) {
33                 List<Annotation> inputAnnotationList = input.getAnnotations();
34                 if (isNotEmpty(inputAnnotationList)) {
35                     for (Annotation annotation : inputAnnotationList) {
36                         AnnotationTypeDefinition dbAnnotationTypeDefinition = annotationTypeOperations.getLatestType(annotation.getType());
37                         validateMergeAndSetAnnoProps(annotation, dbAnnotationTypeDefinition);
38                     }
39                 }
40                 input.setAnnotations(inputAnnotationList);
41             }
42         }
43     }
44
45     public AnnotationTypeOperations getAnnotationTypeOperations() {
46         return annotationTypeOperations;
47     }
48
49     private void validateMergeAndSetAnnoProps(Annotation annotation, AnnotationTypeDefinition dbAnnotationTypeDefinition) {
50         annotationValidator.validateAnnotationsProperties(annotation, dbAnnotationTypeDefinition);
51         List<PropertyDataDefinition> mergedPropertiesList = mergePropsOfAnnoDataTypeWithParsedAnnoProps(annotation.getProperties(), dbAnnotationTypeDefinition.getProperties());
52         annotation.setProperties(mergedPropertiesList);
53     }
54
55     private List<PropertyDataDefinition> mergePropsOfAnnoDataTypeWithParsedAnnoProps(List<PropertyDataDefinition> annoProperties, List<PropertyDefinition> typePropertiesList) {
56         Set<PropertyDataDefinition> mergedPropertiesSet = new HashSet<>(typePropertiesList);
57         Map<String, PropertyDefinition> typePropsMap = MapUtil.toMap(typePropertiesList, PropertyDefinition::getName);
58         for (PropertyDataDefinition propertyDataDefinitionObject : annoProperties) {
59             PropertyDefinition foundTypePropertyDefinitionObject = typePropsMap.get(propertyDataDefinitionObject.getName());
60             //The case of unexisting property was already handled in the validation process (result: failure)
61             PropertyDataDefinition modifiedPropertyDataObject = new PropertyDataDefinition(foundTypePropertyDefinitionObject);
62             modifiedPropertyDataObject.setValue(propertyDataDefinitionObject.getValue());
63             mergedPropertiesSet.add(modifiedPropertyDataObject);
64         }
65         return new ArrayList<>(mergedPropertiesSet);
66     }
67
68 }