Add missing distributionManagement section to poms
[aai/search-data-service.git] / search-data-service-app / src / main / java / org / onap / aai / sa / searchdbabstraction / searchapi / QueryStatement.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 com.fasterxml.jackson.annotation.JsonProperty;
24
25 public class QueryStatement {
26
27     private TermQuery match;
28
29     @JsonProperty("not-match")
30     private TermQuery notMatch;
31
32     @JsonProperty("parsed-query")
33     private ParsedQuery parsedQuery;
34
35     private RangeQuery range;
36
37     public TermQuery getMatch() {
38         return match;
39     }
40
41     public void setMatch(TermQuery match) {
42         this.match = match;
43     }
44
45     public TermQuery getNotMatch() {
46         return notMatch;
47     }
48
49     public void setNotMatch(TermQuery notMatch) {
50         this.notMatch = notMatch;
51     }
52
53     public ParsedQuery getParsedQuery() {
54         return parsedQuery;
55     }
56
57     public void setParsedQuery(ParsedQuery parsedQuery) {
58         this.parsedQuery = parsedQuery;
59     }
60
61     public RangeQuery getRange() {
62         return range;
63     }
64
65     public void setRange(RangeQuery range) {
66         this.range = range;
67     }
68
69     public boolean isNotMatch() {
70         return (notMatch != null);
71     }
72
73     public String toElasticSearch() {
74
75         if (match != null) {
76             return match.toElasticSearch();
77
78         } else if (notMatch != null) {
79             return notMatch.toElasticSearch();
80
81         } else if (parsedQuery != null) {
82
83             // We need some special wrapping if this query is against a nested field.
84             if (fieldIsNested(parsedQuery.getField())) {
85                 return "{\"nested\": { \"path\": \"" + pathForNestedField(parsedQuery.getField()) + "\", \"query\": "
86                         + parsedQuery.toElasticSearch() + "}}";
87             } else {
88                 return parsedQuery.toElasticSearch();
89             }
90
91         } else if (range != null) {
92
93             // We need some special wrapping if this query is against a nested field.
94             if (fieldIsNested(range.getField())) {
95                 return "{\"nested\": { \"path\": \"" + pathForNestedField(range.getField()) + "\", \"query\": "
96                         + range.toElasticSearch() + "}}";
97             } else {
98                 return range.toElasticSearch();
99             }
100
101         } else {
102             // throw an exception?
103             return null;
104         }
105     }
106
107     private boolean fieldIsNested(String field) {
108         return field.contains(".");
109     }
110
111     private String pathForNestedField(String field) {
112         int index = field.lastIndexOf('.');
113         return field.substring(0, index);
114     }
115
116     @Override
117     public String toString() {
118
119         StringBuilder sb = new StringBuilder();
120
121         sb.append("{");
122
123         if (match != null) {
124             sb.append("TERM QUERY: { match: {").append(match.toString()).append("}}");
125         } else if (notMatch != null) {
126             sb.append("TERM QUERY: { not-match: {").append(match.toString()).append("}}");
127         } else if (parsedQuery != null) {
128             sb.append("PARSED QUERY: { ").append(parsedQuery.toString()).append("}");
129         } else if (range != null) {
130             sb.append("RANGE QUERY: { ").append(range.toString()).append("}");
131         } else {
132             sb.append("UNDEFINED");
133         }
134
135         sb.append("}");
136         return sb.toString();
137     }
138 }