UI Exensibility config cleanup
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / sync / 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.sync.entity;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33 import org.onap.aai.sparky.config.oxm.SuggestionEntityLookup;
34 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
35 import org.onap.aai.sparky.search.filters.config.FiltersDetailsConfig;
36 import org.onap.aai.sparky.search.filters.config.UiFilterConfig;
37 import org.onap.aai.sparky.util.NodeUtils;
38 import org.onap.aai.sparky.util.SuggestionsPermutation;
39
40 import com.fasterxml.jackson.databind.JsonNode;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42
43 public class SuggestionSearchEntity extends IndexableEntity implements IndexDocument {
44   private static final String FILTER_ID = "filterId";
45   private static final String FILTER_VALUE = "filterValue"; 
46   private static final String FILTER_LIST = "filterList"; 
47   
48   private String entityType;
49   private List<String> suggestionConnectorWords = new ArrayList<String>();
50   private List<String> suggestionAttributeTypes = new ArrayList<String>();
51   private List<String> suggestionAttributeValues = new ArrayList<String>();
52   private List<String> suggestionTypeAliases = new ArrayList<String>();
53   private List<String> suggestionInputPermutations = new ArrayList<String>();
54   private List<String> suggestableAttr = new ArrayList<String>();
55   
56   private Map<String, String> inputOutputData = new HashMap<String, String>();
57   Map<String, UiFilterConfig> filters = new HashMap<String, UiFilterConfig>();
58   private JSONObject payload = new JSONObject();
59   private JSONArray payloadFilters = new JSONArray();
60   private StringBuffer outputString = new StringBuffer();
61   private String aliasToUse;
62   
63   private SuggestionEntityLookup entityLookup;
64   
65   public JSONObject getPayload() {
66     return payload;
67   }
68
69   public void setPayload(JSONObject payload) {
70     this.payload = payload;
71   }
72   
73   protected ObjectMapper mapper = new ObjectMapper();
74
75   public SuggestionSearchEntity(FiltersConfig filtersConfig) {
76     super();
77     
78     FiltersDetailsConfig filterConfigList = filtersConfig.getFiltersConfig();
79     // Populate the map with keys that will match the suggestableAttr values
80     for(UiFilterConfig filter : filterConfigList.getFilters()) {
81       if(filter.getDataSource() != null) {
82         filters.put(filter.getDataSource().getFieldName(), filter);
83       }
84     }
85   }
86   
87   public SuggestionSearchEntity(FiltersConfig filtersConfig, SuggestionEntityLookup entityLookup) {
88     
89     this.entityLookup = entityLookup;
90     
91     FiltersDetailsConfig filterConfigList = filtersConfig.getFiltersConfig();
92     
93     // Populate the map with keys that will match the suggestableAttr values
94     for(UiFilterConfig filter : filterConfigList.getFilters()) {
95       if(filter.getDataSource() != null) {
96         filters.put(filter.getDataSource().getFieldName(), filter);
97       }
98     }
99   }
100   
101   public SuggestionSearchEntity(SuggestionEntityLookup entityLookup, FiltersConfig config) {
102     
103     FiltersDetailsConfig filterConfigList = config.getFiltersConfig();
104     // Populate the map with keys that will match the suggestableAttr values
105     for(UiFilterConfig filter : filterConfigList.getFilters()) {
106       if(filter.getDataSource() != null) {
107         filters.put(filter.getDataSource().getFieldName(), filter);
108       }
109     }
110   }
111
112   public void setSuggestableAttr(ArrayList<String> attributes) {
113     for (String attribute : attributes) {
114       this.suggestableAttr.add(attribute);
115     }
116   }
117
118   public void setPayloadFromResponse(JsonNode node) {
119     if (suggestableAttr != null) {
120       JSONObject nodePayload = new JSONObject();
121       for (String attribute : suggestableAttr) {
122         if (node.get(attribute) != null) {
123           inputOutputData.put(attribute, node.get(attribute).asText());
124           this.payload.put(attribute, node.get(attribute).asText());
125         }
126       }
127     }
128   }
129   
130   public void setFilterBasedPayloadFromResponse(JsonNode node, String entityName, ArrayList<String> uniqueList) {
131     
132     HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(entityName);
133     
134     if ( desc == null ) {
135       return;
136     }
137     
138     String attr = desc.get("suggestibleAttributes");
139     
140     if ( attr == null ) {
141       return;
142     }
143     
144     List<String> suggestableAttrOxm = Arrays.asList(attr.split(","));
145     
146     /*
147      * Note: 
148      * (1) 'uniqueList' is one item within the power set of the suggestable attributes.
149      * (2) 'inputeOutputData' is used to generate permutations of strings
150      */
151     for (String selectiveAttr: uniqueList) {
152       if (node.get(selectiveAttr) != null) {
153         inputOutputData.put(selectiveAttr, node.get(selectiveAttr).asText());
154       }
155     }
156       
157     if (suggestableAttrOxm != null) {
158       for (String attribute : suggestableAttrOxm) {
159         if (node.get(attribute) != null && uniqueList.contains(attribute)) {
160           UiFilterConfig filterConfig = filters.get(attribute);
161           if(filterConfig != null) {
162             JSONObject filterPayload = new JSONObject();
163             filterPayload.put(FILTER_ID, filterConfig.getFilterId());
164             filterPayload.put(FILTER_VALUE, node.get(attribute).asText());
165             this.payloadFilters.put(filterPayload);
166           } else {
167             this.payload.put(attribute, node.get(attribute).asText()); 
168           }
169         } else {
170           UiFilterConfig emptyValueFilterConfig = filters.get(attribute);
171           if(emptyValueFilterConfig != null) {
172             JSONObject emptyValueFilterPayload = new JSONObject();
173             emptyValueFilterPayload.put(FILTER_ID, emptyValueFilterConfig.getFilterId());
174             this.payloadFilters.put(emptyValueFilterPayload);
175           }
176         }
177       }
178       this.payload.put(FILTER_LIST, this.payloadFilters);
179     }
180   }
181
182   @Override
183   public String getEntityType() {
184     return entityType;
185   }
186
187   @Override
188   public void setEntityType(String entityType) {
189     this.entityType = entityType;
190   }
191
192   public List<String> getSuggestionConnectorWords() {
193     return suggestionConnectorWords;
194   }
195
196   public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
197     this.suggestionConnectorWords = suggestionConnectorWords;
198   }
199
200   public List<String> getSuggestionPropertyTypes() {
201     return this.suggestionAttributeTypes;
202   }
203
204   public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
205     this.suggestionAttributeTypes = suggestionPropertyTypes;
206   }
207
208   public List<String> getSuggestionAttributeValues() {
209     return this.suggestionAttributeValues;
210   }
211
212   public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
213     this.suggestionAttributeValues = suggestionAttributeValues;
214   }
215
216   public List<String> getSuggestionAliases() {
217     return this.suggestionTypeAliases;
218   }
219
220   public void setSuggestionAliases(List<String> suggestionAliases) {
221     this.suggestionTypeAliases = suggestionAliases;
222   }
223
224   public List<String> getSuggestionInputPermutations() {
225     return this.suggestionInputPermutations;
226   }
227
228   public void setSuggestionInputPermutations(List<String> permutations) {
229     this.suggestionInputPermutations = permutations;
230   }
231
232   public void generateSuggestionInputPermutations() {
233
234     List<String> entityNames = new ArrayList<>();
235     entityNames.add(entityType);
236     HashMap<String, String> desc = entityLookup.getSuggestionSearchEntityOxmModel().get(this.entityType);
237     String attr = desc.get("suggestionAliases");
238     String[] suggestionAliasesArray = attr.split(",");
239     suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
240     this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
241     for (String alias : suggestionTypeAliases) {
242       entityNames.add(alias);
243     }
244
245     ArrayList<String> listToPermutate = new ArrayList<>(inputOutputData.values());
246
247     for (String entity : entityNames){
248       listToPermutate.add(entity); // add entity-name or alias in list to permutate
249       List<List<String>> lists = SuggestionsPermutation.getListPermutations(listToPermutate);
250       for (List<String> li : lists){
251         suggestionInputPermutations.add(String.join(" ", li));
252       }
253       // prepare for the next pass: remove the entity-name or alias from the list
254       listToPermutate.remove(entity); 
255     }
256   }
257
258   public boolean isSuggestableDoc() {
259     return this.getPayload().length() != 0;
260   }
261
262
263   @Override
264   public void deriveFields() {
265     
266     int entryCounter = 1;
267     for (Map.Entry<String, String> outputValue : inputOutputData.entrySet()) {
268       if (outputValue.getValue() != null && outputValue.getValue().length() > 0) {
269         this.outputString.append(outputValue.getValue());
270         if (entryCounter < inputOutputData.entrySet().size()) {
271           this.outputString.append(" and ");
272         } else{
273           this.outputString.append(" ");
274         }
275       }
276       entryCounter++;
277     }
278     
279     this.outputString.append(this.getAliasToUse());
280     this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
281   }
282
283   @Override
284   public String getAsJson() {
285     // TODO Auto-generated method stub
286     JSONObject rootNode = new JSONObject();
287
288     JSONArray suggestionsArray = new JSONArray();
289     for (String suggestion : suggestionInputPermutations) {
290       suggestionsArray.put(suggestion);
291     }
292
293     JSONObject entitySuggest = new JSONObject();
294
295     entitySuggest.put("input", suggestionsArray);
296     entitySuggest.put("output", this.outputString);
297     entitySuggest.put("payload", this.payload);
298     rootNode.put("entity_suggest", entitySuggest);
299
300     return rootNode.toString();
301   }
302
303   public String getAliasToUse() {
304     return aliasToUse;
305   }
306
307   public void setAliasToUse(String aliasToUse) {
308     this.aliasToUse = aliasToUse;
309   }
310
311   public Map<String, String> getInputOutputData() {
312     return inputOutputData;
313   }
314
315   public void setInputOutputData(Map<String, String> inputOutputData) {
316     this.inputOutputData = inputOutputData;
317   }
318
319   @Override
320   public String toString() {
321     return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
322         + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
323         + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
324         + suggestionTypeAliases + ", mapper=" + mapper + "]";
325   }
326 }