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