Cleanup project's name in Sonar
[aai/search-data-service.git] / src / main / java / org / onap / aai / sa / searchdbabstraction / searchapi / Filter.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.sa.searchdbabstraction.searchapi;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 /**
29  * This class represents the filter stanza in a search statement.
30  *
31  * <p>The expected JSON structure for a filter stanza is as follows:
32  * <pre>
33  * {
34  *     "filter": {
35  *        "all": [ {query structure}, {query structure}, ... {query structure} ],
36  *        "any": [ {query structure}, {query structure}, ... {query structure} ]
37  *     }
38  * }
39  * </pre>
40  */
41 public class Filter {
42
43   /**
44    * All queries in this list must evaluate to true for the filter to pass.
45    */
46   private QueryStatement[] all;
47
48   /**
49    * Any one of the queries in this list must evaluate to true for the
50    * filter to pass.
51    */
52   private QueryStatement[] any;
53
54
55   public QueryStatement[] getAll() {
56     return all;
57   }
58
59   public void setAll(QueryStatement[] all) {
60     this.all = all;
61   }
62
63   public QueryStatement[] getAny() {
64     return any;
65   }
66
67   public void setAny(QueryStatement[] any) {
68     this.any = any;
69   }
70
71   /**
72    * This method returns a string which represents this filter in syntax
73    * that is understandable by ElasticSearch and is suitable for inclusion
74    * in an ElasticSearch query string.
75    *
76    * @return - ElasticSearch syntax string.
77    */
78   public String toElasticSearch() {
79
80     StringBuilder sb = new StringBuilder();
81
82     List<QueryStatement> notMatchQueries = new ArrayList<QueryStatement>();
83     sb.append("{");
84     sb.append("\"bool\": {");
85
86     // Add the queries from our 'all' list.
87     int matchQueriesCount = 0;
88     int notMatchQueriesCount = 0;
89     if (all != null) {
90       sb.append("\"must\": [");
91
92       for (QueryStatement query : all) {
93         if (matchQueriesCount > 0) {
94           sb.append(", ");
95         }
96
97         if (query.isNotMatch()) {
98           notMatchQueries.add(query);
99         } else {
100           sb.append(query.toElasticSearch());
101           matchQueriesCount++;
102         }
103       }
104       sb.append("],");
105
106
107       sb.append("\"must_not\": [");
108       for (QueryStatement query : notMatchQueries) {
109         if (notMatchQueriesCount > 0) {
110           sb.append(", ");
111         }
112         sb.append(query.toElasticSearch());
113         notMatchQueriesCount++;
114       }
115       sb.append("]");
116     }
117
118     // Add the queries from our 'any' list.
119     notMatchQueries.clear();
120     if (any != null) {
121       if (all != null) {
122         sb.append(",");
123       }
124       sb.append("\"should\": [");
125
126       matchQueriesCount = 0;
127       for (QueryStatement query : any) {
128         //if(!firstQuery.compareAndSet(true, false)) {
129         if (matchQueriesCount > 0) {
130           sb.append(", ");
131         }
132
133         if (query.isNotMatch()) {
134           notMatchQueries.add(query);
135         } else {
136           sb.append(query.toElasticSearch());
137           matchQueriesCount++;
138         }
139       }
140       sb.append("],");
141
142       //firstQuery.set(true);
143       notMatchQueriesCount = 0;
144       sb.append("\"must_not\": [");
145       for (QueryStatement query : notMatchQueries) {
146         //if(!firstQuery.compareAndSet(true, false)) {
147         if (notMatchQueriesCount > 0) {
148           sb.append(", ");
149         }
150         sb.append(query.toElasticSearch());
151         notMatchQueriesCount++;
152       }
153       sb.append("]");
154     }
155     sb.append("}");
156     sb.append("}");
157
158     return sb.toString();
159   }
160
161   @Override
162   public String toString() {
163
164     StringBuilder sb = new StringBuilder();
165
166     sb.append("{");
167
168     sb.append("all: [");
169     if (all != null) {
170       for (QueryStatement query : all) {
171         sb.append(query.toString());
172       }
173     }
174     sb.append("], ");
175
176     sb.append("any: [");
177     if (any != null) {
178       for (QueryStatement query : any) {
179         sb.append(query.toString());
180       }
181     }
182     sb.append("] ");
183
184     sb.append("}");
185
186     return sb.toString();
187   }
188 }