b2a7882d7e81d6c593b0a6966375ca63780a3d21
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.common.database.queries;
23
24 import org.json.JSONArray;
25 import org.json.JSONObject;
26
27 @Deprecated
28 public class BoolQueryBuilder extends QueryBuilder {
29
30     private JSONObject inner;
31
32     public BoolQueryBuilder() {
33         super();
34         this.inner = new JSONObject();
35         this.setQuery("bool", this.inner);
36     }
37
38     @Override
39     public String toString() {
40         return "BoolQueryBuilder [inner=" + inner + "]";
41     }
42
43     public static boolean isEmpty(JSONObject o) {
44         return o.keySet().isEmpty();
45     }
46
47     public BoolQueryBuilder must(QueryBuilder query) {
48
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());
53             } else {
54                 this.inner = new JSONObject();
55                 this.inner.put("must", new JSONObject());
56                 JSONArray a = new JSONArray();
57                 a.put(x);
58                 a.put(query.getInner());
59                 this.inner.put("must", a);
60             }
61         } else {
62             this.inner.put("must", query.getInner());
63         }
64         this.setQuery("bool", this.inner);
65         return this;
66     }
67
68     /**
69      * @param query
70      */
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());
76             } else {
77                 this.inner = new JSONObject();
78                 //                              this.inner.put("should", new JSONObject());
79                 JSONArray a = new JSONArray();
80                 if (!x.toString().equals("{}")) {
81                     a.put(x);
82                 }
83                 a.put(query.getInner());
84                 this.inner.put("should", a);
85             }
86         } else {
87             this.inner.put("should", query.getInner());
88         }
89         this.setQuery("bool", this.inner);
90         return this;
91     }
92
93 }