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

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

The newest version!
package com.backendless.transaction;

import com.backendless.exceptions.ExceptionMessage;
import com.backendless.persistence.BackendlessSerializer;

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

class UnitOfWorkCreateImpl implements UnitOfWorkCreate
{
  private final List operations;
  private final OpResultIdGenerator opResultIdGenerator;
  private final Map clazzes;

  UnitOfWorkCreateImpl( List operations, OpResultIdGenerator opResultIdGenerator,
                        Map clazzes )
  {
    this.operations = operations;
    this.opResultIdGenerator = opResultIdGenerator;
    this.clazzes = clazzes;
  }

  @Override
  public  OpResult create( E instance )
  {
    Map entityMap = SerializationHelper.serializeEntityToMap( instance );
    String tableName = BackendlessSerializer.getSimpleName( instance.getClass() );

    clazzes.put( tableName, instance.getClass() );

    return create( tableName, entityMap );
  }

  @Override
  public OpResult create( String tableName, Map objectMap )
  {
    if( objectMap == null )
      throw new IllegalArgumentException( ExceptionMessage.NULL_MAP );

    TransactionHelper.makeReferenceToValueFromOpResult( objectMap );

    String operationResultId = opResultIdGenerator.generateOpResultId( OperationType.CREATE, tableName );
    OperationCreate operationCreate = new OperationCreate( OperationType.CREATE, tableName, operationResultId, objectMap );

    operations.add( operationCreate );

    return TransactionHelper.makeOpResult( tableName, operationResultId, OperationType.CREATE );
  }

  @Override
  public  OpResult bulkCreate( List instances )
  {
    List> serializedEntities = TransactionHelper.convertInstancesToMaps( instances );

    String tableName = BackendlessSerializer.getSimpleName( instances.get( 0 ).getClass() );

    return bulkCreate( tableName, serializedEntities );
  }

  @Override
  public OpResult bulkCreate( String tableName, List> arrayOfObjectMaps )
  {
    if( arrayOfObjectMaps == null || arrayOfObjectMaps.isEmpty() )
      throw new IllegalArgumentException( ExceptionMessage.NULL_EMPTY_BULK );

    for( Map mapObject : arrayOfObjectMaps )
      if( mapObject != null )
        TransactionHelper.makeReferenceToValueFromOpResult( mapObject );
      else
        throw new IllegalArgumentException( ExceptionMessage.NULL_MAP );

    String operationResultId = opResultIdGenerator.generateOpResultId( OperationType.CREATE_BULK, tableName );
    OperationCreateBulk operationCreateBulk = new OperationCreateBulk( OperationType.CREATE_BULK, tableName,
                                                                       operationResultId, arrayOfObjectMaps );

    operations.add( operationCreateBulk );

    return TransactionHelper.makeOpResult( tableName, operationResultId, OperationType.CREATE_BULK );
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy