fixes in CrossEntityReferenceLookup.java
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / config / oxm / CrossEntityReferenceLookup.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 CrossEntityReferenceLookup implements OxmModelProcessor {
35
36   private static final String ENTITY_NAME  = "entityName";
37   private static final String CROSS_ENTITY_REFERENCE  = "crossEntityReference";
38   private Map<String, HashMap<String, String>> crossReferenceEntityOxmModel;
39   private Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors;
40
41
42
43   public CrossEntityReferenceLookup() {
44     crossReferenceEntityOxmModel = new LinkedHashMap<>();
45     crossReferenceEntityDescriptors = new HashMap<>();
46   }
47
48   @Override
49   public void processOxmModel(DynamicJAXBContext jaxbContext) {
50
51     @SuppressWarnings("rawtypes")
52     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
53
54     for (@SuppressWarnings("rawtypes")
55     Descriptor desc : descriptorsList) {
56
57       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
58
59       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<>();
60
61       // Not all fields have key attributes
62       if (desc.getPrimaryKeyFields() != null) {
63         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
64             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
65       }
66
67       String entityName = desc.getDefaultRootElement();
68       
69       // add entityName
70       oxmProperties.put(ENTITY_NAME, entityName);
71
72       Map<String, String> properties = entity.getDescriptor().getProperties();
73       if (properties != null) {
74         for (Map.Entry<String, String> entry : properties.entrySet()) {
75
76           if (CROSS_ENTITY_REFERENCE.equalsIgnoreCase(entry.getKey())) {
77             oxmProperties.put(CROSS_ENTITY_REFERENCE, entry.getValue());
78           }
79         }
80       }
81
82       if (oxmProperties.containsKey(CROSS_ENTITY_REFERENCE)) {
83         crossReferenceEntityOxmModel.put(entityName, oxmProperties);
84       }
85
86     }
87   
88     for (Entry<String, HashMap<String, String>> crossRefModel : crossReferenceEntityOxmModel
89         .entrySet()) {
90       HashMap<String, String> attribute = crossRefModel.getValue();
91       CrossEntityReferenceDescriptor entity = new CrossEntityReferenceDescriptor();
92       entity.setEntityName(attribute.get(ENTITY_NAME));
93       entity.setPrimaryKeyAttributeNames(
94           Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
95
96       List<String> crossEntityRefTokens =
97           Arrays.asList(attribute.get(CROSS_ENTITY_REFERENCE).split(","));
98
99       if (crossEntityRefTokens.size() >= 2) {
100         CrossEntityReference entityRef = new CrossEntityReference();
101         entityRef.setTargetEntityType(crossEntityRefTokens.get(0));
102
103         for (int i = 1; i < crossEntityRefTokens.size(); i++) {
104           entityRef.addReferenceAttribute(crossEntityRefTokens.get(i));
105         }
106
107         entity.setCrossEntityReference(entityRef);
108       }
109       crossReferenceEntityDescriptors.put(attribute.get(ENTITY_NAME), entity);
110     }
111
112   }
113
114   public Map<String, HashMap<String, String>> getCrossReferenceEntityOxmModel() {
115     return crossReferenceEntityOxmModel;
116   }
117
118   public void setCrossReferenceEntityOxmModel(
119       Map<String, HashMap<String, String>> crossReferenceEntityOxmModel) {
120     this.crossReferenceEntityOxmModel = crossReferenceEntityOxmModel;
121   }
122
123   public Map<String, CrossEntityReferenceDescriptor> getCrossReferenceEntityDescriptors() {
124     return crossReferenceEntityDescriptors;
125   }
126
127   public void setCrossReferenceEntityDescriptors(
128       Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors) {
129     this.crossReferenceEntityDescriptors = crossReferenceEntityDescriptors;
130   }
131   
132   
133
134  
135 }