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 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;
43 public CrossEntityReferenceLookup() {
44 crossReferenceEntityOxmModel = new LinkedHashMap<>();
45 crossReferenceEntityDescriptors = new HashMap<>();
49 public void processOxmModel(DynamicJAXBContext jaxbContext) {
51 @SuppressWarnings("rawtypes")
52 List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
54 for (@SuppressWarnings("rawtypes")
55 Descriptor desc : descriptorsList) {
57 DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
59 LinkedHashMap<String, String> oxmProperties = new LinkedHashMap<>();
61 // Not all fields have key attributes
62 if (desc.getPrimaryKeyFields() != null) {
63 oxmProperties.put("primaryKeyAttributeNames", desc.getPrimaryKeyFields().toString()
64 .replaceAll("/text\\(\\)", "").replaceAll("\\[", "").replaceAll("\\]", ""));
67 String entityName = desc.getDefaultRootElement();
70 oxmProperties.put(ENTITY_NAME, entityName);
72 Map<String, String> properties = entity.getDescriptor().getProperties();
73 if (properties != null) {
74 for (Map.Entry<String, String> entry : properties.entrySet()) {
76 if (CROSS_ENTITY_REFERENCE.equalsIgnoreCase(entry.getKey())) {
77 oxmProperties.put(CROSS_ENTITY_REFERENCE, entry.getValue());
82 if (oxmProperties.containsKey(CROSS_ENTITY_REFERENCE)) {
83 crossReferenceEntityOxmModel.put(entityName, oxmProperties);
88 for (Entry<String, HashMap<String, String>> crossRefModel : crossReferenceEntityOxmModel
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(",")));
96 List<String> crossEntityRefTokens =
97 Arrays.asList(attribute.get(CROSS_ENTITY_REFERENCE).split(","));
99 if (crossEntityRefTokens.size() >= 2) {
100 CrossEntityReference entityRef = new CrossEntityReference();
101 entityRef.setTargetEntityType(crossEntityRefTokens.get(0));
103 for (int i = 1; i < crossEntityRefTokens.size(); i++) {
104 entityRef.addReferenceAttribute(crossEntityRefTokens.get(i));
107 entity.setCrossEntityReference(entityRef);
109 crossReferenceEntityDescriptors.put(attribute.get(ENTITY_NAME), entity);
114 public Map<String, HashMap<String, String>> getCrossReferenceEntityOxmModel() {
115 return crossReferenceEntityOxmModel;
118 public void setCrossReferenceEntityOxmModel(
119 Map<String, HashMap<String, String>> crossReferenceEntityOxmModel) {
120 this.crossReferenceEntityOxmModel = crossReferenceEntityOxmModel;
123 public Map<String, CrossEntityReferenceDescriptor> getCrossReferenceEntityDescriptors() {
124 return crossReferenceEntityDescriptors;
127 public void setCrossReferenceEntityDescriptors(
128 Map<String, CrossEntityReferenceDescriptor> crossReferenceEntityDescriptors) {
129 this.crossReferenceEntityDescriptors = crossReferenceEntityDescriptors;