Replace all tab characters in java files with two spaces to remove linter warning
[aai/schema-service.git] / aai-queries / src / main / java / org / onap / aai / queries / GetCustomQueryConfig.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.onap.aai.queries;
21
22 import com.google.gson.*;
23 import com.google.gson.reflect.TypeToken;
24
25 import java.lang.reflect.Type;
26 import java.util.ArrayList;
27 import java.util.List;
28
29 public class GetCustomQueryConfig {
30
31   private JsonArray storedQueries = null;
32   private CustomQueryConfig customQueryConfig;
33
34
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";
40
41 //  public static final String AAI_HOME_ETC_QUERY_JSON = AAIConstants.AAI_HOME_ETC + "query" + AAIConstants.AAI_FILESEP + "stored-queries.json";
42
43   public GetCustomQueryConfig(String customQueryJson ) {
44     init(customQueryJson);
45   }
46
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)) {
51
52       storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
53     }
54   }
55
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);
60   }
61
62   private List<String> getPropertyList(JsonObject configObject, String config ) {
63     JsonElement subqueryConfig;
64     JsonArray props;
65
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);
72         }
73       }
74     }
75     return toStringList(null);
76   }
77
78   private String getPropertyString(JsonObject configObject, String config) {
79     JsonElement subqueryConfig;
80
81     if ( configObject.has(config)) {
82       subqueryConfig = configObject.get(config);
83       if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
84         return subqueryConfig.getAsString();
85       }
86     }
87     return null;
88   }
89
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>()  );
94       return;
95     }
96
97     JsonElement queryConfig;
98     JsonObject subObject;
99     String multipleStartNodes;
100     List<String> propertyList;
101
102     queryConfig = configObject.get(config);
103     subObject = queryConfig.getAsJsonObject();
104     propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
105     if ( propertyList == null ) {
106       propertyList = new ArrayList<String>();
107     }
108     customQueryConfig.setQueryRequiredProperties( propertyList );
109     propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
110     if ( propertyList == null ) {
111       propertyList = new ArrayList<String>();
112     }
113     customQueryConfig.setQueryOptionalProperties( propertyList );
114
115   }
116
117
118   public CustomQueryConfig getStoredQuery(String queryName ) {
119
120     customQueryConfig = null;
121     JsonObject configObject;
122     JsonElement query;
123     JsonElement queryConfig;
124     String queryString;
125
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());
137           }
138           break;
139         }
140       }
141     }
142
143     return customQueryConfig;
144
145   }
146
147
148
149 }