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