Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / SearchableEntityLookup.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 SearchableEntityLookup implements OxmModelProcessor {
39
40   private Map<String, HashMap<String, String>> searchableOxmModel;
41   private Map<String, SearchableOxmEntityDescriptor> searchableEntityDescriptors;
42
43   public SearchableEntityLookup() {
44     searchableOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
45     searchableEntityDescriptors = new HashMap<String, SearchableOxmEntityDescriptor>();
46   }
47
48   @Override
49   public void processOxmModel(DynamicJAXBContext jaxbContext) {
50     
51     @SuppressWarnings("rawtypes")
52     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
53
54     for (@SuppressWarnings("rawtypes")
55     Descriptor desc : descriptorsList) {
56
57       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
58
59       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
60
61       // Not all fields have key attributes
62       if (desc.getPrimaryKeyFields() != null) {
63         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
64             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
65       }
66
67       String entityName = desc.getDefaultRootElement();
68
69       // add entityName
70       oxmProperties.put("entityName", entityName);
71
72       Map<String, String> properties = entity.getDescriptor().getProperties();
73       if (properties != null) {
74         for (Map.Entry<String, String> entry : properties.entrySet()) {
75
76           if (entry.getKey().equalsIgnoreCase("searchable")) {
77             oxmProperties.put("searchableAttributes", entry.getValue());
78           }
79         }
80       }
81
82       // Add all searchable entity types for reserve lookup
83       if (oxmProperties.containsKey("searchableAttributes")) {
84         searchableOxmModel.put(entityName, oxmProperties);
85       }
86
87     }
88
89     for (Entry<String, HashMap<String, String>> searchableModel : searchableOxmModel.entrySet()) {
90       HashMap<String, String> attribute = searchableModel.getValue();
91       SearchableOxmEntityDescriptor entity = new SearchableOxmEntityDescriptor();
92       entity.setEntityName(attribute.get("entityName"));
93       entity.setPrimaryKeyAttributeNames(
94           Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
95       entity
96           .setSearchableAttributes(Arrays.asList(attribute.get("searchableAttributes").split(",")));
97       searchableEntityDescriptors.put(attribute.get("entityName"), entity);
98     }
99
100   }
101
102   public Map<String, HashMap<String, String>> getSearchableOxmModel() {
103     return searchableOxmModel;
104   }
105
106   public void setSearchableOxmModel(Map<String, HashMap<String, String>> searchableOxmModel) {
107     this.searchableOxmModel = searchableOxmModel;
108   }
109
110   public Map<String, SearchableOxmEntityDescriptor> getSearchableEntityDescriptors() {
111     return searchableEntityDescriptors;
112   }
113
114   public void setSearchableEntityDescriptors(
115       Map<String, SearchableOxmEntityDescriptor> searchableEntityDescriptors) {
116     this.searchableEntityDescriptors = searchableEntityDescriptors;
117   }
118
119 }