Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / SuggestionEntityLookup.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.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.LinkedHashMap;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Map.Entry;
34 import java.util.Vector;
35
36 import org.eclipse.persistence.dynamic.DynamicType;
37 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
38 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
39 import org.eclipse.persistence.mappings.DatabaseMapping;
40 import org.onap.aai.sparky.search.filters.config.FiltersConfig;
41 import org.onap.aai.sparky.sync.entity.SuggestionSearchEntity;
42
43 public class SuggestionEntityLookup implements OxmModelProcessor {
44
45   private Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel;
46   private Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors;
47   private FiltersConfig filtersConfig;
48   
49   public SuggestionEntityLookup(FiltersConfig filtersConfig) {
50     suggestionSearchEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
51     suggestionSearchEntityDescriptors = new HashMap<String, SuggestionEntityDescriptor>();
52     this.filtersConfig = filtersConfig;
53   }
54   
55   @Override
56   public void processOxmModel(DynamicJAXBContext jaxbContext) {
57
58     @SuppressWarnings("rawtypes")
59     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
60
61     for (@SuppressWarnings("rawtypes")
62     Descriptor desc : descriptorsList) {
63
64       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
65
66       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
67
68       // Not all fields have key attributes
69       if (desc.getPrimaryKeyFields() != null) {
70         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
71             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
72       }
73
74       String entityName = desc.getDefaultRootElement();
75
76       // add entityName
77       oxmProperties.put("entityName", entityName);
78
79       Map<String, String> properties = entity.getDescriptor().getProperties();
80       if (properties != null) {
81         for (Map.Entry<String, String> entry : properties.entrySet()) {
82
83
84           if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) {
85
86             oxmProperties.put("containsSuggestibleProps", "true");
87
88             Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
89             List<String> listOfSuggestableAttributes = new ArrayList<String>();
90
91             for (DatabaseMapping descMap : descriptorMaps) {
92               if (descMap.isAbstractDirectMapping()) {
93
94                 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
95                   String suggestableOnSearchString =
96                       String.valueOf(descMap.getProperties().get("suggestibleOnSearch"));
97
98                   boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
99
100                   if (isSuggestibleOnSearch) {
101                     /* Grab attribute types for suggestion */
102                     String attributeName =
103                         descMap.getField().getName().replaceAll("/text\\(\\)", "");
104                     listOfSuggestableAttributes.add(attributeName);
105
106                     if (descMap.getProperties().get("suggestionVerbs") != null) {
107                       String suggestionVerbsString =
108                           String.valueOf(descMap.getProperties().get("suggestionVerbs"));
109
110                       oxmProperties.put("suggestionVerbs", suggestionVerbsString);
111                     }
112                   }
113                 }
114               }
115             }
116             
117             if (!listOfSuggestableAttributes.isEmpty()) {
118               oxmProperties.put("suggestibleAttributes",
119                   String.join(",", listOfSuggestableAttributes));
120             }
121           } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) {
122             oxmProperties.put("suggestionAliases", entry.getValue());
123           }
124         }
125       }
126
127       if (oxmProperties.containsKey("containsSuggestibleProps")) {
128         suggestionSearchEntityOxmModel.put(entityName, oxmProperties);
129       }
130     }
131
132     for (Entry<String, HashMap<String, String>> suggestionEntityModel : suggestionSearchEntityOxmModel
133         .entrySet()) {
134       HashMap<String, String> attribute = suggestionEntityModel.getValue();
135
136       String entityName = attribute.get("entityName");
137       SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity(filtersConfig, this);
138       suggestionSearchEntity.setEntityType(entityName);
139
140       if (attribute.get("suggestionAliases") != null) {
141         suggestionSearchEntity
142             .setSuggestionAliases(Arrays.asList(attribute.get("suggestionAliases").split(",")));
143       }
144
145       if (attribute.get("suggestibleAttributes") != null) {
146         suggestionSearchEntity.setSuggestionPropertyTypes(
147             Arrays.asList(attribute.get("suggestibleAttributes").split(",")));
148       }
149
150       SuggestionEntityDescriptor entity = new SuggestionEntityDescriptor();
151       entity.setSuggestionSearchEntity(suggestionSearchEntity);
152       entity.setEntityName(entityName);
153
154       if (attribute.get("primaryKeyAttributeNames") != null) {
155         entity.setPrimaryKeyAttributeNames(
156             Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
157       }
158
159       suggestionSearchEntityDescriptors.put(entityName, entity);
160     }
161   }
162
163   public Map<String, HashMap<String, String>> getSuggestionSearchEntityOxmModel() {
164     return suggestionSearchEntityOxmModel;
165   }
166
167   public void setSuggestionSearchEntityOxmModel(
168       Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel) {
169     this.suggestionSearchEntityOxmModel = suggestionSearchEntityOxmModel;
170   }
171
172   public Map<String, SuggestionEntityDescriptor> getSuggestionSearchEntityDescriptors() {
173     return suggestionSearchEntityDescriptors;
174   }
175
176   public void setSuggestionSearchEntityDescriptors(
177       Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors) {
178     this.suggestionSearchEntityDescriptors = suggestionSearchEntityDescriptors;
179   }
180   
181 }