Format Java code to ONAP standard
[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>
30  * The expected JSON structure for a filter stanza is as follows:
31  * 
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 filter to pass.
50      */
51     private QueryStatement[] any;
52
53
54     public QueryStatement[] getAll() {
55         return all;
56     }
57
58     public void setAll(QueryStatement[] all) {
59         this.all = all;
60     }
61
62     public QueryStatement[] getAny() {
63         return any;
64     }
65
66     public void setAny(QueryStatement[] any) {
67         this.any = any;
68     }
69
70     /**
71      * This method returns a string which represents this filter in syntax that is understandable by ElasticSearch and
72      * is suitable for inclusion 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 }