faea602806105f420e56421c72e074ef318298e7
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / config / oxm / SearchableEntityLookup.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.config.oxm;
24
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import org.eclipse.persistence.dynamic.DynamicType;
33 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
34 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
35
36 public class SearchableEntityLookup implements OxmModelProcessor {
37
38   private Map<String, HashMap<String, String>> searchableOxmModel;
39   private Map<String, SearchableOxmEntityDescriptor> searchableEntityDescriptors;
40
41   public SearchableEntityLookup() {
42     searchableOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
43     searchableEntityDescriptors = new HashMap<String, SearchableOxmEntityDescriptor>();
44   }
45
46   @Override
47   public void processOxmModel(DynamicJAXBContext jaxbContext) {
48     
49     @SuppressWarnings("rawtypes")
50     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
51
52     for (@SuppressWarnings("rawtypes")
53     Descriptor desc : descriptorsList) {
54
55       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
56
57       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
58
59       // Not all fields have key attributes
60       if (desc.getPrimaryKeyFields() != null) {
61         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
62             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
63       }
64
65       String entityName = desc.getDefaultRootElement();
66
67       // add entityName
68       oxmProperties.put("entityName", entityName);
69
70       Map<String, String> properties = entity.getDescriptor().getProperties();
71       if (properties != null) {
72         for (Map.Entry<String, String> entry : properties.entrySet()) {
73
74           if (entry.getKey().equalsIgnoreCase("searchable")) {
75             oxmProperties.put("searchableAttributes", entry.getValue());
76           }
77         }
78       }
79
80       // Add all searchable entity types for reserve lookup
81       if (oxmProperties.containsKey("searchableAttributes")) {
82         searchableOxmModel.put(entityName, oxmProperties);
83       }
84
85     }
86
87     for (Entry<String, HashMap<String, String>> searchableModel : searchableOxmModel.entrySet()) {
88       HashMap<String, String> attribute = searchableModel.getValue();
89       SearchableOxmEntityDescriptor entity = new SearchableOxmEntityDescriptor();
90       entity.setEntityName(attribute.get("entityName"));
91       entity.setPrimaryKeyAttributeNames(
92           Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
93       entity
94           .setSearchableAttributes(Arrays.asList(attribute.get("searchableAttributes").split(",")));
95       searchableEntityDescriptors.put(attribute.get("entityName"), entity);
96     }
97
98   }
99
100   public Map<String, HashMap<String, String>> getSearchableOxmModel() {
101     return searchableOxmModel;
102   }
103
104   public void setSearchableOxmModel(Map<String, HashMap<String, String>> searchableOxmModel) {
105     this.searchableOxmModel = searchableOxmModel;
106   }
107
108   public Map<String, SearchableOxmEntityDescriptor> getSearchableEntityDescriptors() {
109     return searchableEntityDescriptors;
110   }
111
112   public void setSearchableEntityDescriptors(
113       Map<String, SearchableOxmEntityDescriptor> searchableEntityDescriptors) {
114     this.searchableEntityDescriptors = searchableEntityDescriptors;
115   }
116
117 }