2 * ============LICENSE_START===================================================
3 * SPARKY (AAI UI service)
4 * ============================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=====================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
25 package org.onap.aai.sparky.config.oxm;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.LinkedHashMap;
30 import java.util.List;
32 import java.util.Map.Entry;
34 import org.eclipse.persistence.dynamic.DynamicType;
35 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
36 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
38 public class CrossEntityReferenceLookup implements OxmModelProcessor {
40 private Map<String, HashMap<String, String>> crossReferenceEntityOxmModel;
41 private Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors;
44 public CrossEntityReferenceLookup() {
45 crossReferenceEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
46 crossReferenceEntityDescriptors = new HashMap<String, CrossEntityReferenceDescriptor>();
50 public void processOxmModel(DynamicJAXBContext jaxbContext) {
52 @SuppressWarnings("rawtypes")
53 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
55 for (@SuppressWarnings("rawtypes")
56 Descriptor desc : descriptorsList) {
58 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
60 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
62 // Not all fields have key attributes
63 if (desc.getPrimaryKeyFields() != null) {
64 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
65 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
68 String entityName = desc.getDefaultRootElement();
71 oxmProperties.put("entityName", entityName);
73 Map<String, String> properties = entity.getDescriptor().getProperties();
74 if (properties != null) {
75 for (Map.Entry<String, String> entry : properties.entrySet()) {
77 if (entry.getKey().equalsIgnoreCase("crossEntityReference")) {
78 oxmProperties.put("crossEntityReference", entry.getValue());
83 if (oxmProperties.containsKey("crossEntityReference")) {
84 crossReferenceEntityOxmModel.put(entityName, oxmProperties);
89 for (Entry<String, HashMap<String, String>> crossRefModel : crossReferenceEntityOxmModel
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(",")));
97 List<String> crossEntityRefTokens =
98 Arrays.asList(attribute.get("crossEntityReference").split(","));
100 if (crossEntityRefTokens.size() >= 2) {
101 CrossEntityReference entityRef = new CrossEntityReference();
102 entityRef.setTargetEntityType(crossEntityRefTokens.get(0));
104 for (int i = 1; i < crossEntityRefTokens.size(); i++) {
105 entityRef.addReferenceAttribute(crossEntityRefTokens.get(i));
108 entity.setCrossEntityReference(entityRef);
110 crossReferenceEntityDescriptors.put(attribute.get("entityName"), entity);
115 public Map<String, HashMap<String, String>> getCrossReferenceEntityOxmModel() {
116 return crossReferenceEntityOxmModel;
119 public void setCrossReferenceEntityOxmModel(
120 Map<String, HashMap<String, String>> crossReferenceEntityOxmModel) {
121 this.crossReferenceEntityOxmModel = crossReferenceEntityOxmModel;
124 public Map<String, CrossEntityReferenceDescriptor> getCrossReferenceEntityDescriptors() {
125 return crossReferenceEntityDescriptors;
128 public void setCrossReferenceEntityDescriptors(
129 Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors) {
130 this.crossReferenceEntityDescriptors = crossReferenceEntityDescriptors;