168a4b14b891d345c1fe2cc9169984eb04c4b5b8
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / config / oxm / OxmEntityLookup.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.Arrays;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
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
36 public class OxmEntityLookup implements OxmModelProcessor {
37
38   // TODO: kill singleton collaborator pattern
39   private static OxmEntityLookup instance;
40
41   private Map<String, HashMap<String, String>> oxmModel;
42
43   private Map<String, DynamicType> entityTypeLookup;
44
45   private Map<String, OxmEntityDescriptor> entityDescriptors;
46
47
48   private OxmEntityLookup() {
49     oxmModel = new LinkedHashMap<String, HashMap<String, String>>();
50     entityTypeLookup = new LinkedHashMap<String, DynamicType>();
51     entityDescriptors = new HashMap<String, OxmEntityDescriptor>();
52   }
53
54   public synchronized static OxmEntityLookup getInstance() {
55
56     /*
57      * I hate this method and I want it to go away. The singleton pattern is transitory, I want this
58      * class to be wired via a bean reference instead. But from the starting point, it would require
59      * fixing all the classes across the code base up front and I don't want this task to expand
60      * beyond just refactoring the OxmModelLoader. For now I'll keep the singleton pattern, but I
61      * really want to get rid of it once we are properly spring wired.
62      */
63
64     if (instance == null) {
65       instance = new OxmEntityLookup();
66     }
67
68     return instance;
69   }
70
71
72   @Override
73   public void processOxmModel(DynamicJAXBContext jaxbContext) {
74
75     @SuppressWarnings("rawtypes")
76     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
77
78     for (@SuppressWarnings("rawtypes")
79     Descriptor desc : descriptorsList) {
80
81       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
82
83       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
84
85       // Not all fields have key attributes
86       if (desc.getPrimaryKeyFields() != null) {
87         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
88             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
89       }
90
91       String entityName = desc.getDefaultRootElement();
92
93       entityTypeLookup.put(entityName, entity);
94
95       // add entityName
96       oxmProperties.put("entityName", entityName);
97
98       Map<String, String> properties = entity.getDescriptor().getProperties();
99
100       oxmModel.put(entityName, oxmProperties);
101
102     }
103
104     for (Entry<String, HashMap<String, String>> entityModel : oxmModel.entrySet()) {
105       HashMap<String, String> attribute = entityModel.getValue();
106       OxmEntityDescriptor entity = new OxmEntityDescriptor();
107
108       entity.setEntityName(attribute.get("entityName"));
109
110       if (attribute.containsKey("primaryKeyAttributeNames")) {
111
112         entity.setPrimaryKeyAttributeNames(
113             Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
114
115         entityDescriptors.put(attribute.get("entityName"), entity);
116       }
117     }
118
119   }
120
121   public Map<String, HashMap<String, String>> getOxmModel() {
122     return oxmModel;
123   }
124
125   public void setOxmModel(Map<String, HashMap<String, String>> oxmModel) {
126     this.oxmModel = oxmModel;
127   }
128
129   public Map<String, DynamicType> getEntityTypeLookup() {
130     return entityTypeLookup;
131   }
132
133   public void setEntityTypeLookup(Map<String, DynamicType> entityTypeLookup) {
134     this.entityTypeLookup = entityTypeLookup;
135   }
136
137   public Map<String, OxmEntityDescriptor> getEntityDescriptors() {
138     return entityDescriptors;
139   }
140
141   public void setEntityDescriptors(Map<String, OxmEntityDescriptor> entityDescriptors) {
142     this.entityDescriptors = entityDescriptors;
143   }
144
145   public void addEntityDescriptor(String type, OxmEntityDescriptor descriptor) {
146     if (this.entityDescriptors != null) {
147       this.entityDescriptors.put(type, descriptor);
148     }
149   }
150
151 }