2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.aai.sparky.config.oxm;
23 import java.util.Arrays;
24 import java.util.HashMap;
25 import java.util.LinkedHashMap;
26 import java.util.List;
28 import java.util.Map.Entry;
30 import org.eclipse.persistence.dynamic.DynamicType;
31 import org.eclipse.persistence.internal.oxm.mappings.Descriptor;
32 import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContext;
34 public class CrossEntityReferenceLookup implements OxmModelProcessor {
36 private Map<String, HashMap<String, String>> crossReferenceEntityOxmModel;
37 private Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors;
40 public CrossEntityReferenceLookup() {
41 crossReferenceEntityOxmModel = new LinkedHashMap<String, HashMap<String, String>>();
42 crossReferenceEntityDescriptors = new HashMap<String, CrossEntityReferenceDescriptor>();
46 public void processOxmModel(DynamicJAXBContext jaxbContext) {
48 @SuppressWarnings("rawtypes")
49 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
51 for (@SuppressWarnings("rawtypes")
52 Descriptor desc : descriptorsList) {
54 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
56 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<String, String>();
58 // Not all fields have key attributes
59 if (desc.getPrimaryKeyFields() != null) {
60 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
61 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
64 String entityName = desc.getDefaultRootElement();
67 oxmProperties.put("entityName", entityName);
69 Map<String, String> properties = entity.getDescriptor().getProperties();
70 if (properties != null) {
71 for (Map.Entry<String, String> entry : properties.entrySet()) {
73 if (entry.getKey().equalsIgnoreCase("crossEntityReference")) {
74 oxmProperties.put("crossEntityReference", entry.getValue());
79 if (oxmProperties.containsKey("crossEntityReference")) {
80 crossReferenceEntityOxmModel.put(entityName, oxmProperties);
85 for (Entry<String, HashMap<String, String>> crossRefModel : crossReferenceEntityOxmModel
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(",")));
93 List<String> crossEntityRefTokens =
94 Arrays.asList(attribute.get("crossEntityReference").split(","));
96 if (crossEntityRefTokens.size() >= 2) {
97 CrossEntityReference entityRef = new CrossEntityReference();
98 entityRef.setTargetEntityType(crossEntityRefTokens.get(0));
100 for (int i = 1; i < crossEntityRefTokens.size(); i++) {
101 entityRef.addReferenceAttribute(crossEntityRefTokens.get(i));
104 entity.setCrossEntityReference(entityRef);
106 crossReferenceEntityDescriptors.put(attribute.get("entityName"), entity);
111 public Map<String, HashMap<String, String>> getCrossReferenceEntityOxmModel() {
112 return crossReferenceEntityOxmModel;
115 public void setCrossReferenceEntityOxmModel(
116 Map<String, HashMap<String, String>> crossReferenceEntityOxmModel) {
117 this.crossReferenceEntityOxmModel = crossReferenceEntityOxmModel;
120 public Map<String, CrossEntityReferenceDescriptor> getCrossReferenceEntityDescriptors() {
121 return crossReferenceEntityDescriptors;
124 public void setCrossReferenceEntityDescriptors(
125 Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors) {
126 this.crossReferenceEntityDescriptors = crossReferenceEntityDescriptors;