Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / OxmEntityLookup.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.config.oxm;
26
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Map.Entry;
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
38 public class OxmEntityLookup implements OxmModelProcessor {
39
40   private Map<String, HashMap<String, String>> oxmModel;
41
42   private Map<String, DynamicType> entityTypeLookup;
43
44   private Map<String, OxmEntityDescriptor> entityDescriptors;
45
46
47   public OxmEntityLookup() {
48     oxmModel = new LinkedHashMap<String, HashMap<String, String>>();
49     entityTypeLookup = new LinkedHashMap<String, DynamicType>();
50     entityDescriptors = new HashMap<String, OxmEntityDescriptor>();
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       entityTypeLookup.put(entityName, entity);
75
76       // add entityName
77       oxmProperties.put("entityName", entityName);
78
79       Map<String, String> properties = entity.getDescriptor().getProperties();
80
81       oxmModel.put(entityName, oxmProperties);
82
83     }
84
85     for (Entry<String, HashMap<String, String>> entityModel : oxmModel.entrySet()) {
86       HashMap<String, String> attribute = entityModel.getValue();
87       OxmEntityDescriptor entity = new OxmEntityDescriptor();
88
89       entity.setEntityName(attribute.get("entityName"));
90
91       if (attribute.containsKey("primaryKeyAttributeNames")) {
92
93         entity.setPrimaryKeyAttributeNames(
94             Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
95
96         entityDescriptors.put(attribute.get("entityName"), entity);
97       }
98     }
99
100   }
101
102   public Map<String, HashMap<String, String>> getOxmModel() {
103     return oxmModel;
104   }
105
106   public void setOxmModel(Map<String, HashMap<String, String>> oxmModel) {
107     this.oxmModel = oxmModel;
108   }
109
110   public Map<String, DynamicType> getEntityTypeLookup() {
111     return entityTypeLookup;
112   }
113
114   public void setEntityTypeLookup(Map<String, DynamicType> entityTypeLookup) {
115     this.entityTypeLookup = entityTypeLookup;
116   }
117
118   public Map<String, OxmEntityDescriptor> getEntityDescriptors() {
119     return entityDescriptors;
120   }
121
122   public void setEntityDescriptors(Map<String, OxmEntityDescriptor> entityDescriptors) {
123     this.entityDescriptors = entityDescriptors;
124   }
125   
126   public void addEntityDescriptor(String type, OxmEntityDescriptor descriptor) {
127     if ( this.entityDescriptors != null ) {
128       this.entityDescriptors.put(type, descriptor);
129     }
130   }
131
132 }