8c79022677fe40c8b4e1398d4c0d1b10f3c566f3
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / util / DocumentSchemaUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
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.sa.searchdbabstraction.util;
22
23 import com.fasterxml.jackson.core.JsonParseException;
24 import com.fasterxml.jackson.databind.JsonMappingException;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.onap.aai.sa.rest.DocumentFieldSchema;
27 import org.onap.aai.sa.rest.DocumentSchema;
28
29 import java.io.IOException;
30 import java.util.List;
31 import java.util.concurrent.atomic.AtomicBoolean;
32
33 public class DocumentSchemaUtil {
34
35   public static String generateDocumentMappings(String documentSchema)
36       throws JsonParseException, JsonMappingException, IOException {
37
38     // Unmarshal the json content into a document schema object.
39     ObjectMapper mapper = new ObjectMapper();
40     DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
41
42     return generateDocumentMappings(schema);
43   }
44
45   public static String generateDocumentMappings(DocumentSchema schema) {
46
47     // Now, generate the Elastic Search mapping json and return it.
48     StringBuilder sb = new StringBuilder();
49     sb.append("{");
50     sb.append("\"properties\": {");
51
52     generateFieldMappings(schema.getFields(), sb);
53
54     sb.append("}");
55     sb.append("}");
56
57     return sb.toString();
58   }
59
60
61   private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
62
63     AtomicBoolean firstField = new AtomicBoolean(true);
64
65     for (DocumentFieldSchema field : fields) {
66
67       // If this isn't the first field in the list, prepend it with a ','
68       if (!firstField.compareAndSet(true, false)) {
69         sb.append(", ");
70       }
71
72       // Now, append the translated field contents.
73       generateFieldMapping(field, sb);
74     }
75   }
76
77   private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
78
79     sb.append("\"").append(fieldSchema.getName()).append("\": {");
80
81     // The field type is mandatory.
82     sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
83
84     // For date type fields we may optionally supply a format specifier.
85     if (fieldSchema.getDataType().equals("date")) {
86       if (fieldSchema.getFormat() != null) {
87         sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
88       }
89     }
90
91     // If the index field was specified, then append it.
92     if (fieldSchema.getSearchable() != null) {
93       sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
94           ? "analyzed" : "not_analyzed").append("\"");
95     }
96
97     // If a search analyzer was specified, then append it.
98     if (fieldSchema.getSearchAnalyzer() != null) {
99       sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
100     }
101
102     // If an indexing analyzer was specified, then append it.
103     if (fieldSchema.getIndexAnalyzer() != null) {
104       sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
105     }
106
107
108     if (fieldSchema.getDataType().equals("nested")) {
109
110       sb.append(", \"properties\": {");
111       generateFieldMappings(fieldSchema.getSubFields(), sb);
112       sb.append("}");
113     }
114
115     sb.append("}");
116   }
117
118 }
119