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