2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.datarouter.entity;
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;
32 import javax.json.Json;
33 import javax.json.JsonObject;
35 import org.onap.aai.datarouter.util.NodeUtils;
38 * Note: AAIEventEntity is a port forward of IndexDocument Has been renamed here to move forward
39 * with abstraction of document store technology.
41 public class AaiEventEntity implements DocumentStoreDataEntity, Serializable {
43 private static final long serialVersionUID = -5188479658230319058L;
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;
54 private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";
56 * Generated fields, leave the settings for junit overrides
59 // generated, SHA-256 digest
63 * generated based on searchTagCollection values
65 protected String searchTags;
66 protected String searchTagIds;
67 protected String crossReferenceEntityValues;
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]));
75 return hexString.toString();
78 private static String concatArray(List<String> list, char delimiter) {
80 if (list == null || list.isEmpty()) {
84 StringBuilder result = new StringBuilder(64);
86 boolean firstValue = true;
88 for (String item : list) {
94 result.append(delimiter).append(item);
99 return result.toString();
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;
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, ';');
121 * @see org.onap.aai.datarouter.entity.AAIEventEntity#getAsJson()
124 public String getAsJson() throws IOException {
126 JsonObject obj = Json.createObjectBuilder().add("entityType", entityType)
127 .add("entityPrimaryKeyValue", entityPrimaryKeyValue).add("searchTagIDs", searchTagIds)
128 .add("searchTags", searchTags).add("crossEntityReferenceValues", crossReferenceEntityValues)
129 .add("lastmodTimestamp", lastmodTimestamp).add("link", link).build();
131 return obj.toString();
135 public void addSearchTagWithKey(String searchTag, String key) {
136 searchTagIdCollection.add(key);
137 searchTagCollection.add(searchTag);
140 public void addCrossEntityReferenceValue(String crossEntityReferenceValue) {
141 if (!crossEntityReferenceCollection.contains(crossEntityReferenceValue)) {
142 crossEntityReferenceCollection.add(crossEntityReferenceValue);
146 public String getEntityType() {
150 public String getEntityPrimaryKeyName() {
151 return entityPrimaryKeyName;
154 public String getEntityPrimaryKeyValue() {
155 return entityPrimaryKeyValue;
162 * @see org.onap.aai.datarouter.entity.AAIEventEntity#getId()
165 public String getId() {
169 public ArrayList<String> getSearchTagCollection() {
170 return searchTagCollection;
173 public String getSearchTags() {
177 public String getSearchTagIDs() {
181 public void setSearchTagIDs(String searchTagIDs) {
182 this.searchTagIds = searchTagIDs;
185 public void setEntityType(String entityType) {
186 this.entityType = entityType;
189 public void setId(String id) {
193 public void setSearchTagCollection(ArrayList<String> searchTagCollection) {
194 this.searchTagCollection = searchTagCollection;
197 public void setSearchTags(String searchTags) {
198 this.searchTags = searchTags;
201 public ArrayList<String> getSearchTagIdCollection() {
202 return searchTagIdCollection;
205 public void setSearchTagIdCollection(ArrayList<String> searchTagIdCollection) {
206 this.searchTagIdCollection = searchTagIdCollection;
209 public String getLastmodTimestamp() {
210 return lastmodTimestamp;
213 public void setLastmodTimestamp(String lastmodTimestamp) {
214 this.lastmodTimestamp = lastmodTimestamp;
217 public void setEntityPrimaryKeyName(String entityPrimaryKeyName) {
218 this.entityPrimaryKeyName = entityPrimaryKeyName;
221 public void setEntityPrimaryKeyValue(String entityPrimaryKeyValue) {
222 this.entityPrimaryKeyValue = entityPrimaryKeyValue;
225 public String getLink() {
229 public void setLink(String link) {
234 * public void mergeEntity(AAIEventEntity entityToMergeIn) {
236 * if ( entityToMergeIn == null ) { return; }
238 * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
239 * entityToMergeIn.getEntityType(); }
241 * if ( !entityToMergeIn.getEntityType().equals( entityType )) { entityType =
242 * entityToMergeIn.getEntityType(); }
247 public String getCrossReferenceEntityValues() {
248 return crossReferenceEntityValues;
251 public void setCrossReferenceEntityValues(String crossReferenceEntityValues) {
252 this.crossReferenceEntityValues = crossReferenceEntityValues;
256 public String toString() {
257 return "AAIEventEntity [" + (entityType != null ? "entityType=" + entityType + ", " : "")
258 + (entityPrimaryKeyName != null ? "entityPrimaryKeyName=" + entityPrimaryKeyName + ", "
260 + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
262 + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
263 + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
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 : "")