Add missing distributionManagement section to poms
[aai/search-data-service.git] / search-data-service-app / 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<>();
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 (matchQueriesCount > 0) {
127                     sb.append(", ");
128                 }
129
130                 if (query.isNotMatch()) {
131                     notMatchQueries.add(query);
132                 } else {
133                     sb.append(query.toElasticSearch());
134                     matchQueriesCount++;
135                 }
136             }
137             sb.append("],");
138
139             notMatchQueriesCount = 0;
140             sb.append("\"must_not\": [");
141             for (QueryStatement query : notMatchQueries) {
142                 if (notMatchQueriesCount > 0) {
143                     sb.append(", ");
144                 }
145                 sb.append(query.toElasticSearch());
146                 notMatchQueriesCount++;
147             }
148             sb.append("]");
149         }
150         sb.append("}");
151         sb.append("}");
152
153         return sb.toString();
154     }
155
156     @Override
157     public String toString() {
158
159         StringBuilder sb = new StringBuilder();
160
161         sb.append("{");
162
163         sb.append("all: [");
164         if (all != null) {
165             for (QueryStatement query : all) {
166                 sb.append(query.toString());
167             }
168         }
169         sb.append("], ");
170
171         sb.append("any: [");
172         if (any != null) {
173             for (QueryStatement query : any) {
174                 sb.append(query.toString());
175             }
176         }
177         sb.append("] ");
178
179         sb.append("}");
180
181         return sb.toString();
182     }
183 }