re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / resources / data / ComponentMetadataData.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.resources.data;
22
23 import com.google.gson.reflect.TypeToken;
24 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
25 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
26 import org.openecomp.sdc.be.datatypes.components.ComponentMetadataDataDefinition;
27 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
28
29 import java.lang.reflect.Type;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 public abstract class ComponentMetadataData extends GraphNode {
35
36         protected ComponentMetadataDataDefinition metadataDataDefinition;
37         protected Integer componentInstanceCounter;
38
39         public ComponentMetadataData(NodeTypeEnum label, ComponentMetadataDataDefinition metadataDataDefinition) {
40                 super(label);
41                 this.metadataDataDefinition = metadataDataDefinition;
42                 this.componentInstanceCounter = 0;
43         }
44
45         @SuppressWarnings("unchecked")
46         public ComponentMetadataData(NodeTypeEnum label, ComponentMetadataDataDefinition metadataDataDefinition, Map<String, Object> properties) {
47                 this(label, metadataDataDefinition);
48                 metadataDataDefinition.setUniqueId((String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty()));
49                 metadataDataDefinition.setCreationDate((Long) properties.get(GraphPropertiesDictionary.CREATION_DATE.getProperty()));
50                 metadataDataDefinition.setDescription((String) properties.get(GraphPropertiesDictionary.DESCRIPTION.getProperty()));
51                 metadataDataDefinition.setConformanceLevel((String) properties.get(GraphPropertiesDictionary.CONFORMANCE_LEVEL.getProperty()));
52                 metadataDataDefinition.setIcon((String) properties.get(GraphPropertiesDictionary.ICON.getProperty()));
53                 metadataDataDefinition.setHighestVersion((Boolean) properties.get(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty()));
54                 metadataDataDefinition.setLastUpdateDate((Long) properties.get(GraphPropertiesDictionary.LAST_UPDATE_DATE.getProperty()));
55                 metadataDataDefinition.setName((String) properties.get(GraphPropertiesDictionary.NAME.getProperty()));
56                 metadataDataDefinition.setState((String) properties.get(GraphPropertiesDictionary.STATE.getProperty()));
57                 List<String> tagsFromJson;
58                 if(properties.get(GraphPropertiesDictionary.TAGS.getProperty()) instanceof List<?>){
59                         tagsFromJson = (List<String>) properties.get(GraphPropertiesDictionary.TAGS.getProperty());
60                 } else {
61                         Type listType = new TypeToken<List<String>>() {}.getType();
62                         tagsFromJson = getGson().fromJson((String) properties.get(GraphPropertiesDictionary.TAGS.getProperty()), listType);
63                 }
64                 metadataDataDefinition.setTags(tagsFromJson);
65                 metadataDataDefinition.setVersion((String) properties.get(GraphPropertiesDictionary.VERSION.getProperty()));
66                 metadataDataDefinition.setContactId((String) properties.get(GraphPropertiesDictionary.CONTACT_ID.getProperty()));
67                 metadataDataDefinition.setUUID((String) properties.get(GraphPropertiesDictionary.UUID.getProperty()));
68                 metadataDataDefinition.setNormalizedName((String) properties.get(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty()));
69                 metadataDataDefinition.setSystemName((String) properties.get(GraphPropertiesDictionary.SYSTEM_NAME.getProperty()));
70                 metadataDataDefinition.setIsDeleted((Boolean) properties.get(GraphPropertiesDictionary.IS_DELETED.getProperty()));
71                 metadataDataDefinition.setProjectCode((String) properties.get(GraphPropertiesDictionary.PROJECT_CODE.getProperty()));
72                 metadataDataDefinition.setCsarUUID((String) properties.get(GraphPropertiesDictionary.CSAR_UUID.getProperty()));
73                 metadataDataDefinition.setCsarVersion((String) properties.get(GraphPropertiesDictionary.CSAR_VERSION.getProperty()));
74                 metadataDataDefinition.setImportedToscaChecksum((String) properties.get(GraphPropertiesDictionary.IMPORTED_TOSCA_CHECKSUM.getProperty()));
75                 metadataDataDefinition.setInvariantUUID((String) properties.get(GraphPropertiesDictionary.INVARIANT_UUID.getProperty()));
76 //              metadataDataDefinition.setComponentType(ComponentTypeEnum.valueOf((String) properties.get(GraphPropertyEnum.COMPONENT_TYPE.getProperty())));
77                 metadataDataDefinition.setArchived((Boolean) properties.get(GraphPropertiesDictionary.IS_ARCHIVED.getProperty()));
78                 metadataDataDefinition.setVspArchived((Boolean) properties.get(GraphPropertiesDictionary.IS_VSP_ARCHIVED.getProperty()));
79                 Object archiveTime = properties.get(GraphPropertiesDictionary.ARCHIVE_TIME.getProperty());
80                 if (archiveTime instanceof Integer){
81                         metadataDataDefinition.setArchiveTime(new Long((Integer) archiveTime));
82                 } else if (archiveTime instanceof Long) {
83                         metadataDataDefinition.setArchiveTime((Long)archiveTime);
84                 }
85                 componentInstanceCounter = (Integer) properties.get(GraphPropertiesDictionary.INSTANCE_COUNTER.getProperty());
86         }
87
88         @Override
89         public Map<String, Object> toGraphMap() {
90                 Map<String, Object> map = new HashMap<>();
91
92                 addIfExists(map, GraphPropertiesDictionary.UNIQUE_ID, metadataDataDefinition.getUniqueId());
93                 addIfExists(map, GraphPropertiesDictionary.VERSION, metadataDataDefinition.getVersion());
94                 addIfExists(map, GraphPropertiesDictionary.CREATION_DATE, metadataDataDefinition.getCreationDate());
95                 addIfExists(map, GraphPropertiesDictionary.DESCRIPTION, metadataDataDefinition.getDescription());
96                 addIfExists(map, GraphPropertiesDictionary.CONFORMANCE_LEVEL, metadataDataDefinition.getConformanceLevel());
97                 addIfExists(map, GraphPropertiesDictionary.ICON, metadataDataDefinition.getIcon());
98                 addIfExists(map, GraphPropertiesDictionary.IS_HIGHEST_VERSION, metadataDataDefinition.isHighestVersion());
99                 addIfExists(map, GraphPropertiesDictionary.LAST_UPDATE_DATE, metadataDataDefinition.getLastUpdateDate());
100                 addIfExists(map, GraphPropertiesDictionary.STATE, metadataDataDefinition.getState());
101                 addIfExists(map, GraphPropertiesDictionary.TAGS, metadataDataDefinition.getTags());
102                 addIfExists(map, GraphPropertiesDictionary.CONTACT_ID, metadataDataDefinition.getContactId());
103                 addIfExists(map, GraphPropertiesDictionary.NAME, metadataDataDefinition.getName());
104                 addIfExists(map, GraphPropertiesDictionary.UUID, metadataDataDefinition.getUUID());
105                 addIfExists(map, GraphPropertiesDictionary.NORMALIZED_NAME, metadataDataDefinition.getNormalizedName());
106                 addIfExists(map, GraphPropertiesDictionary.SYSTEM_NAME, metadataDataDefinition.getSystemName());
107                 addIfExists(map, GraphPropertiesDictionary.IS_DELETED, metadataDataDefinition.isDeleted());
108                 addIfExists(map, GraphPropertiesDictionary.INSTANCE_COUNTER, componentInstanceCounter);
109                 addIfExists(map, GraphPropertiesDictionary.PROJECT_CODE, metadataDataDefinition.getProjectCode());
110                 addIfExists(map, GraphPropertiesDictionary.CSAR_UUID, metadataDataDefinition.getCsarUUID());
111                 addIfExists(map, GraphPropertiesDictionary.CSAR_VERSION, metadataDataDefinition.getCsarVersion());
112                 addIfExists(map, GraphPropertiesDictionary.IMPORTED_TOSCA_CHECKSUM, metadataDataDefinition.getImportedToscaChecksum());
113                 addIfExists(map, GraphPropertiesDictionary.INVARIANT_UUID, metadataDataDefinition.getInvariantUUID());
114                 return map;
115         }
116
117         @Override
118         public String getUniqueId() {
119                 return metadataDataDefinition.getUniqueId();
120         }
121
122         public ComponentMetadataDataDefinition getMetadataDataDefinition() {
123                 return metadataDataDefinition;
124         }
125
126         public void setMetadataDataDefinition(ComponentMetadataDataDefinition metadataDataDefinition) {
127                 this.metadataDataDefinition = metadataDataDefinition;
128         }
129
130         public Integer getComponentInstanceCounter() {
131                 return componentInstanceCounter;
132         }
133
134         public void setComponentInstanceCounter(Integer componentInstanceCounter) {
135                 this.componentInstanceCounter = componentInstanceCounter;
136         }
137
138         public Integer increaseAndGetComponentInstanceCounter() {
139                 return ++componentInstanceCounter;
140         }
141
142         @Override
143         public String toString() {
144                 return "ComponentMetadataData [metadataDataDefinition=" + metadataDataDefinition + ", componentInstanceCounter=" + componentInstanceCounter + "]";
145         }
146
147 }