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