2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 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 package org.onap.aai.sparky.config.oxm;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.HashMap;
26 import java.util.LinkedHashMap;
27 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.Vector;
32 import org.eclipse.persistence.dynamic.DynamicType;
33 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
34 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
35 import org.eclipse.persistence.mappings.DatabaseMapping;
36 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
37 import org.onap.aai.sparky.sync.entity.SuggestionSearchEntity;
39 public class SuggestionEntityLookup implements OxmModelProcessor {
41 private Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel;
42 private Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors;
43 private FiltersConfig filtersConfig;
45 public SuggestionEntityLookup(FiltersConfig filtersConfig) {
46 suggestionSearchEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
47 suggestionSearchEntityDescriptors = new HashMap<String, SuggestionEntityDescriptor>();
48 this.filtersConfig = filtersConfig;
52 public void processOxmModel(DynamicJAXBContext jaxbContext) {
54 @SuppressWarnings("rawtypes")
55 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
57 for (@SuppressWarnings("rawtypes")
58 Descriptor desc : descriptorsList) {
60 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
62 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
64 // Not all fields have key attributes
65 if (desc.getPrimaryKeyFields() != null) {
66 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
67 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
70 String entityName = desc.getDefaultRootElement();
73 oxmProperties.put("entityName", entityName);
75 Map<String, String> properties = entity.getDescriptor().getProperties();
76 if (properties != null) {
77 for (Map.Entry<String, String> entry : properties.entrySet()) {
80 if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) {
82 oxmProperties.put("containsSuggestibleProps", "true");
84 Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
85 List<String> listOfSuggestableAttributes = new ArrayList<String>();
87 for (DatabaseMapping descMap : descriptorMaps) {
88 if (descMap.isAbstractDirectMapping()) {
90 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
91 String suggestableOnSearchString =
92 String.valueOf(descMap.getProperties().get("suggestibleOnSearch"));
94 boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
96 if (isSuggestibleOnSearch) {
97 /* Grab attribute types for suggestion */
98 String attributeName =
99 descMap.getField().getName().replaceAll("/text\\(\\)", "");
100 listOfSuggestableAttributes.add(attributeName);
102 if (descMap.getProperties().get("suggestionVerbs") != null) {
103 String suggestionVerbsString =
104 String.valueOf(descMap.getProperties().get("suggestionVerbs"));
106 oxmProperties.put("suggestionVerbs", suggestionVerbsString);
113 if (!listOfSuggestableAttributes.isEmpty()) {
114 oxmProperties.put("suggestibleAttributes",
115 String.join(",", listOfSuggestableAttributes));
117 } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) {
118 oxmProperties.put("suggestionAliases", entry.getValue());
123 if (oxmProperties.containsKey("containsSuggestibleProps")) {
124 suggestionSearchEntityOxmModel.put(entityName, oxmProperties);
128 for (Entry<String, HashMap<String, String>> suggestionEntityModel : suggestionSearchEntityOxmModel
130 HashMap<String, String> attribute = suggestionEntityModel.getValue();
132 String entityName = attribute.get("entityName");
133 SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity(filtersConfig, this);
134 suggestionSearchEntity.setEntityType(entityName);
136 if (attribute.get("suggestionAliases") != null) {
137 suggestionSearchEntity
138 .setSuggestionAliases(Arrays.asList(attribute.get("suggestionAliases").split(",")));
141 if (attribute.get("suggestibleAttributes") != null) {
142 suggestionSearchEntity.setSuggestionPropertyTypes(
143 Arrays.asList(attribute.get("suggestibleAttributes").split(",")));
146 SuggestionEntityDescriptor entity = new SuggestionEntityDescriptor();
147 entity.setSuggestionSearchEntity(suggestionSearchEntity);
148 entity.setEntityName(entityName);
150 if (attribute.get("primaryKeyAttributeNames") != null) {
151 entity.setPrimaryKeyAttributeNames(
152 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
155 suggestionSearchEntityDescriptors.put(entityName, entity);
159 public Map<String, HashMap<String, String>> getSuggestionSearchEntityOxmModel() {
160 return suggestionSearchEntityOxmModel;
163 public void setSuggestionSearchEntityOxmModel(
164 Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel) {
165 this.suggestionSearchEntityOxmModel = suggestionSearchEntityOxmModel;
168 public Map<String, SuggestionEntityDescriptor> getSuggestionSearchEntityDescriptors() {
169 return suggestionSearchEntityDescriptors;
172 public void setSuggestionSearchEntityDescriptors(
173 Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors) {
174 this.suggestionSearchEntityDescriptors = suggestionSearchEntityDescriptors;