Remove Multiplicity feature
[aai/gizmo.git] / src / main / java / org / onap / crud / entity / Vertex.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21 package org.onap.crud.entity;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import org.eclipse.persistence.dynamic.DynamicType;
29 import org.eclipse.persistence.internal.helper.DatabaseField;
30 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
31 import org.eclipse.persistence.mappings.DatabaseMapping;
32 import org.json.JSONArray;
33 import org.json.JSONObject;
34 import org.onap.crud.exception.CrudException;
35 import org.onap.crud.util.CrudServiceUtil;
36 import org.onap.schema.OxmModelLoader;
37 import org.onap.schema.OxmModelValidator;
38
39 import com.google.common.base.CaseFormat;
40 import com.google.gson.Gson;
41 import com.google.gson.GsonBuilder;
42 import com.google.gson.annotations.SerializedName;
43 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
44
45 public class Vertex {
46   private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
47       .create();
48
49   @SerializedName(value = "id", alternate = { "key" })
50   private final Optional<String> id;
51
52   private final String type;
53   private final Map<String, Object> properties;
54
55   private Vertex(Builder builder) {
56     this.id = builder.id;
57     this.type = builder.type;
58     this.properties = builder.properties;
59   }
60
61   public static class Builder {
62     private Optional<String> id = Optional.empty();
63     private final String type;
64     private final Map<String, Object> properties = new HashMap<String, Object>();
65
66     public Builder(String type) {
67       if (type == null) {
68         throw new IllegalArgumentException("Type cannot be null");
69       }
70       this.type = type;
71     }
72
73     public Builder id(String id) {
74       if (id == null) {
75         throw new IllegalArgumentException("id cannot be null");
76       }
77
78       this.id = Optional.of(id);
79       return this;
80     }
81
82     public Builder property(String key, Object value) {
83       if (key == null || value == null) {
84         throw new IllegalArgumentException("Property key/value cannot be null");
85       }
86       properties.put(key, value);
87       return this;
88     }
89
90     public Vertex build() {
91       return new Vertex(this);
92     }
93   }
94
95   public String toJson() {
96     return gson.toJson(this);
97   }
98
99   public String toJson(Gson customGson) {
100     return customGson.toJson(this);
101   }
102
103   public static Vertex fromJson(String jsonString, String version) throws CrudException {
104     JSONObject doc = new JSONObject(jsonString);
105     return fromJson(doc, version);
106   }
107
108   public static Vertex fromJson(JSONObject jsonObject, String version) throws CrudException {
109     Builder builder;
110
111     try {
112       String type = jsonObject.getString("type");
113       builder = new Builder(type).id(jsonObject.getString("key"));
114
115       type = OxmModelValidator.resolveCollectionType(version, type);
116       DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
117       
118       final DynamicType modelObjectType = OxmModelLoader.getDynamicTypeForVersion(version, type);
119       final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
120
121
122       if (modelObjectType == null) {
123         throw new CrudException("Unable to load oxm version", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
124       }
125
126       if (jsonObject.has("properties")) {
127         JSONObject jsonProps = jsonObject.getJSONObject("properties");
128         for (String key : jsonProps.keySet()) {
129           String keyJavaName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key);
130           DatabaseMapping mapping = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName);
131
132           if (mapping == null) {
133             // This might be one of the reserved properties
134             mapping = reservedType.getDescriptor().getMappingForAttributeName(keyJavaName);
135           }
136
137           if (mapping != null) {
138             DatabaseField field = mapping.getField();
139             Object value = CrudServiceUtil.validateFieldType(jsonProps.get(key).toString(), field.getType());
140             builder.property(key, value);
141           }
142         }
143       }
144     }
145     catch (Exception ex) {
146       throw new CrudException("Unable to transform response: " + jsonObject.toString(), javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
147     }
148
149     return builder.build();
150   }
151
152   public static List<Vertex> collectionFromJson(String jsonString, String version) throws CrudException {
153     List<Vertex> result = new ArrayList<>();
154     JSONArray array = new JSONArray(jsonString);
155
156     for (Object jsonObject : array) {
157       result.add(Vertex.fromJson((JSONObject)jsonObject, version));
158     }
159
160     return result;
161   }
162
163   @Override
164   public String toString() {
165     return "Vertex [id=" + id + ", type=" + type + ", properties=" + properties + "]";
166   }
167
168   public Optional<String> getId() {
169     return id;
170   }
171
172   public String getType() {
173     return type;
174   }
175
176   public Map<String, Object> getProperties() {
177     return properties;
178   }
179
180 }