Adding UI extensibility
[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 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.config.oxm;
24
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.LinkedHashMap;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Map.Entry;
31
32 import org.eclipse.persistence.dynamic.DynamicType;
33 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
34 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
35
36 public class CrossEntityReferenceLookup implements OxmModelProcessor {
37
38   // TODO: kill singleton collaborator pattern
39   private static CrossEntityReferenceLookup instance;
40
41   private Map<String, HashMap<String, String>> crossReferenceEntityOxmModel;
42   private Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors;
43
44
45   private CrossEntityReferenceLookup() {
46     crossReferenceEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
47     crossReferenceEntityDescriptors = new HashMap<String, CrossEntityReferenceDescriptor>();
48   }
49
50   public synchronized static CrossEntityReferenceLookup getInstance() {
51
52     /*
53      * I hate this method and I want it to go away. The singleton pattern is transitory, I want this
54      * class to be wired via a bean reference instead. But from the starting point, it would require
55      * fixing all the classes across the code base up front and I don't want this task to expand
56      * beyond just refactoring the OxmModelLoader. For now I'll keep the singleton pattern, but I
57      * really want to get rid of it once we are properly spring wired.
58      */
59
60     if (instance == null) {
61       instance = new CrossEntityReferenceLookup();
62     }
63
64     return instance;
65   }
66
67
68   @Override
69   public void processOxmModel(DynamicJAXBContext jaxbContext) {
70
71     @SuppressWarnings("rawtypes")
72     List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
73
74     for (@SuppressWarnings("rawtypes")
75     Descriptor desc : descriptorsList) {
76
77       DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
78
79       LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
80
81       // Not all fields have key attributes
82       if (desc.getPrimaryKeyFields() != null) {
83         oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
84             .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
85       }
86
87       String entityName = desc.getDefaultRootElement();
88
89       // add entityName
90       oxmProperties.put("entityName", entityName);
91
92       Map<String, String> properties = entity.getDescriptor().getProperties();
93       if (properties != null) {
94         for (Map.Entry<String, String> entry : properties.entrySet()) {
95
96           if (entry.getKey().equalsIgnoreCase("crossEntityReference")) {
97             oxmProperties.put("crossEntityReference", entry.getValue());
98           }
99         }
100       }
101
102       if (oxmProperties.containsKey("crossEntityReference")) {
103         crossReferenceEntityOxmModel.put(entityName, oxmProperties);
104       }
105
106     }
107
108     for (Entry<String, HashMap<String, String>> crossRefModel : crossReferenceEntityOxmModel
109         .entrySet()) {
110       HashMap<String, String> attribute = crossRefModel.getValue();
111       CrossEntityReferenceDescriptor entity = new CrossEntityReferenceDescriptor();
112       entity.setEntityName(attribute.get("entityName"));
113       entity.setPrimaryKeyAttributeNames(
114           Arrays.asList(attribute.get("primaryKeyAttributeNames").replace(" ", "").split(",")));
115
116       List<String> crossEntityRefTokens =
117           Arrays.asList(attribute.get("crossEntityReference").split(","));
118
119       if (crossEntityRefTokens.size() >= 2) {
120         CrossEntityReference entityRef = new CrossEntityReference();
121         entityRef.setTargetEntityType(crossEntityRefTokens.get(0));
122
123         for (int i = 1; i < crossEntityRefTokens.size(); i++) {
124           entityRef.addReferenceAttribute(crossEntityRefTokens.get(i));
125         }
126
127         entity.setCrossEntityReference(entityRef);
128       }
129       crossReferenceEntityDescriptors.put(attribute.get("entityName"), entity);
130     }
131
132   }
133
134   public Map<String, HashMap<String, String>> getCrossReferenceEntityOxmModel() {
135     return crossReferenceEntityOxmModel;
136   }
137
138   public void setCrossReferenceEntityOxmModel(
139       Map<String, HashMap<String, String>> crossReferenceEntityOxmModel) {
140     this.crossReferenceEntityOxmModel = crossReferenceEntityOxmModel;
141   }
142
143   public Map<String, CrossEntityReferenceDescriptor> getCrossReferenceEntityDescriptors() {
144     return crossReferenceEntityDescriptors;
145   }
146
147   public void setCrossReferenceEntityDescriptors(
148       Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors) {
149     this.crossReferenceEntityDescriptors = crossReferenceEntityDescriptors;
150   }
151
152
153
154 }