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