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