/*!
  @header FTSystemDictionary
  @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

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

#include <Encore/Encore.h>
#include <FT/FTDefines.h>
#include <FT/FTSystemDictionary.h>
#include <FT/FTServer.h>
#include <FT/FTLogging.h>

/*!
 * @macro __FT_SYSDICT_GRAPH_CREATION_COUNTER
 * abstract Entry for storing the graph creation counter
 */
#define __FT_SYSDICT_GRAPH_CREATION_COUNTER @"@graphCreationCounter"



@implementation FTSystemDictionary

- initWithDictionaryProvider: (id <FTDictionaryProvider>) aDictionaryProvider
  forServer: (id <FTServer>) aServer {
  self = [super init];
  self->dictionaryProvider = [aDictionaryProvider retain];
  self->server = [aServer retain];

  return self;
}


- (void) dealloc {
  [self->dictionaryProvider release];
  if( nil != self->server ) {
    [self->server release];
  }

  [super dealloc];
}


- (NSString *) newUniqueNameWithScheme: (NSString *) schemeInfoEntry
  counterName: (NSString *) aCounterName increment: (int) increment {
  NSString *newDatabaseName = nil;
  int counterValue;
  
  NSString *nameScheme = [[[self->server config]
    databaseNames]
      databaseNameForEntry: schemeInfoEntry];
	
  if( nil == nameScheme ) {
    [[[ECIllegalArgumentException alloc] 
	  initWithArgumentInfo: [[NSString alloc] initWithFormat:
	    @"Unable to find info.plist entry %@", schemeInfoEntry ]] raise];
  }
  
  counterValue = [self 
    updateCounterWithName: aCounterName increment: increment];
	
  newDatabaseName = [[NSString alloc] initWithFormat: nameScheme,
    counterValue];

  return newDatabaseName;
}


- (NSString *) newGraphDatabaseName {
  return [self newUniqueNameWithScheme: FT_IP_GRAPH_DBNAME_SCHEME
    counterName: __FT_SYSDICT_GRAPH_CREATION_COUNTER increment: 1 ];
}



- setup {
  NSNumber *number;
  
  EC_AUTORELEASEPOOL_BEGIN
  if( [[FTLogging coreLog] isInfoEnabled] ) {    
    [[FTLogging coreLog]
      info: @"FTSystemDictionary::setup: Setting up system dictionary..." ];
  }
  
  /*
   * create the graph creation counter
   */
  number = [[[NSNumber alloc] initWithInt: 0] autorelease];
  [self->dictionaryProvider setObject: number 
    forKey: __FT_SYSDICT_GRAPH_CREATION_COUNTER];
  
  EC_AUTORELEASEPOOL_END
  return self;
}


- (int) updateCounterWithName: (NSString *) aName increment: (int) increment {
   NSNumber *counter;
  int counterValue;

  EC_AUTORELEASEPOOL_BEGIN
  
  NS_DURING
    counter = [self->dictionaryProvider 
      objectForKey:  aName];
  NS_HANDLER
    [[[ECIllegalStateException alloc] 
	  initWithIllegalStateInfo: [[NSString alloc] initWithFormat: 
	    @"FTSystemDictionary::updateCounterWithName: Unable "\
	  "to fetch counter %@. Has the system dictionary being set up "\
	  "before?", aName]] raise];
  NS_ENDHANDLER
  
  if( nil == counter ) {
    [[[ECIllegalStateException alloc] 
	  initWithIllegalStateInfo: [[NSString alloc] initWithFormat:
	    @"FTSystemDictionary::newGraphDatabaseName: Unable "\
	  "to fetch counter %@. Has the system dictionary being set up "\
	  "before?", aName ]] raise];
  }
  // [counter autorelease] ?
  /* increment this counter */
  counterValue = [counter intValue];
  counterValue++;
  
  /*
   * Try to store this counter before:
   */
  NS_DURING
    counter = [[[NSNumber alloc] initWithInt: counterValue]
	  autorelease];
	  
    [self->dictionaryProvider setObject: counter
	  forKey: aName];
  NS_HANDLER
	[[[ECIllegalStateException alloc] 
	  initWithIllegalStateInfo: [[NSString alloc] initWithFormat: 
	    @"FTSystemDictionary::newGraphDatabaseName: Unable "\
	  "to update counter %@. Has the system dictionary being set up "\
	  "before?", aName] caughtException: localException] raise];
  NS_ENDHANDLER
  
  EC_AUTORELEASEPOOL_END

  return counterValue;
}
@end
