ba62f747d78c333a103d2912b0cd588531311ce0
[aai/search-data-service.git] / src / main / java / org / openecomp / sa / searchdbabstraction / searchapi / AggregationStatement.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 import java.util.Arrays;
28
29 public class AggregationStatement {
30
31   @JsonProperty("group-by")
32   private GroupByAggregation groupBy;
33
34   @JsonProperty("date-range")
35   private DateRangeAggregation dateRange;
36
37   @JsonProperty("date-histogram")
38   private DateHistogramAggregation dateHist;
39
40   @JsonProperty("nested")
41   private Aggregation[] nested;
42
43   @JsonProperty("sub-aggregations")
44   private Aggregation[] subAggregations;
45
46   public GroupByAggregation getGroupBy() {
47     return groupBy;
48   }
49
50   public void setGroupBy(GroupByAggregation groupBy) {
51     this.groupBy = groupBy;
52   }
53
54   public DateRangeAggregation getDateRange() {
55     return dateRange;
56   }
57
58   public void setDateRange(DateRangeAggregation dateRange) {
59     this.dateRange = dateRange;
60   }
61
62   public DateHistogramAggregation getDateHist() {
63     return dateHist;
64   }
65
66   public void setDateHist(DateHistogramAggregation dateHist) {
67     this.dateHist = dateHist;
68   }
69
70   public Aggregation[] getNested() {
71     return nested;
72   }
73
74   public void setNested(Aggregation[] nested) {
75     this.nested = nested;
76   }
77
78   public Aggregation[] getSubAggregations() {
79     return subAggregations;
80   }
81
82   public void setSubAggregations(Aggregation[] subAggregations) {
83     this.subAggregations = subAggregations;
84   }
85
86   public String toElasticSearch() {
87     StringBuffer sb = new StringBuffer();
88
89     sb.append("{");
90
91     if (nested != null && nested.length > 0) {
92       sb.append("\"nested\": {\"path\": \"");
93       if (nested[0].getStatement() != null) {
94         sb.append(nested[0].getStatement().getNestedPath());
95       }
96       sb.append("\"}, \"aggs\": {");
97       for (int i = 0; i < nested.length; i++) {
98         if (i > 0) {
99           sb.append(",");
100         }
101         sb.append(nested[i].toElasticSearch());
102       }
103
104       sb.append("}");
105     } else {
106       if (groupBy != null) {
107         sb.append(groupBy.toElasticSearch());
108       } else if (dateRange != null) {
109         sb.append(dateRange.toElasticSearch());
110       } else if (dateHist != null) {
111         sb.append(dateHist.toElasticSearch());
112       }
113
114       if (subAggregations != null && subAggregations.length > 0) {
115         sb.append(", \"aggs\": {");
116         for (int i = 0; i < subAggregations.length; i++) {
117           if (i > 0) {
118             sb.append(",");
119           }
120           sb.append(subAggregations[i].toElasticSearch());
121         }
122         sb.append("}");
123       }
124     }
125
126     sb.append("}");
127
128     return sb.toString();
129   }
130
131   @Override
132   public String toString() {
133     StringBuffer sb = new StringBuffer();
134
135     if (nested != null) {
136       sb.append("{nested: ");
137       sb.append(Arrays.toString(nested));
138     } else if (groupBy != null) {
139       sb.append(groupBy.toString());
140     } else if (dateHist != null) {
141       sb.append(dateHist.toString());
142     } else if (dateRange != null) {
143       sb.append(dateRange.toString());
144     }
145
146     if (subAggregations != null) {
147       sb.append(", sub-aggregations: ");
148       sb.append(Arrays.toString(subAggregations));
149     }
150
151     sb.append("}");
152
153     return sb.toString();
154   }
155
156   public String getNestedPath() {
157     String path = null;
158     String fieldName = null;
159
160     if (groupBy != null) {
161       fieldName = groupBy.getField();
162     } else if (dateRange != null) {
163       fieldName = dateRange.getField();
164     } else if (dateHist != null) {
165       fieldName = dateHist.getField();
166     }
167
168     if (fieldName != null && fieldName.contains(".")) {
169       // we have nested field
170       path = fieldName.substring(0, fieldName.indexOf("."));
171     }
172
173     return path;
174   }
175
176 }