2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.resources.data;
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.dao.utils.Constants;
27 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
28 import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
29 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
31 import java.lang.reflect.Type;
32 import java.util.HashMap;
33 import java.util.List;
36 public class PropertyData extends GraphNode {
38 PropertyDataDefinition propertyDataDefinition;
40 private List<String> constraints;
42 public PropertyData() {
43 super(NodeTypeEnum.Property);
44 propertyDataDefinition = new PropertyDataDefinition();
47 public PropertyData(PropertyDataDefinition propertyDataDefinition, List<String> constraints) {
48 super(NodeTypeEnum.Property);
49 this.propertyDataDefinition = propertyDataDefinition;
50 this.constraints = constraints;
53 public PropertyData(Map<String, Object> properties) {
57 propertyDataDefinition.setUniqueId((String) properties.get(GraphPropertiesDictionary.UNIQUE_ID.getProperty()));
59 propertyDataDefinition.setType((String) properties.get(GraphPropertiesDictionary.TYPE.getProperty()));
61 propertyDataDefinition.setRequired((Boolean) properties.get(GraphPropertiesDictionary.REQUIRED.getProperty()));
63 String defaultValue = (String) properties.get(GraphPropertiesDictionary.DEFAULT_VALUE.getProperty());
64 if (Constants.GRAPH_EMPTY_VALUE.equals(defaultValue)) {
65 propertyDataDefinition.setDefaultValue(null);
67 propertyDataDefinition.setDefaultValue(defaultValue);
70 propertyDataDefinition
71 .setDescription((String) properties.get(GraphPropertiesDictionary.DESCRIPTION.getProperty()));
73 Type listType = new TypeToken<List<String>>() {
75 List<String> constraintsfromJson = getGson()
76 .fromJson((String) properties.get(GraphPropertiesDictionary.CONSTRAINTS.getProperty()), listType);
77 setConstraints(constraintsfromJson);
78 // setConstraints((List<String>)
79 // properties.get(GraphPropertiesDictionary.CONSTRAINTS.getProperty()));
81 Type schemaType = new TypeToken<SchemaDefinition>() {
83 SchemaDefinition schema = getGson()
84 .fromJson((String) properties.get(GraphPropertiesDictionary.ENTRY_SCHEMA.getProperty()), schemaType);
85 propertyDataDefinition.setSchema(schema);
90 public Map<String, Object> toGraphMap() {
92 Map<String, Object> map = new HashMap<>();
94 addIfExists(map, GraphPropertiesDictionary.UNIQUE_ID, propertyDataDefinition.getUniqueId());
96 addIfExists(map, GraphPropertiesDictionary.TYPE, propertyDataDefinition.getType());
98 addIfExists(map, GraphPropertiesDictionary.DESCRIPTION, propertyDataDefinition.getDescription());
100 String defaultValue = propertyDataDefinition.getDefaultValue();
101 if (defaultValue == null) {
102 defaultValue = Constants.GRAPH_EMPTY_VALUE;
104 addIfExists(map, GraphPropertiesDictionary.DEFAULT_VALUE, defaultValue);
106 addIfExists(map, GraphPropertiesDictionary.REQUIRED, propertyDataDefinition.isRequired());
108 addIfExists(map, GraphPropertiesDictionary.CONSTRAINTS, getConstraints());
110 SchemaDefinition entrySchema = propertyDataDefinition.getSchema();
111 if (entrySchema != null) {
112 String entrySchemaStr = getGson().toJson(entrySchema);
113 addIfExists(map, GraphPropertiesDictionary.ENTRY_SCHEMA, entrySchemaStr);
115 // String constraintsAsJson = getGson().toJson(getConstraints());
116 // addIfExists(map, GraphPropertiesDictionary.CONSTRAINTS,
117 // constraintsAsJson);
122 public List<String> getConstraints() {
126 public void setConstraints(List<String> constraints) {
127 this.constraints = constraints;
131 public String getUniqueId() {
132 return propertyDataDefinition.getUniqueId();
135 public PropertyDataDefinition getPropertyDataDefinition() {
136 return propertyDataDefinition;
139 public void setPropertyDataDefinition(PropertyDataDefinition propertyDataDefinition) {
140 this.propertyDataDefinition = propertyDataDefinition;
144 public String toString() {
145 return "PropertyData [propertyDataDefinition=" + propertyDataDefinition + ", constraints=" + constraints + "]";