2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.crud.entity;
26 import net.dongliu.gson.GsonJava8TypeAdapterFactory;
28 import com.google.common.base.CaseFormat;
29 import com.google.gson.Gson;
30 import com.google.gson.GsonBuilder;
31 import com.google.gson.annotations.SerializedName;
33 import java.util.HashMap;
35 import java.util.Optional;
38 import org.eclipse.persistence.dynamic.DynamicType;
39 import org.eclipse.persistence.internal.helper.DatabaseField;
40 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
41 import org.eclipse.persistence.mappings.DatabaseMapping;
42 import org.json.JSONObject;
43 import org.onap.aaiutils.oxm.OxmModelLoader;
44 import org.onap.crud.exception.CrudException;
45 import org.onap.crud.util.CrudServiceUtil;
46 import org.onap.schema.OxmModelValidator;
49 private static final Gson gson = new GsonBuilder().registerTypeAdapterFactory(new GsonJava8TypeAdapterFactory())
52 @SerializedName(value = "id", alternate = { "key" })
53 private final Optional<String> id;
55 private final String type;
56 private final Map<String, Object> properties;
58 private Vertex(Builder builder) {
60 this.type = builder.type;
61 this.properties = builder.properties;
64 public static class Builder {
65 private Optional<String> id = Optional.empty();
66 private final String type;
67 private final Map<String, Object> properties = new HashMap<String, Object>();
69 public Builder(String type) {
71 throw new IllegalArgumentException("Type cannot be null");
76 public Builder id(String id) {
78 throw new IllegalArgumentException("id cannot be null");
81 this.id = Optional.of(id);
85 public Builder property(String key, Object value) {
86 if (key == null || value == null) {
87 throw new IllegalArgumentException("Property key/value cannot be null");
89 properties.put(key, value);
93 public Vertex build() {
94 return new Vertex(this);
98 public String toJson() {
99 return gson.toJson(this);
102 public String toJson(Gson customGson) {
103 return customGson.toJson(this);
106 public static Vertex fromJson(String jsonString, String version) throws CrudException {
110 JSONObject doc = new JSONObject(jsonString);
111 String type = doc.getString("type");
112 builder = new Builder(type).id(doc.getString("key"));
114 type = OxmModelValidator.resolveCollectionType(version, type);
115 DynamicJAXBContext jaxbContext = OxmModelLoader.getContextForVersion(version);
116 String modelObjectClass = CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, type));
118 final DynamicType modelObjectType = jaxbContext.getDynamicType(modelObjectClass);
119 final DynamicType reservedType = jaxbContext.getDynamicType("ReservedPropNames");
122 if (modelObjectType == null) {
123 throw new CrudException("Unable to load oxm version", javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
126 if (doc.has("properties")) {
127 JSONObject jsonProps = doc.getJSONObject("properties");
128 for (String key : (Set<String>)jsonProps.keySet()) {
129 String keyJavaName = CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_CAMEL, key);
130 DatabaseMapping mapping = modelObjectType.getDescriptor().getMappingForAttributeName(keyJavaName);
132 if (mapping == null) {
133 // This might be one of the reserved properties
134 mapping = reservedType.getDescriptor().getMappingForAttributeName(keyJavaName);
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);
145 catch (Exception ex) {
146 throw new CrudException("Unable to transform response: " + jsonString, javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR);
149 return builder.build();
153 public String toString() {
154 return "Vertex [id=" + id + ", type=" + type + ", properties=" + properties + "]";
157 public Optional<String> getId() {
161 public String getType() {
165 public Map<String, Object> getProperties() {