d087867d7223ec0ea441b8ed80f0d301016b65ac
[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 BoolQueryBuilder must(QueryBuilder query) {
43                 
44                 if(this.inner.has("must") || this.inner.has("match") || this.inner.has("regexp") || this.inner.has("range")) {
45                         Object x = this.inner.has("must") ?this.inner.get("must"):this.inner;
46                         if(x instanceof JSONArray) {
47                                 ((JSONArray)x).put(query.getInner());
48                         }
49                         else {
50                                 this.inner = new JSONObject();
51                                 this.inner.put("must", new JSONObject());
52                                 JSONArray a=new JSONArray();
53                                 a.put(x);
54                                 a.put(query.getInner());
55                                 this.inner.put("must", a);
56                         }
57                 }
58                 
59                 else {
60                         this.inner = query.getInner();
61                 }
62                 this.setQuery("bool", this.inner);
63                 return this;
64         }
65
66 }