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