Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / GeoEntityLookup.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.config.oxm;
26
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
33
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
38 public class GeoEntityLookup implements OxmModelProcessor {
39
40   private Map<String, HashMap<String, String>> geoEntityOxmModel;
41
42   private Map<String, GeoOxmEntityDescriptor> geoEntityDescriptors;
43
44   public GeoEntityLookup() {
45     geoEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
46     geoEntityDescriptors = new HashMap<String, GeoOxmEntityDescriptor>();
47   }
48
49   public Map<String, HashMap<String, String>> getGeoEntityOxmModel() {
50     return geoEntityOxmModel;
51   }
52
53   public void setGeoEntityOxmModel(Map<String, HashMap<String, String>> geoEntityOxmModel) {
54     this.geoEntityOxmModel = geoEntityOxmModel;
55   }
56
57   public Map<String, GeoOxmEntityDescriptor> getGeoEntityDescriptors() {
58     return geoEntityDescriptors;
59   }
60
61   public void setGeoEntityDescriptors(Map<String, GeoOxmEntityDescriptor> geoEntityDescriptors) {
62     this.geoEntityDescriptors = geoEntityDescriptors;
63   }
64
65   @Override
66   public void processOxmModel(DynamicJAXBContext jaxbContext) {
67     
68     @SuppressWarnings("rawtypes")
69     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
70
71     for (@SuppressWarnings("rawtypes")
72     Descriptor desc : descriptorsList) {
73
74       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
75
76       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
77
78       // Not all fields have key attributes
79       if (desc.getPrimaryKeyFields() != null) {
80         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
81             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
82       }
83
84       String entityName = desc.getDefaultRootElement();
85
86       // add entityName
87       oxmProperties.put("entityName", entityName);
88
89       Map<String, String> properties = entity.getDescriptor().getProperties();
90
91       if (properties != null) {
92         for (Map.Entry<String, String> entry : properties.entrySet()) {
93
94           if (entry.getKey().equalsIgnoreCase("geoLat")) {
95             if (entry.getValue().length() > 0) {
96               oxmProperties.put("geoLat", entry.getValue());
97             }
98           } else if (entry.getKey().equalsIgnoreCase("geoLong")) {
99             if (entry.getValue().length() > 0) {
100               oxmProperties.put("geoLong", entry.getValue());
101             }
102           }
103         }
104       }
105
106       if (oxmProperties.containsKey("geoLat") && oxmProperties.containsKey("geoLong")) {
107         geoEntityOxmModel.put(entityName, oxmProperties);
108       }
109
110     }
111
112     for (Entry<String, HashMap<String, String>> entityModel : geoEntityOxmModel.entrySet()) {
113
114       HashMap<String, String> attribute = entityModel.getValue();
115
116       GeoOxmEntityDescriptor entity = new GeoOxmEntityDescriptor();
117
118       entity.setEntityName(attribute.get("entityName"));
119
120       if (attribute.containsKey("primaryKeyAttributeNames")) {
121
122         entity.setPrimaryKeyAttributeNames(
123             Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
124
125         if (attribute.containsKey("geoLat") || attribute.containsKey("geoLong")) {
126           entity.setGeoLatName(attribute.get("geoLat"));
127           entity.setGeoLongName(attribute.get("geoLong"));
128         }
129
130         geoEntityDescriptors.put(attribute.get("entityName"), entity);
131       }
132     }
133
134   }
135
136
137 }