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.sync.entity.SuggestionSearchEntity;
40 public class SuggestionEntityLookup implements OxmModelProcessor {
42 // TODO: kill singleton collaborator pattern
43 private static SuggestionEntityLookup instance;
45 private Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel;
46 private Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors;
48 private SuggestionEntityLookup() {
49 suggestionSearchEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
50 suggestionSearchEntityDescriptors = new HashMap<String, SuggestionEntityDescriptor>();
53 public synchronized static SuggestionEntityLookup getInstance() {
56 * I hate this method and I want it to go away. The singleton pattern is transitory, I want this
57 * class to be wired via a bean reference instead. But from the starting point, it would require
58 * fixing all the classes across the code base up front and I don't want this task to expand
59 * beyond just refactoring the OxmModelLoader. For now I'll keep the singleton pattern, but I
60 * really want to get rid of it once we are properly spring wired.
63 if (instance == null) {
64 instance = new SuggestionEntityLookup();
72 public void processOxmModel(DynamicJAXBContext jaxbContext) {
74 @SuppressWarnings("rawtypes")
75 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
77 for (@SuppressWarnings("rawtypes")
78 Descriptor desc : descriptorsList) {
80 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
82 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
84 // Not all fields have key attributes
85 if (desc.getPrimaryKeyFields() != null) {
86 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
87 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
90 String entityName = desc.getDefaultRootElement();
93 oxmProperties.put("entityName", entityName);
95 Map<String, String> properties = entity.getDescriptor().getProperties();
96 if (properties != null) {
97 for (Map.Entry<String, String> entry : properties.entrySet()) {
100 if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) {
102 oxmProperties.put("containsSuggestibleProps", "true");
104 Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
105 List<String> listOfSuggestableAttributes = new ArrayList<String>();
107 for (DatabaseMapping descMap : descriptorMaps) {
108 if (descMap.isAbstractDirectMapping()) {
110 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
111 String suggestableOnSearchString =
112 String.valueOf(descMap.getProperties().get("suggestibleOnSearch"));
114 boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
116 if (isSuggestibleOnSearch) {
117 /* Grab attribute types for suggestion */
118 String attributeName =
119 descMap.getField().getName().replaceAll("/text\\(\\)", "");
120 listOfSuggestableAttributes.add(attributeName);
122 if (descMap.getProperties().get("suggestionVerbs") != null) {
123 String suggestionVerbsString =
124 String.valueOf(descMap.getProperties().get("suggestionVerbs"));
126 oxmProperties.put("suggestionVerbs", suggestionVerbsString);
133 if (!listOfSuggestableAttributes.isEmpty()) {
134 oxmProperties.put("suggestibleAttributes",
135 String.join(",", listOfSuggestableAttributes));
137 } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) {
138 oxmProperties.put("suggestionAliases", entry.getValue());
143 if (oxmProperties.containsKey("containsSuggestibleProps")) {
144 suggestionSearchEntityOxmModel.put(entityName, oxmProperties);
148 for (Entry<String, HashMap<String, String>> suggestionEntityModel : suggestionSearchEntityOxmModel
150 HashMap<String, String> attribute = suggestionEntityModel.getValue();
152 String entityName = attribute.get("entityName");
153 SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity(this);
154 suggestionSearchEntity.setEntityType(entityName);
156 if (attribute.get("suggestionAliases") != null) {
157 suggestionSearchEntity
158 .setSuggestionAliases(Arrays.asList(attribute.get("suggestionAliases").split(",")));
161 if (attribute.get("suggestibleAttributes") != null) {
162 suggestionSearchEntity.setSuggestionPropertyTypes(
163 Arrays.asList(attribute.get("suggestibleAttributes").split(",")));
166 SuggestionEntityDescriptor entity = new SuggestionEntityDescriptor();
167 entity.setSuggestionSearchEntity(suggestionSearchEntity);
168 entity.setEntityName(entityName);
170 if (attribute.get("primaryKeyAttributeNames") != null) {
171 entity.setPrimaryKeyAttributeNames(
172 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
175 suggestionSearchEntityDescriptors.put(entityName, entity);
179 public Map<String, HashMap<String, String>> getSuggestionSearchEntityOxmModel() {
180 return suggestionSearchEntityOxmModel;
183 public void setSuggestionSearchEntityOxmModel(
184 Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel) {
185 this.suggestionSearchEntityOxmModel = suggestionSearchEntityOxmModel;
188 public Map<String, SuggestionEntityDescriptor> getSuggestionSearchEntityDescriptors() {
189 return suggestionSearchEntityDescriptors;
192 public void setSuggestionSearchEntityDescriptors(
193 Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors) {
194 this.suggestionSearchEntityDescriptors = suggestionSearchEntityDescriptors;