package com.backendless.persistence;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class DataQueryBuilder
{
  private PagedQueryBuilder<DataQueryBuilder> pagedQueryBuilder;
  private QueryOptionsBuilder<DataQueryBuilder> queryOptionsBuilder;
  private List<String> properties;
  private String whereClause;
  private List<String> groupBy;
  private String havingClause;

  private DataQueryBuilder()
  {
    properties = new ArrayList<>();
    pagedQueryBuilder = new PagedQueryBuilder<>( this );
    queryOptionsBuilder = new QueryOptionsBuilder<>( this );
    groupBy = new ArrayList<>();
    havingClause = "";
  }

  public static DataQueryBuilder create()
  {
    return new DataQueryBuilder();
  }

  public BackendlessDataQuery build()
  {
    BackendlessDataQuery dataQuery = pagedQueryBuilder.build();

    dataQuery.setQueryOptions( queryOptionsBuilder.build() );
    dataQuery.setProperties( properties );
    dataQuery.setWhereClause( whereClause );
    dataQuery.setGroupBy( groupBy );
    dataQuery.setHavingClause( havingClause );

    return dataQuery;
  }

  /*--- Auto-generated code ---*/

  public DataQueryBuilder setPageSize( int pageSize )
  {
    return pagedQueryBuilder.setPageSize( pageSize );
  }

  public DataQueryBuilder setOffset( int offset )
  {
    return pagedQueryBuilder.setOffset( offset );
  }

  public DataQueryBuilder prepareNextPage()
  {
    return pagedQueryBuilder.prepareNextPage();
  }

  public DataQueryBuilder preparePreviousPage()
  {
    return pagedQueryBuilder.preparePreviousPage();
  }

  public List<String> getProperties()
  {
    return properties;
  }

  public DataQueryBuilder setProperties( List<String> properties )
  {
    this.properties = properties;
    return this;
  }

  public DataQueryBuilder setProperties( String... properties )
  {
    Collections.addAll( this.properties, properties );
    return this;
  }

  public DataQueryBuilder addProperty( String property )
  {
    this.properties.add( property );
    return this;
  }

  public String getWhereClause()
  {
    return whereClause;
  }

  public DataQueryBuilder setWhereClause( String whereClause )
  {
    this.whereClause = whereClause;
    return this;
  }

  public List<String> getSortBy()
  {
    return queryOptionsBuilder.getSortBy();
  }

  public DataQueryBuilder setSortBy( List<String> sortBy )
  {
    return queryOptionsBuilder.setSortBy( sortBy );
  }

  public DataQueryBuilder setSortBy( String... sortBy )
  {
    return queryOptionsBuilder.setSortBy( sortBy );
  }

  public DataQueryBuilder addSortBy( String sortBy )
  {
    return queryOptionsBuilder.addSortBy( sortBy );
  }

  public List<String> getRelated()
  {
    return queryOptionsBuilder.getRelated();
  }

  public DataQueryBuilder setRelated( List<String> related )
  {
    return queryOptionsBuilder.setRelated( related );
  }

  public DataQueryBuilder setRelated( String... related )
  {
    return queryOptionsBuilder.setRelated( related );
  }

  public Integer getRelationsDepth()
  {
    return queryOptionsBuilder.getRelationsDepth();
  }

  public DataQueryBuilder setRelationsDepth( Integer relationsDepth )
  {
    return queryOptionsBuilder.setRelationsDepth( relationsDepth );
  }

  public List<String> getGroupBy()
  {
    return groupBy;
  }

  public DataQueryBuilder setGroupBy( List<String> groupBy )
  {
    this.groupBy = groupBy;
    return this;
  }

  public DataQueryBuilder setGroupBy( String... groupBy )
  {
    Collections.addAll( this.groupBy, groupBy );
    return this;
  }

  public DataQueryBuilder addGroupBy( String groupBy )
  {
    this.groupBy.add( groupBy );
    return this;
  }

  public String getHavingClause()
  {
    return havingClause;
  }

  public DataQueryBuilder setHavingClause( String havingClause )
  {
    this.havingClause = havingClause;
    return this;
  }
}

@@ -3256,72 +3256,8 @@
  %7D%0A%0A
-  public List%3CString%3E getGroupBy()%0A  %7B%0A    return groupBy;%0A  %7D%0A%0A
   pu
@@ -3288,16 +3288,26 @@
 tGroupBy
+Properties
 ( List%3CS
@@ -3403,26 +3403,36 @@
 Builder 
-set
+add
 GroupBy
+Properties
 ( String
@@ -3556,16 +3556,24 @@
 dGroupBy
+Property
 ( String
@@ -3646,76 +3646,8 @@
  %7D%0A%0A
-  public String getHavingClause()%0A  %7B%0A    return havingClause;%0A  %7D%0A%0A
   pu

Improve naming of groupBy and having methods; remove getters.