| 1 | |||
| 2 | package Xmldoom::Definition::LinkTree; | ||
| 3 | |||
| 4 | use Xmldoom::Definition::Link; | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | sub new | ||
| 8 | { | ||
| 9 | my $class = shift; | ||
| 10 | my $args = shift; | ||
| 11 | |||
| 12 | my $self = | ||
| 13 | { | ||
| 14 | links => { } | ||
| 15 | }; | ||
| 16 | |||
| 17 | bless $self, $class; | ||
| 18 | return $self; | ||
| 19 | } | ||
| 20 | |||
| 21 | sub clear { shift->{links} = { }; } | ||
| 22 | |||
| 23 | sub _add_if_not_present | ||
| 24 | { | ||
| 25 | my ($list, $link) = @_; | ||
| 26 | |||
| 27 | # return if it already exists | ||
| 28 | foreach my $other ( @$list ) | ||
| 29 | { | ||
| 30 | if ( $link->equals( $other ) ) | ||
| 31 | { | ||
| 32 | return; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | push @$list, $link; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub add_link | ||
| 40 | { | ||
| 41 | my ($self, $link) = @_; | ||
| 42 | |||
| 43 | my $table1 = $link->get_start_table_name(); | ||
| 44 | my $table2 = $link->get_end_table_name(); | ||
| 45 | |||
| 46 | # create slots in the links hash | ||
| 47 | if ( not defined $self->{links}->{$table1} ) | ||
| 48 | { | ||
| 49 | $self->{links}->{$table1} = { }; | ||
| 50 | } | ||
| 51 | if ( not defined $self->{links}->{$table1}->{$table2} ) | ||
| 52 | { | ||
| 53 | $self->{links}->{$table1}->{$table2} = [ ]; | ||
| 54 | } | ||
| 55 | if ( not defined $self->{links}->{$table2} ) | ||
| 56 | { | ||
| 57 | $self->{links}->{$table2} = { }; | ||
| 58 | } | ||
| 59 | if ( not defined $self->{links}->{$table2}->{$table1} ) | ||
| 60 | { | ||
| 61 | $self->{links}->{$table2}->{$table1} = [ ]; | ||
| 62 | } | ||
| 63 | |||
| 64 | # add both the forward and backward version | ||
| 65 | _add_if_not_present($self->{links}->{$table1}->{$table2}, $link); | ||
| 66 | _add_if_not_present($self->{links}->{$table2}->{$table1}, $link->clone_reverse()); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub get_links | ||
| 70 | { | ||
| 71 | my ($self, $table1, $table2, $start_columns) = @_; | ||
| 72 | |||
| 73 | if ( not defined $self->{links}->{$table1} ) | ||
| 74 | { | ||
| 75 | return undef; | ||
| 76 | } | ||
| 77 | if ( not defined $table2 ) | ||
| 78 | { | ||
| 79 | return $self->{links}->{$table1}; | ||
| 80 | } | ||
| 81 | if ( not defined $self->{links}->{$table1}->{$table2} ) | ||
| 82 | { | ||
| 83 | return undef; | ||
| 84 | } | ||
| 85 | |||
| 86 | my $links = $self->{links}->{$table1}->{$table2}; | ||
| 87 | |||
| 88 | if ( defined $start_columns ) | ||
| 89 | { | ||
| 90 | my @temp; | ||
| 91 | foreach my $link ( @$links ) | ||
| 92 | { | ||
| 93 | if ( $link->is_start_column_names( $start_columns ) ) | ||
| 94 | { | ||
| 95 | push @temp, $link; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | $links = \@temp; | ||
| 99 | } | ||
| 100 | |||
| 101 | return $links; | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; | ||
| 105 |