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

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

There is a newer version: 7.0-alpha
Show newest version
package com.backendless.transaction;

import com.backendless.Invoker;
import com.backendless.async.callback.AsyncCallback;
import com.backendless.core.responder.AdaptingResponder;
import com.backendless.exceptions.ExceptionMessage;
import com.backendless.utils.ResponderHelper;
import weborb.types.Types;

import java.util.Map;

class UnitOfWorkExecutorImpl implements UnitOfWorkExecutor
{
  private final static String TRANSACTION_MANAGER_SERVER_ALIAS = "com.backendless.transaction.TransactionService";

  private final UnitOfWork unitOfWork;
  private final Map clazzes;

  UnitOfWorkExecutorImpl( UnitOfWork unitOfWork, Map clazzes )
  {
    this.unitOfWork = unitOfWork;
    this.clazzes = clazzes;
  }

  @Override
  public UnitOfWorkResult execute()
  {
    return execute( null, false );
  }

  @Override
  public void execute( AsyncCallback responder )
  {
    execute( responder, true );
  }

  private UnitOfWorkResult execute( AsyncCallback responder, boolean async )
  {
    if( unitOfWork.getOperations() == null || unitOfWork.getOperations().isEmpty() )
      throw new IllegalArgumentException( ExceptionMessage.LIST_OPERATIONS_NULL_EMPTY );

    Object[] args = new Object[]{ unitOfWork };

    for( Map.Entry entry : clazzes.entrySet() )
    {
      if( Types.getMappedClientClass( entry.getKey() ) == null )
      {
        Types.addClientClassMapping( entry.getKey(), entry.getValue() );
      }
    }

    AdaptingResponder unitOfWorkAdaptingResponder = ResponderHelper.getPOJOAdaptingResponder( UnitOfWorkResult.class );
    if( async )
      Invoker.invokeAsync( TRANSACTION_MANAGER_SERVER_ALIAS, "execute", args, responder, unitOfWorkAdaptingResponder );
    else
      return Invoker.invokeSync( TRANSACTION_MANAGER_SERVER_ALIAS, "execute", args, unitOfWorkAdaptingResponder );

    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy