All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.backendless.persistence.LoadRelationsQueryBuilder Maven / Gradle / Ivy

The newest version!
package com.backendless.persistence;

import com.backendless.IDataStore;
import com.backendless.exceptions.ExceptionMessage;
import com.backendless.utils.StringUtils;

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

/**
 * BackendlessDataQuery builder for {@link IDataStore#loadRelations}.
 *
 * Accepts page size, offset and child relation name.
 * Parameter R added for type safety.
 *
 * Examples:
 * Typed relation find
 * 
 * LoadRelationsQueryBuilder queryBuilder = LoadRelationsQueryBuilder
 *    .of( Employee.class )
 *    .setRelationName( "employees" );
 * List employees1 = Backendless.Data.of( Department.class )
 *    .loadRelations( "6286C24A-C17A-9897-FF9C-B08D492CB000", queryBuilder );
 * 
 *
 * Map-driven relation find
 * 
 *   LoadRelationsQueryBuilder> queryBuilderMap = LoadRelationsQueryBuilder
 *      .ofMap()
 *      .setRelationName( "employees" );
 *   List> employeesMapList = Backendless.Data.of( "Department" )
 *      .loadRelations( "6286C24A-C17A-9897-FF9C-B08D492CB000", queryBuilderMap );
 * 
 *
 * @param  child relation type
 */
public final class LoadRelationsQueryBuilder
{
  private String relationName;
  private final Class relationType;
  private final PagedQueryBuilder> pagedQueryBuilder;
  private List properties;
  private List sortBy;

  private LoadRelationsQueryBuilder( Class relationType )
  {
    pagedQueryBuilder = new PagedQueryBuilder<>( this );
    this.relationType = relationType;
    this.properties = new ArrayList<>();
    this.sortBy = new ArrayList<>();
  }

  public static LoadRelationsQueryBuilder> ofMap()
  {
    @SuppressWarnings( "unchecked" )
    LoadRelationsQueryBuilder> queryBuilder = new LoadRelationsQueryBuilder( Map.class );
    return queryBuilder;
  }

  public static  LoadRelationsQueryBuilder of( Class relationType )
  {
    return new LoadRelationsQueryBuilder<>( relationType );
  }

  public BackendlessDataQuery build()
  {
    StringUtils.checkEmpty( relationName, ExceptionMessage.NULL_FIELD( "relationName" ) );
    BackendlessDataQuery dataQuery = pagedQueryBuilder.build();
    QueryOptions queryOptions = new QueryOptions();
    queryOptions.setRelated( Collections.singletonList( relationName ) );
    queryOptions.setSortBy( sortBy );
    dataQuery.setQueryOptions( queryOptions );
    dataQuery.setProperties( properties );

    return dataQuery;
  }

  public LoadRelationsQueryBuilder setRelationName( String relationName )
  {
    this.relationName = relationName;
    return this;
  }

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

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

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

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

  public Class getRelationType()
  {
    return relationType;
  }

  public List getProperties()
  {
    return properties;
  }

  public LoadRelationsQueryBuilder setProperties( List properties )
  {
    this.properties = properties;
    return this;
  }

  public LoadRelationsQueryBuilder setProperties( String... properties )
  {
    this.properties = new ArrayList<>();
    Collections.addAll( this.properties, properties );
    return this;
  }

  public LoadRelationsQueryBuilder addProperty( List properties )
  {
    this.properties.addAll( properties );
    return this;
  }

  public LoadRelationsQueryBuilder addProperty( String... properties )
  {
    Collections.addAll( this.properties, properties );
    return this;
  }

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

  public List getSortBy()
  {
    return sortBy;
  }

  public LoadRelationsQueryBuilder setSortBy( List sortBy )
  {
    this.sortBy = sortBy;
    return this;
  }

  public LoadRelationsQueryBuilder setSortBy( String... sortBy )
  {
    this.sortBy = new ArrayList<>();
    Collections.addAll( this.sortBy, sortBy );
    return this;
  }

  public LoadRelationsQueryBuilder addSortBy( List sortBy )
  {
    this.sortBy.addAll( sortBy );
    return this;
  }

  public LoadRelationsQueryBuilder addSortBy( String... sortBy )
  {
    Collections.addAll( this.sortBy, sortBy );
    return this;
  }

  public LoadRelationsQueryBuilder addSortBy( String sortBy )
  {
    this.sortBy.add( sortBy );
    return this;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy