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;
23 * ============LICENSE_START=======================================================
25 * ================================================================================
26 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
27 * ================================================================================
28 * Licensed under the Apache License, Version 2.0 (the "License");
29 * you may not use this file except in compliance with the License.
30 * You may obtain a copy of the License at
32 * http://www.apache.org/licenses/LICENSE-2.0
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS,
36 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 * See the License for the specific language governing permissions and
38 * limitations under the License.
39 * ============LICENSE_END=========================================================
42 import java.io.IOException;
43 import java.lang.reflect.Type;
44 import java.nio.file.Files;
45 import java.nio.file.Paths;
46 import java.util.ArrayList;
47 import java.util.List;
49 import org.onap.aai.util.AAIConstants;
51 import com.google.gson.Gson;
52 import com.google.gson.JsonArray;
53 import com.google.gson.JsonElement;
54 import com.google.gson.JsonObject;
55 import com.google.gson.JsonParser;
56 import com.google.gson.reflect.TypeToken;
58 public class GetCustomQueryConfig {
60 private JsonArray storedQueries = null;
61 private CustomQueryConfig customQueryConfig;
64 private final static String QUERY_CONFIG = "query";
65 private final static String REQUIRED_CONFIG = "required-properties";
66 private final static String OPTIONAL_CONFIG = "optional-properties";
67 private final static String STORED_QUERIES_CONFIG = "stored-queries";
68 private final static String STORED_QUERY_CONFIG = "stored-query";
70 public static final String AAI_HOME_ETC_QUERY_JSON = AAIConstants.AAI_HOME_ETC + "query" + AAIConstants.AAI_FILESEP + "stored-queries.json";
72 public GetCustomQueryConfig(String customQueryJson ) {
73 init(customQueryJson);
76 private void init( String customQueryJson) {
77 JsonParser parser = new JsonParser();
78 JsonObject queriesObject = parser.parse(customQueryJson).getAsJsonObject();
79 if (queriesObject.has(STORED_QUERIES_CONFIG)) {
81 storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
85 private List<String> toStringList(JsonArray array) {
86 Gson converter = new Gson();
87 Type listType = new TypeToken<List<String>>() {}.getType();
88 return converter.fromJson(array, listType);
91 private List<String> getPropertyList(JsonObject configObject, String config ) {
92 JsonElement subqueryConfig;
95 if ( configObject.has(config)) {
96 subqueryConfig = configObject.get(config);
97 if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
98 props = subqueryConfig.getAsJsonArray();
99 if ( props != null ) {
100 return toStringList(props);
104 return toStringList(null);
107 private String getPropertyString(JsonObject configObject, String config) {
108 JsonElement subqueryConfig;
110 if ( configObject.has(config)) {
111 subqueryConfig = configObject.get(config);
112 if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
113 return subqueryConfig.getAsString();
119 private void getStoredQueryBlock( JsonObject configObject, String config ) {
120 if ( !configObject.has(config)) {
121 customQueryConfig.setQueryRequiredProperties( new ArrayList<String>() );
122 customQueryConfig.setQueryOptionalProperties( new ArrayList<String>() );
126 JsonElement queryConfig;
127 JsonObject subObject;
128 String multipleStartNodes;
129 List<String> propertyList;
131 queryConfig = configObject.get(config);
132 subObject = queryConfig.getAsJsonObject();
133 propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
134 if ( propertyList == null ) {
135 propertyList = new ArrayList<String>();
137 customQueryConfig.setQueryRequiredProperties( propertyList );
138 propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
139 if ( propertyList == null ) {
140 propertyList = new ArrayList<String>();
142 customQueryConfig.setQueryOptionalProperties( propertyList );
147 public CustomQueryConfig getStoredQuery(String queryName ) {
149 customQueryConfig = null;
150 JsonObject configObject;
152 JsonElement queryConfig;
155 for (JsonElement storedQuery : storedQueries) {
156 if (storedQuery.isJsonObject()) {
157 JsonObject queryObject = storedQuery.getAsJsonObject();
158 query = queryObject.get(queryName);
159 if ( query != null ) {
160 customQueryConfig = new CustomQueryConfig();
161 configObject = query.getAsJsonObject();
162 getStoredQueryBlock(configObject, QUERY_CONFIG);
163 if ( configObject.has(STORED_QUERY_CONFIG)) {
164 queryConfig = configObject.get(STORED_QUERY_CONFIG);
165 customQueryConfig.setQuery(queryConfig.getAsString());
172 return customQueryConfig;