# Relationship.pm -- Represents a relationship between two Entities
# ---------------    inside Crystal Palace.
# September 13, 2000 -- Copyright Sunir Shah. All rights reserved.

package Relationship;

use Element;
@ISA = (Element);

sub SEPARATOR { '!@#'; }

sub _initialize
{
    my ($self, $key) = @_;
    $self->SUPER::_initialize($key);

    if( $key ) {
        my $sep = SEPARATOR;
        $key =~ /(.*)$sep(.*)/o;
        ($a, $b) = ($1, $2);

        $self->{a} = Entity->new($a);
        $self->{b} = Entity->new($b);

        $self->{a}->addRelationship($self);
        $self->{b}->addRelationship($self);
    }

    $self->{type} = '';
}

# generatePrimaryKey()
# --------------------
# Given the primary keys of the two Entitys [sic], this
# will generate the canonical primary key for this Relationship.
sub generatePrimaryKey
{
    my ($a, $b) = @_;
    die 'Must specify two Entity primary keys.' unless $a && $b;

    # Canonical order is lexical order.
    if( $a le $b ) { return $a . SEPARATOR . $b; }
    return $b . SEPARATOR . $a;
}

1;
