ba8091bdb2769782062fe6e029c2e7ce13999108
[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   private Map<String, HashMap<String, String>> oxmModel;
39
40   private Map<String, DynamicType> entityTypeLookup;
41
42   private Map<String, OxmEntityDescriptor> entityDescriptors;
43
44
45   public OxmEntityLookup() {
46     oxmModel = new LinkedHashMap<String, HashMap<String, String>>();
47     entityTypeLookup = new LinkedHashMap<String, DynamicType>();
48     entityDescriptors = new HashMap<String, OxmEntityDescriptor>();
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       entityTypeLookup.put(entityName, entity);
73
74       // add entityName
75       oxmProperties.put("entityName", entityName);
76
77       Map<String, String> properties = entity.getDescriptor().getProperties();
78
79       oxmModel.put(entityName, oxmProperties);
80
81     }
82
83     for (Entry<String, HashMap<String, String>> entityModel : oxmModel.entrySet()) {
84       HashMap<String, String> attribute = entityModel.getValue();
85       OxmEntityDescriptor entity = new OxmEntityDescriptor();
86
87       entity.setEntityName(attribute.get("entityName"));
88
89       if (attribute.containsKey("primaryKeyAttributeNames")) {
90
91         entity.setPrimaryKeyAttributeNames(
92             Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
93
94         entityDescriptors.put(attribute.get("entityName"), entity);
95       }
96     }
97
98   }
99
100   public Map<String, HashMap<String, String>> getOxmModel() {
101     return oxmModel;
102   }
103
104   public void setOxmModel(Map<String, HashMap<String, String>> oxmModel) {
105     this.oxmModel = oxmModel;
106   }
107
108   public Map<String, DynamicType> getEntityTypeLookup() {
109     return entityTypeLookup;
110   }
111
112   public void setEntityTypeLookup(Map<String, DynamicType> entityTypeLookup) {
113     this.entityTypeLookup = entityTypeLookup;
114   }
115
116   public Map<String, OxmEntityDescriptor> getEntityDescriptors() {
117     return entityDescriptors;
118   }
119
120   public void setEntityDescriptors(Map<String, OxmEntityDescriptor> entityDescriptors) {
121     this.entityDescriptors = entityDescriptors;
122   }
123   
124   public void addEntityDescriptor(String type, OxmEntityDescriptor descriptor) {
125     if ( this.entityDescriptors != null ) {
126       this.entityDescriptors.put(type, descriptor);
127     }
128   }
129
130 }