Adding UI extensibility
[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.sync.entity.SuggestionSearchEntity;
39
40 public class SuggestionEntityLookup implements OxmModelProcessor {
41
42   // TODO: kill singleton collaborator pattern
43   private static SuggestionEntityLookup instance;
44
45   private Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel;
46   private Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors;
47
48   private SuggestionEntityLookup() {
49     suggestionSearchEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
50     suggestionSearchEntityDescriptors = new HashMap<String, SuggestionEntityDescriptor>();
51   }
52
53   public synchronized static SuggestionEntityLookup getInstance() {
54
55     /*
56      * I hate this method and I want it to go away. The singleton pattern is transitory, I want this
57      * class to be wired via a bean reference instead. But from the starting point, it would require
58      * fixing all the classes across the code base up front and I don't want this task to expand
59      * beyond just refactoring the OxmModelLoader. For now I'll keep the singleton pattern, but I
60      * really want to get rid of it once we are properly spring wired.
61      */
62
63     if (instance == null) {
64       instance = new SuggestionEntityLookup();
65     }
66
67     return instance;
68   }
69
70
71   @Override
72   public void processOxmModel(DynamicJAXBContext jaxbContext) {
73
74     @SuppressWarnings("rawtypes")
75     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
76
77     for (@SuppressWarnings("rawtypes")
78     Descriptor desc : descriptorsList) {
79
80       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
81
82       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
83
84       // Not all fields have key attributes
85       if (desc.getPrimaryKeyFields() != null) {
86         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
87             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
88       }
89
90       String entityName = desc.getDefaultRootElement();
91
92       // add entityName
93       oxmProperties.put("entityName", entityName);
94
95       Map<String, String> properties = entity.getDescriptor().getProperties();
96       if (properties != null) {
97         for (Map.Entry<String, String> entry : properties.entrySet()) {
98
99
100           if (entry.getKey().equalsIgnoreCase("containsSuggestibleProps")) {
101
102             oxmProperties.put("containsSuggestibleProps", "true");
103
104             Vector<DatabaseMapping> descriptorMaps = entity.getDescriptor().getMappings();
105             List<String> listOfSuggestableAttributes = new ArrayList<String>();
106
107             for (DatabaseMapping descMap : descriptorMaps) {
108               if (descMap.isAbstractDirectMapping()) {
109
110                 if (descMap.getProperties().get("suggestibleOnSearch") != null) {
111                   String suggestableOnSearchString =
112                       String.valueOf(descMap.getProperties().get("suggestibleOnSearch"));
113
114                   boolean isSuggestibleOnSearch = Boolean.valueOf(suggestableOnSearchString);
115
116                   if (isSuggestibleOnSearch) {
117                     /* Grab attribute types for suggestion */
118                     String attributeName =
119                         descMap.getField().getName().replaceAll("/text\\(\\)", "");
120                     listOfSuggestableAttributes.add(attributeName);
121
122                     if (descMap.getProperties().get("suggestionVerbs") != null) {
123                       String suggestionVerbsString =
124                           String.valueOf(descMap.getProperties().get("suggestionVerbs"));
125
126                       oxmProperties.put("suggestionVerbs", suggestionVerbsString);
127                     }
128                   }
129                 }
130               }
131             }
132
133             if (!listOfSuggestableAttributes.isEmpty()) {
134               oxmProperties.put("suggestibleAttributes",
135                   String.join(",", listOfSuggestableAttributes));
136             }
137           } else if (entry.getKey().equalsIgnoreCase("suggestionAliases")) {
138             oxmProperties.put("suggestionAliases", entry.getValue());
139           }
140         }
141       }
142
143       if (oxmProperties.containsKey("containsSuggestibleProps")) {
144         suggestionSearchEntityOxmModel.put(entityName, oxmProperties);
145       }
146     }
147
148     for (Entry<String, HashMap<String, String>> suggestionEntityModel : suggestionSearchEntityOxmModel
149         .entrySet()) {
150       HashMap<String, String> attribute = suggestionEntityModel.getValue();
151
152       String entityName = attribute.get("entityName");
153       SuggestionSearchEntity suggestionSearchEntity = new SuggestionSearchEntity(this);
154       suggestionSearchEntity.setEntityType(entityName);
155
156       if (attribute.get("suggestionAliases") != null) {
157         suggestionSearchEntity
158             .setSuggestionAliases(Arrays.asList(attribute.get("suggestionAliases").split(",")));
159       }
160
161       if (attribute.get("suggestibleAttributes") != null) {
162         suggestionSearchEntity.setSuggestionPropertyTypes(
163             Arrays.asList(attribute.get("suggestibleAttributes").split(",")));
164       }
165
166       SuggestionEntityDescriptor entity = new SuggestionEntityDescriptor();
167       entity.setSuggestionSearchEntity(suggestionSearchEntity);
168       entity.setEntityName(entityName);
169
170       if (attribute.get("primaryKeyAttributeNames") != null) {
171         entity.setPrimaryKeyAttributeNames(
172             Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
173       }
174
175       suggestionSearchEntityDescriptors.put(entityName, entity);
176     }
177   }
178
179   public Map<String, HashMap<String, String>> getSuggestionSearchEntityOxmModel() {
180     return suggestionSearchEntityOxmModel;
181   }
182
183   public void setSuggestionSearchEntityOxmModel(
184       Map<String, HashMap<String, String>> suggestionSearchEntityOxmModel) {
185     this.suggestionSearchEntityOxmModel = suggestionSearchEntityOxmModel;
186   }
187
188   public Map<String, SuggestionEntityDescriptor> getSuggestionSearchEntityDescriptors() {
189     return suggestionSearchEntityDescriptors;
190   }
191
192   public void setSuggestionSearchEntityDescriptors(
193       Map<String, SuggestionEntityDescriptor> suggestionSearchEntityDescriptors) {
194     this.suggestionSearchEntityDescriptors = suggestionSearchEntityDescriptors;
195   }
196
197 }