Add a display name for the category
[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.setDisplayName((String) properties.get(GraphPropertiesDictionary.DISPLAY_NAME.getProperty()));
52         categoryDataDefinition.setName((String) properties.get(GraphPropertiesDictionary.NAME.getProperty()));
53         final Object useServiceSubstitutionForNestedServicesProperty = properties
54             .get(GraphPropertiesDictionary.USE_SERVICE_SUBSTITUTION_FOR_NESTED_SERVICES.getProperty());
55         final boolean useServiceSubstitutionForNestedServices =
56             useServiceSubstitutionForNestedServicesProperty != null && (boolean) useServiceSubstitutionForNestedServicesProperty;
57         categoryDataDefinition.setUseServiceSubstitutionForNestedServices(useServiceSubstitutionForNestedServices);
58         Type listType = new TypeToken<List<String>>() {
59         }.getType();
60         List<String> iconsfromJson = getGson().fromJson((String) properties.get(GraphPropertiesDictionary.ICONS.getProperty()), listType);
61         categoryDataDefinition.setIcons(iconsfromJson);
62         categoryDataDefinition.setModels(getGson().fromJson((String) properties.get(GraphPropertiesDictionary.MODEL.getProperty()), listType));
63         final Type metadataKeylistType = new TypeToken<List<MetadataKeyDataDefinition>>() {
64         }.getType();
65         final List<MetadataKeyDataDefinition> metadataKeysfromJson = getGson()
66             .fromJson((String) properties.get(GraphPropertiesDictionary.METADATA_KEYS.getProperty()), metadataKeylistType);
67         categoryDataDefinition.setMetadataKeys(metadataKeysfromJson);
68     }
69
70     @Override
71     public String getUniqueId() {
72         return categoryDataDefinition.getUniqueId();
73     }
74
75     public CategoryDataDefinition getCategoryDataDefinition() {
76         return categoryDataDefinition;
77     }
78
79     @Override
80     public Map<String, Object> toGraphMap() {
81         Map<String, Object> map = new HashMap<>();
82         addIfExists(map, GraphPropertiesDictionary.UNIQUE_ID, categoryDataDefinition.getUniqueId());
83         addIfExists(map, GraphPropertiesDictionary.NAME, categoryDataDefinition.getName());
84         addIfExists(map, GraphPropertiesDictionary.DISPLAY_NAME, categoryDataDefinition.getDisplayName());
85         addIfExists(map, GraphPropertiesDictionary.NORMALIZED_NAME, categoryDataDefinition.getNormalizedName());
86         addIfExists(map, GraphPropertiesDictionary.MODEL, categoryDataDefinition.getModels());
87         addIfExists(map, GraphPropertiesDictionary.ICONS, categoryDataDefinition.getIcons());
88         addIfExists(map, GraphPropertiesDictionary.USE_SERVICE_SUBSTITUTION_FOR_NESTED_SERVICES,
89             categoryDataDefinition.isUseServiceSubstitutionForNestedServices());
90         addIfExists(map, GraphPropertiesDictionary.METADATA_KEYS, categoryDataDefinition.getMetadataKeys());
91         return map;
92     }
93
94     @Override
95     public String toString() {
96         return "CategoryData [categoryDataDefinition=" + categoryDataDefinition + "]";
97     }
98 }