Initial search service commit
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / searchdbabstraction / util / DocumentSchemaUtil.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License ati
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.searchdbabstraction.util;
26
27 import com.fasterxml.jackson.core.JsonParseException;
28 import com.fasterxml.jackson.databind.JsonMappingException;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import org.openecomp.sa.rest.DocumentFieldSchema;
31 import org.openecomp.sa.rest.DocumentSchema;
32
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.concurrent.atomic.AtomicBoolean;
36
37 public class DocumentSchemaUtil {
38
39   public static String generateDocumentMappings(String documentSchema)
40       throws JsonParseException, JsonMappingException, IOException {
41
42     // Unmarshal the json content into a document schema object.
43     ObjectMapper mapper = new ObjectMapper();
44     DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);
45
46     return generateDocumentMappings(schema);
47   }
48
49   public static String generateDocumentMappings(DocumentSchema schema) {
50
51     // Now, generate the Elastic Search mapping json and return it.
52     StringBuilder sb = new StringBuilder();
53     sb.append("{");
54     sb.append("\"properties\": {");
55
56     generateFieldMappings(schema.getFields(), sb);
57
58     sb.append("}");
59     sb.append("}");
60
61     return sb.toString();
62   }
63
64
65   private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {
66
67     AtomicBoolean firstField = new AtomicBoolean(true);
68
69     for (DocumentFieldSchema field : fields) {
70
71       // If this isn't the first field in the list, prepend it with a ','
72       if (!firstField.compareAndSet(true, false)) {
73         sb.append(", ");
74       }
75
76       // Now, append the translated field contents.
77       generateFieldMapping(field, sb);
78     }
79   }
80
81   private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {
82
83     sb.append("\"").append(fieldSchema.getName()).append("\": {");
84
85     // The field type is mandatory.
86     sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");
87
88     // For date type fields we may optionally supply a format specifier.
89     if (fieldSchema.getDataType().equals("date")) {
90       if (fieldSchema.getFormat() != null) {
91         sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
92       }
93     }
94
95     // If the index field was specified, then append it.
96     if (fieldSchema.getSearchable() != null) {
97       sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
98           ? "analyzed" : "not_analyzed").append("\"");
99     }
100
101     // If a search analyzer was specified, then append it.
102     if (fieldSchema.getSearchAnalyzer() != null) {
103       sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
104     }
105
106     // If an indexing analyzer was specified, then append it.
107     if (fieldSchema.getIndexAnalyzer() != null) {
108       sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
109     }
110
111
112     if (fieldSchema.getDataType().equals("nested")) {
113
114       sb.append(", \"properties\": {");
115       generateFieldMappings(fieldSchema.getSubFields(), sb);
116       sb.append("}");
117     }
118
119     sb.append("}");
120   }
121
122 }
123