2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.openecomp.sparky.synchronizer.entity;
27 import com.fasterxml.jackson.databind.JsonNode;
28 import com.fasterxml.jackson.databind.ObjectMapper;
29 import com.fasterxml.jackson.databind.node.ObjectNode;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.HashMap;
34 import java.util.List;
37 import org.json.JSONArray;
38 import org.json.JSONObject;
39 import org.openecomp.sparky.config.oxm.OxmModelLoader;
40 import org.openecomp.sparky.util.NodeUtils;
42 public class SuggestionSearchEntity extends IndexableEntity implements IndexDocument {
44 private String entityType;
45 private List<String> suggestionConnectorWords = new ArrayList<String>();
46 private List<String> suggestionAttributeTypes = new ArrayList<String>();
47 private List<String> suggestionAttributeValues = new ArrayList<String>();
48 private List<String> suggestionTypeAliases = new ArrayList<String>();
49 private List<String> suggestionInputPermutations = new ArrayList<String>();
50 private List<String> suggestableAttr = new ArrayList<String>();
51 private Map<String, String> payload = new HashMap<String, String>();
52 private JSONObject payloadJsonNode = new JSONObject();
53 private StringBuffer outputString = new StringBuffer();
54 private String aliasToUse;
56 public Map<String, String> getPayload() {
60 public void setPayload(Map<String, String> payload) {
61 this.payload = payload;
65 public JSONObject getPayloadJsonNode() {
66 return payloadJsonNode;
69 public void setPayloadJsonNode(JSONObject payloadJsonNode) {
70 this.payloadJsonNode = payloadJsonNode;
74 protected ObjectMapper mapper = new ObjectMapper();
76 public SuggestionSearchEntity() {
80 public void setSuggestableAttr(ArrayList<String> attributes) {
81 for (String attribute : attributes) {
82 this.suggestableAttr.add(attribute);
86 public void setPayloadFromResponse(JsonNode node) {
87 Map<String, String> nodePayload = new HashMap<String, String>();
88 if (suggestableAttr != null) {
89 for (String attribute : suggestableAttr) {
90 if (node.get(attribute) != null) {
91 nodePayload.put(attribute, node.get(attribute).asText());
94 this.setPayload(nodePayload);
99 public SuggestionSearchEntity(OxmModelLoader loader) {
104 public String getEntityType() {
109 public void setEntityType(String entityType) {
110 this.entityType = entityType;
113 public List<String> getSuggestionConnectorWords() {
114 return suggestionConnectorWords;
117 public void setSuggestionConnectorWords(List<String> suggestionConnectorWords) {
118 this.suggestionConnectorWords = suggestionConnectorWords;
121 public List<String> getSuggestionPropertyTypes() {
122 return this.suggestionAttributeTypes;
125 public void setSuggestionPropertyTypes(List<String> suggestionPropertyTypes) {
126 this.suggestionAttributeTypes = suggestionPropertyTypes;
129 public List<String> getSuggestionAttributeValues() {
130 return this.suggestionAttributeValues;
133 public void setSuggestionAttributeValues(List<String> suggestionAttributeValues) {
134 this.suggestionAttributeValues = suggestionAttributeValues;
137 public List<String> getSuggestionAliases() {
138 return this.suggestionTypeAliases;
141 public void setSuggestionAliases(List<String> suggestionAliases) {
142 this.suggestionTypeAliases = suggestionAliases;
145 public List<String> getSuggestionInputPermutations() {
146 return this.suggestionInputPermutations;
149 public void setSuggestionInputPermutations(List<String> permutations) {
150 this.suggestionInputPermutations = permutations;
153 public void generateSuggestionInputPermutations() {
156 List<String> entityNames = new ArrayList<>();
157 entityNames.add(entityType);
158 HashMap<String, String> desc = loader.getOxmModel().get(this.entityType);
159 String attr = desc.get("suggestionAliases");
160 String[] suggestionAliasesArray = attr.split(",");
161 suggestionTypeAliases = Arrays.asList(suggestionAliasesArray);
162 this.setAliasToUse(suggestionAliasesArray[suggestionAliasesArray.length - 1]);
163 for (String alias : suggestionTypeAliases) {
164 entityNames.add(alias);
166 ArrayList<String> listOfSearchSuggestionPermutations = new ArrayList<>();
168 ArrayList<String> listToPermutate = new ArrayList<>(payload.values());
170 for (String entityName : entityNames) {
171 listToPermutate.add(entityName);
172 permutateList(listToPermutate, new ArrayList<String>(), listToPermutate.size(),
173 listOfSearchSuggestionPermutations);
174 listToPermutate.remove(entityName);
176 suggestionInputPermutations = listOfSearchSuggestionPermutations;
180 * Generate all permutations of a list of Strings
186 private void permutateList(List<String> list, List<String> permutation, int size,
187 List<String> listOfSearchSuggestionPermutationList) {
188 if (permutation.size() == size) {
189 StringBuilder newPermutation = new StringBuilder();
191 for (int i = 0; i < permutation.size(); i++) {
192 newPermutation.append(permutation.get(i)).append(" ");
195 listOfSearchSuggestionPermutationList.add(newPermutation.toString().trim());
200 String[] availableItems = list.toArray(new String[0]);
202 for (String i : availableItems) {
205 permutateList(list, permutation, size, listOfSearchSuggestionPermutationList);
207 permutation.remove(i);
211 public boolean isSuggestableDoc() {
212 return this.getPayload().size() != 0;
217 public void deriveFields() {
219 int payloadEntryCounter = 1;
220 for (Map.Entry<String, String> payload : getPayload().entrySet()) {
221 // Add the payload(status) only if a valid value is present
222 if (payload.getValue() != null &&payload.getValue().length() > 0) {
223 this.getPayloadJsonNode().put(payload.getKey(), payload.getValue());
224 this.outputString.append(payload.getValue());
225 if (payloadEntryCounter < getPayload().entrySet().size()) {
226 this.outputString.append(" and ");
228 this.outputString.append(" ");
231 payloadEntryCounter++;
234 this.outputString.append(this.getAliasToUse());
235 this.id = NodeUtils.generateUniqueShaDigest(outputString.toString());
239 public String getIndexDocumentJson() {
240 // TODO Auto-generated method stub
241 JSONObject rootNode = new JSONObject();
243 JSONArray suggestionsArray = new JSONArray();
244 for (String suggestion : suggestionInputPermutations) {
245 suggestionsArray.put(suggestion);
248 JSONObject entitySuggest = new JSONObject();
250 entitySuggest.put("input", suggestionsArray);
251 entitySuggest.put("output", this.outputString);
252 entitySuggest.put("payload", this.payloadJsonNode);
253 rootNode.put("entity_suggest", entitySuggest);
255 return rootNode.toString();
259 public ObjectNode getBulkImportEntity() {
260 // TODO Auto-generated method stub
264 public String getAliasToUse() {
268 public void setAliasToUse(String aliasToUse) {
269 this.aliasToUse = aliasToUse;
273 public String toString() {
274 return "SuggestionSearchEntity [entityType=" + entityType + ", suggestionConnectorWords="
275 + suggestionConnectorWords + ", suggestionAttributeTypes=" + suggestionAttributeTypes
276 + ", suggestionAttributeValues=" + suggestionAttributeValues + ", suggestionTypeAliases="
277 + suggestionTypeAliases + ", mapper=" + mapper + "]";