Update the license for 2017-2018 license
[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 /*-
23  * ============LICENSE_START=======================================================
24  * org.onap.aai
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
31  * 
32  *      http://www.apache.org/licenses/LICENSE-2.0
33  * 
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=========================================================
40  */
41
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;
48
49 import org.onap.aai.util.AAIConstants;
50
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;
57
58 public class GetCustomQueryConfig {
59
60         private JsonArray storedQueries = null;
61         private CustomQueryConfig customQueryConfig;
62         
63         
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";
69         
70         public static final String AAI_HOME_ETC_QUERY_JSON = AAIConstants.AAI_HOME_ETC + "query" + AAIConstants.AAI_FILESEP + "stored-queries.json";
71         
72         public GetCustomQueryConfig(String customQueryJson ) {
73                 init(customQueryJson);
74         }
75         
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)) {
80                         
81                         storedQueries = queriesObject.getAsJsonArray(STORED_QUERIES_CONFIG);
82                 }
83         }
84
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);
89         }
90         
91         private List<String> getPropertyList(JsonObject configObject, String config ) {
92                 JsonElement subqueryConfig;
93                 JsonArray props;
94                 
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);
101                                 }
102                         }
103                 }
104                 return toStringList(null);
105         }
106         
107         private String getPropertyString(JsonObject configObject, String config) {
108                 JsonElement subqueryConfig;
109                 
110                 if ( configObject.has(config)) {
111                         subqueryConfig = configObject.get(config);
112                         if ( subqueryConfig != null && !subqueryConfig.isJsonNull() ) {
113                                 return subqueryConfig.getAsString();
114                         }
115                 }
116                 return null;
117         }
118         
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>()  );
123                         return;
124                 }
125                 
126                 JsonElement queryConfig;
127                 JsonObject subObject;
128                 String multipleStartNodes;
129                 List<String> propertyList;
130
131                 queryConfig = configObject.get(config);
132                 subObject = queryConfig.getAsJsonObject();
133                 propertyList = getPropertyList(subObject, REQUIRED_CONFIG);
134                 if ( propertyList == null ) {
135                         propertyList = new ArrayList<String>();
136                 }
137                 customQueryConfig.setQueryRequiredProperties( propertyList );
138                 propertyList = getPropertyList(subObject, OPTIONAL_CONFIG);
139                 if ( propertyList == null ) {
140                         propertyList = new ArrayList<String>();
141                 }
142                 customQueryConfig.setQueryOptionalProperties( propertyList );
143                         
144         }
145         
146         
147         public CustomQueryConfig getStoredQuery(String queryName ) {
148         
149                 customQueryConfig = null;
150                 JsonObject configObject;
151                 JsonElement query;
152                 JsonElement queryConfig;
153                 String queryString;
154
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());
166                                         }
167                                         break;
168                                 } 
169                         }
170                 }
171
172                 return customQueryConfig;
173                 
174         }
175
176
177
178 }