Merge "[AAI] Fix doc config files"
[aai/aai-common.git] / aai-schema-abstraction / src / main / java / org / onap / aai / schemaif / oxm / FromOxmPropertySchema.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2019 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
22 package org.onap.aai.schemaif.oxm;
23
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.eclipse.persistence.dynamic.DynamicType;
29 import org.eclipse.persistence.internal.helper.DatabaseField;
30 import org.eclipse.persistence.mappings.DatabaseMapping;
31 import org.eclipse.persistence.oxm.XMLField;
32 import org.onap.aai.schemaif.SchemaProviderException;
33 import org.onap.aai.schemaif.definitions.PropertySchema;
34 import org.onap.aai.schemaif.definitions.types.BooleanDataType;
35 import org.onap.aai.schemaif.definitions.types.DataType;
36 import org.onap.aai.schemaif.definitions.types.FloatDataType;
37 import org.onap.aai.schemaif.definitions.types.IntDataType;
38 import org.onap.aai.schemaif.definitions.types.LongDataType;
39 import org.onap.aai.schemaif.definitions.types.StringDataType;
40
41 public class FromOxmPropertySchema extends PropertySchema {
42
43     // Handle vertex properties from OXM
44     public void fromOxm(DatabaseMapping mapping, DynamicType dynType, boolean reserved) throws SchemaProviderException {
45         DatabaseField field = mapping.getField();
46         if (field.getName().contains("/")) {
47             name = field.getName().substring(0, field.getName().indexOf("/"));
48         } else {
49             name = field.getName();
50         }
51
52         defaultValue = mapping.getProperties().get("defaultValue") == null ? ""
53                 : mapping.getProperties().get("defaultValue").toString();
54
55         if (isPrimaryKeyOxm(name, dynType)) {
56             unique = true;
57             required = true;
58         } else {
59             required = ((XMLField) field).isRequired();
60             unique = false;
61         }
62
63         isReserved = reserved;
64
65         String oxmType = ((XMLField) field).getTypeName() != null ? ((XMLField) field).getTypeName() : "";
66         if (!mapping.isAbstractDirectMapping()) {
67             // treat complex types as string blobs in oxm
68             dataType = new StringDataType();
69         } else {
70             if (oxmType.equalsIgnoreCase("java.lang.String")) {
71                 dataType = new StringDataType();
72             } else if (oxmType.equalsIgnoreCase("java.lang.Long")) {
73                 dataType = new LongDataType();
74             } else if (oxmType.equalsIgnoreCase("java.lang.Boolean")) {
75                 dataType = new BooleanDataType();
76             } else if (oxmType.equalsIgnoreCase("java.lang.Integer")) {
77                 dataType = new IntDataType();
78             } else if (oxmType.equalsIgnoreCase("java.lang.Float")) {
79                 dataType = new FloatDataType();
80             } else {
81                 throw new SchemaProviderException("Invalid OXM property type: " + oxmType);
82             }
83         }
84
85         // Check annotations
86         annotations = new HashMap<String, String>();
87         Map<String, Object> oxmProps = mapping.getProperties();
88         for (Map.Entry<String, Object> entry : oxmProps.entrySet()) {
89             if (entry.getValue() instanceof String) {
90                 annotations.put(entry.getKey().toLowerCase(), (String) entry.getValue());
91             }
92         }
93     }
94
95     // Handle edge properties from DBEdgeRules
96     public void fromRelationship(String propName, DataType.Type propDataType) throws SchemaProviderException {
97         name = propName;
98         required = false;
99         defaultValue = "";
100         unique = false;
101         annotations = new HashMap<String, String>();
102
103         switch (propDataType) {
104             case STRING:
105                 dataType = new StringDataType();
106                 break;
107             case INT:
108                 dataType = new IntDataType();
109                 break;
110             case FLOAT:
111                 dataType = new FloatDataType();
112                 break;
113             case LONG:
114                 dataType = new LongDataType();
115                 break;
116             case BOOL:
117                 dataType = new BooleanDataType();
118                 break;
119             default:
120                 throw new SchemaProviderException("Invalid EdgeRule property type: " + propDataType);
121         }
122     }
123
124     private boolean isPrimaryKeyOxm(String propName, DynamicType dynType) {
125         List<String> primaryKeyList = dynType.getDescriptor().getPrimaryKeyFieldNames();
126         if ((primaryKeyList == null) || (primaryKeyList.size() == 0)) {
127             return false;
128         }
129
130         for (String key : primaryKeyList) {
131             String keyName = key.substring(0, key.indexOf('/'));
132             if (keyName.equals(propName)) {
133                 return true;
134             }
135         }
136
137         return false;
138     }
139
140 }