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.Arrays;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
30 import java.util.Map.Entry;
32 import org.eclipse.persistence.dynamic.DynamicType;
33 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
34 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
36 public class SearchableEntityLookup implements OxmModelProcessor {
38 // TODO: kill singleton collaborator pattern
39 private static SearchableEntityLookup instance;
41 private Map<String, HashMap<String, String>> searchableOxmModel;
42 private Map<String, SearchableOxmEntityDescriptor> searchableEntityDescriptors;
44 private SearchableEntityLookup() {
45 searchableOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
46 searchableEntityDescriptors = new HashMap<String, SearchableOxmEntityDescriptor>();
49 public synchronized static SearchableEntityLookup getInstance() {
52 * I hate this method and I want it to go away. The singleton pattern is transitory, I want this
53 * class to be wired via a bean reference instead. But from the starting point, it would require
54 * fixing all the classes across the code base up front and I don't want this task to expand
55 * beyond just refactoring the OxmModelLoader. For now I'll keep the singleton pattern, but I
56 * really want to get rid of it once we are properly spring wired.
59 if (instance == null) {
60 instance = new SearchableEntityLookup();
68 public void processOxmModel(DynamicJAXBContext jaxbContext) {
70 @SuppressWarnings("rawtypes")
71 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
73 for (@SuppressWarnings("rawtypes")
74 Descriptor desc : descriptorsList) {
76 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
78 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
80 // Not all fields have key attributes
81 if (desc.getPrimaryKeyFields() != null) {
82 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
83 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
86 String entityName = desc.getDefaultRootElement();
89 oxmProperties.put("entityName", entityName);
91 Map<String, String> properties = entity.getDescriptor().getProperties();
92 if (properties != null) {
93 for (Map.Entry<String, String> entry : properties.entrySet()) {
95 if (entry.getKey().equalsIgnoreCase("searchable")) {
96 oxmProperties.put("searchableAttributes", entry.getValue());
101 // Add all searchable entity types for reserve lookup
102 if (oxmProperties.containsKey("searchableAttributes")) {
103 searchableOxmModel.put(entityName, oxmProperties);
108 for (Entry<String, HashMap<String, String>> searchableModel : searchableOxmModel.entrySet()) {
109 HashMap<String, String> attribute = searchableModel.getValue();
110 SearchableOxmEntityDescriptor entity = new SearchableOxmEntityDescriptor();
111 entity.setEntityName(attribute.get("entityName"));
112 entity.setPrimaryKeyAttributeNames(
113 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
115 .setSearchableAttributes(Arrays.asList(attribute.get("searchableAttributes").split(",")));
116 searchableEntityDescriptors.put(attribute.get("entityName"), entity);
121 public Map<String, HashMap<String, String>> getSearchableOxmModel() {
122 return searchableOxmModel;
125 public void setSearchableOxmModel(Map<String, HashMap<String, String>> searchableOxmModel) {
126 this.searchableOxmModel = searchableOxmModel;
129 public Map<String, SearchableOxmEntityDescriptor> getSearchableEntityDescriptors() {
130 return searchableEntityDescriptors;
133 public void setSearchableEntityDescriptors(
134 Map<String, SearchableOxmEntityDescriptor> searchableEntityDescriptors) {
135 this.searchableEntityDescriptors = searchableEntityDescriptors;