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;
27 public class QueryBuilder {
29 private JSONObject innerQuery;
30 private final JSONObject outerQuery;
31 private final JSONObject queryObj;
33 public QueryBuilder() {
34 this.outerQuery = new JSONObject();
35 this.queryObj = new JSONObject();
36 this.outerQuery.put("query", this.queryObj);
40 public QueryBuilder from(long from) {
41 this.outerQuery.put("from", from);
45 public QueryBuilder size(long size) {
46 this.outerQuery.put("size", size);
50 public QueryBuilder sort(String prop, SortOrder order) {
52 if (this.outerQuery.has("sort")) {
53 a = this.outerQuery.getJSONArray("sort");
57 JSONObject sortObj = new JSONObject();
58 JSONObject orderObj = new JSONObject();
59 orderObj.put("order", order.getValue());
60 sortObj.put(prop, orderObj);
62 this.outerQuery.put("sort", a);
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);
77 keyquery.put(key, terms);
78 this.outerQuery.put("aggs", keyquery);
82 protected QueryBuilder setQuery(String key, JSONObject query) {
83 this.innerQuery = query;
84 this.queryObj.put(key, this.innerQuery);
88 public JSONObject getInner() {
92 public boolean contains(String match) {
93 return this.toJSON().contains(match);
96 public String toJSON() {
97 return this.outerQuery.toString();
100 public QueryBuilder aggregations(String key) {
101 return this.aggregations(key, null);
104 public void doFullsizeRequest() {
105 this.setFullsizeRequest(true);
108 public QueryBuilder setFullsizeRequest(boolean doFullsizeRequest) {
109 if (doFullsizeRequest) {
110 this.outerQuery.put("track_total_hits", doFullsizeRequest);
112 this.outerQuery.remove("track_total_hits");