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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
17 ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.database.queries;
20 import org.json.JSONArray;
21 import org.json.JSONObject;
23 public class QueryBuilder {
25 private JSONObject innerQuery;
26 private final JSONObject outerQuery;
27 private final JSONObject queryObj;
29 public QueryBuilder() {
30 this.outerQuery = new JSONObject();
31 this.queryObj = new JSONObject();
32 this.outerQuery.put("query", this.queryObj);
36 public QueryBuilder from(long from) {
37 this.outerQuery.put("from", from);
41 public QueryBuilder size(long size) {
42 this.outerQuery.put("size", size);
46 public QueryBuilder sort(String prop, SortOrder order) {
48 if (this.outerQuery.has("sort")) {
49 a = this.outerQuery.getJSONArray("sort");
53 JSONObject sortObj = new JSONObject();
54 JSONObject orderObj = new JSONObject();
55 orderObj.put("order", order.getValue());
56 sortObj.put(prop, orderObj);
58 this.outerQuery.put("sort", a);
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);
69 JSONObject so = new JSONObject();
70 so.put("_key",sortOrder.getValue());
71 terms.put("order", so);
73 keyquery.put(key, terms);
74 this.outerQuery.put("aggs", keyquery);
78 protected QueryBuilder setQuery(String key, JSONObject query) {
79 this.innerQuery = query;
80 this.queryObj.put(key, this.innerQuery);
84 public JSONObject getInner() {
87 public boolean contains(String match) {
88 return this.toJSON().contains(match);
90 public String toJSON() {
91 return this.outerQuery.toString();
94 public QueryBuilder aggregations(String key) {
95 return this.aggregations(key, null);