Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / OxmEntityLookup.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.Arrays;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 import org.eclipse.persistence.dynamic.DynamicType;
31 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
32 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
33
34 public class OxmEntityLookup implements OxmModelProcessor {
35
36   private Map<String, HashMap<String, String>> oxmModel;
37
38   private Map<String, DynamicType> entityTypeLookup;
39
40   private Map<String, OxmEntityDescriptor> entityDescriptors;
41
42
43   public OxmEntityLookup() {
44     oxmModel = new LinkedHashMap<String, HashMap<String, String>>();
45     entityTypeLookup = new LinkedHashMap<String, DynamicType>();
46     entityDescriptors = new HashMap<String, OxmEntityDescriptor>();
47   }
48
49   @Override
50   public void processOxmModel(DynamicJAXBContext jaxbContext) {
51
52     @SuppressWarnings("rawtypes")
53     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
54
55     for (@SuppressWarnings("rawtypes")
56     Descriptor desc : descriptorsList) {
57
58       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
59
60       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
61
62       // Not all fields have key attributes
63       if (desc.getPrimaryKeyFields() != null) {
64         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
65             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
66       }
67
68       String entityName = desc.getDefaultRootElement();
69
70       entityTypeLookup.put(entityName, entity);
71
72       // add entityName
73       oxmProperties.put("entityName", entityName);
74
75       Map<String, String> properties = entity.getDescriptor().getProperties();
76
77       oxmModel.put(entityName, oxmProperties);
78
79     }
80
81     for (Entry<String, HashMap<String, String>> entityModel : oxmModel.entrySet()) {
82       HashMap<String, String> attribute = entityModel.getValue();
83       OxmEntityDescriptor entity = new OxmEntityDescriptor();
84
85       entity.setEntityName(attribute.get("entityName"));
86
87       if (attribute.containsKey("primaryKeyAttributeNames")) {
88
89         entity.setPrimaryKeyAttributeNames(
90             Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
91
92         entityDescriptors.put(attribute.get("entityName"), entity);
93       }
94     }
95
96   }
97
98   public Map<String, HashMap<String, String>> getOxmModel() {
99     return oxmModel;
100   }
101
102   public void setOxmModel(Map<String, HashMap<String, String>> oxmModel) {
103     this.oxmModel = oxmModel;
104   }
105
106   public Map<String, DynamicType> getEntityTypeLookup() {
107     return entityTypeLookup;
108   }
109
110   public void setEntityTypeLookup(Map<String, DynamicType> entityTypeLookup) {
111     this.entityTypeLookup = entityTypeLookup;
112   }
113
114   public Map<String, OxmEntityDescriptor> getEntityDescriptors() {
115     return entityDescriptors;
116   }
117
118   public void setEntityDescriptors(Map<String, OxmEntityDescriptor> entityDescriptors) {
119     this.entityDescriptors = entityDescriptors;
120   }
121   
122   public void addEntityDescriptor(String type, OxmEntityDescriptor descriptor) {
123     if ( this.entityDescriptors != null ) {
124       this.entityDescriptors.put(type, descriptor);
125     }
126   }
127
128 }