540ee10223670ed7452288a1c4f80c97e1425e33
[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 @Deprecated
28 public class QueryBuilder {
29
30     private JSONObject innerQuery;
31     private final JSONObject outerQuery;
32     private final JSONObject queryObj;
33
34     public QueryBuilder() {
35         this.outerQuery = new JSONObject();
36         this.queryObj = new JSONObject();
37         this.outerQuery.put("query", this.queryObj);
38
39     }
40
41     public QueryBuilder from(long from) {
42         this.outerQuery.put("from", from);
43         return this;
44     }
45
46     public QueryBuilder size(long size) {
47         this.outerQuery.put("size", size);
48         return this;
49     }
50
51     public QueryBuilder sort(String prop, SortOrder order) {
52         JSONArray a;
53         if (this.outerQuery.has("sort")) {
54             a = this.outerQuery.getJSONArray("sort");
55         } else {
56             a = new JSONArray();
57         }
58         JSONObject sortObj = new JSONObject();
59         JSONObject orderObj = new JSONObject();
60         orderObj.put("order", order.getValue());
61         sortObj.put(prop, orderObj);
62         a.put(sortObj);
63         this.outerQuery.put("sort", a);
64         return this;
65     }
66
67     public QueryBuilder aggregations(String key, SortOrder sortOrder) {
68         JSONObject keyquery = new JSONObject();
69         JSONObject terms = new JSONObject();
70         JSONObject field = new JSONObject();
71         field.put("field", key);
72         terms.put("terms", field);
73         if (sortOrder != null) {
74             JSONObject so = new JSONObject();
75             so.put("_key", sortOrder.getValue());
76             terms.put("order", so);
77         }
78         keyquery.put(key, terms);
79         this.outerQuery.put("aggs", keyquery);
80         return this;
81     }
82
83     protected QueryBuilder setQuery(String key, JSONObject query) {
84         this.innerQuery = query;
85         this.queryObj.put(key, this.innerQuery);
86         return this;
87     }
88
89     public JSONObject getInner() {
90         return this.queryObj;
91     }
92
93     public boolean contains(String match) {
94         return this.toJSON().contains(match);
95     }
96
97     public String toJSON() {
98         return this.outerQuery.toString();
99     }
100
101     public QueryBuilder aggregations(String key) {
102         return this.aggregations(key, null);
103     }
104
105     public void doFullsizeRequest() {
106         this.setFullsizeRequest(true);
107     }
108
109     public QueryBuilder setFullsizeRequest(boolean doFullsizeRequest) {
110         if (doFullsizeRequest) {
111             this.outerQuery.put("track_total_hits", doFullsizeRequest);
112         } else {
113             this.outerQuery.remove("track_total_hits");
114         }
115         return this;
116     }
117 }