Initial search service commit
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / searchdbabstraction / searchapi / AggregationStatement.java
1 /**
2  * ============LICENSE_START=======================================================
3  * Search Data Service
4  * ================================================================================
5  * Copyright © 2017 AT&T Intellectual Property.
6  * Copyright © 2017 Amdocs
7  * All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License ati
12  *
13  *    http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  *
22  * ECOMP and OpenECOMP are trademarks
23  * and service marks of AT&T Intellectual Property.
24  */
25 package org.openecomp.sa.searchdbabstraction.searchapi;
26
27 import com.fasterxml.jackson.annotation.JsonProperty;
28
29 import java.util.Arrays;
30
31 public class AggregationStatement {
32
33   @JsonProperty("group-by")
34   private GroupByAggregation groupBy;
35
36   @JsonProperty("date-range")
37   private DateRangeAggregation dateRange;
38
39   @JsonProperty("date-histogram")
40   private DateHistogramAggregation dateHist;
41
42   @JsonProperty("nested")
43   private Aggregation[] nested;
44
45   @JsonProperty("sub-aggregations")
46   private Aggregation[] subAggregations;
47
48   public GroupByAggregation getGroupBy() {
49     return groupBy;
50   }
51
52   public void setGroupBy(GroupByAggregation groupBy) {
53     this.groupBy = groupBy;
54   }
55
56   public DateRangeAggregation getDateRange() {
57     return dateRange;
58   }
59
60   public void setDateRange(DateRangeAggregation dateRange) {
61     this.dateRange = dateRange;
62   }
63
64   public DateHistogramAggregation getDateHist() {
65     return dateHist;
66   }
67
68   public void setDateHist(DateHistogramAggregation dateHist) {
69     this.dateHist = dateHist;
70   }
71
72   public Aggregation[] getNested() {
73     return nested;
74   }
75
76   public void setNested(Aggregation[] nested) {
77     this.nested = nested;
78   }
79
80   public Aggregation[] getSubAggregations() {
81     return subAggregations;
82   }
83
84   public void setSubAggregations(Aggregation[] subAggregations) {
85     this.subAggregations = subAggregations;
86   }
87
88   public String toElasticSearch() {
89     StringBuffer sb = new StringBuffer();
90
91     sb.append("{");
92
93     if (nested != null && nested.length > 0) {
94       sb.append("\"nested\": {\"path\": \"");
95       if (nested[0].getStatement() != null) {
96         sb.append(nested[0].getStatement().getNestedPath());
97       }
98       sb.append("\"}, \"aggs\": {");
99       for (int i = 0; i < nested.length; i++) {
100         if (i > 0) {
101           sb.append(",");
102         }
103         sb.append(nested[i].toElasticSearch());
104       }
105
106       sb.append("}");
107     } else {
108       if (groupBy != null) {
109         sb.append(groupBy.toElasticSearch());
110       } else if (dateRange != null) {
111         sb.append(dateRange.toElasticSearch());
112       } else if (dateHist != null) {
113         sb.append(dateHist.toElasticSearch());
114       }
115
116       if (subAggregations != null && subAggregations.length > 0) {
117         sb.append(", \"aggs\": {");
118         for (int i = 0; i < subAggregations.length; i++) {
119           if (i > 0) {
120             sb.append(",");
121           }
122           sb.append(subAggregations[i].toElasticSearch());
123         }
124         sb.append("}");
125       }
126     }
127
128     sb.append("}");
129
130     return sb.toString();
131   }
132
133   @Override
134   public String toString() {
135     StringBuffer sb = new StringBuffer();
136
137     if (nested != null) {
138       sb.append("{nested: ");
139       sb.append(Arrays.toString(nested));
140     } else if (groupBy != null) {
141       sb.append(groupBy.toString());
142     } else if (dateHist != null) {
143       sb.append(dateHist.toString());
144     } else if (dateRange != null) {
145       sb.append(dateRange.toString());
146     }
147
148     if (subAggregations != null) {
149       sb.append(", sub-aggregations: ");
150       sb.append(Arrays.toString(subAggregations));
151     }
152
153     sb.append("}");
154
155     return sb.toString();
156   }
157
158   public String getNestedPath() {
159     String path = null;
160     String fieldName = null;
161
162     if (groupBy != null) {
163       fieldName = groupBy.getField();
164     } else if (dateRange != null) {
165       fieldName = dateRange.getField();
166     } else if (dateHist != null) {
167       fieldName = dateHist.getField();
168     }
169
170     if (fieldName != null && fieldName.contains(".")) {
171       // we have nested field
172       path = fieldName.substring(0, fieldName.indexOf("."));
173     }
174
175     return path;
176   }
177
178 }