665e5c737d8dd82f3ce15bf2c9382f03258d52dc
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / resources / data / category / CategoryData.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 package org.openecomp.sdc.be.resources.data.category;
21
22 import com.google.gson.reflect.TypeToken;
23 import java.lang.reflect.Type;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
28 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
29 import org.openecomp.sdc.be.datatypes.category.CategoryDataDefinition;
30 import org.openecomp.sdc.be.datatypes.category.MetadataKeyDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
32
33 public class CategoryData extends GraphNode {
34
35     private CategoryDataDefinition categoryDataDefinition;
36
37     public CategoryData(NodeTypeEnum label) {
38         super(label);
39         categoryDataDefinition = new CategoryDataDefinition();
40     }
41
42     public CategoryData(NodeTypeEnum label, CategoryDataDefinition categoryDataDefinition) {
43         super(label);
44         this.categoryDataDefinition = categoryDataDefinition;
45     }
46
47     public CategoryData(Map<String, Object> properties) {
48         this(NodeTypeEnum.getByName((String) properties.get(GraphPropertiesDictionary.LABEL.getProperty())));
49         categoryDataDefinition.setUniqueId((String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty()));
50         categoryDataDefinition.setNormalizedName((String) properties.get(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty()));
51         categoryDataDefinition.setName((String) properties.get(GraphPropertiesDictionary.NAME.getProperty()));
52         final Object useServiceSubstitutionForNestedServicesProperty = properties
53             .get(GraphPropertiesDictionary.USE_SERVICE_SUBSTITUTION_FOR_NESTED_SERVICES.getProperty());
54         final boolean useServiceSubstitutionForNestedServices =
55             useServiceSubstitutionForNestedServicesProperty != null && (boolean) useServiceSubstitutionForNestedServicesProperty;
56         categoryDataDefinition.setUseServiceSubstitutionForNestedServices(useServiceSubstitutionForNestedServices);
57         Type listType = new TypeToken<List<String>>() {
58         }.getType();
59         List<String> iconsfromJson = getGson().fromJson((String) properties.get(GraphPropertiesDictionary.ICONS.getProperty()), listType);
60         categoryDataDefinition.setIcons(iconsfromJson);
61         categoryDataDefinition.setModels(getGson().fromJson((String) properties.get(GraphPropertiesDictionary.MODEL.getProperty()), listType));
62         final Type metadataKeylistType = new TypeToken<List<MetadataKeyDataDefinition>>() {
63         }.getType();
64         final List<MetadataKeyDataDefinition> metadataKeysfromJson = getGson()
65             .fromJson((String) properties.get(GraphPropertiesDictionary.METADATA_KEYS.getProperty()), metadataKeylistType);
66         categoryDataDefinition.setMetadataKeys(metadataKeysfromJson);
67     }
68
69     @Override
70     public String getUniqueId() {
71         return categoryDataDefinition.getUniqueId();
72     }
73
74     public CategoryDataDefinition getCategoryDataDefinition() {
75         return categoryDataDefinition;
76     }
77
78     @Override
79     public Map<String, Object> toGraphMap() {
80         Map<String, Object> map = new HashMap<>();
81         addIfExists(map, GraphPropertiesDictionary.UNIQUE_ID, categoryDataDefinition.getUniqueId());
82         addIfExists(map, GraphPropertiesDictionary.NAME, categoryDataDefinition.getName());
83         addIfExists(map, GraphPropertiesDictionary.NORMALIZED_NAME, categoryDataDefinition.getNormalizedName());
84         addIfExists(map, GraphPropertiesDictionary.MODEL, categoryDataDefinition.getModels());
85         addIfExists(map, GraphPropertiesDictionary.ICONS, categoryDataDefinition.getIcons());
86         addIfExists(map, GraphPropertiesDictionary.USE_SERVICE_SUBSTITUTION_FOR_NESTED_SERVICES,
87             categoryDataDefinition.isUseServiceSubstitutionForNestedServices());
88         addIfExists(map, GraphPropertiesDictionary.METADATA_KEYS, categoryDataDefinition.getMetadataKeys());
89         return map;
90     }
91
92     @Override
93     public String toString() {
94         return "CategoryData [categoryDataDefinition=" + categoryDataDefinition + "]";
95     }
96 }