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.onap.aai.sparky.config.oxm;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.LinkedHashMap;
31 import java.util.List;
33 import java.util.Map.Entry;
34 import java.util.Vector;
36 import org.eclipse.persistence.dynamic.DynamicType;
37 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
38 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
39 import org.eclipse.persistence.mappings.DatabaseMapping;
40 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
41 import org.onap.aai.sparky.sync.entity.SuggestionSearchEntity;
43 public class SuggestionEntityLookup implements OxmModelProcessor {
45 private Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel;
46 private Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors;
47 private FiltersConfig filtersConfig;
49 public SuggestionEntityLookup(FiltersConfig filtersConfig) {
50 suggestionSearchEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
51 suggestionSearchEntityDescriptors = new HashMap<String, SuggestionEntityDescriptor>();
52 this.filtersConfig = filtersConfig;
56 public void processOxmModel(DynamicJAXBContext jaxbContext) {
58 @SuppressWarnings("rawtypes")
59 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
61 for (@SuppressWarnings("rawtypes")
62 Descriptor desc : descriptorsList) {
64 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
66 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
68 // Not all fields have key attributes
69 if (desc.getPrimaryKeyFields() != null) {
70 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
71 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
74 String entityName = desc.getDefaultRootElement();
77 oxmProperties.put("entityName", entityName);
79 Map<String, String> properties = entity.getDescriptor().getProperties();
80 if (properties != null) {
81 for (Map.Entry<String, String> entry : properties.entrySet()) {
84 if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) {
86 oxmProperties.put("containsSuggestibleProps", "true");
88 Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
89 List<String> listOfSuggestableAttributes = new ArrayList<String>();
91 for (DatabaseMapping descMap : descriptorMaps) {
92 if (descMap.isAbstractDirectMapping()) {
94 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
95 String suggestableOnSearchString =
96 String.valueOf(descMap.getProperties().get("suggestibleOnSearch"));
98 boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
100 if (isSuggestibleOnSearch) {
101 /* Grab attribute types for suggestion */
102 String attributeName =
103 descMap.getField().getName().replaceAll("/text\\(\\)", "");
104 listOfSuggestableAttributes.add(attributeName);
106 if (descMap.getProperties().get("suggestionVerbs") != null) {
107 String suggestionVerbsString =
108 String.valueOf(descMap.getProperties().get("suggestionVerbs"));
110 oxmProperties.put("suggestionVerbs", suggestionVerbsString);
117 if (!listOfSuggestableAttributes.isEmpty()) {
118 oxmProperties.put("suggestibleAttributes",
119 String.join(",", listOfSuggestableAttributes));
121 } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) {
122 oxmProperties.put("suggestionAliases", entry.getValue());
127 if (oxmProperties.containsKey("containsSuggestibleProps")) {
128 suggestionSearchEntityOxmModel.put(entityName, oxmProperties);
132 for (Entry<String, HashMap<String, String>> suggestionEntityModel : suggestionSearchEntityOxmModel
134 HashMap<String, String> attribute = suggestionEntityModel.getValue();
136 String entityName = attribute.get("entityName");
137 SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity(filtersConfig, this);
138 suggestionSearchEntity.setEntityType(entityName);
140 if (attribute.get("suggestionAliases") != null) {
141 suggestionSearchEntity
142 .setSuggestionAliases(Arrays.asList(attribute.get("suggestionAliases").split(",")));
145 if (attribute.get("suggestibleAttributes") != null) {
146 suggestionSearchEntity.setSuggestionPropertyTypes(
147 Arrays.asList(attribute.get("suggestibleAttributes").split(",")));
150 SuggestionEntityDescriptor entity = new SuggestionEntityDescriptor();
151 entity.setSuggestionSearchEntity(suggestionSearchEntity);
152 entity.setEntityName(entityName);
154 if (attribute.get("primaryKeyAttributeNames") != null) {
155 entity.setPrimaryKeyAttributeNames(
156 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
159 suggestionSearchEntityDescriptors.put(entityName, entity);
163 public Map<String, HashMap<String, String>> getSuggestionSearchEntityOxmModel() {
164 return suggestionSearchEntityOxmModel;
167 public void setSuggestionSearchEntityOxmModel(
168 Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel) {
169 this.suggestionSearchEntityOxmModel = suggestionSearchEntityOxmModel;
172 public Map<String, SuggestionEntityDescriptor> getSuggestionSearchEntityDescriptors() {
173 return suggestionSearchEntityDescriptors;
176 public void setSuggestionSearchEntityDescriptors(
177 Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors) {
178 this.suggestionSearchEntityDescriptors = suggestionSearchEntityDescriptors;