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