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