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 BoolQueryBuilder extends QueryBuilder {
30 private JSONObject inner;
32 public BoolQueryBuilder() {
34 this.inner = new JSONObject();
35 this.setQuery("bool", this.inner);
39 public String toString() {
40 return "BoolQueryBuilder [inner=" + inner + "]";
43 public static boolean isEmpty(JSONObject o) {
44 return o.keySet().isEmpty();
47 public BoolQueryBuilder must(QueryBuilder query) {
49 if (!isEmpty(this.inner)) {
50 Object x = this.inner.has("must") ? this.inner.get("must") : this.inner;
51 if (x instanceof JSONArray) {
52 ((JSONArray) x).put(query.getInner());
54 this.inner = new JSONObject();
55 this.inner.put("must", new JSONObject());
56 JSONArray a = new JSONArray();
58 a.put(query.getInner());
59 this.inner.put("must", a);
62 this.inner.put("must", query.getInner());
64 this.setQuery("bool", this.inner);
71 public BoolQueryBuilder should(QueryBuilder query) {
72 if (!isEmpty(this.inner)) {
73 Object x = this.inner.has("should") ? this.inner.get("should") : this.inner;
74 if (x instanceof JSONArray) {
75 ((JSONArray) x).put(query.getInner());
77 this.inner = new JSONObject();
78 // this.inner.put("should", new JSONObject());
79 JSONArray a = new JSONArray();
80 if (!x.toString().equals("{}")) {
83 a.put(query.getInner());
84 this.inner.put("should", a);
87 this.inner.put("should", query.getInner());
89 this.setQuery("bool", this.inner);