Release 1.14.3 docker artifact of aai-traversal
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / search / GetCustomQueryConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.rest.search;
21
22 import java.lang.reflect.Type;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import com.google.gson.Gson;
27 import com.google.gson.JsonArray;
28 import com.google.gson.JsonElement;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonParser;
31 import com.google.gson.reflect.TypeToken;
32
33 public class GetCustomQueryConfig {
34
35     private JsonArray storedQueries = null;
36     private CustomQueryConfig customQueryConfig;
37
38     private static final String QUERY_CONFIG = "query";
39     private static final String REQUIRED_CONFIG = "required-properties";
40     private static final String OPTIONAL_CONFIG = "optional-properties";
41     private static final String STORED_QUERIES_CONFIG = "stored-queries";
42     private static final String STORED_QUERY_CONFIG = "stored-query";
43
44     public GetCustomQueryConfig(String customQueryJson) {
45         init(customQueryJson);
46     }
47
48     private void init(String customQueryJson) {
49         JsonObject queriesObject = JsonParser.parseString(customQueryJson).getAsJsonObject();
50         if (queriesObject.has(STORED_QUERIES_CONFIG)) {
51
52             storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
53         }
54     }
55
56     private List<String> toStringList(JsonArray array) {
57         Gson converter = new Gson();
58         Type listType = new TypeToken<List<String>>() {}.getType();
59         return converter.fromJson(array, listType);
60     }
61
62     private List<String> getPropertyList(JsonObject configObject, String config) {
63         JsonElement subqueryConfig;
64         JsonArray props;
65
66         if (configObject.has(config)) {
67             subqueryConfig = configObject.get(config);
68             if (subqueryConfig != null && !subqueryConfig.isJsonNull()) {
69                 props = subqueryConfig.getAsJsonArray();
70                 if (props != null) {
71                     return toStringList(props);
72                 }
73             }
74         }
75         return toStringList(null);
76     }
77
78     private void getStoredQueryBlock(JsonObject configObject, String config) {
79         if (!configObject.has(config)) {
80             customQueryConfig.setQueryRequiredProperties(new ArrayList<>());
81             customQueryConfig.setQueryOptionalProperties(new ArrayList<>());
82             return;
83         }
84
85         JsonElement queryConfig;
86         JsonObject subObject;
87         List<String> propertyList;
88
89         queryConfig = configObject.get(config);
90         subObject = queryConfig.getAsJsonObject();
91         propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
92         if (propertyList == null) {
93             propertyList = new ArrayList<>();
94         }
95         customQueryConfig.setQueryRequiredProperties(propertyList);
96         propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
97         if (propertyList == null) {
98             propertyList = new ArrayList<>();
99         }
100         customQueryConfig.setQueryOptionalProperties(propertyList);
101
102     }
103
104     public CustomQueryConfig getStoredQuery(String queryName) {
105
106         customQueryConfig = null;
107         JsonObject configObject;
108         JsonElement query;
109         JsonElement queryConfig;
110
111         for (JsonElement storedQuery : storedQueries) {
112             if (storedQuery.isJsonObject()) {
113                 JsonObject queryObject = storedQuery.getAsJsonObject();
114                 query = queryObject.get(queryName);
115                 if (query != null) {
116                     customQueryConfig = new CustomQueryConfig();
117                     configObject = query.getAsJsonObject();
118                     getStoredQueryBlock(configObject, QUERY_CONFIG);
119                     if (configObject.has(STORED_QUERY_CONFIG)) {
120                         queryConfig = configObject.get(STORED_QUERY_CONFIG);
121                         customQueryConfig.setQuery(queryConfig.getAsString());
122                     }
123                     break;
124                 }
125             }
126         }
127
128         return customQueryConfig;
129
130     }
131
132 }