b362c4684623b77eaf57c94239a410597a00c5a7
[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 public class BoolQueryBuilder extends QueryBuilder {
28
29         private JSONObject inner;
30
31         public BoolQueryBuilder() {
32                 super();
33                 this.inner = new JSONObject();
34                 this.setQuery("bool", this.inner);
35         }
36
37         @Override
38         public String toString() {
39                 return "BoolQueryBuilder [inner=" + inner + "]";
40         }
41
42         public static boolean isEmpty(JSONObject o) {
43                 return o.keySet().size() <= 0;
44         }
45
46         public BoolQueryBuilder must(QueryBuilder query) {
47
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());
52                         } else {
53                                 this.inner = new JSONObject();
54                                 this.inner.put("must", new JSONObject());
55                                 JSONArray a = new JSONArray();
56                                 a.put(x);
57                                 a.put(query.getInner());
58                                 this.inner.put("must", a);
59                         }
60                 } else {
61                         this.inner.put("must", query.getInner());
62                 }
63                 this.setQuery("bool", this.inner);
64                 return this;
65         }
66
67         /**
68          * @param query
69          */
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());
75                         } else {
76                                 this.inner = new JSONObject();
77                                 //                              this.inner.put("should", new JSONObject());
78                                 JSONArray a = new JSONArray();
79                                 if (!x.toString().equals("{}")) {
80                                         a.put(x);
81                                 }
82                                 a.put(query.getInner());
83                                 this.inner.put("should", a);
84                         }
85                 } else {
86                         this.inner.put("should", query.getInner());
87                 }
88                 this.setQuery("bool", this.inner);
89                 return this;
90         }
91
92 }