package com.backendless.transaction;

import com.backendless.persistence.BackendlessDataQuery;
import com.backendless.persistence.DataQueryBuilder;
import com.backendless.transaction.operations.Operation;
import com.backendless.transaction.operations.OperationFind;
import com.backendless.transaction.payload.FindPayload;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class UnitOfWorkFindImpl implements UnitOfWorkFind
{
  AtomicInteger countFind = new AtomicInteger( 1 );

  private final List<Operation<?>> operations;

  public UnitOfWorkFindImpl( List<Operation<?>> operations )
  {
    this.operations = operations;
  }

  @Override
  public OpResult find( String tableName, DataQueryBuilder queryBuilder )
  {
    BackendlessDataQuery query = queryBuilder.build();

    String operationResultId = OperationType.FIND + "_" + countFind.getAndIncrement();
    FindPayload<?> payload = convertQueryToPayload( query );

    OperationFind operationFind = new OperationFind( OperationType.FIND, tableName, operationResultId, payload );

    operations.add( operationFind );

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

  private FindPayload<?> convertQueryToPayload( BackendlessDataQuery query )
  {
    return new FindPayload<Object>( query.getPageSize(), query.getOffset(), query.getProperties(), query.getWhereClause(),
                                    query.getHavingClause(), query.getQueryOptions().getSortBy(),
                                    query.getQueryOptions().getRelated(), query.getQueryOptions().getRelationsDepth(),
                                    query.getQueryOptions().getRelationsPageSize(), query.getGroupBy(), null );
  }
}

@@ -262,64 +262,8 @@
 ind;
-%0Aimport com.backendless.transaction.payload.FindPayload;
 %0A%0Aim
@@ -835,69 +835,8 @@
 t();
-%0A    FindPayload%3C?%3E payload = convertQueryToPayload( query );
 %0A%0A  
@@ -850,16 +850,19 @@
 tionFind
+%3C?%3E
  operati
@@ -887,16 +887,18 @@
 tionFind
+%3C%3E
 ( Operat
@@ -945,15 +945,13 @@
 Id, 
-payload
+query
  );%0A
@@ -1091,545 +1091,7 @@
 %0A  %7D
-%0A%0A  private FindPayload%3C?%3E convertQueryToPayload( BackendlessDataQuery query )%0A  %7B%0A    return new FindPayload%3CObject%3E( query.getPageSize(), query.getOffset(), query.getProperties(), query.getWhereClause(),%0A                                    query.getHavingClause(), query.getQueryOptions().getSortBy(),%0A                                    query.getQueryOptions().getRelated(), query.getQueryOptions().getRelationsDepth(),%0A                                    query.getQueryOptions().getRelationsPageSize(), query.getGroupBy(), null );%0A  %7D
 %0A%7D%0A

remove FindPayload