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