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

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

#include <FT/FTEdgeImpl.h>
#include <FT/FTGraphImpl.h>
#include <FT/FTSessionImpl.h>

@implementation FTEdgeImpl
- (id) initWithCoder:(NSCoder *) decoder {
  id <FTId> graphId = nil;
  id correspondingGraph = nil;
  FTSessionImpl *session;
  
  NS_DURING
    self->edgeId = [[decoder decodeObject] retain];
    self->targetNodeId = [[decoder decodeObject] retain];
    self->sourceNodeId = [[decoder decodeObject] retain];
    graphId = [[decoder decodeObject] retain];
    /**
     * now determine graph based on its id:
     */
  
    session = [FTSessionImpl currentSession];  
    correspondingGraph = [[session graphManager] graphWithId: graphId];
    NSAssert1( nil != correspondingGraph, @"FTNodeImpl::initWithCoder: Unable "\
      "to fetch graph with id=\"%@\" for the previously loaded nodes!", 
       graphId );
    
    self->graph = (FTGraphImpl *) [correspondingGraph retain];
  NS_HANDLER
    if( nil != graphId ) {
      [graphId release];
    }
    
    [localException raise];
  NS_ENDHANDLER
  
  return self;
}


- initWithEdgeId: (id <FTId>) anId targetNode: (id <FTNode>) aTargetNode
  sourceNode: (id <FTNode>) aSourceNode forGraph: (FTGraphImpl *) aGraph {
  self = [super init];

  self->graph = aGraph;
  
  self->edgeId = [anId retain];
  self->targetNodeId = [[aTargetNode nodeId] retain];
  self->sourceNodeId = [[aSourceNode nodeId] retain];

  return self;
}
  
  
- (void) dealloc {
  [self->edgeId release];
  [self->targetNodeId release];
  [self->sourceNodeId release];

  [super dealloc];
}


- (NSString *) description {
  return [[NSString alloc] initWithFormat: @"{FTEdgeImpl, edgeId=%@, "\
    "sourceNodeId=%@, targetNodeId=%@", self->edgeId, self->sourceNodeId,
    self->targetNodeId];
}


- (id <FTId>) edgeId {
  return self->edgeId;
}


- (void) encodeWithCoder: (NSCoder *) encoder {
  [encoder encodeObject: self->edgeId];
  [encoder encodeObject: self->targetNodeId];
  [encoder encodeObject: self->sourceNodeId];
  [encoder encodeObject: [self->graph graphId]];
}


- (unsigned) hash {
  if( nil == self->edgeId ) {
    return [super hash];
  } else {
    return [self->edgeId hash];
  }
}


- (BOOL) isEqual: (id)anObject {
  BOOL toReturn = NO;
  
  if( self == anObject ) {
    toReturn = YES;
  } else {
    if( nil == anObject ) {
      toReturn = NO;
    } else {
      if( [anObject isKindOfClass: [self class]] ) {
        [((FTEdgeImpl *) anObject)->edgeId isEqual: self->edgeId];
      } else {
        toReturn = NO;
      }
    }
  }

  return toReturn;
}


- (id <FTNode>) sourceNode {
  return [self->graph nodeWithId: self->sourceNodeId];
}


- (id <FTId>) sourceNodeId {
  return self->sourceNodeId;
}


- (id <FTNode>) targetNode {
  return [self->graph nodeWithId: self->targetNodeId];
} 


- (id <FTId>) targetNodeId {
  return self->targetNodeId;
}

@end
