/*!
  @header FTServiceTEST
  @abstract Module of FT
 
  @availability OS X, GNUstep
  @copyright 2004, 2005 Free Software Foundation, Inc.
  
  Author: Oliver Langer

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program 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 General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

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

  06.10.05 ola     initial version
  -------------------------------------------------------------------------
  </pre>
*/

#include <FTServiceTEST.h>
#include <FT/FTDictionaryService.h>

@implementation FTServiceTEST
- evaluateNode: (id <FTNode>) aNode {
  id <FTDictionaryService> dictionaryService;
  id <ECIterator> allKeys;

  NSLog( @"Evaluating node with id=%@", [aNode nodeId] );
 
  dictionaryService = (id <FTDictionaryService>) 
    [aNode serviceWithId: @"FTDictionaryService"];
  ECAssertTrue( nil != dictionaryService, @"Unable to get service "\
    "FTDictionaryService!" );
    
  allKeys = [dictionaryService allKeys];

  while( [allKeys hasNext] ) {
    id nextKey = [allKeys next];
    NSLog( @"Found key=%@ with value=%@",
      nextKey, [dictionaryService objectForKey: nextKey]);
  }
  
  [dictionaryService close];

 
  return self;
}


- setObject: (id <NSCoding>) anObject forKey: (id <NSCoding>) aKey
  forNode: (id <FTNode>) aNode {
  id <FTDictionaryService> dictionaryService;

  NSLog( @"KEY=%@", aKey);
  dictionaryService = (id <FTDictionaryService>) 
    [aNode serviceWithId: @"FTDictionaryService"];
  ECAssertTrue( nil != dictionaryService, @"Unable to get service "\
    "FTDictionaryService!" );
    
  [dictionaryService setObject: anObject forKey: aKey ];
  
  [dictionaryService close];
  return self;
}


- objectForKey: (id <NSCoding>) aKey forNode: (id <FTNode>) aNode {
  id <FTDictionaryService> dictionaryService;
  id toReturn = nil;
 
   dictionaryService = (id <FTDictionaryService>) 
    [aNode serviceWithId: @"FTDictionaryService"];
  ECAssertTrue( nil != dictionaryService, @"Unable to get service "\
    "FTDictionaryService!" );
    
  NSLog( @"KEY=%@", aKey);
  toReturn = [dictionaryService objectForKey: aKey ];
  
  [dictionaryService close];

  return toReturn;
}


- testServices {
  id <FTGraphManager> graphManager;
  id <FTGraph> graph;
  id <FTId> graphId;
  id <FTObjectToIdMapper> objectToIdMapper;
  id <FTTransaction> transaction;
  id <FTObjectToIdMapper> graphIdMapper;
  id <FTId> nid;
  id <FTNode> node;
  id <ECIterator> nodeIterator;
  NSString *value;
  
  NSLog( @"FTServiceTEST::testServices: BEGINNING..." );
  EC_AUTORELEASEPOOL_BEGIN
  objectToIdMapper = [session defaultObjectToIdMapper];
  graphManager = [session graphManager];

  NSLog( @"Now creating graph..." );
  transaction = [session beginTransactionWithParent: nil
    withSettings: nil];
  
  graphId = [objectToIdMapper mapObject: @"serviceTestGraph"];
  graph = [graphManager createGraphWithId: graphId];
  ECAssertTrue( nil != graph, @"Graph creation failed!" );
  
  graphIdMapper = [[graph objectToIdMapper] retain];

  nid = [graphIdMapper mapObject: @"nodeA"];
  node = [graph createNodeWithId: nid];
  
  NSLog( @"FTServiceTEST::testServices: Adding entries..." );
  [self setObject: @"testValue1" forKey: @"testKey1" forNode: node];

  nid = [graphIdMapper mapObject: @"nodeB"];
  node = [graph createNodeWithId: nid];
  [self setObject: @"testValue1ForB" forKey: @"testKey1" forNode: node];

  [transaction commit];
  [graph close];

  /* find objects by their keys: */
  graphId = [objectToIdMapper mapObject: @"serviceTestGraph"];
  graph = [graphManager graphWithId: graphId];
  graphIdMapper = [[graph objectToIdMapper] retain];
  
  NSLog( @"Now reading graph..." );
  transaction = [session beginTransactionWithParent: nil
    withSettings: nil];
    
  nid = [graphIdMapper mapObject: @"nodeA"];
  node = [graph nodeWithId: nid];  
  value = [self objectForKey: @"testKey1" forNode: node];
  
  NSLog( @"FTServiceTEST::testServices: Got \"%@\" for key=\"testkey1\"",
    value );
  ECAssertTrue( [value isEqual: @"testValue1"], @"Values differ!" );

  nid = [graphIdMapper mapObject: @"nodeB"];
  node = [graph nodeWithId: nid];  
  value = [self objectForKey: @"testKey1" forNode: node];

  NSLog( @"FTServiceTEST::testServices: Got \"%@\" for key=\"testkey1\"",
    value );
  ECAssertTrue( [value isEqual: @"testValue1ForB"], @"Values differ!" );
  
  NSLog( @"FTServiceTEST::testServices: Closing FTDictionaryService..." );
  [transaction commit];
  [graph close];
  
  EC_AUTORELEASEPOOL_END

  /* find objects indirectly using the key iterator: */
  EC_AUTORELEASEPOOL_BEGIN
  NSLog( @"Now using key iterator..." );

  graphId = [objectToIdMapper mapObject: @"serviceTestGraph"];
  graph = [graphManager graphWithId: graphId];
  graphIdMapper = [[graph objectToIdMapper] retain];
  
  transaction = [session beginTransactionWithParent: nil
    withSettings: nil];

  nodeIterator = [graph nodeIterator];
  while( [nodeIterator hasNext] ) {
    [self evaluateNode: [nodeIterator next]];
  }

  [transaction commit];
  [graph close];
  EC_AUTORELEASEPOOL_END
  NSLog( @"FTServiceTEST::testServices: FINISHED!" );
  return self;
}
@end
