2b1343642890bb402347aa0d02e8e61586d1a400
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / entity / SpikeEventEntity.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * DataRouter
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 and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.onap.aai.datarouter.entity;
27
28 import java.io.IOException;
29 import java.io.Serializable;
30 import java.security.NoSuchAlgorithmException;
31 import java.sql.Timestamp;
32 import java.text.SimpleDateFormat;
33 import java.util.ArrayList;
34 import java.util.List;
35
36 import javax.json.Json;
37 import javax.json.JsonObject;
38
39 import org.onap.aai.datarouter.util.NodeUtils;
40
41 /**
42  * Note: SpikeEventEntity is a port forward of IndexDocument Has been renamed here to move forward
43  * with abstraction of document store technology.
44  */
45 public class SpikeEventEntity implements DocumentStoreDataEntity, Serializable {
46
47   private static final long serialVersionUID = -5188479658230319058L;
48
49   protected String entityType;
50   protected String entityPrimaryKeyName;
51   protected String entityPrimaryKeyValue;
52   protected ArrayList<String> searchTagCollection = new ArrayList<>();
53   protected ArrayList<String> searchTagIdCollection = new ArrayList<>();
54   protected ArrayList<String> crossEntityReferenceCollection = new ArrayList<>();
55   protected String lastmodTimestamp;
56   protected String link;
57
58   private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
59   /*
60    * Generated fields, leave the settings for junit overrides
61    */
62
63   // generated, SHA-256 digest
64   protected String id;
65
66   /*
67    * generated based on searchTagCollection values
68    */
69   protected String searchTags;
70   protected String searchTagIds;
71   protected String crossReferenceEntityValues;
72
73
74
75   private static String concatArray(List<String> list, char delimiter) {
76
77     if (list == null || list.isEmpty()) {
78       return "";
79     }
80
81     StringBuilder result = new StringBuilder(64);
82
83     boolean firstValue = true;
84
85     for (String item : list) {
86
87       if (firstValue) {
88         result.append(item);
89         firstValue = false;
90       } else {
91         result.append(delimiter).append(item);
92       }
93
94     }
95
96     return result.toString();
97
98   }
99
100   public SpikeEventEntity() {
101     SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
102     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
103     String currentFormattedTimeStamp = dateFormat.format(timestamp);
104     this.lastmodTimestamp = currentFormattedTimeStamp;
105   }
106
107   public void deriveFields() throws NoSuchAlgorithmException {
108     this.id = NodeUtils.generateUniqueShaDigest(link);
109     this.searchTags = concatArray(searchTagCollection, ';');
110     this.searchTagIds = concatArray(searchTagIdCollection, ';');
111     this.crossReferenceEntityValues = concatArray(crossEntityReferenceCollection, ';');
112   }
113
114
115   /*
116    * (non-Javadoc)
117    * 
118    * @see org.onap.aai.datarouter.entity.SpikeEventEntity#getAsJson()
119    */
120   @Override
121   public String getAsJson() throws IOException {
122
123     JsonObject obj = Json.createObjectBuilder().add("entityType", entityType)
124         .add("entityPrimaryKeyValue", entityPrimaryKeyValue).add("searchTagIDs", searchTagIds)
125         .add("searchTags", searchTags).add("crossEntityReferenceValues", crossReferenceEntityValues)
126         .add("lastmodTimestamp", lastmodTimestamp).add("link", link).build();
127
128     return obj.toString();
129   }
130
131
132   public void addSearchTagWithKey(String searchTag, String key) {
133     searchTagIdCollection.add(key);
134     searchTagCollection.add(searchTag);
135   }
136
137   public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
138     if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
139       crossEntityReferenceCollection.add(crossEntityReferenceValue);
140     }
141   }
142
143   public String getEntityType() {
144     return entityType;
145   }
146
147   public String getEntityPrimaryKeyName() {
148     return entityPrimaryKeyName;
149   }
150
151   public String getEntityPrimaryKeyValue() {
152     return entityPrimaryKeyValue;
153   }
154
155
156   /*
157    * (non-Javadoc)
158    * 
159    * @see org.onap.aai.datarouter.entity.SpikeEventEntity#getId()
160    */
161   @Override
162   public String getId() {
163     return id;
164   }
165
166   public ArrayList<String> getSearchTagCollection() {
167     return searchTagCollection;
168   }
169
170   public String getSearchTags() {
171     return searchTags;
172   }
173
174   public String getSearchTagIDs() {
175     return searchTagIds;
176   }
177
178   public void setSearchTagIDs(String searchTagIDs) {
179     this.searchTagIds = searchTagIDs;
180   }
181
182   public void setEntityType(String entityType) {
183     this.entityType = entityType;
184   }
185
186   public void setId(String id) {
187     this.id = id;
188   }
189
190   public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
191     this.searchTagCollection = searchTagCollection;
192   }
193
194   public void setSearchTags(String searchTags) {
195     this.searchTags = searchTags;
196   }
197
198   public ArrayList<String> getSearchTagIdCollection() {
199     return searchTagIdCollection;
200   }
201
202   public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
203     this.searchTagIdCollection = searchTagIdCollection;
204   }
205
206   public String getLastmodTimestamp() {
207     return lastmodTimestamp;
208   }
209
210   public void setLastmodTimestamp(String lastmodTimestamp) {
211     this.lastmodTimestamp = lastmodTimestamp;
212   }
213
214   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
215     this.entityPrimaryKeyName = entityPrimaryKeyName;
216   }
217
218   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
219     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
220   }
221   
222   public String getLink() {
223     return link;
224   }
225   
226   public void setLink(String link) {
227     this.link = link;
228   }
229
230
231
232   public String getCrossReferenceEntityValues() {
233     return crossReferenceEntityValues;
234   }
235
236   public void setCrossReferenceEntityValues(String crossReferenceEntityValues) {
237     this.crossReferenceEntityValues = crossReferenceEntityValues;
238   }
239
240   @Override
241   public String toString() {
242     return "SpikeEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
243         + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
244             : "")
245         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
246             : "")
247         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
248         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
249             : "")
250         + (crossEntityReferenceCollection != null
251             ? "crossEntityReferenceCollection=" + crossEntityReferenceCollection + ", " : "")
252         + "lastmodTimestamp=" + lastmodTimestamp + ", " + (id != null ? "id=" + id + ", " : "")
253         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
254         + (searchTagIds != null ? "searchTagIDs=" + searchTagIds + ", " : "")
255         + (crossReferenceEntityValues != null
256             ? "crossReferenceEntityValues=" + crossReferenceEntityValues : "")
257         + "]";
258   }
259
260 }