Fixed a typo in a name
[aai/data-router.git] / src / main / java / org / onap / aai / datarouter / entity / AaiEventEntity.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.aai.datarouter.entity;
22
23 import java.io.IOException;
24 import java.io.Serializable;
25 import java.security.MessageDigest;
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: AAIEventEntity is a port forward of IndexDocument Has been renamed here to move forward
39  * with abstraction of document store technology.
40  */
41 public class AaiEventEntity 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   private static String convertBytesToHexString(byte[] bytesToConvert) {
71     StringBuilder hexString = new StringBuilder();
72     for (int i = 0; i < bytesToConvert.length; i++) {
73       hexString.append(Integer.toHexString(0xFF & bytesToConvert[i]));
74     }
75     return hexString.toString();
76   }
77
78   private static String concatArray(List<String> list, char delimiter) {
79
80     if (list == null || list.isEmpty()) {
81       return "";
82     }
83
84     StringBuilder result = new StringBuilder(64);
85
86     boolean firstValue = true;
87
88     for (String item : list) {
89
90       if (firstValue) {
91         result.append(item);
92         firstValue = false;
93       } else {
94         result.append(delimiter).append(item);
95       }
96
97     }
98
99     return result.toString();
100
101   }
102
103   public AaiEventEntity() {
104     SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT);
105     Timestamp timestamp = new Timestamp(System.currentTimeMillis());
106     String currentFormattedTimeStamp = dateFormat.format(timestamp);
107     this.lastmodTimestamp = currentFormattedTimeStamp;
108   }
109
110   public void deriveFields() throws NoSuchAlgorithmException {
111     this.id = NodeUtils.generateUniqueShaDigest(link);
112     this.searchTags = concatArray(searchTagCollection, ';');
113     this.searchTagIds = concatArray(searchTagIdCollection, ';');
114     this.crossReferenceEntityValues = concatArray(crossEntityReferenceCollection, ';');
115   }
116
117
118   /*
119    * (non-Javadoc)
120    * 
121    * @see org.onap.aai.datarouter.entity.AAIEventEntity#getAsJson()
122    */
123   @Override
124   public String getAsJson() throws IOException {
125
126     JsonObject obj = Json.createObjectBuilder().add("entityType", entityType)
127         .add("entityPrimaryKeyValue", entityPrimaryKeyValue).add("searchTagIDs", searchTagIds)
128         .add("searchTags", searchTags).add("crossReferenceEntityValues", crossReferenceEntityValues)
129         .add("lastmodTimestamp", lastmodTimestamp).add("link", link).build();
130
131     return obj.toString();
132   }
133
134
135   public void addSearchTagWithKey(String searchTag, String key) {
136     searchTagIdCollection.add(key);
137     searchTagCollection.add(searchTag);
138   }
139
140   public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
141     if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
142       crossEntityReferenceCollection.add(crossEntityReferenceValue);
143     }
144   }
145
146   public String getEntityType() {
147     return entityType;
148   }
149
150   public String getEntityPrimaryKeyName() {
151     return entityPrimaryKeyName;
152   }
153
154   public String getEntityPrimaryKeyValue() {
155     return entityPrimaryKeyValue;
156   }
157
158
159   /*
160    * (non-Javadoc)
161    * 
162    * @see org.onap.aai.datarouter.entity.AAIEventEntity#getId()
163    */
164   @Override
165   public String getId() {
166     return id;
167   }
168
169   public ArrayList<String> getSearchTagCollection() {
170     return searchTagCollection;
171   }
172
173   public String getSearchTags() {
174     return searchTags;
175   }
176
177   public String getSearchTagIDs() {
178     return searchTagIds;
179   }
180
181   public void setSearchTagIDs(String searchTagIDs) {
182     this.searchTagIds = searchTagIDs;
183   }
184
185   public void setEntityType(String entityType) {
186     this.entityType = entityType;
187   }
188
189   public void setId(String id) {
190     this.id = id;
191   }
192
193   public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
194     this.searchTagCollection = searchTagCollection;
195   }
196
197   public void setSearchTags(String searchTags) {
198     this.searchTags = searchTags;
199   }
200
201   public ArrayList<String> getSearchTagIdCollection() {
202     return searchTagIdCollection;
203   }
204
205   public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
206     this.searchTagIdCollection = searchTagIdCollection;
207   }
208
209   public String getLastmodTimestamp() {
210     return lastmodTimestamp;
211   }
212
213   public void setLastmodTimestamp(String lastmodTimestamp) {
214     this.lastmodTimestamp = lastmodTimestamp;
215   }
216
217   public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
218     this.entityPrimaryKeyName = entityPrimaryKeyName;
219   }
220
221   public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
222     this.entityPrimaryKeyValue = entityPrimaryKeyValue;
223   }
224   
225   public String getLink() {
226     return link;
227   }
228   
229   public void setLink(String link) {
230     this.link = link;
231   }
232
233   /*
234    * public void mergeEntity(AAIEventEntity entityToMergeIn) {
235    * 
236    * if ( entityToMergeIn == null ) { return; }
237    * 
238    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
239    * entityToMergeIn.getEntityType(); }
240    * 
241    * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
242    * entityToMergeIn.getEntityType(); }
243    * 
244    * }
245    */
246
247   public String getCrossReferenceEntityValues() {
248     return crossReferenceEntityValues;
249   }
250
251   public void setCrossReferenceEntityValues(String crossReferenceEntityValues) {
252     this.crossReferenceEntityValues = crossReferenceEntityValues;
253   }
254
255   @Override
256   public String toString() {
257     return "AAIEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
258         + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
259             : "")
260         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
261             : "")
262         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
263         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
264             : "")
265         + (crossEntityReferenceCollection != null
266             ? "crossEntityReferenceCollection=" + crossEntityReferenceCollection + ", " : "")
267         + "lastmodTimestamp=" + lastmodTimestamp + ", " + (id != null ? "id=" + id + ", " : "")
268         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
269         + (searchTagIds != null ? "searchTagIDs=" + searchTagIds + ", " : "")
270         + (crossReferenceEntityValues != null
271             ? "crossReferenceEntityValues=" + crossReferenceEntityValues : "")
272         + "]";
273   }
274
275 }