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 GeoEntityLookup implements OxmModelProcessor {
38 // TODO: kill singleton collaborator pattern
39 private static GeoEntityLookup instance;
41 private Map<String, HashMap<String, String>> geoEntityOxmModel;
43 private Map<String, GeoOxmEntityDescriptor> geoEntityDescriptors;
45 private GeoEntityLookup() {
46 geoEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
47 geoEntityDescriptors = new HashMap<String, GeoOxmEntityDescriptor>();
50 public synchronized static GeoEntityLookup getInstance() {
53 * I hate this method and I want it to go away. The singleton pattern is transitory, I want this
54 * class to be wired via a bean reference instead. But from the starting point, it would require
55 * fixing all the classes across the code base up front and I don't want this task to expand
56 * beyond just refactoring the OxmModelLoader. For now I'll keep the singleton pattern, but I
57 * really want to get rid of it once we are properly spring wired.
60 if (instance == null) {
61 instance = new GeoEntityLookup();
67 public Map<String, HashMap<String, String>> getGeoEntityOxmModel() {
68 return geoEntityOxmModel;
71 public void setGeoEntityOxmModel(Map<String, HashMap<String, String>> geoEntityOxmModel) {
72 this.geoEntityOxmModel = geoEntityOxmModel;
75 public Map<String, GeoOxmEntityDescriptor> getGeoEntityDescriptors() {
76 return geoEntityDescriptors;
79 public void setGeoEntityDescriptors(Map<String, GeoOxmEntityDescriptor> geoEntityDescriptors) {
80 this.geoEntityDescriptors = geoEntityDescriptors;
84 public void processOxmModel(DynamicJAXBContext jaxbContext) {
86 @SuppressWarnings("rawtypes")
87 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
89 for (@SuppressWarnings("rawtypes")
90 Descriptor desc : descriptorsList) {
92 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
94 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
96 // Not all fields have key attributes
97 if (desc.getPrimaryKeyFields() != null) {
98 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
99 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
102 String entityName = desc.getDefaultRootElement();
105 oxmProperties.put("entityName", entityName);
107 Map<String, String> properties = entity.getDescriptor().getProperties();
109 if (properties != null) {
110 for (Map.Entry<String, String> entry : properties.entrySet()) {
112 if (entry.getKey().equalsIgnoreCase("geoLat")) {
113 if (entry.getValue().length() > 0) {
114 oxmProperties.put("geoLat", entry.getValue());
116 } else if (entry.getKey().equalsIgnoreCase("geoLong")) {
117 if (entry.getValue().length() > 0) {
118 oxmProperties.put("geoLong", entry.getValue());
124 if (oxmProperties.containsKey("geoLat") && oxmProperties.containsKey("geoLong")) {
125 geoEntityOxmModel.put(entityName, oxmProperties);
130 for (Entry<String, HashMap<String, String>> entityModel : geoEntityOxmModel.entrySet()) {
132 HashMap<String, String> attribute = entityModel.getValue();
134 GeoOxmEntityDescriptor entity = new GeoOxmEntityDescriptor();
136 entity.setEntityName(attribute.get("entityName"));
138 if (attribute.containsKey("primaryKeyAttributeNames")) {
140 entity.setPrimaryKeyAttributeNames(
141 Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
143 if (attribute.containsKey("geoLat") || attribute.containsKey("geoLong")) {
144 entity.setGeoLatName(attribute.get("geoLat"));
145 entity.setGeoLongName(attribute.get("geoLong"));
148 geoEntityDescriptors.put(attribute.get("entityName"), entity);