538d9c09e9ec2647d46204b54ce0ea07d538e72e
[aai/gizmo.git] / src / main / java / org / onap / crud / service / BulkPayload.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Gizmo
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
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
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  *
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23  */
24 package org.onap.crud.service;
25
26 import com.google.gson.Gson;
27 import com.google.gson.GsonBuilder;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31
32 import org.onap.crud.exception.CrudException;
33
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.Set;
38
39 import javax.ws.rs.core.Response.Status;
40
41 public class BulkPayload {
42   public enum OperationType {
43     CREATE, UPDATE, DELETE
44   }
45
46   private List<JsonElement> objects = new ArrayList<JsonElement>();
47   private List<JsonElement> relationships = new ArrayList<JsonElement>();
48
49   private static final Gson gson = new GsonBuilder().disableHtmlEscaping().create();
50
51   public String toJson() {
52     return gson.toJson(this);
53   }
54
55   public static BulkPayload fromJson(String payload) throws CrudException {
56     try {
57       if (payload == null || payload.isEmpty()) {
58         throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
59       }
60       return gson.fromJson(payload, BulkPayload.class);
61     } catch (Exception ex) {
62       throw new CrudException("Invalid Json Payload", Status.BAD_REQUEST);
63     }
64   }
65
66   public List<JsonElement> getObjects() {
67     return objects;
68   }
69
70   public void setObjects(List<JsonElement> objects) {
71     this.objects = objects;
72   }
73
74   public List<JsonElement> getRelationships() {
75     return relationships;
76   }
77
78   public void setRelationships(List<JsonElement> relationships) {
79     this.relationships = relationships;
80   }
81
82   @Override
83   public String toString() {
84     return "BulkPayload [objects=" + objects + ", relationships=" + relationships + "]";
85   }
86
87   public static void main(String[] args) throws Exception {
88     BulkPayload p = new BulkPayload();
89     JsonObject root = new JsonObject();
90     JsonArray vertices = new JsonArray();
91     JsonObject v1 = new JsonObject();
92     JsonObject v2 = new JsonObject();
93     JsonObject prop = new JsonObject();
94
95     prop.addProperty("p1", "value1");
96     prop.addProperty("p2", "value2");
97     v1.add("v1", prop);
98     v2.add("v2", prop);
99
100     vertices.add(v1);
101     vertices.add(v2);
102
103     root.add("objects", vertices);
104
105     String s = "{\"objects\":[{\"v1\":{\"p1\":\"value1\",\"p2\":\"value2\"}},{\"v2\":{\"p1\":\"value1\",\"p2\":\"value2\"}}]}";
106
107     p = BulkPayload.fromJson(s);
108
109     List<JsonElement> po = p.getObjects();
110     List<String> ids = new ArrayList<String>();
111     for (JsonElement e : po) {
112       Set<Map.Entry<String, JsonElement>> entries = e.getAsJsonObject().entrySet();
113
114       for (Map.Entry<String, JsonElement> entry : entries) {
115         ids.add(entry.getKey());
116       }
117     }
118
119     System.out.println("root: " + root.toString());
120     System.out.println("payload ids: " + ids.toString());
121
122   }
123
124 }