Update the dependencies to use project version
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / synchronizer / entity / SuggestionSearchEntity.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.JsonNode;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.fasterxml.jackson.databind.node.ObjectNode;
28
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
34
35 import org.json.JSONArray;
36 import org.json.JSONObject;
37 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
38 import org.onap.aai.sparky.util.NodeUtils;
39
40 public class SuggestionSearchEntity extends IndexableEntity implements IndexDocument {
41
42   private String entityType;
43   private List<String> suggestionConnectorWords = new ArrayList<String>();
44   private List<String> suggestionAttributeTypes = new ArrayList<String>();
45
46   /**
47    * @return the suggestionAttributeTypes
48    */
49   public List<String> getSuggestionAttributeTypes() {
50     return suggestionAttributeTypes;
51   }
52
53   /**
54    * @param suggestionAttributeTypes the suggestionAttributeTypes to set
55    */
56   public void setSuggestionAttributeTypes(List<String> suggestionAttributeTypes) {
57     this.suggestionAttributeTypes = suggestionAttributeTypes;
58   }
59
60   /**
61    * @return the suggestionTypeAliases
62    */
63   public List<String> getSuggestionTypeAliases() {
64     return suggestionTypeAliases;
65   }
66
67   /**
68    * @param suggestionTypeAliases the suggestionTypeAliases to set
69    */
70   public void setSuggestionTypeAliases(List<String> suggestionTypeAliases) {
71     this.suggestionTypeAliases = suggestionTypeAliases;
72   }
73
74   /**
75    * @return the suggestableAttr
76    */
77   public List<String> getSuggestableAttr() {
78     return suggestableAttr;
79   }
80
81   /**
82    * @param suggestableAttr the suggestableAttr to set
83    */
84   public void setSuggestableAttr(List<String> suggestableAttr) {
85     this.suggestableAttr = suggestableAttr;
86   }
87
88   /**
89    * @return the outputString
90    */
91   public StringBuffer getOutputString() {
92     return outputString;
93   }
94
95   /**
96    * @param outputString the outputString to set
97    */
98   public void setOutputString(StringBuffer outputString) {
99     this.outputString = outputString;
100   }
101
102   /**
103    * @return the mapper
104    */
105   public ObjectMapper getMapper() {
106     return mapper;
107   }
108
109   /**
110    * @param mapper the mapper to set
111    */
112   public void setMapper(ObjectMapper mapper) {
113     this.mapper = mapper;
114   }
115
116
117   private List<String> suggestionAttributeValues = new ArrayList<String>();
118   private List<String> suggestionTypeAliases = new ArrayList<String>();
119   private List<String> suggestionInputPermutations = new ArrayList<String>();
120   private List<String> suggestableAttr = new ArrayList<String>();
121   private Map<String, String> payload = new HashMap<String, String>();
122   private JSONObject payloadJsonNode = new JSONObject();
123   private StringBuffer outputString = new StringBuffer();
124   private String aliasToUse;
125
126   public Map<String, String> getPayload() {
127     return payload;
128   }
129
130   public void setPayload(Map<String, String> payload) {
131     this.payload = payload;
132   }
133
134
135   public JSONObject getPayloadJsonNode() {
136     return payloadJsonNode;
137   }
138
139   public void setPayloadJsonNode(JSONObject payloadJsonNode) {
140     this.payloadJsonNode = payloadJsonNode;
141   }
142
143
144   protected ObjectMapper mapper = new ObjectMapper();
145
146   public SuggestionSearchEntity() {
147     super();
148   }
149
150   public void setSuggestableAttr(ArrayList<String> attributes) {
151     for (String attribute : attributes) {
152       this.suggestableAttr.add(attribute);
153     }
154   }
155
156   public void setPayloadFromResponse(JsonNode node) {
157     Map<String, String> nodePayload = new HashMap<String, String>();
158     if (suggestableAttr != null) {
159       for (String attribute : suggestableAttr) {
160         if (node.get(attribute) != null) {
161           nodePayload.put(attribute, node.get(attribute).asText());
162         }
163       }
164       this.setPayload(nodePayload);
165     }
166   }
167
168
169   public SuggestionSearchEntity(OxmModelLoader loader) {
170     super(loader);
171   }
172
173   @Override
174   public String getEntityType() {
175     return entityType;
176   }
177
178   @Override
179   public void setEntityType(String entityType) {
180     this.entityType = entityType;
181   }
182
183   public List<String> getSuggestionConnectorWords() {
184     return suggestionConnectorWords;
185   }
186
187   public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
188     this.suggestionConnectorWords = suggestionConnectorWords;
189   }
190
191   public List<String> getSuggestionPropertyTypes() {
192     return this.suggestionAttributeTypes;
193   }
194
195   public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
196     this.suggestionAttributeTypes = suggestionPropertyTypes;
197   }
198
199   public List<String> getSuggestionAttributeValues() {
200     return this.suggestionAttributeValues;
201   }
202
203   public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
204     this.suggestionAttributeValues = suggestionAttributeValues;
205   }
206
207   public List<String> getSuggestionAliases() {
208     return this.suggestionTypeAliases;
209   }
210
211   public void setSuggestionAliases(List<String> suggestionAliases) {
212     this.suggestionTypeAliases = suggestionAliases;
213   }
214
215   public List<String> getSuggestionInputPermutations() {
216     return this.suggestionInputPermutations;
217   }
218
219   public void setSuggestionInputPermutations(List<String> permutations) {
220     this.suggestionInputPermutations = permutations;
221   }
222
223   public void generateSuggestionInputPermutations() {
224
225
226     List<String> entityNames = new ArrayList<>();
227     entityNames.add(entityType);
228     HashMap<String, String> desc = loader.getOxmModel().get(this.entityType);
229     String attr = desc.get("suggestionAliases");
230     String[] suggestionAliasesArray = attr.split(",");
231     suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
232     this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
233     for (String alias : suggestionTypeAliases) {
234       entityNames.add(alias);
235     }
236     ArrayList<String> listOfSearchSuggestionPermutations = new ArrayList<>();
237
238     ArrayList<String> listToPermutate = new ArrayList<>(payload.values());
239
240     for (String entityName : entityNames) {
241       listToPermutate.add(entityName);
242       permutateList(listToPermutate, new ArrayList<String>(), listToPermutate.size(),
243           listOfSearchSuggestionPermutations);
244       listToPermutate.remove(entityName);
245     }
246     suggestionInputPermutations = listOfSearchSuggestionPermutations;
247   }
248
249   /**
250    * Generate all permutations of a list of Strings
251    * 
252    * @param list
253    * @param permutation
254    * @param size
255    */
256   private void permutateList(List<String> list, List<String> permutation, int size,
257       List<String> listOfSearchSuggestionPermutationList) {
258     if (permutation.size() == size) {
259       StringBuilder newPermutation = new StringBuilder();
260
261       for (int i = 0; i < permutation.size(); i++) {
262         newPermutation.append(permutation.get(i)).append(" ");
263       }
264
265       listOfSearchSuggestionPermutationList.add(newPermutation.toString().trim());
266
267       return;
268     }
269
270     String[] availableItems = list.toArray(new String[0]);
271
272     for (String i : availableItems) {
273       permutation.add(i);
274       list.remove(i);
275       permutateList(list, permutation, size, listOfSearchSuggestionPermutationList);
276       list.add(i);
277       permutation.remove(i);
278     }
279   }
280
281   public boolean isSuggestableDoc() {
282     return this.getPayload().size() != 0;
283   }
284
285
286   @Override
287   public void deriveFields() {
288
289     int payloadEntryCounter = 1;
290     for (Map.Entry<String, String> payload : getPayload().entrySet()) {
291       // Add the payload(status) only if a valid value is present
292       if (payload.getValue() != null && payload.getValue().length() > 0) {
293         this.getPayloadJsonNode().put(payload.getKey(), payload.getValue());
294         this.outputString.append(payload.getValue());
295         if (payloadEntryCounter < getPayload().entrySet().size()) {
296           this.outputString.append(" and ");
297         } else {
298           this.outputString.append(" ");
299         }
300       }
301       payloadEntryCounter++;
302     }
303
304     this.outputString.append(this.getAliasToUse());
305     this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
306   }
307
308   @Override
309   public String getIndexDocumentJson() {
310     // TODO Auto-generated method stub
311     JSONObject rootNode = new JSONObject();
312
313     JSONArray suggestionsArray = new JSONArray();
314     for (String suggestion : suggestionInputPermutations) {
315       suggestionsArray.put(suggestion);
316     }
317
318     JSONObject entitySuggest = new JSONObject();
319
320     entitySuggest.put("input", suggestionsArray);
321     entitySuggest.put("output", this.outputString);
322     entitySuggest.put("payload", this.payloadJsonNode);
323     rootNode.put("entity_suggest", entitySuggest);
324
325     return rootNode.toString();
326   }
327
328   @Override
329   public ObjectNode getBulkImportEntity() {
330     // TODO Auto-generated method stub
331     return null;
332   }
333
334   public String getAliasToUse() {
335     return aliasToUse;
336   }
337
338   public void setAliasToUse(String aliasToUse) {
339     this.aliasToUse = aliasToUse;
340   }
341
342   @Override
343   public String toString() {
344     return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
345         + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
346         + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
347         + suggestionTypeAliases + ", mapper=" + mapper + "]";
348   }
349 }