2  * ============LICENSE_START=======================================================
 
   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
 
  12  *       http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.aai.sa.searchdbabstraction.searchapi;
 
  23 import com.fasterxml.jackson.annotation.JsonProperty;
 
  24 import java.util.Arrays;
 
  26 public class AggregationStatement {
 
  28     @JsonProperty("group-by")
 
  29     private GroupByAggregation groupBy;
 
  31     @JsonProperty("date-range")
 
  32     private DateRangeAggregation dateRange;
 
  34     @JsonProperty("date-histogram")
 
  35     private DateHistogramAggregation dateHist;
 
  37     @JsonProperty("nested")
 
  38     private Aggregation[] nested;
 
  40     @JsonProperty("sub-aggregations")
 
  41     private Aggregation[] subAggregations;
 
  43     public GroupByAggregation getGroupBy() {
 
  47     public void setGroupBy(GroupByAggregation groupBy) {
 
  48         this.groupBy = groupBy;
 
  51     public DateRangeAggregation getDateRange() {
 
  55     public void setDateRange(DateRangeAggregation dateRange) {
 
  56         this.dateRange = dateRange;
 
  59     public DateHistogramAggregation getDateHist() {
 
  63     public void setDateHist(DateHistogramAggregation dateHist) {
 
  64         this.dateHist = dateHist;
 
  67     public Aggregation[] getNested() {
 
  71     public void setNested(Aggregation[] nested) {
 
  75     public Aggregation[] getSubAggregations() {
 
  76         return subAggregations;
 
  79     public void setSubAggregations(Aggregation[] subAggregations) {
 
  80         this.subAggregations = subAggregations;
 
  83     public String toElasticSearch() {
 
  84         StringBuilder sb = new StringBuilder();
 
  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());
 
  93             sb.append("\"}, \"aggs\": {");
 
  94             for (int i = 0; i < nested.length; i++) {
 
  98                 sb.append(nested[i].toElasticSearch());
 
 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());
 
 111             if (subAggregations != null && subAggregations.length > 0) {
 
 112                 sb.append(", \"aggs\": {");
 
 113                 for (int i = 0; i < subAggregations.length; i++) {
 
 117                     sb.append(subAggregations[i].toElasticSearch());
 
 125         return sb.toString();
 
 129     public String toString() {
 
 130         StringBuilder sb = new StringBuilder();
 
 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());
 
 143         if (subAggregations != null) {
 
 144             sb.append(", sub-aggregations: ");
 
 145             sb.append(Arrays.toString(subAggregations));
 
 150         return sb.toString();
 
 153     public String getNestedPath() {
 
 155         String fieldName = null;
 
 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();
 
 165         if (fieldName != null && fieldName.contains(".")) {
 
 166             // we have nested field
 
 167             path = fieldName.substring(0, fieldName.indexOf("."));