5edc2613d384aa8ab631536404239abf6c480efb
[ccsdk/features.git] /
1 /*******************************************************************************
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  ******************************************************************************/
18 package org.onap.ccsdk.features.sdnr.wt.common.database.queries;
19
20 import org.json.JSONArray;
21 import org.json.JSONObject;
22
23 public class BoolQueryBuilder extends QueryBuilder {
24
25         private JSONObject inner;
26
27         public BoolQueryBuilder() {
28                 super();
29                 this.inner = new JSONObject();
30                 this.setQuery("bool", this.inner);
31         }
32
33         @Override
34         public String toString() {
35                 return "BoolQueryBuilder [inner=" + inner + "]";
36         }
37
38         public BoolQueryBuilder must(QueryBuilder matchQuery) {
39                 
40                 if(this.inner.has("must") || this.inner.has("match") || this.inner.has("regexp")) {
41                         Object x = this.inner.has("must") ?this.inner.get("must"):this.inner;
42                         if(x instanceof JSONArray) {
43                                 ((JSONArray)x).put(matchQuery.getInner());
44                         }
45                         else {
46                                 this.inner = new JSONObject();
47                                 this.inner.put("must", new JSONObject());
48                                 JSONArray a=new JSONArray();
49                                 a.put(x);
50                                 a.put(matchQuery.getInner());
51                                 this.inner.put("must", a);
52                         }
53                 }
54                 
55                 else {
56                         this.inner = matchQuery.getInner();
57                 }
58                 this.setQuery("bool", this.inner);
59                 return this;
60         }
61
62 }