2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.common.database.queries;
24 import org.json.JSONArray;
25 import org.json.JSONObject;
28 public class QueryBuilder {
30 private JSONObject innerQuery;
31 private final JSONObject outerQuery;
32 private final JSONObject queryObj;
34 public QueryBuilder() {
35 this.outerQuery = new JSONObject();
36 this.queryObj = new JSONObject();
37 this.outerQuery.put("query", this.queryObj);
41 public QueryBuilder from(long from) {
42 this.outerQuery.put("from", from);
46 public QueryBuilder size(long size) {
47 this.outerQuery.put("size", size);
51 public QueryBuilder sort(String prop, SortOrder order) {
53 if (this.outerQuery.has("sort")) {
54 a = this.outerQuery.getJSONArray("sort");
58 JSONObject sortObj = new JSONObject();
59 JSONObject orderObj = new JSONObject();
60 orderObj.put("order", order.getValue());
61 sortObj.put(prop, orderObj);
63 this.outerQuery.put("sort", a);
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);
78 keyquery.put(key, terms);
79 this.outerQuery.put("aggs", keyquery);
83 protected QueryBuilder setQuery(String key, JSONObject query) {
84 this.innerQuery = query;
85 this.queryObj.put(key, this.innerQuery);
89 public JSONObject getInner() {
93 public boolean contains(String match) {
94 return this.toJSON().contains(match);
97 public String toJSON() {
98 return this.outerQuery.toString();
101 public QueryBuilder aggregations(String key) {
102 return this.aggregations(key, null);
105 public void doFullsizeRequest() {
106 this.setFullsizeRequest(true);
109 public QueryBuilder setFullsizeRequest(boolean doFullsizeRequest) {
110 if (doFullsizeRequest) {
111 this.outerQuery.put("track_total_hits", doFullsizeRequest);
113 this.outerQuery.remove("track_total_hits");