Update the dependencies to use project version
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / synchronizer / entity / SearchableEntity.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.synchronizer.entity;
24
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import com.fasterxml.jackson.databind.node.ObjectNode;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
32 import org.onap.aai.sparky.util.NodeUtils;
33
34 /**
35  * The Class SearchableEntity.
36  */
37 public class SearchableEntity extends IndexableEntity implements IndexDocument {
38   protected List<String> searchTagCollection = new ArrayList<String>();
39
40   /**
41    * @return the mapper
42    */
43   public ObjectMapper getMapper() {
44     return mapper;
45   }
46
47   /**
48    * @param mapper the mapper to set
49    */
50   public void setMapper(ObjectMapper mapper) {
51     this.mapper = mapper;
52   }
53
54   /**
55    * @param searchTagCollection the searchTagCollection to set
56    */
57   public void setSearchTagCollection(List<String> searchTagCollection) {
58     this.searchTagCollection = searchTagCollection;
59   }
60
61   /**
62    * @param searchTagIdCollection the searchTagIdCollection to set
63    */
64   public void setSearchTagIdCollection(List<String> searchTagIdCollection) {
65     this.searchTagIdCollection = searchTagIdCollection;
66   }
67
68   /**
69    * @param searchTags the searchTags to set
70    */
71   public void setSearchTags(String searchTags) {
72     this.searchTags = searchTags;
73   }
74
75   /**
76    * @param searchTagIDs the searchTagIDs to set
77    */
78   public void setSearchTagIDs(String searchTagIDs) {
79     this.searchTagIDs = searchTagIDs;
80   }
81
82   protected List<String> searchTagIdCollection = new ArrayList<String>();
83   protected ObjectMapper mapper = new ObjectMapper();
84
85   /**
86    * Instantiates a new searchable entity.
87    */
88   public SearchableEntity() {
89     super();
90   }
91
92   /**
93    * Instantiates a new searchable entity.
94    *
95    * @param loader the loader
96    */
97   public SearchableEntity(OxmModelLoader loader) {
98     super(loader);
99   }
100
101   /*
102    * Generated fields, leave the settings for junit overrides
103    */
104   protected String searchTags; // generated based on searchTagCollection values
105   protected String searchTagIDs;
106
107   /**
108    * Generates the sha based id.
109    */
110   public void generateId() {
111     this.id = NodeUtils.generateUniqueShaDigest(link);
112   }
113
114   /*
115    * (non-Javadoc)
116    * 
117    * @see org.onap.aai.sparky.synchronizer.entity.IndexDocument#deriveFields()
118    */
119   @Override
120   public void deriveFields() {
121
122     /*
123      * We'll try and create a unique identity key that we can use for differencing the previously
124      * imported record sets as we won't have granular control of what is created/removed and when.
125      * The best we can hope for is identification of resources by generated Id until the
126      * Identity-Service UUID is tagged against all resources, then we can use that instead.
127      */
128     generateId();
129     this.searchTags = NodeUtils.concatArray(searchTagCollection, ";");
130     this.searchTagIDs = NodeUtils.concatArray(this.searchTagIdCollection, ";");
131   }
132
133   /**
134    * Adds the search tag with key.
135    *
136    * @param searchTag the search tag
137    * @param searchTagKey the key associated with the search tag (key:value)
138    */
139   public void addSearchTagWithKey(String searchTag, String searchTagKey) {
140     searchTagIdCollection.add(searchTagKey);
141     searchTagCollection.add(searchTag);
142   }
143
144   public List<String> getSearchTagCollection() {
145     return searchTagCollection;
146   }
147
148   public String getSearchTags() {
149     return searchTags;
150   }
151
152   public String getSearchTagIDs() {
153     return searchTagIDs;
154   }
155
156   public List<String> getSearchTagIdCollection() {
157     return searchTagIdCollection;
158   }
159
160   @Override
161   public String getIndexDocumentJson() {
162     ObjectNode rootNode = mapper.createObjectNode();
163     rootNode.put("entityType", this.getEntityType());
164     rootNode.put("entityPrimaryKeyValue", this.getEntityPrimaryKeyValue());
165     rootNode.put("searchTagIDs", this.getSearchTagIDs());
166     rootNode.put("searchTags", this.getSearchTags());
167     rootNode.put("link", this.getLink());
168     rootNode.put("lastmodTimestamp", this.getEntityTimeStamp());
169     return rootNode.toString();
170   }
171
172   @Override
173   public ObjectNode getBulkImportEntity() {
174     // TODO Auto-generated method stub
175     return null;
176   }
177
178   /*
179    * (non-Javadoc)
180    * 
181    * @see java.lang.Object#toString()
182    */
183   @Override
184   public String toString() {
185     return "IndexDocument [" + (entityType != null ? "entityType=" + entityType + ", " : "")
186         + (entityPrimaryKeyValue != null ? "entityPrimaryKeyValue=" + entityPrimaryKeyValue + ", "
187             : "")
188         + (searchTagCollection != null ? "searchTagCollection=" + searchTagCollection + ", " : "")
189         + (searchTagIdCollection != null ? "searchTagIDCollection=" + searchTagIdCollection + ", "
190             : "")
191         + (mapper != null ? "mapper=" + mapper + ", " : "") + (id != null ? "id=" + id + ", " : "")
192         + (lastmodTimestamp != null ? "lastmodTimestamp=" + lastmodTimestamp + ", " : "")
193         + (searchTags != null ? "searchTags=" + searchTags + ", " : "")
194         + (searchTagIDs != null ? "searchTagIDs=" + searchTagIDs : "") + "]";
195   }
196 }