1 package org.onap.aai.rest.search;
4 * ============LICENSE_START=======================================================
6 * ================================================================================
7 * Copyright (C) 2017 AT&T Intellectual Property. 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 at
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 import java.io.IOException;
24 import java.lang.reflect.Type;
25 import java.nio.file.Files;
26 import java.nio.file.Paths;
27 import java.util.ArrayList;
28 import java.util.List;
30 import org.onap.aai.util.AAIConstants;
32 import com.google.gson.Gson;
33 import com.google.gson.JsonArray;
34 import com.google.gson.JsonElement;
35 import com.google.gson.JsonObject;
36 import com.google.gson.JsonParser;
37 import com.google.gson.reflect.TypeToken;
39 public class GetCustomQueryConfig {
41 private JsonArray storedQueries = null;
42 private CustomQueryConfig customQueryConfig;
45 private final static String QUERY_CONFIG = "query";
46 private final static String REQUIRED_CONFIG = "required-properties";
47 private final static String OPTIONAL_CONFIG = "optional-properties";
48 private final static String STORED_QUERIES_CONFIG = "stored-queries";
49 private final static String STORED_QUERY_CONFIG = "stored-query";
51 public static final String AAI_HOME_ETC_QUERY_JSON = AAIConstants.AAI_HOME_ETC + "query" + AAIConstants.AAI_FILESEP + "stored-queries.json";
53 public GetCustomQueryConfig(String customQueryJson ) {
54 init(customQueryJson);
57 private void init( String customQueryJson) {
58 JsonParser parser = new JsonParser();
59 JsonObject queriesObject = parser.parse(customQueryJson).getAsJsonObject();
60 if (queriesObject.has(STORED_QUERIES_CONFIG)) {
62 storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
66 private List<String> toStringList(JsonArray array) {
67 Gson converter = new Gson();
68 Type listType = new TypeToken<List<String>>() {}.getType();
69 return converter.fromJson(array, listType);
72 private List<String> getPropertyList(JsonObject configObject, String config ) {
73 JsonElement subqueryConfig;
76 if ( configObject.has(config)) {
77 subqueryConfig = configObject.get(config);
78 if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
79 props = subqueryConfig.getAsJsonArray();
80 if ( props != null ) {
81 return toStringList(props);
85 return toStringList(null);
88 private String getPropertyString(JsonObject configObject, String config) {
89 JsonElement subqueryConfig;
91 if ( configObject.has(config)) {
92 subqueryConfig = configObject.get(config);
93 if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
94 return subqueryConfig.getAsString();
100 private void getStoredQueryBlock( JsonObject configObject, String config ) {
101 if ( !configObject.has(config)) {
102 customQueryConfig.setQueryRequiredProperties( new ArrayList<String>() );
103 customQueryConfig.setQueryOptionalProperties( new ArrayList<String>() );
107 JsonElement queryConfig;
108 JsonObject subObject;
109 String multipleStartNodes;
110 List<String> propertyList;
112 queryConfig = configObject.get(config);
113 subObject = queryConfig.getAsJsonObject();
114 propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
115 if ( propertyList == null ) {
116 propertyList = new ArrayList<String>();
118 customQueryConfig.setQueryRequiredProperties( propertyList );
119 propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
120 if ( propertyList == null ) {
121 propertyList = new ArrayList<String>();
123 customQueryConfig.setQueryOptionalProperties( propertyList );
128 public CustomQueryConfig getStoredQuery(String queryName ) {
130 customQueryConfig = null;
131 JsonObject configObject;
133 JsonElement queryConfig;
136 for (JsonElement storedQuery : storedQueries) {
137 if (storedQuery.isJsonObject()) {
138 JsonObject queryObject = storedQuery.getAsJsonObject();
139 query = queryObject.get(queryName);
140 if ( query != null ) {
141 customQueryConfig = new CustomQueryConfig();
142 configObject = query.getAsJsonObject();
143 getStoredQueryBlock(configObject, QUERY_CONFIG);
144 if ( configObject.has(STORED_QUERY_CONFIG)) {
145 queryConfig = configObject.get(STORED_QUERY_CONFIG);
146 customQueryConfig.setQuery(queryConfig.getAsString());
153 return customQueryConfig;