2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.config.oxm;
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
29 import java.util.List;
31 import java.util.Map.Entry;
32 import java.util.Vector;
34 import org.eclipse.persistence.dynamic.DynamicType;
35 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
36 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
37 import org.eclipse.persistence.mappings.DatabaseMapping;
38 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
39 import org.onap.aai.sparky.sync.entity.SuggestionSearchEntity;
41 public class SuggestionEntityLookup implements OxmModelProcessor {
43 private Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel;
44 private Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors;
45 private FiltersConfig filtersConfig;
47 public SuggestionEntityLookup(FiltersConfig filtersConfig) {
48 suggestionSearchEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
49 suggestionSearchEntityDescriptors = new HashMap<String, SuggestionEntityDescriptor>();
50 this.filtersConfig = filtersConfig;
54 public void processOxmModel(DynamicJAXBContext jaxbContext) {
56 @SuppressWarnings("rawtypes")
57 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
59 for (@SuppressWarnings("rawtypes")
60 Descriptor desc : descriptorsList) {
62 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
64 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
66 // Not all fields have key attributes
67 if (desc.getPrimaryKeyFields() != null) {
68 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
69 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
72 String entityName = desc.getDefaultRootElement();
75 oxmProperties.put("entityName", entityName);
77 Map<String, String> properties = entity.getDescriptor().getProperties();
78 if (properties != null) {
79 for (Map.Entry<String, String> entry : properties.entrySet()) {
82 if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) {
84 oxmProperties.put("containsSuggestibleProps", "true");
86 Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
87 List<String> listOfSuggestableAttributes = new ArrayList<String>();
89 for (DatabaseMapping descMap : descriptorMaps) {
90 if (descMap.isAbstractDirectMapping()) {
92 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
93 String suggestableOnSearchString =
94 String.valueOf(descMap.getProperties().get("suggestibleOnSearch"));
96 boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
98 if (isSuggestibleOnSearch) {
99 /* Grab attribute types for suggestion */
100 String attributeName =
101 descMap.getField().getName().replaceAll("/text\\(\\)", "");
102 listOfSuggestableAttributes.add(attributeName);
104 if (descMap.getProperties().get("suggestionVerbs") != null) {
105 String suggestionVerbsString =
106 String.valueOf(descMap.getProperties().get("suggestionVerbs"));
108 oxmProperties.put("suggestionVerbs", suggestionVerbsString);
115 if (!listOfSuggestableAttributes.isEmpty()) {
116 oxmProperties.put("suggestibleAttributes",
117 String.join(",", listOfSuggestableAttributes));
119 } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) {
120 oxmProperties.put("suggestionAliases", entry.getValue());
125 if (oxmProperties.containsKey("containsSuggestibleProps")) {
126 suggestionSearchEntityOxmModel.put(entityName, oxmProperties);
130 for (Entry<String, HashMap<String, String>> suggestionEntityModel : suggestionSearchEntityOxmModel
132 HashMap<String, String> attribute = suggestionEntityModel.getValue();
134 String entityName = attribute.get("entityName");
135 SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity(filtersConfig, this);
136 suggestionSearchEntity.setEntityType(entityName);
138 if (attribute.get("suggestionAliases") != null) {
139 suggestionSearchEntity
140 .setSuggestionAliases(Arrays.asList(attribute.get("suggestionAliases").split(",")));
143 if (attribute.get("suggestibleAttributes") != null) {
144 suggestionSearchEntity.setSuggestionPropertyTypes(
145 Arrays.asList(attribute.get("suggestibleAttributes").split(",")));
148 SuggestionEntityDescriptor entity = new SuggestionEntityDescriptor();
149 entity.setSuggestionSearchEntity(suggestionSearchEntity);
150 entity.setEntityName(entityName);
152 if (attribute.get("primaryKeyAttributeNames") != null) {
153 entity.setPrimaryKeyAttributeNames(
154 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
157 suggestionSearchEntityDescriptors.put(entityName, entity);
162 public Map<String, HashMap<String, String>> getSuggestionSearchEntityOxmModel() {
163 return suggestionSearchEntityOxmModel;
166 public void setSuggestionSearchEntityOxmModel(
167 Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel) {
168 this.suggestionSearchEntityOxmModel = suggestionSearchEntityOxmModel;
171 public Map<String, SuggestionEntityDescriptor> getSuggestionSearchEntityDescriptors() {
172 return suggestionSearchEntityDescriptors;
175 public void setSuggestionSearchEntityDescriptors(
176 Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors) {
177 this.suggestionSearchEntityDescriptors = suggestionSearchEntityDescriptors;