ad6c199eab9aabdb98a740c18eb74e224aaa4446
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.database.queries;
23
24 import org.json.JSONArray;
25 import org.json.JSONObject;
26
27 public class QueryBuilder {
28
29         private JSONObject innerQuery;
30         private final JSONObject outerQuery;
31         private final JSONObject queryObj;
32
33         public QueryBuilder() {
34                 this.outerQuery = new JSONObject();
35                 this.queryObj = new JSONObject();
36                 this.outerQuery.put("query", this.queryObj);
37
38         }
39
40         public QueryBuilder from(long from) {
41                 this.outerQuery.put("from", from);
42                 return this;
43         }
44
45         public QueryBuilder size(long size) {
46                 this.outerQuery.put("size", size);
47                 return this;
48         }
49
50         public QueryBuilder sort(String prop, SortOrder order) {
51                 JSONArray a;
52                 if (this.outerQuery.has("sort")) {
53                         a = this.outerQuery.getJSONArray("sort");
54                 } else {
55                         a = new JSONArray();
56                 }
57                 JSONObject sortObj = new JSONObject();
58                 JSONObject orderObj = new JSONObject();
59                 orderObj.put("order", order.getValue());
60                 sortObj.put(prop, orderObj);
61                 a.put(sortObj);
62                 this.outerQuery.put("sort", a);
63                 return this;
64         }
65
66         public QueryBuilder aggregations(String key, SortOrder sortOrder) {
67                 JSONObject keyquery = new JSONObject();
68                 JSONObject terms = new JSONObject();
69                 JSONObject field = new JSONObject();
70                 field.put("field", key);
71                 terms.put("terms", field);
72                 if(sortOrder!=null) {
73                         JSONObject so = new JSONObject();
74                         so.put("_key",sortOrder.getValue());
75                         terms.put("order", so);
76                 }
77                 keyquery.put(key, terms);
78                 this.outerQuery.put("aggs", keyquery);
79                 return this;
80         }
81
82         protected QueryBuilder setQuery(String key, JSONObject query) {
83                 this.innerQuery = query;
84                 this.queryObj.put(key, this.innerQuery);
85                 return this;
86         }
87
88         public JSONObject getInner() {
89                 return this.queryObj;
90         }
91         public boolean contains(String match) {
92                 return this.toJSON().contains(match);
93         }
94         public String toJSON() {
95                 return this.outerQuery.toString();
96         }
97
98         public QueryBuilder aggregations(String key) {
99                 return this.aggregations(key, null);
100         }
101 }