aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openecomp/sa/searchdbabstraction/util/DocumentSchemaUtil.java
blob: 34d18377dfc7967efbd4adf8bfc1e8306ad43985 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 *
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */
package org.openecomp.sa.searchdbabstraction.util;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.openecomp.sa.rest.DocumentFieldSchema;
import org.openecomp.sa.rest.DocumentSchema;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class DocumentSchemaUtil {

  public static String generateDocumentMappings(String documentSchema)
      throws JsonParseException, JsonMappingException, IOException {

    // Unmarshal the json content into a document schema object.
    ObjectMapper mapper = new ObjectMapper();
    DocumentSchema schema = mapper.readValue(documentSchema, DocumentSchema.class);

    return generateDocumentMappings(schema);
  }

  public static String generateDocumentMappings(DocumentSchema schema) {

    // Now, generate the Elastic Search mapping json and return it.
    StringBuilder sb = new StringBuilder();
    sb.append("{");
    sb.append("\"properties\": {");

    generateFieldMappings(schema.getFields(), sb);

    sb.append("}");
    sb.append("}");

    return sb.toString();
  }


  private static void generateFieldMappings(List<DocumentFieldSchema> fields, StringBuilder sb) {

    AtomicBoolean firstField = new AtomicBoolean(true);

    for (DocumentFieldSchema field : fields) {

      // If this isn't the first field in the list, prepend it with a ','
      if (!firstField.compareAndSet(true, false)) {
        sb.append(", ");
      }

      // Now, append the translated field contents.
      generateFieldMapping(field, sb);
    }
  }

  private static void generateFieldMapping(DocumentFieldSchema fieldSchema, StringBuilder sb) {

    sb.append("\"").append(fieldSchema.getName()).append("\": {");

    // The field type is mandatory.
    sb.append("\"type\": \"").append(fieldSchema.getDataType()).append("\"");

    // For date type fields we may optionally supply a format specifier.
    if (fieldSchema.getDataType().equals("date")) {
      if (fieldSchema.getFormat() != null) {
        sb.append(", \"format\": \"").append(fieldSchema.getFormat()).append("\"");
      }
    }

    // If the index field was specified, then append it.
    if (fieldSchema.getSearchable() != null) {
      sb.append(", \"index\": \"").append(fieldSchema.getSearchable()
          ? "analyzed" : "not_analyzed").append("\"");
    }

    // If a search analyzer was specified, then append it.
    if (fieldSchema.getSearchAnalyzer() != null) {
      sb.append(", \"search_analyzer\": \"").append(fieldSchema.getSearchAnalyzer()).append("\"");
    }

    // If an indexing analyzer was specified, then append it.
    if (fieldSchema.getIndexAnalyzer() != null) {
      sb.append(", \"analyzer\": \"").append(fieldSchema.getIndexAnalyzer()).append("\"");
    }


    if (fieldSchema.getDataType().equals("nested")) {

      sb.append(", \"properties\": {");
      generateFieldMappings(fieldSchema.getSubFields(), sb);
      sb.append("}");
    }

    sb.append("}");
  }

}