Update license and poms
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / search / filters / entity / BoolQueryBuilder.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
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 package org.onap.aai.sparky.search.filters.entity;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import javax.json.Json;
27 import javax.json.JsonArray;
28 import javax.json.JsonArrayBuilder;
29 import javax.json.JsonBuilderFactory;
30 import javax.json.JsonObject;
31 import javax.json.JsonObjectBuilder;
32
33 public class BoolQueryBuilder {
34
35   private List<MatchFilterCriteriaEntity> mustFilters;
36   private List<MatchFilterCriteriaEntity> shouldFilters;
37   
38   private int minShouldMatch;
39
40   public BoolQueryBuilder() {
41
42     mustFilters = new ArrayList<MatchFilterCriteriaEntity>();
43     shouldFilters = new ArrayList<MatchFilterCriteriaEntity>();
44     minShouldMatch = -1;
45
46   }
47
48   public void addMustFilter(MatchFilterCriteriaEntity filter) {
49
50     if (!mustFilters.contains(filter)) {
51       mustFilters.add(filter);
52     }
53
54   }
55
56   public void addShouldFilter(MatchFilterCriteriaEntity filter) {
57
58     if (!shouldFilters.contains(filter)) {
59       shouldFilters.add(filter);
60     }
61
62   }
63   
64   public void setMinShouldMatch(int minShouldMatch) {
65     this.minShouldMatch = minShouldMatch;
66   }
67   
68   public boolean isMatchAll() {
69     return (mustFilters.isEmpty() && shouldFilters.isEmpty());
70   }
71   
72   public JsonObject getJsonObject() {
73     /*
74      * Specify a null config for now, but if we want normalize all the builders, we can do it at one
75      * location, when we are ready.
76      */
77     JsonBuilderFactory factory = Json.createBuilderFactory(null);
78
79     JsonObjectBuilder boolBuilder = factory.createObjectBuilder();
80
81     if(!mustFilters.isEmpty()){
82       JsonArrayBuilder mustArrayBuilder = factory.createArrayBuilder();
83       
84       for (MatchFilterCriteriaEntity matchCriteria : mustFilters) {
85         mustArrayBuilder.add(matchCriteria.getJsonObject());
86       }
87       
88       JsonArray mustArray = mustArrayBuilder.build();
89       boolBuilder.add("must", mustArray);
90     }
91
92     if (!shouldFilters.isEmpty()) {
93       JsonArray shouldArray = null;
94       JsonArrayBuilder shouldArrayBuilder = factory.createArrayBuilder();
95       
96       for (MatchFilterCriteriaEntity matchCriteria : shouldFilters) {
97         shouldArrayBuilder.add(matchCriteria.getJsonObject());
98       }
99
100       shouldArray = shouldArrayBuilder.build();
101       boolBuilder.add("should", shouldArray).add("min_should_match", minShouldMatch);
102     }
103     
104     JsonObjectBuilder queryObjectBuilder = factory.createObjectBuilder();
105     
106     /* 
107      * If both filter lists are empty then we are doing an aggregation
108      * based off fields. Just match-all for the query.
109     */
110     if(isMatchAll()) {
111       JsonObject matchAllObject = factory.createObjectBuilder().build();
112       queryObjectBuilder.add("match_all", matchAllObject);
113     } else {
114       queryObjectBuilder.add("bool", boolBuilder.build());
115     }
116
117     return queryObjectBuilder.build();
118   }
119 }