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