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