org.sormula.active.operation
Class ActiveOperation<R extends ActiveRecord<R>,T>

java.lang.Object
  extended by org.sormula.active.operation.ActiveOperation<R,T>
Type Parameters:
R - record type
T - execute return type
Direct Known Subclasses:
ActiveLazySelector, Delete, DeleteAll, DeleteAllBatch, Insert, InsertAll, InsertAllBatch, Save, SaveAll, Select, SelectAll, SelectAllCustom, SelectAllWhere, SelectAllWhereOrdered, SelectAvg, SelectCount, SelectCustom, SelectMax, SelectMin, SelectSum, SelectWhere, Update, UpdateAll, UpdateAllBatch

public abstract class ActiveOperation<R extends ActiveRecord<R>,T>
extends java.lang.Object

Template for executing operate() on an active table. Used by ActiveTable for thread-safe and transaction based operation for a table (and any cascaded tables). A database transaction will only be used if Connection.getAutoCommit() returns false.

Since:
1.7 and 2.1
Author:
Jeff Miller

Constructor Summary
ActiveOperation(ActiveTable<R> activeTable, java.lang.String exceptionMessage)
          Constructs for a table and fail message.
 
Method Summary
protected  void attach(java.util.Collection<R> records)
          Attaches a collection of active record to the active database used by this operation with ActiveRecord.attach(ActiveDatabase).
protected  void attach(R record)
          Attaches an active record to the active database used by this operation with ActiveRecord.attach(ActiveDatabase).
protected  void close()
          Cleans up any used resources.
 T execute()
          Executes operate() within a try/catch and transaction.
protected  OperationDatabase getOperationDatabase()
          Gets the database used in execute().
protected  Table<R> getTable()
          Gets the table used in execute().
 boolean isLocalTransaction()
          Reports type of transaction used.
protected abstract  T operate()
          Invoked by execute() to perform the operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ActiveOperation

public ActiveOperation(ActiveTable<R> activeTable,
                       java.lang.String exceptionMessage)
Constructs for a table and fail message.

Parameters:
activeTable - table to operate upon
exceptionMessage - exception message for ActiveException if operation fails
Method Detail

execute

public T execute()
          throws ActiveException
Executes operate() within a try/catch and transaction. If operate() throws an exception, then transaction is rolled back.

Returns:
value returned by operate()
Throws:
ActiveException - if error

isLocalTransaction

public boolean isLocalTransaction()
Reports type of transaction used. If true, then the transaction is valid only while execute() is in progress.

Returns:
true if transaction is for use only by execute()

operate

protected abstract T operate()
                      throws java.lang.Exception
Invoked by execute() to perform the operation. Subclasses must implement.

Returns:
result of operation (Integer, List, etc.)
Throws:
java.lang.Exception

getOperationDatabase

protected OperationDatabase getOperationDatabase()
Gets the database used in execute(). Database is closed when the transaction is closed.

Returns:
database used to by this operation

getTable

protected Table<R> getTable()
Gets the table used in execute(). Table is obtained with OperationDatabase.getTable(Class).

Returns:
table used to by this operation

attach

protected void attach(R record)
Attaches an active record to the active database used by this operation with ActiveRecord.attach(ActiveDatabase).

Parameters:
record - active record to be attached

attach

protected void attach(java.util.Collection<R> records)
Attaches a collection of active record to the active database used by this operation with ActiveRecord.attach(ActiveDatabase).

Parameters:
records - active records to be attached

close

protected void close()
              throws ActiveException
Cleans up any used resources. The most important clean up is closing the JDBC connection if isLocalTransaction() is true. Otherwise JDBC connection will be closed when transaction completes.

Throws:
ActiveException - if error