2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.rest.search;
22 import java.lang.reflect.Type;
23 import java.util.ArrayList;
24 import java.util.List;
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;
33 public class GetCustomQueryConfig {
35 private JsonArray storedQueries = null;
36 private CustomQueryConfig customQueryConfig;
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";
44 public GetCustomQueryConfig(String customQueryJson) {
45 init(customQueryJson);
48 private void init(String customQueryJson) {
49 JsonObject queriesObject = JsonParser.parseString(customQueryJson).getAsJsonObject();
50 if (queriesObject.has(STORED_QUERIES_CONFIG)) {
52 storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
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);
62 private List<String> getPropertyList(JsonObject configObject, String config) {
63 JsonElement subqueryConfig;
66 if (configObject.has(config)) {
67 subqueryConfig = configObject.get(config);
68 if (subqueryConfig != null && !subqueryConfig.isJsonNull()) {
69 props = subqueryConfig.getAsJsonArray();
71 return toStringList(props);
75 return toStringList(null);
78 private void getStoredQueryBlock(JsonObject configObject, String config) {
79 if (!configObject.has(config)) {
80 customQueryConfig.setQueryRequiredProperties(new ArrayList<>());
81 customQueryConfig.setQueryOptionalProperties(new ArrayList<>());
85 JsonElement queryConfig;
87 List<String> propertyList;
89 queryConfig = configObject.get(config);
90 subObject = queryConfig.getAsJsonObject();
91 propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
92 if (propertyList == null) {
93 propertyList = new ArrayList<>();
95 customQueryConfig.setQueryRequiredProperties(propertyList);
96 propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
97 if (propertyList == null) {
98 propertyList = new ArrayList<>();
100 customQueryConfig.setQueryOptionalProperties(propertyList);
104 public CustomQueryConfig getStoredQuery(String queryName) {
106 customQueryConfig = null;
107 JsonObject configObject;
109 JsonElement queryConfig;
111 for (JsonElement storedQuery : storedQueries) {
112 if (storedQuery.isJsonObject()) {
113 JsonObject queryObject = storedQuery.getAsJsonObject();
114 query = queryObject.get(queryName);
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());
128 return customQueryConfig;