Update license date and text
[aai/gizmo.git] / src / main / java / org / onap / crud / event / GraphEvent.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.event;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import com.google.gson.annotations.SerializedName;
26
27 import org.onap.crud.exception.CrudException;
28
29 import javax.ws.rs.core.Response.Status;
30
31 public class GraphEvent {
32
33   public enum GraphEventOperation {
34     CREATE, UPDATE, DELETE
35   }
36
37   public enum GraphEventResult {
38     SUCCESS, FAILURE
39   }
40
41   private GraphEventOperation operation;
42
43   @SerializedName("transaction-id")
44   private String transactionId;
45   
46   @SerializedName("database-transaction-id")
47   private String dbTransactionId;
48
49   private long timestamp;
50
51   private GraphEventVertex vertex;
52
53   private GraphEventEdge edge;
54
55   private GraphEventResult result;
56
57   @SerializedName("error-message")
58   private String errorMessage;
59
60   private Status httpErrorStatus;
61
62   /**
63    * Marshaller/unmarshaller for converting to/from JSON.
64    */
65   private static final Gson gson = new GsonBuilder().disableHtmlEscaping()
66       .setPrettyPrinting().create();
67
68   public static Builder builder(GraphEventOperation operation) {
69     return new Builder(operation);
70   }
71
72   public GraphEventOperation getOperation() {
73     return operation;
74   }
75
76   public String getTransactionId() {
77     return transactionId;
78   }
79   
80   public String getDbTransactionId() {
81     return dbTransactionId;
82   }
83   
84   public void setDbTransactionId(String id) {
85     dbTransactionId = id;
86   }
87
88   public long getTimestamp() {
89     return timestamp;
90   }
91
92   public GraphEventVertex getVertex() {
93     return vertex;
94   }
95
96   public GraphEventEdge getEdge() {
97     return edge;
98   }
99
100   public GraphEventResult getResult() {
101     return result;
102   }
103
104   public String getErrorMessage() {
105     return errorMessage;
106   }
107
108   public void setResult(GraphEventResult result) {
109     this.result = result;
110   }
111
112
113   public Status getHttpErrorStatus() {
114     return httpErrorStatus;
115   }
116
117   public void setHttpErrorStatus(Status httpErrorStatus) {
118     this.httpErrorStatus = httpErrorStatus;
119   }
120
121   public void setTimestamp(long timestamp) {
122     this.timestamp = timestamp;
123   }
124
125   public void setErrorMessage(String errorMessage) {
126     this.errorMessage = errorMessage;
127   }
128
129   public void setVertex(GraphEventVertex vertex) {
130     this.vertex = vertex;
131   }
132
133   public void setEdge(GraphEventEdge edge) {
134     this.edge = edge;
135   }
136
137   /**
138    * Unmarshalls this Vertex object into a JSON string.
139    *
140    * @return - A JSON format string representation of this Vertex.
141    */
142   public String toJson() {
143     return gson.toJson(this);
144   }
145
146   /**
147    * Marshalls the provided JSON string into a Vertex object.
148    *
149    * @param json - The JSON string to produce the Vertex from.
150    * @return - A Vertex object.
151    * @throws SpikeException
152    */
153   public static GraphEvent fromJson(String json) throws CrudException {
154
155     try {
156
157       // Make sure that we were actually provided a non-empty string
158       // before we
159       // go any further.
160       if (json == null || json.isEmpty()) {
161         throw new CrudException("Empty or null JSON string.", Status.BAD_REQUEST);
162       }
163
164       // Marshall the string into a Vertex object.
165       return gson.fromJson(json, GraphEvent.class);
166
167     } catch (Exception ex) {
168       throw new CrudException("Unable to parse JSON string: "+json, Status.BAD_REQUEST);
169     }
170   }
171
172   @Override
173   public String toString() {
174
175     return toJson();
176   }
177
178   public String getObjectKey() {
179     if (this.getVertex() != null) {
180       return this.getVertex().getId();
181     } else if (this.getEdge() != null) {
182       return this.getEdge().getId();
183     }
184     
185     return null;
186   }
187
188   public String getObjectType() {
189     if (this.getVertex() != null) {
190       return "vertex->" + this.getVertex().getType();
191     } else if (this.getEdge() != null) {
192       return "edge->" + this.getEdge().getType();
193     }
194
195     return null;
196   }
197
198   public static class Builder {
199
200     GraphEvent event = null;
201
202     public Builder(GraphEventOperation operation) {
203       event = new GraphEvent();
204       event.operation = operation;
205     }
206
207     public Builder vertex(GraphEventVertex vertex) {
208       event.vertex = vertex;
209       return this;
210     }
211
212     public Builder edge(GraphEventEdge edge) {
213       event.edge = edge;
214       return this;
215     }
216
217     public Builder result(GraphEventResult result) {
218       event.result = result;
219       return this;
220     }
221
222     public Builder errorMessage(String errorMessage) {
223       event.errorMessage = errorMessage;
224       return this;
225     }
226
227     public Builder httpErrorStatus(Status httpErrorStatus) {
228       event.httpErrorStatus = httpErrorStatus;
229       return this;
230     }
231
232     public GraphEvent build() {
233
234       event.timestamp = System.currentTimeMillis();
235       event.transactionId = java.util.UUID.randomUUID().toString();
236
237       return event;
238     }
239   }
240
241 }