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 BoolQueryBuilder extends QueryBuilder {
29 private JSONObject inner;
31 public BoolQueryBuilder() {
33 this.inner = new JSONObject();
34 this.setQuery("bool", this.inner);
38 public String toString() {
39 return "BoolQueryBuilder [inner=" + inner + "]";
42 public static boolean isEmpty(JSONObject o) {
43 return o.keySet().isEmpty();
46 public BoolQueryBuilder must(QueryBuilder query) {
48 if (!isEmpty(this.inner)) {
49 Object x = this.inner.has("must") ? this.inner.get("must") : this.inner;
50 if (x instanceof JSONArray) {
51 ((JSONArray) x).put(query.getInner());
53 this.inner = new JSONObject();
54 this.inner.put("must", new JSONObject());
55 JSONArray a = new JSONArray();
57 a.put(query.getInner());
58 this.inner.put("must", a);
61 this.inner.put("must", query.getInner());
63 this.setQuery("bool", this.inner);
70 public BoolQueryBuilder should(QueryBuilder query) {
71 if (!isEmpty(this.inner)) {
72 Object x = this.inner.has("should") ? this.inner.get("should") : this.inner;
73 if (x instanceof JSONArray) {
74 ((JSONArray) x).put(query.getInner());
76 this.inner = new JSONObject();
77 // this.inner.put("should", new JSONObject());
78 JSONArray a = new JSONArray();
79 if (!x.toString().equals("{}")) {
82 a.put(query.getInner());
83 this.inner.put("should", a);
86 this.inner.put("should", query.getInner());
88 this.setQuery("bool", this.inner);