Reformat catalog-dao
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / resources / data / category / SubCategoryData.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.MetadataKeyDataDefinition;
30 import org.openecomp.sdc.be.datatypes.category.SubCategoryDataDefinition;
31 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
32
33 public class SubCategoryData extends GraphNode {
34
35     private SubCategoryDataDefinition subCategoryDataDefinition;
36
37     public SubCategoryData(NodeTypeEnum label) {
38         super(label);
39         subCategoryDataDefinition = new SubCategoryDataDefinition();
40     }
41
42     public SubCategoryData(NodeTypeEnum label, SubCategoryDataDefinition subCategoryDataDefinition) {
43         super(label);
44         this.subCategoryDataDefinition = subCategoryDataDefinition;
45     }
46
47     public SubCategoryData(Map<String, Object> properties) {
48         this(NodeTypeEnum.getByName((String) properties.get(GraphPropertiesDictionary.LABEL.getProperty())));
49         subCategoryDataDefinition.setUniqueId((String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty()));
50         subCategoryDataDefinition.setNormalizedName((String) properties.get(GraphPropertiesDictionary.NORMALIZED_NAME.getProperty()));
51         subCategoryDataDefinition.setName((String) properties.get(GraphPropertiesDictionary.NAME.getProperty()));
52         Type listType = new TypeToken<List<String>>() {
53         }.getType();
54         List<String> iconsfromJson = getGson().fromJson((String) properties.get(GraphPropertiesDictionary.ICONS.getProperty()), listType);
55         subCategoryDataDefinition.setIcons(iconsfromJson);
56         final Type metadataKeylistType = new TypeToken<List<MetadataKeyDataDefinition>>() {
57         }.getType();
58         final List<MetadataKeyDataDefinition> metadataKeysfromJson = getGson()
59             .fromJson((String) properties.get(GraphPropertiesDictionary.METADATA_KEYS.getProperty()), metadataKeylistType);
60         subCategoryDataDefinition.setMetadataKeys(metadataKeysfromJson);
61     }
62
63     public SubCategoryDataDefinition getSubCategoryDataDefinition() {
64         return subCategoryDataDefinition;
65     }
66
67     @Override
68     public String getUniqueId() {
69         return subCategoryDataDefinition.getUniqueId();
70     }
71
72     @Override
73     public Map<String, Object> toGraphMap() {
74         Map<String, Object> map = new HashMap<>();
75         addIfExists(map, GraphPropertiesDictionary.UNIQUE_ID, subCategoryDataDefinition.getUniqueId());
76         addIfExists(map, GraphPropertiesDictionary.NAME, subCategoryDataDefinition.getName());
77         addIfExists(map, GraphPropertiesDictionary.NORMALIZED_NAME, subCategoryDataDefinition.getNormalizedName());
78         // String icons=getGson().toJson(subCategoryDataDefinition.getIcons());
79
80         // addIfExists(map, GraphPropertiesDictionary.ICONS, icons);
81         addIfExists(map, GraphPropertiesDictionary.ICONS, subCategoryDataDefinition.getIcons());
82         addIfExists(map, GraphPropertiesDictionary.METADATA_KEYS, subCategoryDataDefinition.getMetadataKeys());
83         return map;
84     }
85 }