Sonar fixes
[aai/traversal.git] / aai-traversal / src / main / java / org / onap / aai / rest / search / 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.rest.search;
21
22 import java.lang.reflect.Type;
23 import java.util.ArrayList;
24 import java.util.List;
25
26
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;
33
34 public class GetCustomQueryConfig {
35
36         private JsonArray storedQueries = null;
37         private CustomQueryConfig customQueryConfig;
38         
39         
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";
45         
46         public GetCustomQueryConfig(String customQueryJson ) {
47                 init(customQueryJson);
48         }
49         
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)) {
54                         
55                         storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
56                 }
57         }
58
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);
63         }
64         
65         private List<String> getPropertyList(JsonObject configObject, String config ) {
66                 JsonElement subqueryConfig;
67                 JsonArray props;
68                 
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);
75                                 }
76                         }
77                 }
78                 return toStringList(null);
79         }
80         
81         private void getStoredQueryBlock( JsonObject configObject, String config ) {
82                 if ( !configObject.has(config)) {
83                         customQueryConfig.setQueryRequiredProperties( new ArrayList<>() );
84                         customQueryConfig.setQueryOptionalProperties( new ArrayList<>()  );
85                         return;
86                 }
87                 
88                 JsonElement queryConfig;
89                 JsonObject subObject;
90                 List<String> propertyList;
91
92                 queryConfig = configObject.get(config);
93                 subObject = queryConfig.getAsJsonObject();
94                 propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
95                 if ( propertyList == null ) {
96                         propertyList = new ArrayList<>();
97                 }
98                 customQueryConfig.setQueryRequiredProperties( propertyList );
99                 propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
100                 if ( propertyList == null ) {
101                         propertyList = new ArrayList<>();
102                 }
103                 customQueryConfig.setQueryOptionalProperties( propertyList );
104                         
105         }
106         
107         
108         public CustomQueryConfig getStoredQuery(String queryName ) {
109         
110                 customQueryConfig = null;
111                 JsonObject configObject;
112                 JsonElement query;
113                 JsonElement queryConfig;
114
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());
126                                         }
127                                         break;
128                                 } 
129                         }
130                 }
131
132                 return customQueryConfig;
133                 
134         }
135
136
137
138 }