Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / GeoEntityLookup.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.aai.sparky.config.oxm;
22
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 import org.eclipse.persistence.dynamic.DynamicType;
31 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
32 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
33
34 public class GeoEntityLookup implements OxmModelProcessor {
35
36   private Map<String, HashMap<String, String>> geoEntityOxmModel;
37
38   private Map<String, GeoOxmEntityDescriptor> geoEntityDescriptors;
39
40   public GeoEntityLookup() {
41     geoEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
42     geoEntityDescriptors = new HashMap<String, GeoOxmEntityDescriptor>();
43   }
44
45   public Map<String, HashMap<String, String>> getGeoEntityOxmModel() {
46     return geoEntityOxmModel;
47   }
48
49   public void setGeoEntityOxmModel(Map<String, HashMap<String, String>> geoEntityOxmModel) {
50     this.geoEntityOxmModel = geoEntityOxmModel;
51   }
52
53   public Map<String, GeoOxmEntityDescriptor> getGeoEntityDescriptors() {
54     return geoEntityDescriptors;
55   }
56
57   public void setGeoEntityDescriptors(Map<String, GeoOxmEntityDescriptor> geoEntityDescriptors) {
58     this.geoEntityDescriptors = geoEntityDescriptors;
59   }
60
61   @Override
62   public void processOxmModel(DynamicJAXBContext jaxbContext) {
63     
64     @SuppressWarnings("rawtypes")
65     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
66
67     for (@SuppressWarnings("rawtypes")
68     Descriptor desc : descriptorsList) {
69
70       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
71
72       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
73
74       // Not all fields have key attributes
75       if (desc.getPrimaryKeyFields() != null) {
76         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
77             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
78       }
79
80       String entityName = desc.getDefaultRootElement();
81
82       // add entityName
83       oxmProperties.put("entityName", entityName);
84
85       Map<String, String> properties = entity.getDescriptor().getProperties();
86
87       if (properties != null) {
88         for (Map.Entry<String, String> entry : properties.entrySet()) {
89
90           if (entry.getKey().equalsIgnoreCase("geoLat")) {
91             if (entry.getValue().length() > 0) {
92               oxmProperties.put("geoLat", entry.getValue());
93             }
94           } else if (entry.getKey().equalsIgnoreCase("geoLong")) {
95             if (entry.getValue().length() > 0) {
96               oxmProperties.put("geoLong", entry.getValue());
97             }
98           }
99         }
100       }
101
102       if (oxmProperties.containsKey("geoLat") && oxmProperties.containsKey("geoLong")) {
103         geoEntityOxmModel.put(entityName, oxmProperties);
104       }
105
106     }
107
108     for (Entry<String, HashMap<String, String>> entityModel : geoEntityOxmModel.entrySet()) {
109
110       HashMap<String, String> attribute = entityModel.getValue();
111
112       GeoOxmEntityDescriptor entity = new GeoOxmEntityDescriptor();
113
114       entity.setEntityName(attribute.get("entityName"));
115
116       if (attribute.containsKey("primaryKeyAttributeNames")) {
117
118         entity.setPrimaryKeyAttributeNames(
119             Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
120
121         if (attribute.containsKey("geoLat") || attribute.containsKey("geoLong")) {
122           entity.setGeoLatName(attribute.get("geoLat"));
123           entity.setGeoLongName(attribute.get("geoLong"));
124         }
125
126         geoEntityDescriptors.put(attribute.get("entityName"), entity);
127       }
128     }
129
130   }
131
132
133 }