re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / AnnotationTypeOperations.java
1 package org.openecomp.sdc.be.model.operations.impl;
2
3 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
4 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
5 import org.openecomp.sdc.be.model.operations.api.TypeOperations;
6 import org.openecomp.sdc.be.resources.data.AnnotationTypeData;
7 import org.springframework.stereotype.Component;
8
9 import javax.validation.constraints.NotNull;
10
11 @Component
12 public class AnnotationTypeOperations implements TypeOperations<AnnotationTypeDefinition> {
13
14     private final CommonTypeOperations commonTypeOperations;
15
16     public AnnotationTypeOperations(CommonTypeOperations commonTypeOperations) {
17         this.commonTypeOperations = commonTypeOperations;
18     }
19
20     @Override
21     public AnnotationTypeDefinition addType(AnnotationTypeDefinition newTypeDefinition) {
22         AnnotationTypeData annotationTypeData = new AnnotationTypeData(newTypeDefinition);
23         String uniqueId = UniqueIdBuilder.buildTypeUid(newTypeDefinition.getType(), newTypeDefinition.getVersion(), "annotationtype");
24         annotationTypeData.setInitialCreationProperties(uniqueId);
25         commonTypeOperations.addType(annotationTypeData, AnnotationTypeData.class);
26         commonTypeOperations.addProperties(uniqueId, NodeTypeEnum.AnnotationType, newTypeDefinition.getProperties());
27         return getType(uniqueId);
28     }
29
30     @Override
31     public AnnotationTypeDefinition getType(String uniqueId) {
32         return commonTypeOperations.getType(uniqueId, AnnotationTypeData.class, NodeTypeEnum.AnnotationType)
33                 .map(this::populateTypeDefinition)
34                 .orElse(null);
35     }
36
37     private AnnotationTypeDefinition populateTypeDefinition(@NotNull AnnotationTypeData annotationTypeData) {
38         AnnotationTypeDefinition annotationTypeDefinition = new AnnotationTypeDefinition(annotationTypeData.getAnnotationTypeDataDefinition());
39         commonTypeOperations.fillProperties(annotationTypeDefinition.getUniqueId(), NodeTypeEnum.AnnotationType, annotationTypeDefinition::setProperties);
40         return annotationTypeDefinition;
41     }
42
43     @Override
44     public AnnotationTypeDefinition getLatestType(String type) {
45         return commonTypeOperations.getLatestType(type, AnnotationTypeData.class, NodeTypeEnum.AnnotationType)
46                 .map(this::populateTypeDefinition)
47                 .orElse(null);
48     }
49
50     @Override
51     public boolean isSameType(AnnotationTypeDefinition type1, AnnotationTypeDefinition type2) {
52         return type1.isSameDefinition(type2);
53     }
54
55     @Override
56     public AnnotationTypeDefinition updateType(AnnotationTypeDefinition currentTypeDefinition,
57                                                AnnotationTypeDefinition updatedTypeDefinition) {
58         AnnotationTypeData updatedTypeData = new AnnotationTypeData(updatedTypeDefinition);
59         updatedTypeData.setUpdateProperties(currentTypeDefinition);
60         commonTypeOperations.updateType(updatedTypeData, updatedTypeDefinition.getProperties(), AnnotationTypeData.class, NodeTypeEnum.AnnotationType);
61         return getType(updatedTypeData.getUniqueId());
62     }
63
64 }