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;
27 import com.google.gson.Gson;
28 import com.google.gson.JsonArray;
29 import com.google.gson.JsonElement;
30 import com.google.gson.JsonObject;
31 import com.google.gson.JsonParser;
32 import com.google.gson.reflect.TypeToken;
34 public class GetCustomQueryConfig {
36 private JsonArray storedQueries = null;
37 private CustomQueryConfig customQueryConfig;
40 private static final String QUERY_CONFIG = "query";
41 private static final String REQUIRED_CONFIG = "required-properties";
42 private static final String OPTIONAL_CONFIG = "optional-properties";
43 private static final String STORED_QUERIES_CONFIG = "stored-queries";
44 private static final String STORED_QUERY_CONFIG = "stored-query";
46 public GetCustomQueryConfig(String customQueryJson ) {
47 init(customQueryJson);
50 private void init( String customQueryJson) {
51 JsonParser parser = new JsonParser();
52 JsonObject queriesObject = parser.parse(customQueryJson).getAsJsonObject();
53 if (queriesObject.has(STORED_QUERIES_CONFIG)) {
55 storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
59 private List<String> toStringList(JsonArray array) {
60 Gson converter = new Gson();
61 Type listType = new TypeToken<List<String>>() {}.getType();
62 return converter.fromJson(array, listType);
65 private List<String> getPropertyList(JsonObject configObject, String config ) {
66 JsonElement subqueryConfig;
69 if ( configObject.has(config)) {
70 subqueryConfig = configObject.get(config);
71 if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
72 props = subqueryConfig.getAsJsonArray();
73 if ( props != null ) {
74 return toStringList(props);
78 return toStringList(null);
81 private void getStoredQueryBlock( JsonObject configObject, String config ) {
82 if ( !configObject.has(config)) {
83 customQueryConfig.setQueryRequiredProperties( new ArrayList<>() );
84 customQueryConfig.setQueryOptionalProperties( new ArrayList<>() );
88 JsonElement queryConfig;
90 List<String> propertyList;
92 queryConfig = configObject.get(config);
93 subObject = queryConfig.getAsJsonObject();
94 propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
95 if ( propertyList == null ) {
96 propertyList = new ArrayList<>();
98 customQueryConfig.setQueryRequiredProperties( propertyList );
99 propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
100 if ( propertyList == null ) {
101 propertyList = new ArrayList<>();
103 customQueryConfig.setQueryOptionalProperties( propertyList );
108 public CustomQueryConfig getStoredQuery(String queryName ) {
110 customQueryConfig = null;
111 JsonObject configObject;
113 JsonElement queryConfig;
115 for (JsonElement storedQuery : storedQueries) {
116 if (storedQuery.isJsonObject()) {
117 JsonObject queryObject = storedQuery.getAsJsonObject();
118 query = queryObject.get(queryName);
119 if ( query != null ) {
120 customQueryConfig = new CustomQueryConfig();
121 configObject = query.getAsJsonObject();
122 getStoredQueryBlock(configObject, QUERY_CONFIG);
123 if ( configObject.has(STORED_QUERY_CONFIG)) {
124 queryConfig = configObject.get(STORED_QUERY_CONFIG);
125 customQueryConfig.setQuery(queryConfig.getAsString());
132 return customQueryConfig;