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

com.backendless.DataStore Maven / Gradle / Ivy

The newest version!
package com.backendless;

import com.backendless.async.callback.AsyncCallback;
import com.backendless.commons.persistence.GroupResult;
import com.backendless.exceptions.BackendlessException;
import com.backendless.persistence.BackendlessSerializer;
import com.backendless.persistence.DataQueryBuilder;
import com.backendless.persistence.GroupDataQueryBuilder;
import com.backendless.persistence.LoadRelationsQueryBuilder;
import com.backendless.rt.data.EventHandler;

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


class DataStore implements IDataStore
{
  private final Class entityClass;
  private final Persistence persistence;
  private final EventHandler eventHandler;

  DataStore( Class entityClass, Persistence persistence, EventHandler eventHandler )
  {
    this.entityClass = entityClass;
    this.persistence = persistence;
    this.eventHandler = eventHandler;
  }

  @Override
  public List create( List objects ) throws BackendlessException
  {
    return persistence.create( objects );
  }

  @Override
  public void create( List objects, AsyncCallback> responder ) throws BackendlessException
  {
    persistence.create( objects, responder );
  }

  @Override
  public T save( final T entity ) throws BackendlessException
  {
    return persistence.save( entity );
  }

  @Override
  public T save( final T entity, boolean isUpsert ) throws BackendlessException
  {
    return persistence.save( entity );
  }

  @Override
  public void save( final T entity, final AsyncCallback responder )
  {
    persistence.save( entity, responder );
  }

  @Override
  public void save( final T entity, boolean isUpsert, final AsyncCallback responder )
  {
    persistence.save( entity, responder );
  }

  @Override
  public T deepSave( final T entity ) throws BackendlessException
  {
    return persistence.deepSave( entity );
  }

  @Override
  public void deepSave( final T entity, final AsyncCallback responder )
  {
    persistence.deepSave( entity, responder );
  }

  @Override
  public Long remove( final T entity ) throws BackendlessException
  {
    return persistence.remove( entity );
  }

  @Override
  public void remove( final T entity, final AsyncCallback responder )
  {
    persistence.remove( entity, responder );
  }

  @Override
  public int remove( final String whereClause ) throws BackendlessException
  {
    return persistence.remove( BackendlessSerializer.getSimpleName( entityClass ), whereClause );
  }

  @Override
  public void remove( final String whereClause, AsyncCallback responder ) throws BackendlessException
  {
    persistence.remove( BackendlessSerializer.getSimpleName( entityClass ), whereClause, responder );
  }

  @Override
  public int update( final String whereClause, Map changes ) throws BackendlessException
  {
    return persistence.update( BackendlessSerializer.getSimpleName( entityClass ), whereClause, changes );
  }

  @Override
  public void update( final String whereClause, Map changes, AsyncCallback responder ) throws BackendlessException
  {
    persistence.update( BackendlessSerializer.getSimpleName( entityClass ), whereClause, changes, responder );
  }

  @Override
  public T findFirst() throws BackendlessException
  {
    return persistence.first( entityClass );
  }

  @Override
  public T findFirst( Integer relationsDepth ) throws BackendlessException
  {
    return findFirst( Collections.emptyList(), relationsDepth, null );
  }

  @Override
  public T findFirst( List relations ) throws BackendlessException
  {
    return findFirst( relations, null, null );
  }

  @Override
  public T findFirst( List relations, Integer relationsDepth, Integer relationsPageSize ) throws BackendlessException
  {
    return persistence.first( entityClass, relations, relationsDepth, relationsPageSize );
  }

  @Override
  public int getObjectCount()
  {
    return persistence.getObjectCount( entityClass );
  }

  @Override
  public int getObjectCount( DataQueryBuilder dataQueryBuilder )
  {
    return persistence.getObjectCount( entityClass, dataQueryBuilder );
  }

  @Override
  public int getObjectCountInGroup( GroupDataQueryBuilder dataQueryBuilder )
  {
    return persistence.getObjectCountInGroup( entityClass, dataQueryBuilder );
  }

  public void findFirst( final AsyncCallback responder )
  {
    persistence.first( entityClass, responder );
  }

  @Override
  public void findFirst( Integer relationsDepth, final AsyncCallback responder )
  {

    findFirst( Collections.emptyList(), relationsDepth, null, responder );
  }

  @Override
  public void findFirst( List relations, AsyncCallback responder )
  {
    findFirst( relations, null, null, responder );
  }

  @Override
  public void findFirst( List relations, Integer relationsDepth, Integer relationsPageSize, final AsyncCallback responder )
  {
    persistence.first( entityClass, relations, relationsDepth, relationsPageSize, responder );
  }

  @Override
  public T findLast() throws BackendlessException
  {
    return persistence.last( entityClass );
  }

  @Override
  public T findLast( Integer relationsDepth ) throws BackendlessException
  {
    return findLast( Collections.emptyList(), relationsDepth, null );
  }

  @Override
  public T findLast( List relations ) throws BackendlessException
  {
    return findLast( relations, null, null );
  }

  @Override
  public T findLast( List relations, Integer relationsDepth, Integer relationsPageSize ) throws BackendlessException
  {
    return persistence.last( entityClass, relations, relationsDepth, relationsPageSize );
  }

  @Override
  public void findLast( final AsyncCallback responder )
  {
    persistence.last( entityClass, responder );
  }

  @Override
  public void findLast( Integer relationsDepth, final AsyncCallback responder )
  {
    findLast( Collections.emptyList(), relationsDepth, null, responder );
  }

  @Override
  public void findLast( List relations, AsyncCallback responder )
  {
    findLast( relations, null, null, responder );
  }

  @Override
  public void findLast( List relations, Integer relationsDepth, Integer relationsPageSize, final AsyncCallback responder )
  {
    persistence.last( entityClass, relations, relationsDepth, relationsPageSize, responder );
  }

  @Override
  public List find() throws BackendlessException
  {
    return persistence.find( entityClass, DataQueryBuilder.create() );
  }

  @Override
  public List find( DataQueryBuilder dataQueryBuilder ) throws BackendlessException
  {
    return persistence.find( entityClass, dataQueryBuilder );
  }

  @Override
  public void find( AsyncCallback> responder )
  {
    persistence.find( entityClass, DataQueryBuilder.create(), responder );
  }

  @Override
  public void find( DataQueryBuilder dataQueryBuilder, AsyncCallback> responder )
  {
    persistence.find( entityClass, dataQueryBuilder, responder );
  }

  @Override
  public GroupResult group( GroupDataQueryBuilder dataQueryBuilder ) throws BackendlessException
  {
    return persistence.group( entityClass, dataQueryBuilder );
  }

  @Override
  public void group( GroupDataQueryBuilder dataQueryBuilder, AsyncCallback> responder )
  {
    persistence.group( entityClass, dataQueryBuilder, responder );
  }

  @Override
  public T findById( String objectId ) throws BackendlessException
  {
    return findById( objectId, Collections.emptyList() );
  }

  @Override
  public T findById( String objectId, List relations ) throws BackendlessException
  {
    return persistence.findById( entityClass, objectId, relations );
  }

  @Override
  public T findById( String objectId, Integer relationsDepth ) throws BackendlessException
  {
    return persistence.findById( entityClass, objectId, Collections.emptyList(), relationsDepth );
  }

  @Override
  public T findById( String objectId, List relations, Integer relationsDepth ) throws BackendlessException
  {
    return persistence.findById( entityClass, objectId, relations, relationsDepth );
  }

  @Override
  public T findById( T entity )
  {
    return findById( entity, Collections.emptyList() );
  }

  @Override
  public T findById( T entity, List relations )
  {
    return findById( entity, relations, (Integer) null );
  }

  @Override
  public T findById( T entity, Integer relationsDepth )
  {
    return findById( entity, Collections.emptyList(), relationsDepth );
  }

  @Override
  public T findById( T entity, List relations, Integer relationsDepth )
  {
    return persistence.findById( entity, relations, relationsDepth );
  }

  @Override
  public void findById( String objectId, AsyncCallback responder )
  {
    findById( objectId, Collections.emptyList(), responder );
  }

  @Override
  public void findById( String objectId, List relations, AsyncCallback responder )
  {
    persistence.findById( entityClass, objectId, relations, responder );
  }

  @Override
  public void findById( String objectId, Integer relationsDepth, AsyncCallback responder )
  {
    findById( objectId, Collections.emptyList(), relationsDepth, responder );
  }

  @Override
  public void findById( String objectId, List relations, Integer relationsDepth, AsyncCallback responder )
  {
    persistence.findById( entityClass, objectId, relations, relationsDepth, responder );
  }

  @Override
  public void findById( T entity, AsyncCallback responder )
  {
    findById( entity, Collections.emptyList(), responder );
  }

  @Override
  public void findById( T entity, List relations, AsyncCallback responder )
  {
    findById( entity, relations, null, responder );
  }

  @Override
  public void findById( T entity, Integer relationsDepth, AsyncCallback responder )
  {
    findById( entity, Collections.emptyList(), relationsDepth, responder );
  }

  @Override
  public void findById( T entity, List relations, Integer relationsDepth, AsyncCallback responder )
  {
    persistence.findById( entity, relations, relationsDepth, responder );
  }

  @Override
  public T findById( String id, DataQueryBuilder queryBuilder ) throws BackendlessException
  {
    return persistence.findById( entityClass, id, queryBuilder );
  }

  @Override
  public T findById( T entity, DataQueryBuilder queryBuilder ) throws BackendlessException
  {
    return persistence.findById( entity, queryBuilder );
  }

  @Override
  public void findById( String id, DataQueryBuilder queryBuilder, AsyncCallback responder )
  {
    persistence.findById( entityClass, id, queryBuilder, responder );
  }

  @Override
  public void findById( T entity, DataQueryBuilder queryBuilder, AsyncCallback responder )
  {
    persistence.findById( entity, queryBuilder, responder );
  }

  @Override
  public  List loadRelations( String objectId, LoadRelationsQueryBuilder queryBuilder )
  {
    String typeName = BackendlessSerializer.getSimpleName( entityClass );
    return persistence.loadRelations( typeName, objectId, queryBuilder, queryBuilder.getRelationType() );
  }

  @Override
  public  void loadRelations( String objectId, LoadRelationsQueryBuilder queryBuilder, AsyncCallback> responder )
  {
    String typeName = BackendlessSerializer.getSimpleName( entityClass );
    persistence.loadRelations( typeName, objectId, queryBuilder, queryBuilder.getRelationType(), responder );
  }

  @Override
  public void getObjectCount( AsyncCallback responder )
  {
    persistence.getObjectCount( entityClass, responder );
  }

  @Override
  public void getObjectCount( DataQueryBuilder dataQueryBuilder, AsyncCallback responder )
  {
    persistence.getObjectCount( entityClass, dataQueryBuilder, responder );
  }

  @Override
  public void getObjectCountInGroup( GroupDataQueryBuilder dataQueryBuilder, AsyncCallback responder )
  {
    persistence.getObjectCountInGroup( entityClass, dataQueryBuilder, responder );
  }

  @Override
  public  int addRelation( T parent, String relationColumnName, Collection children )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    Collection childrenObjectIds = new ArrayList<>();
    for( R child : children )
    {
      String childObjectId = Persistence.getEntityId( child );
      childrenObjectIds.add( childObjectId );
    }

    return addRelation( parentObjectId, relationColumnName, childrenObjectIds );
  }

  @Override
  public  void addRelation( T parent, String relationColumnName, Collection children, AsyncCallback callback )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    Collection childrenObjectIds = new ArrayList<>();
    for( R child : children )
    {
      String childObjectId = Persistence.getEntityId( child );
      childrenObjectIds.add( childObjectId );
    }

    addRelation( parentObjectId, relationColumnName, childrenObjectIds, callback );
  }

  @Override
  public int addRelation( T parent, String relationColumnName, String whereClause )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    return addRelation( parentObjectId, relationColumnName, whereClause );
  }

  @Override
  public void addRelation( T parent, String relationColumnName, String whereClause, AsyncCallback callback )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    addRelation( parentObjectId, relationColumnName, whereClause, callback );
  }

  @Override
  public  int setRelation( T parent, String relationColumnName, Collection children )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    Collection childrenObjectIds = new ArrayList<>();
    for( R child : children )
    {
      String childObjectId = Persistence.getEntityId( child );
      childrenObjectIds.add( childObjectId );
    }

    return setRelation( parentObjectId, relationColumnName, childrenObjectIds );
  }

  @Override
  public  void setRelation( T parent, String relationColumnName, Collection children, AsyncCallback callback )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    Collection childrenObjectIds = new ArrayList<>();
    for( R child : children )
    {
      String childObjectId = Persistence.getEntityId( child );
      childrenObjectIds.add( childObjectId );
    }

    setRelation( parentObjectId, relationColumnName, childrenObjectIds, callback );
  }

  @Override
  public int setRelation( T parent, String relationColumnName, String whereClause )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    return setRelation( parentObjectId, relationColumnName, whereClause );
  }

  @Override
  public void setRelation( T parent, String relationColumnName, String whereClause, AsyncCallback callback )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    setRelation( parentObjectId, relationColumnName, whereClause, callback );
  }

  @Override
  public  int deleteRelation( T parent, String relationColumnName, Collection children )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    Collection childrenObjectIds = new ArrayList<>();
    for( R child : children )
    {
      String childObjectId = Persistence.getEntityId( child );
      childrenObjectIds.add( childObjectId );
    }

    return deleteRelation( parentObjectId, relationColumnName, childrenObjectIds );
  }

  @Override
  public  void deleteRelation( T parent, String relationColumnName, Collection children, AsyncCallback callback )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    Collection childrenObjectIds = new ArrayList<>();
    for( R child : children )
    {
      String childObjectId = Persistence.getEntityId( child );
      childrenObjectIds.add( childObjectId );
    }

    deleteRelation( parentObjectId, relationColumnName, childrenObjectIds, callback );
  }

  @Override
  public int deleteRelation( T parent, String relationColumnName, String whereClause )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    return deleteRelation( parentObjectId, relationColumnName, whereClause );
  }

  @Override
  public void deleteRelation( T parent, String relationColumnName, String whereClause, AsyncCallback callback )
  {
    String parentObjectId = Persistence.getEntityId( parent );

    deleteRelation( parentObjectId, relationColumnName, whereClause, callback );
  }

  @Override
  public  int addRelation( String parentObjectId, String relationColumnName, Collection childrenObjectIds )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, childrenObjectIds };
    return Invoker.invokeSync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "addRelation", args );
  }

  @Override
  public  void addRelation( String parentObjectId, String relationColumnName, Collection childrenObjectIds,
                               AsyncCallback callback )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, childrenObjectIds };
    Invoker.invokeAsync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "addRelation", args, callback );
  }

  @Override
  public int addRelation( String parentObjectId, String relationColumnName, String whereClause )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, whereClause };
    return Invoker.invokeSync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "addRelation", args );
  }

  @Override
  public void addRelation( String parentObjectId, String relationColumnName, String whereClause, AsyncCallback callback )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, whereClause };
    Invoker.invokeAsync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "addRelation", args, callback );
  }

  @Override
  public  int setRelation( String parentObjectId, String relationColumnName, Collection childrenObjectIds )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, childrenObjectIds };
    return Invoker.invokeSync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "setRelation", args );
  }

  @Override
  public  void setRelation( String parentObjectId, String relationColumnName, Collection childrenObjectIds,
                               AsyncCallback callback )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, childrenObjectIds };
    Invoker.invokeAsync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "setRelation", args, callback );
  }

  @Override
  public int setRelation( String parentObjectId, String relationColumnName, String whereClause )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, whereClause };
    return Invoker.invokeSync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "setRelation", args );
  }

  @Override
  public void setRelation( String parentObjectId, String relationColumnName, String whereClause, AsyncCallback callback )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, whereClause };
    Invoker.invokeAsync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "setRelation", args, callback );
  }

  @Override
  public  int deleteRelation( String parentObjectId, String relationColumnName, Collection childrenObjectIds )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, childrenObjectIds };
    return Invoker.invokeSync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "deleteRelation", args );
  }

  @Override
  public  void deleteRelation( String parentObjectId, String relationColumnName, Collection childrenObjectIds,
                                  AsyncCallback callback )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, childrenObjectIds };
    Invoker.invokeAsync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "deleteRelation", args, callback );
  }

  @Override
  public int deleteRelation( String parentObjectId, String relationColumnName, String whereClause )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, whereClause };
    return Invoker.invokeSync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "deleteRelation", args );
  }

  @Override
  public void deleteRelation( String parentObjectId, String relationColumnName, String whereClause, AsyncCallback callback )
  {
    String parentTableName = BackendlessSerializer.getSimpleName( entityClass );

    Object[] args = new Object[] { parentTableName, relationColumnName, parentObjectId, whereClause };
    Invoker.invokeAsync( Persistence.PERSISTENCE_MANAGER_SERVER_ALIAS, "deleteRelation", args, callback );
  }

  @Override
  public EventHandler rt()
  {
    return eventHandler;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy