package com.backendless.transaction;

import com.backendless.exceptions.ExceptionMessage;
import com.backendless.transaction.operations.Operation;
import com.backendless.UnitOfWork;

import java.util.HashMap;
import java.util.Map;

public class OpResult
{
  private final String tableName;
  private final OperationType operationType;
  private final String opResultId;

  OpResult( String tableName, OperationType operationType, String opResultId )
  {
    this.tableName = tableName;
    this.operationType = operationType;
    this.opResultId = opResultId;
  }

  public OperationType getOperationType()
  {
    return operationType;
  }

  public String getTableName()
  {
    return tableName;
  }

  public OpResultValueReference resolveTo( int resultIndex, String propName )
  {
    return new OpResultValueReference( this, resultIndex, propName );
  }

  public OpResultValueReference resolveTo( int resultIndex )
  {
    return new OpResultValueReference( this, resultIndex );
  }

  public OpResultValueReference resolveTo( String propName )
  {
    return new OpResultValueReference( this, propName );
  }

  public Map<String, Object> makeReference()
  {
    Map<String, Object> referenceMap = new HashMap<>();
    referenceMap.put( UnitOfWork.REFERENCE_MARKER, true );
    referenceMap.put( UnitOfWork.OP_RESULT_ID, opResultId );
    return referenceMap;
  }

  public void setOpResultId( UnitOfWork unitOfWork, String newOpResultId )
  {
    if( unitOfWork.getOpResultIdStrings().contains( newOpResultId ) )
      throw new IllegalArgumentException( ExceptionMessage.OP_RESULT_ID_ALREADY_PRESENT );

    String oldOpResultId = (String) this.reference.get( UnitOfWork.OP_RESULT_ID );
    this.reference.put( UnitOfWork.OP_RESULT_ID, newOpResultId );

    for( Operation<?> operation : unitOfWork.getOperations() )
      if( operation.getOpResultId().equals( oldOpResultId ) )
      {
        operation.setOpResultId( newOpResultId );
        break;
      }
  }
}

@@ -334,30 +334,24 @@
 ;%0A  private 
-final 
 String opRes
@@ -1605,158 +1605,8 @@
 );%0A%0A
-    String oldOpResultId = (String) this.reference.get( UnitOfWork.OP_RESULT_ID );%0A    this.reference.put( UnitOfWork.OP_RESULT_ID, newOpResultId );%0A%0A
     
@@ -1713,11 +1713,8 @@
 s( o
-ldO
 pRes
@@ -1800,15 +1800,48 @@
 ak;%0A
-
       %7D
+%0A%0A    opResultId = newOpResultId;
 %0A  %7D

generate with new OpResult