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