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

com.backendless.transaction.RelationOperationImpl Maven / Gradle / Ivy

The newest version!
package com.backendless.transaction;

import com.backendless.Persistence;
import com.backendless.exceptions.ExceptionMessage;
import com.backendless.persistence.BackendlessSerializer;
import com.backendless.transaction.payload.Relation;

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

class RelationOperationImpl implements RelationOperation
{
  private final List operations;
  private final OpResultIdGenerator opResultIdGenerator;

  public RelationOperationImpl( List operations, OpResultIdGenerator opResultIdGenerator )
  {
    this.operations = operations;
    this.opResultIdGenerator = opResultIdGenerator;
  }

  @Override
  public OpResult addOperation( OperationType operationType, String parentTable, Map parentObject,
                                String columnName, String[] childrenObjectIds )
  {
    String parentObjectId = TransactionHelper.convertObjectMapToObjectId( parentObject );

    if( childrenObjectIds == null || childrenObjectIds.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    return addOperation( operationType, parentTable, parentObjectId, columnName, null, childrenObjectIds );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, String parentTable,
                                    Map parentObject, String columnName, E[] childrenInstances )
  {
    if( childrenInstances == null || childrenInstances.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    if( childrenInstances[ 0 ] instanceof String )
    {
      String[] childrenObjectIds = (String[]) childrenInstances;
      return addOperation( operationType, parentTable, parentObject, columnName, childrenObjectIds );
    }

    String parentObjectId = TransactionHelper.convertObjectMapToObjectId( parentObject );
    return addOperation( operationType, parentTable, parentObjectId, columnName, childrenInstances );
  }

  @Override
  public OpResult addOperation( OperationType operationType, String parentTable,
                                Map parentObject, String columnName, List> childrenMaps )
  {
    String parentObjectId = TransactionHelper.convertObjectMapToObjectId( parentObject );
    return addOperation( operationType, parentTable, parentObjectId, columnName, childrenMaps );
  }

  @Override
  public OpResult addOperation( OperationType operationType, String parentTable,
                                Map parentObject, String columnName, OpResult children )
  {
    String parentObjectId = TransactionHelper.convertObjectMapToObjectId( parentObject );
    return addOperation( operationType, parentTable, parentObjectId, columnName, children );
  }

  @Override
  public OpResult addOperation( OperationType operationType, String parentTable, Map parentObject,
                                String columnName, String whereClauseForChildren )
  {
    String parentObjectId = TransactionHelper.convertObjectMapToObjectId( parentObject );
    return addOperation( operationType, parentTable, parentObjectId, columnName, whereClauseForChildren );
  }

  @Override
  public OpResult addOperation( OperationType operationType, String parentTable, String parentObjectId,
                                String columnName, String[] childrenObjectIds )
  {
    if( childrenObjectIds == null || childrenObjectIds.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    return addOperation( operationType, parentTable, parentObjectId, columnName, null, childrenObjectIds );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, String parentTable, String parentObjectId,
                                    String columnName, E[] childrenInstances )
  {
    if( childrenInstances == null || childrenInstances.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    if( childrenInstances[ 0 ] instanceof String )
    {
      String[] childrenObjectIds = (String[]) childrenInstances;
      return addOperation( operationType, parentTable, parentObjectId, columnName, childrenObjectIds );
    }

    List childrenIds = getChildrenFromArrayInstances( childrenInstances );

    return addOperation( operationType, parentTable, parentObjectId, columnName,
                         null, childrenIds );
  }

  @Override
  public OpResult addOperation( OperationType operationType, String parentTable, String parentObjectId,
                                String columnName, List> childrenMaps )
  {
    List childrenIds = getChildrenFromListMap( childrenMaps );

    return addOperation( operationType, parentTable, parentObjectId, columnName,
                         null, childrenIds );
  }

  @Override
  public OpResult addOperation( OperationType operationType, String parentTable, String parentObjectId,
                                String columnName, OpResult children )
  {
    return addOperation( operationType, parentTable, parentObjectId, columnName,
                         null, makeChildren( children ) );
  }

  @Override
  public OpResult addOperation( OperationType operationType, String parentTable, String parentObjectId,
                                String columnName, String whereClauseForChildren )
  {
    return addOperation( operationType, parentTable, parentObjectId, columnName,
                         whereClauseForChildren, null );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, E parentObject, String columnName,
                                    String[] childrenObjectIds )
  {
    String parentObjectId = getParentObjectIdFromInstance( parentObject );
    String parentTable = BackendlessSerializer.getSimpleName( parentObject.getClass() );

    if( childrenObjectIds == null || childrenObjectIds.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    return addOperation( operationType, parentTable, parentObjectId, columnName, null, childrenObjectIds );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, E parentObject, String columnName, U[] childrenInstances )
  {
    if( childrenInstances == null || childrenInstances.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    if( childrenInstances[ 0 ] instanceof String )
    {
      String[] childrenObjectIds = (String[]) childrenInstances;
      return addOperation( operationType, parentObject, columnName, childrenObjectIds );
    }

    String parentObjectId = getParentObjectIdFromInstance( parentObject );
    String parentTable = BackendlessSerializer.getSimpleName( parentObject.getClass() );

    List childrenIds = getChildrenFromArrayInstances( childrenInstances );

    return addOperation( operationType, parentTable, parentObjectId, columnName, null, childrenIds );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, E parentObject, String columnName, List> childrenMaps )
  {
    String parentObjectId = getParentObjectIdFromInstance( parentObject );
    String parentTable = BackendlessSerializer.getSimpleName( parentObject.getClass() );

    List childrenIds = getChildrenFromListMap( childrenMaps );

    return addOperation( operationType, parentTable, parentObjectId, columnName, null, childrenIds );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, E parentObject, String columnName,
                                    OpResult children )
  {
    String parentObjectId = getParentObjectIdFromInstance( parentObject );
    String parentTable = BackendlessSerializer.getSimpleName( parentObject.getClass() );

    return addOperation( operationType, parentTable, parentObjectId, columnName,
                         null, makeChildren( children ) );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, E parentObject, String columnName,
                                    String whereClauseForChildren )
  {
    String parentObjectId = getParentObjectIdFromInstance( parentObject );
    String parentTable = BackendlessSerializer.getSimpleName( parentObject.getClass() );

    return addOperation( operationType, parentTable, parentObjectId, columnName, whereClauseForChildren, null );
  }

  @Override
  public OpResult addOperation( OperationType operationType, OpResult parentObject, String columnName,
                                String[] childrenObjectIds )
  {
    if( childrenObjectIds == null || childrenObjectIds.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    return addOperation( operationType, parentObject.getTableName(),
                         parentObject.resolveTo( Persistence.DEFAULT_OBJECT_ID_FIELD ).makeReference(),
                         columnName, null, childrenObjectIds );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, OpResult parentObject, String columnName, E[] childrenInstances )
  {
    if( childrenInstances == null || childrenInstances.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    if( childrenInstances[ 0 ] instanceof String )
    {
      String[] childrenObjectIds = (String[]) childrenInstances;
      return addOperation( operationType, parentObject, columnName, childrenObjectIds );
    }

    checkOpResultFoParent( parentObject );

    List childrenIds = getChildrenFromArrayInstances( childrenInstances );

    return addOperation( operationType, parentObject.getTableName(),
                         parentObject.resolveTo( Persistence.DEFAULT_OBJECT_ID_FIELD ).makeReference(),
                         columnName, null, childrenIds );
  }

  @Override
  public OpResult addOperation( OperationType operationType, OpResult parentObject,
                                String columnName, List> childrenMaps )
  {
    checkOpResultFoParent( parentObject );

    List childrenIds = getChildrenFromListMap( childrenMaps );

    return addOperation( operationType, parentObject.getTableName(),
                         parentObject.resolveTo( Persistence.DEFAULT_OBJECT_ID_FIELD ).makeReference(),
                         columnName, null, childrenIds );
  }

  @Override
  public OpResult addOperation( OperationType operationType, OpResult parentObject,
                                String columnName, OpResult children )
  {
    checkOpResultFoParent( parentObject );

    return addOperation( operationType, parentObject.getTableName(),
                         parentObject.resolveTo( Persistence.DEFAULT_OBJECT_ID_FIELD ).makeReference(),
                         columnName, null, makeChildren( children ) );
  }

  @Override
  public OpResult addOperation( OperationType operationType, OpResult parentObject,
                                String columnName, String whereClauseForChildren )
  {
    checkOpResultFoParent( parentObject );

    return addOperation( operationType, parentObject.getTableName(),
                         parentObject.resolveTo( Persistence.DEFAULT_OBJECT_ID_FIELD ).makeReference(),
                         columnName, whereClauseForChildren, null );
  }

  @Override
  public OpResult addOperation( OperationType operationType, OpResultValueReference parentObject, String columnName,
                                String[] childrenObjectIds )
  {
    Map referenceToObjectId = getReferenceToParentFromOpResultValue( parentObject );

    if( childrenObjectIds == null || childrenObjectIds.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    return addOperation( operationType, parentObject.getOpResult().getTableName(), referenceToObjectId, columnName,
                         null, childrenObjectIds );
  }

  @Override
  public  OpResult addOperation( OperationType operationType, OpResultValueReference parentObject,
                                    String columnName, E[] childrenInstances )
  {
    if( childrenInstances == null || childrenInstances.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    if( childrenInstances[ 0 ] instanceof String )
    {
      String[] childrenObjectIds = (String[]) childrenInstances;
      return addOperation( operationType, parentObject, columnName, childrenObjectIds );
    }

    Map referenceToObjectId = getReferenceToParentFromOpResultValue( parentObject );

    List childrenIds = getChildrenFromArrayInstances( childrenInstances );

    return addOperation( operationType, parentObject.getOpResult().getTableName(), referenceToObjectId, columnName,
                         null, childrenIds );
  }

  @Override
  public OpResult addOperation( OperationType operationType, OpResultValueReference parentObject,
                                String columnName, List> childrenMaps )
  {
    Map referenceToObjectId = getReferenceToParentFromOpResultValue( parentObject );

    List childrenIds = getChildrenFromListMap( childrenMaps );

    return addOperation( operationType, parentObject.getOpResult().getTableName(), referenceToObjectId, columnName,
                         null, childrenIds );
  }

  @Override
  public OpResult addOperation( OperationType operationType, OpResultValueReference parentObject,
                                String columnName, OpResult children )
  {
    Map referenceToObjectId = getReferenceToParentFromOpResultValue( parentObject );

    return addOperation( operationType, parentObject.getOpResult().getTableName(), referenceToObjectId, columnName,
                         null, makeChildren( children ) );
  }

  @Override
  public OpResult addOperation( OperationType operationType, OpResultValueReference parentObject,
                                String columnName, String whereClauseForChildren )
  {
    Map referenceToObjectId = getReferenceToParentFromOpResultValue( parentObject );

    return addOperation( operationType, parentObject.getOpResult().getTableName(), referenceToObjectId, columnName,
                         whereClauseForChildren, null );
  }

  private OpResult addOperation( OperationType operationType, String parentTable, Object parentObject,
                                 String columnName, String whereClauseForChildren, Object children )
  {
    if( parentTable == null || parentTable.equals( "" ) )
      throw new IllegalArgumentException( ExceptionMessage.NULL_PARENT_TABLE_NAME );

    if( columnName == null || columnName.equals( "" ) )
      throw new IllegalArgumentException( ExceptionMessage.NULL_RELATION_COLUMN_NAME );

    String operationResultId = opResultIdGenerator.generateOpResultId( operationType, parentTable );

    Relation relation = new Relation();
    relation.setParentObject( parentObject );
    relation.setRelationColumn( columnName );
    relation.setConditional( whereClauseForChildren );
    relation.setUnconditional( children );
    switch( operationType )
    {
      case ADD_RELATION:
        operations.add( new OperationAddRelation( operationType, parentTable, operationResultId, relation ) );
        break;
      case SET_RELATION:
        operations.add( new OperationSetRelation( operationType, parentTable, operationResultId, relation ) );
        break;
      case DELETE_RELATION:
        operations.add( new OperationDeleteRelation( operationType, parentTable, operationResultId, relation ) );
        break;
    }

    return TransactionHelper.makeOpResult( parentTable, operationResultId, operationType );
  }

  private void checkOpResultFoParent( OpResult parentObject )
  {
    if( parentObject == null )
      throw new IllegalArgumentException( ExceptionMessage.NULL_OP_RESULT );

    if( !OperationType.supportEntityDescriptionResultType.contains( parentObject.getOperationType() ) )
      throw new IllegalArgumentException( ExceptionMessage.REF_TYPE_NOT_SUPPORT );
  }

  private  String getParentObjectIdFromInstance( E parentObject )
  {
    if( parentObject == null )
      throw new IllegalArgumentException( ExceptionMessage.NULL_ENTITY );
    String parentObjectId = Persistence.getEntityId( parentObject );
    if( parentObjectId == null )
      throw new IllegalArgumentException( ExceptionMessage.NULL_OBJECT_ID_IN_INSTANCE );
    return parentObjectId;
  }

  private Map getReferenceToParentFromOpResultValue( OpResultValueReference parentObject )
  {
    if( parentObject == null )
      throw new IllegalArgumentException( ExceptionMessage.NULL_OP_RESULT_VALUE_REFERENCE );

    if( parentObject.getResultIndex() == null || parentObject.getPropName() != null )
      throw new IllegalArgumentException( ExceptionMessage.OP_RESULT_INDEX_YES_PROP_NAME_NOT );

    return TransactionHelper.convertCreateBulkOrFindResultIndexToObjectId( parentObject );
  }

  private Object makeChildren( OpResult children )
  {
    if( children == null )
      throw new IllegalArgumentException( ExceptionMessage.NULL_OP_RESULT );

    if( OperationType.supportCollectionEntityDescriptionType.contains( children.getOperationType() )
        || OperationType.supportListIdsResultType.contains( children.getOperationType() ) )
    {
      return children.makeReference();
    }
    else if( OperationType.supportEntityDescriptionResultType.contains( children.getOperationType() ) )
    {
      return Collections.singletonList( children.resolveTo( Persistence.DEFAULT_OBJECT_ID_FIELD ).makeReference() );
    }
    else
    {
      throw new IllegalArgumentException( ExceptionMessage.REF_TYPE_NOT_SUPPORT );
    }
  }

  private List getChildrenFromListMap( List> childrenMaps )
  {
    if( childrenMaps == null || childrenMaps.isEmpty() )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    return TransactionHelper.convertMapsToObjectIds( childrenMaps );
  }

  private  List getChildrenFromArrayInstances( E[] children )
  {
    if( children == null || children.length == 0 )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    return TransactionHelper.getObjectIdsFromListInstances( children );
  }
}