Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / operations / impl / AnnotationTypeOperations.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 package org.openecomp.sdc.be.model.operations.impl;
21
22 import javax.validation.constraints.NotNull;
23 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
24 import org.openecomp.sdc.be.model.AnnotationTypeDefinition;
25 import org.openecomp.sdc.be.model.operations.api.TypeOperations;
26 import org.openecomp.sdc.be.resources.data.AnnotationTypeData;
27 import org.springframework.stereotype.Component;
28
29 @Component
30 public class AnnotationTypeOperations implements TypeOperations<AnnotationTypeDefinition> {
31
32     private final CommonTypeOperations commonTypeOperations;
33
34     public AnnotationTypeOperations(CommonTypeOperations commonTypeOperations) {
35         this.commonTypeOperations = commonTypeOperations;
36     }
37
38     @Override
39     public AnnotationTypeDefinition addType(AnnotationTypeDefinition newTypeDefinition) {
40         AnnotationTypeData annotationTypeData = new AnnotationTypeData(newTypeDefinition);
41         String uniqueId = UniqueIdBuilder.buildTypeUid(newTypeDefinition.getType(), newTypeDefinition.getVersion(), "annotationtype");
42         annotationTypeData.setInitialCreationProperties(uniqueId);
43         commonTypeOperations.addType(annotationTypeData, AnnotationTypeData.class);
44         commonTypeOperations.addProperties(uniqueId, NodeTypeEnum.AnnotationType, newTypeDefinition.getProperties());
45         return getType(uniqueId);
46     }
47
48     @Override
49     public AnnotationTypeDefinition getType(String uniqueId) {
50         return commonTypeOperations.getType(uniqueId, AnnotationTypeData.class, NodeTypeEnum.AnnotationType).map(this::populateTypeDefinition)
51             .orElse(null);
52     }
53
54     private AnnotationTypeDefinition populateTypeDefinition(@NotNull AnnotationTypeData annotationTypeData) {
55         AnnotationTypeDefinition annotationTypeDefinition = new AnnotationTypeDefinition(annotationTypeData.getAnnotationTypeDataDefinition());
56         commonTypeOperations
57             .fillProperties(annotationTypeDefinition.getUniqueId(), NodeTypeEnum.AnnotationType, annotationTypeDefinition::setProperties);
58         return annotationTypeDefinition;
59     }
60
61     @Override
62     public AnnotationTypeDefinition getLatestType(String type) {
63         return commonTypeOperations.getLatestType(type, AnnotationTypeData.class, NodeTypeEnum.AnnotationType).map(this::populateTypeDefinition)
64             .orElse(null);
65     }
66
67     @Override
68     public boolean isSameType(AnnotationTypeDefinition type1, AnnotationTypeDefinition type2) {
69         return type1.isSameDefinition(type2);
70     }
71
72     @Override
73     public AnnotationTypeDefinition updateType(AnnotationTypeDefinition currentTypeDefinition, AnnotationTypeDefinition updatedTypeDefinition) {
74         AnnotationTypeData updatedTypeData = new AnnotationTypeData(updatedTypeDefinition);
75         updatedTypeData.setUpdateProperties(currentTypeDefinition);
76         commonTypeOperations
77             .updateType(updatedTypeData, updatedTypeDefinition.getProperties(), AnnotationTypeData.class, NodeTypeEnum.AnnotationType);
78         return getType(updatedTypeData.getUniqueId());
79     }
80 }