Convert Sparky to Spring-Boot
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / CrossEntityReferenceLookup.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 CrossEntityReferenceLookup implements OxmModelProcessor {
39
40   private Map<String, HashMap<String, String>> crossReferenceEntityOxmModel;
41   private Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors;
42
43
44   public CrossEntityReferenceLookup() {
45     crossReferenceEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
46     crossReferenceEntityDescriptors = new HashMap<String, CrossEntityReferenceDescriptor>();
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       // add entityName
71       oxmProperties.put("entityName", entityName);
72
73       Map<String, String> properties = entity.getDescriptor().getProperties();
74       if (properties != null) {
75         for (Map.Entry<String, String> entry : properties.entrySet()) {
76
77           if (entry.getKey().equalsIgnoreCase("crossEntityReference")) {
78             oxmProperties.put("crossEntityReference", entry.getValue());
79           }
80         }
81       }
82
83       if (oxmProperties.containsKey("crossEntityReference")) {
84         crossReferenceEntityOxmModel.put(entityName, oxmProperties);
85       }
86
87     }
88   
89     for (Entry<String, HashMap<String, String>> crossRefModel : crossReferenceEntityOxmModel
90         .entrySet()) {
91       HashMap<String, String> attribute = crossRefModel.getValue();
92       CrossEntityReferenceDescriptor entity = new CrossEntityReferenceDescriptor();
93       entity.setEntityName(attribute.get("entityName"));
94       entity.setPrimaryKeyAttributeNames(
95           Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
96
97       List<String> crossEntityRefTokens =
98           Arrays.asList(attribute.get("crossEntityReference").split(","));
99
100       if (crossEntityRefTokens.size() >= 2) {
101         CrossEntityReference entityRef = new CrossEntityReference();
102         entityRef.setTargetEntityType(crossEntityRefTokens.get(0));
103
104         for (int i = 1; i < crossEntityRefTokens.size(); i++) {
105           entityRef.addReferenceAttribute(crossEntityRefTokens.get(i));
106         }
107
108         entity.setCrossEntityReference(entityRef);
109       }
110       crossReferenceEntityDescriptors.put(attribute.get("entityName"), entity);
111     }
112
113   }
114
115   public Map<String, HashMap<String, String>> getCrossReferenceEntityOxmModel() {
116     return crossReferenceEntityOxmModel;
117   }
118
119   public void setCrossReferenceEntityOxmModel(
120       Map<String, HashMap<String, String>> crossReferenceEntityOxmModel) {
121     this.crossReferenceEntityOxmModel = crossReferenceEntityOxmModel;
122   }
123
124   public Map<String, CrossEntityReferenceDescriptor> getCrossReferenceEntityDescriptors() {
125     return crossReferenceEntityDescriptors;
126   }
127
128   public void setCrossReferenceEntityDescriptors(
129       Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors) {
130     this.crossReferenceEntityDescriptors = crossReferenceEntityDescriptors;
131   }
132   
133   
134
135  
136 }