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.queries;
22 import com.google.gson.*;
23 import com.google.gson.reflect.TypeToken;
25 import java.lang.reflect.Type;
26 import java.util.ArrayList;
27 import java.util.List;
29 public class GetCustomQueryConfig {
31 private JsonArray storedQueries = null;
32 private CustomQueryConfig customQueryConfig;
35 private final static String QUERY_CONFIG = "query";
36 private final static String REQUIRED_CONFIG = "required-properties";
37 private final static String OPTIONAL_CONFIG = "optional-properties";
38 private final static String STORED_QUERIES_CONFIG = "stored-queries";
39 private final static String STORED_QUERY_CONFIG = "stored-query";
41 // public static final String AAI_HOME_ETC_QUERY_JSON = AAIConstants.AAI_HOME_ETC + "query" + AAIConstants.AAI_FILESEP + "stored-queries.json";
43 public GetCustomQueryConfig(String customQueryJson ) {
44 init(customQueryJson);
47 private void init( String customQueryJson) {
48 JsonParser parser = new JsonParser();
49 JsonObject queriesObject = parser.parse(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();
70 if ( props != null ) {
71 return toStringList(props);
75 return toStringList(null);
78 private String getPropertyString(JsonObject configObject, String config) {
79 JsonElement subqueryConfig;
81 if ( configObject.has(config)) {
82 subqueryConfig = configObject.get(config);
83 if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
84 return subqueryConfig.getAsString();
90 private void getStoredQueryBlock( JsonObject configObject, String config ) {
91 if ( !configObject.has(config)) {
92 customQueryConfig.setQueryRequiredProperties( new ArrayList<String>() );
93 customQueryConfig.setQueryOptionalProperties( new ArrayList<String>() );
97 JsonElement queryConfig;
99 String multipleStartNodes;
100 List<String> propertyList;
102 queryConfig = configObject.get(config);
103 subObject = queryConfig.getAsJsonObject();
104 propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
105 if ( propertyList == null ) {
106 propertyList = new ArrayList<String>();
108 customQueryConfig.setQueryRequiredProperties( propertyList );
109 propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
110 if ( propertyList == null ) {
111 propertyList = new ArrayList<String>();
113 customQueryConfig.setQueryOptionalProperties( propertyList );
118 public CustomQueryConfig getStoredQuery(String queryName ) {
120 customQueryConfig = null;
121 JsonObject configObject;
123 JsonElement queryConfig;
126 for (JsonElement storedQuery : storedQueries) {
127 if (storedQuery.isJsonObject()) {
128 JsonObject queryObject = storedQuery.getAsJsonObject();
129 query = queryObject.get(queryName);
130 if ( query != null ) {
131 customQueryConfig = new CustomQueryConfig();
132 configObject = query.getAsJsonObject();
133 getStoredQueryBlock(configObject, QUERY_CONFIG);
134 if ( configObject.has(STORED_QUERY_CONFIG)) {
135 queryConfig = configObject.get(STORED_QUERY_CONFIG);
136 customQueryConfig.setQuery(queryConfig.getAsString());
143 return customQueryConfig;