/*!
  @header FTTransactionManagerImpl
  @abstract Used to globally manager transactions
 
  @availability OS X, GNUstep
  @copyright 2004, 2005, 2006 Free Software Foundation, Inc.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

  <pre>
  -------------------------------------------------------------------------
  Modification history

  26.06.05 ola     initial version
  23.08.06 ola     license changed
  -------------------------------------------------------------------------
  </pre>
*/

#if !defined(__FTTransactionManagerImpl_H)
#define __FTTransactionManagerImpl_H

#include <Foundation/Foundation.h>
#include <FT/FTObject.h>
#include <FT/FTTransaction.h>
#include <FT/FTTransactionImpl.h>
#include <FT/FTTransactionManager.h>
#include <FT/FTTransactionOptimizer.h>

@protocol FTSession;

/*!
 * @class FTTransactionManagerImpl
 * @abstract The transaction manager is a global object used to globally manage
 *   transactions
 */
@interface FTTransactionManagerImpl : FTObject <FTTransactionManager> {
  @private
    NSLock *globalLock;
    NSMutableDictionary *sessionIdToTransactionArray;
    
    /**
     * Array of optimizer for transactions
     */
    NSMutableArray *transactionOptimizers;
}

/*!
 * @method init
 */
- init;


/*!
 * @method dealloc
 */
- (void) dealloc;


/*!
 * @method addTransactionOptimizer
 * @abstract adds an optimizer with the given priority to the set of
 *   optimizers
 * @param optimizerToAdd instance to add
 * @param priority low values represent higher priorities. If an
 *   optimizer already exists with this priority then the given one with this
 *   call will have a higher priority. At present the priority value is not
 *   used and must always equal 0.
 * @result self
 */
- addTransactionOptimizer: (id <FTTransactionOptimizer>) optimizerToAdd
  withPriority: (unsigned) priority;


/*!
 * @method commitTransaction
 * @abstract Commits the given transaction
 * @discussion On successful commitment it removes the transaction
 *   from the stack of transaction of the corresponding session.
 * @param transactionToCommit
 * @result YES if transaction has successfully been commited
 * @throws FTTransactionStepException if a transaction step has thrown an
 *   exception during a commitment or an "undo" operation
 */
- (BOOL) commitTransaction: (id <FTTransaction>) transactionToCommit;


/*!
 * @method createTransactionForSession
 * @abstract opens and returns a new transaction for the specified session.
 * @discussion If there is another transaction already being open then the
 *   the new one will be regarded as a nested one of the already opened 
 *   transaction
 * @result new transaction
 */
- (id <FTTransaction>) createTransactionForSession: (id <FTSession>) session;


/*!
 * @method currentTransactionForSession
 * @abstract Returns the current transaction for the given session
 * @result the current transaction for the given session
 */
- (id <FTTransaction>) currentTransactionForSession: (id <FTSession>) session;

/*!
 * @method optimizeTransaction
 * @abstract Runs all installed transaction optimizer over the given transaction
 *   instance and returns the modified one
 * @discussion Returns the original transaction if no optimizer is installed
 *   or if none of the optimizers has modified it.
 * @param transaction transaction to optimize
 * @result possibily modified transaction
 */
- (id <FTTransaction>) optimizeTransaction: (id <FTTransaction>) transaction;
@end


/*!
 * @class FTTransactionUndoStack
 * @abstract For internal use. It stores steps which may have to perform an
 *   "undo"
 */
@interface FTTransactionUndoStack : FTObject {
  @private
  
  /**
   * Contains all added steps which have not run an "undo" so far
   */
  ECStack *undoSteps;
  
  
  /**
   * Contains all steps which have successfully run an "undo" so far.
   */
  ECStack *performedUndoSteps;
  
  
  /**
   * Contains the current undo step to be performed
   */
  FTTransactionStepAndContext *currentUndoStep;
}

- init;
- (void) dealloc;

/*!
 * @method addPerformedStep
 * @abstract Adds a step which has been performed and which may have to
 *   be run via an "undo".
 * @param stepToAdd step to tadd
 * @result self
 */
- addPerformedStep: (FTTransactionStepAndContext *) stepToAdd;


/*!
 * @method undoAll
 * @abstract runs an "undo" for all steps being added
 * @discussion Note that this implementation throughs <b>any</b> exception
 *   a step throughs. This means that it will simply interrupts the undo 
 *   operation<p/>
 * TODO: This very simple behaviour should at least through an exception
 *   which gives additional information about the undo process and which
 *   wraps the original exception.
 * @result self
 */
- undoAll;
@end

#endif
