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