/*!
  @header FTTransactionImpl
  @abstract Module of FT
 
  @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

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

#include <FT/FTTransactionImpl.h>
#include <FT/FTTransactionManager.h>
#include <FT/FTLogging.h>

@implementation FTTransactionStepAndContext

- initWithTransactionStep: (id <FTTransactionStep>) aTransactionStep
  withContext: (FTTransactionContext *) aTransactionContext {
  self = [super init];
  
  self->transactionStep = [aTransactionStep retain];
  self->transactionContext = [aTransactionContext retain];
  
  return self;
}


- (void) dealloc {
  if( nil != self->transactionStep ) {
    [self->transactionStep release];
  }
  
  if( nil != self->transactionContext ) {
    [self->transactionContext release];
  }
  
  [super dealloc];
}


- (id <FTTransactionStep>) transactionStep {
  return transactionStep;
}


- (FTTransactionContext *) transactionContext {
  return transactionContext;
}
@end


@implementation FTTransactionImpl

- initForTransactionManager: (id <FTTransactionManager>) aManager {
  self = [super init];
  self->transactionManager = [aManager retain];
  self->transactionStepsAndContexts = [[NSMutableArray alloc] init];
  self->transactionKeyToArrayIndex = [[NSMutableDictionary alloc] init];
  
  return self;
}


- (void) dealloc {
  if( nil != self->transactionManager ) {
    [self->transactionManager release];
  }
  
  if( nil != self->transactionStepsAndContexts ) {
    [self->transactionStepsAndContexts release];
  }
  
  if( nil != self->transactionKeyToArrayIndex ) {
    [self->transactionKeyToArrayIndex release];
  }
  
  [super dealloc];
  return;
}


- (void) abort {
}


- (unsigned) addTransactionStep: (FTTransactionStepAndContext *) stepToAdd {
  unsigned index = [self->transactionStepsAndContexts count];
  
  [self->transactionStepsAndContexts addObject: stepToAdd];
  
  return index;
}


- addTransactionStep: (id <FTTransactionStep>) stepToAdd 
  withContext: (FTTransactionContext *) context {

  [self addTransactionStep: 
    [[FTTransactionStepAndContext alloc] initWithTransactionStep: stepToAdd
      withContext: context]];

  return self;
}


- addTransactionStep: (id <FTTransactionStep>) stepToAdd 
  withContext: (FTTransactionContext *) context
  identifiedByKey: (id <NSObject>) aKey {
  id indexObj;
  
  unsigned index = [self addTransactionStep:
    [[FTTransactionStepAndContext alloc] initWithTransactionStep: stepToAdd
      withContext: context]];
  
  indexObj = [NSNumber numberWithUnsignedInt: index];
  
  if( nil != [self->transactionKeyToArrayIndex objectForKey: indexObj] ) {
    NSString *msg;
    msg = [NSString stringWithFormat:
        @"FTTransactioImpl::addTransactionStep: trying to add a "\
        "transaction for key=\"%@\" although a transaction with this key "\
        "alreday exisrs!", aKey];
    
    [[FTLogging coreLog] fatal: msg];
    
    [[[ECIllegalStateException alloc]
      initWithIllegalStateInfo: msg]
      raise];
  } else {
    [self->transactionKeyToArrayIndex setObject: indexObj forKey: aKey];
  }
  
  return self;
}


- (void) commit {
  [self->transactionManager commitTransaction: self];
}


- (FTTransactionContext *) createContext {
  return [[FTTransactionContext alloc] init];
}


- (id <FTTransactionStep>) transactionStepForKey: (id <NSObject>) aKey {
  FTTransactionStepAndContext *stepAndContext;
  
  NSNumber *index = 
    (NSNumber *) [self->transactionKeyToArrayIndex objectForKey: aKey];
    
  if( nil != index ) {
    stepAndContext = (FTTransactionStepAndContext *)
      [self->transactionStepsAndContexts 
        objectAtIndex: [index unsignedIntValue]];
        
    if( nil != stepAndContext ) {
      return [stepAndContext transactionStep];
    } else {
      [self->transactionKeyToArrayIndex removeObjectForKey: aKey];
      return nil;
    }    
  }
  
  return nil;
}


- (NSMutableArray *) transactionSteps {
  return self->transactionStepsAndContexts;
}
@end
