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