/******************************************************************************
  @header FTSessionImpl
  @abstract base implementation for session handling
 
  @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

  31.01.2005 ola     initial version
  23.08.2006 ola     license changed
  -------------------------------------------------------------------------
  </pre>
******************************************************************************/

#include <Encore/ECExceptions.h>
#include <FT/FTDefines.h>
#include <FT/FTSessionImpl.h>
#include <FT/FTSessionManagerImpl.h>
#include <FT/FTServerImpl.h>
#include <FT/FTTransactionImpl.h>
#include <FT/FTLogging.h>
#include <FT/FTTransactionManager.h>

@implementation FTSessionImpl

+ (FTSessionImpl *) currentSession {
  FTSessionImpl *toReturn = nil;
  
  return [[[NSThread currentThread] threadDictionary] 
    objectForKey: FT_THREAD_DICT_REF_SESSION];
    
  return toReturn;
}


- initForSessionManager: (FTSessionManagerImpl *) theSessionManager 
 server: (FTServerImpl *) theServer useSessionId: (id) aSessionId {
  self = [super init];
  self->sessionManager = [theSessionManager retain];
  self->server = [theServer retain];
  self->sessionId = [aSessionId retain];
  
  [[[NSThread currentThread] threadDictionary ]
      setValue: self forKey: FT_THREAD_DICT_REF_SESSION];

  [self beginTransactionWithParent: nil withSettings: nil];
    
  return self;
}


- (void) dealloc {
  [self->sessionManager release];
  [self->server release];
  [self->sessionId release];
  
  [super dealloc];
}


- (id <FTTransaction>) beginTransactionWithParent: 
  (id <FTTransaction>) parentTransaction
  withSettings: (id <FTTransactionSettings>) transactionSettings {
  
  return [[self->server transactionManager] createTransactionForSession: self];
}


- close {
  if( [[FTLogging coreLog] isDebugEnabled] ) {    
     [[FTLogging coreLog] debug: @"Closing session..." ];
  }
  
  [[[NSThread currentThread] threadDictionary] 
    removeObjectForKey: FT_THREAD_DICT_REF_SESSION];

  return self;
}


- (FTTransactionImpl *) currentTransaction {
  return (FTTransactionImpl *) [[[self server] transactionManager] 
    currentTransactionForSession: self];
}


- (id <FTObjectToIdMapper>) defaultObjectToIdMapper {
  return [self->server defaultObjectToIdMapper];
}


- (id <FTGraphManager>) graphManager {
  return [self->server graphManager];
}


- (FTServerImpl *) server {
  return self->server;
}


- (id) sessionId {
  return self->sessionId;
}
@end
