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