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