Adding interfaces in documentation
[aai/sparky-be.git] / sparkybe-onap-service / src / main / java / org / onap / aai / sparky / search / filters / entity / BoolQueryBuilder.java
1 /**
2  * ============LICENSE_START===================================================
3  * SPARKY (AAI UI service)
4  * ============================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=====================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.onap.aai.sparky.search.filters.entity;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import javax.json.Json;
31 import javax.json.JsonArray;
32 import javax.json.JsonArrayBuilder;
33 import javax.json.JsonBuilderFactory;
34 import javax.json.JsonObject;
35 import javax.json.JsonObjectBuilder;
36
37 public class BoolQueryBuilder {
38
39   private List<MatchFilterCriteriaEntity> mustFilters;
40   private List<MatchFilterCriteriaEntity> shouldFilters;
41   
42   private int minShouldMatch;
43
44   public BoolQueryBuilder() {
45
46     mustFilters = new ArrayList<MatchFilterCriteriaEntity>();
47     shouldFilters = new ArrayList<MatchFilterCriteriaEntity>();
48     minShouldMatch = -1;
49
50   }
51
52   public void addMustFilter(MatchFilterCriteriaEntity filter) {
53
54     if (!mustFilters.contains(filter)) {
55       mustFilters.add(filter);
56     }
57
58   }
59
60   public void addShouldFilter(MatchFilterCriteriaEntity filter) {
61
62     if (!shouldFilters.contains(filter)) {
63       shouldFilters.add(filter);
64     }
65
66   }
67   
68   public void setMinShouldMatch(int minShouldMatch) {
69     this.minShouldMatch = minShouldMatch;
70   }
71   
72   public boolean isMatchAll() {
73     return (mustFilters.isEmpty() && shouldFilters.isEmpty());
74   }
75   
76   public JsonObject getJsonObject() {
77     /*
78      * Specify a null config for now, but if we want normalize all the builders, we can do it at one
79      * location, when we are ready.
80      */
81     JsonBuilderFactory factory = Json.createBuilderFactory(null);
82
83     JsonObjectBuilder boolBuilder = factory.createObjectBuilder();
84
85     if(!mustFilters.isEmpty()){
86       JsonArrayBuilder mustArrayBuilder = factory.createArrayBuilder();
87       
88       for (MatchFilterCriteriaEntity matchCriteria : mustFilters) {
89         mustArrayBuilder.add(matchCriteria.getJsonObject());
90       }
91       
92       JsonArray mustArray = mustArrayBuilder.build();
93       boolBuilder.add("must", mustArray);
94     }
95
96     if (!shouldFilters.isEmpty()) {
97       JsonArray shouldArray = null;
98       JsonArrayBuilder shouldArrayBuilder = factory.createArrayBuilder();
99       
100       for (MatchFilterCriteriaEntity matchCriteria : shouldFilters) {
101         shouldArrayBuilder.add(matchCriteria.getJsonObject());
102       }
103
104       shouldArray = shouldArrayBuilder.build();
105       boolBuilder.add("should", shouldArray).add("min_should_match", minShouldMatch);
106     }
107     
108     JsonObjectBuilder queryObjectBuilder = factory.createObjectBuilder();
109     
110     /* 
111      * If both filter lists are empty then we are doing an aggregation
112      * based off fields. Just match-all for the query.
113     */
114     if(isMatchAll()) {
115       JsonObject matchAllObject = factory.createObjectBuilder().build();
116       queryObjectBuilder.add("match_all", matchAllObject);
117     } else {
118       queryObjectBuilder.add("bool", boolBuilder.build());
119     }
120
121     return queryObjectBuilder.build();
122   }
123 }