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