| 1 | package SPOPS::Tool::DBI::MaintainLinkedList; | ||
| 2 | |||
| 3 | # $Id: MaintainLinkedList.pm,v 1.5 2004/06/02 00:48:24 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use Log::Log4perl qw( get_logger ); | ||
| 7 | use Data::Dumper qw( Dumper ); | ||
| 8 | use SPOPS; | ||
| 9 | use SPOPS::ClassFactory qw( OK DONE ERROR RULESET_METHOD ); | ||
| 10 | |||
| 11 | use constant HEAD_DEFAULT => 'null'; | ||
| 12 | |||
| 13 | my $log = get_logger(); | ||
| 14 | |||
| 15 | $SPOPS::Tool::DBI::MaintainLinkedList::VERSION = sprintf("%d.%02d", q$Revision: 1.5 $ =~ /(\d+)\.(\d+)/); | ||
| 16 | |||
| 17 | ######################################## | ||
| 18 | # CODE GENERATION BEHAVIOR | ||
| 19 | ######################################## | ||
| 20 | |||
| 21 | sub behavior_factory { | ||
| 22 | my ( $class ) = @_; | ||
| 23 | return { read_code => \&generate_link_methods }; | ||
| 24 | } | ||
| 25 | |||
| 26 | # | ||
| 27 | # Create previous_in_list(), next_in_list() methods | ||
| 28 | |||
| 29 | my $LINK_METHODS = <<'METHODS'; | ||
| 30 | |||
| 31 | # Generate the previous/next links | ||
| 32 | |||
| 33 | sub %%CLASS%%::previous_in_list { | ||
| 34 | my ( $self, $p ) = @_; | ||
| 35 | my $previous_id = $self->{ %%PREVIOUS_ID_FIELD%% }; | ||
| 36 | return $self->fetch( $previous_id, $p ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub %%CLASS%%::next_in_list { | ||
| 40 | my ( $self, $p ) = @_; | ||
| 41 | my $next_id = $self->{ %%NEXT_ID_FIELD%% }; | ||
| 42 | return $self->fetch( $next_id, $p ); | ||
| 43 | } | ||
| 44 | |||
| 45 | METHODS | ||
| 46 | |||
| 47 | sub generate_link_methods { | ||
| 48 | my ( $class ) = @_; | ||
| 49 | my $CONFIG = $class->CONFIG; | ||
| 50 | my $field_prev = $CONFIG->{linklist_previous}; | ||
| 51 | my $field_next = $CONFIG->{linklist_next}; | ||
| 52 | my $methods = $LINK_METHODS; | ||
| 53 | $methods =~ s/%%CLASS%%/$class/g; | ||
| 54 | $methods =~ s/%%PREVIOUS_ID_FIELD%%/$field_prev/g; | ||
| 55 | $methods =~ s/%%NEXT_ID_FIELD%%/$field_next/g; | ||
| 56 | { | ||
| 57 | local $SIG{__WARN__} = sub { return undef }; | ||
| 58 | eval $methods; | ||
| 59 | if ( $@ ) { | ||
| 60 | return ( ERROR, "Cannot generate 'previous_in_list()' and " . | ||
| 61 | "'next_in_list()' methods for object linked " . | ||
| 62 | "list in class [$class]. Error: $@" ); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | return ( OK, undef ); | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | ######################################## | ||
| 70 | # RUNTIME RULES | ||
| 71 | ######################################## | ||
| 72 | |||
| 73 | sub ruleset_factory { | ||
| 74 | my ( $class, $rs_table ) = @_; | ||
| 75 | push @{ $rs_table->{post_save_action} }, \&update_links; | ||
| 76 | push @{ $rs_table->{post_remove_action} }, \&remove_links; | ||
| 77 | $log->is_debug && | ||
| 78 | $log->debug( "Adding rules to [$class] from [", __PACKAGE__, "]" ); | ||
| 79 | return __PACKAGE__; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub update_links { | ||
| 83 | my ( $self, $p ) = @_; | ||
| 84 | return 1 unless ( $p->{is_add} ); | ||
| 85 | return 1 if ( $p->{skip_linklist} ); | ||
| 86 | |||
| 87 | my @bad_params = (); | ||
| 88 | my $field_prev = $self->CONFIG->{linklist_previous}; | ||
| 89 | my $field_next = $self->CONFIG->{linklist_next}; | ||
| 90 | unless ( $field_prev ) { | ||
| 91 | push @bad_params, "[linklist_previous: $field_prev]"; | ||
| 92 | } | ||
| 93 | unless ( $field_next ) { | ||
| 94 | push @bad_params, "[linklist_next: $field_next]"; | ||
| 95 | } | ||
| 96 | |||
| 97 | my $head_type = $self->CONFIG->{linklist_head} || HEAD_DEFAULT; | ||
| 98 | my ( $order_by, $head_value ); | ||
| 99 | if ( $head_type eq 'order' ) { | ||
| 100 | $order_by = $self->CONFIG->{linklist_head_order}; | ||
| 101 | unless ( $order_by ) { | ||
| 102 | push @bad_params, "[linklist_head_order: $order_by]"; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | if ( $head_type eq 'value' ) { | ||
| 106 | $head_value = $self->CONFIG->{linklist_head_value}; | ||
| 107 | unless ( $head_value ) { | ||
| 108 | push @bad_params, "[linklist_head_value: $head_value]"; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | if ( scalar @bad_params ) { | ||
| 114 | $log->warn( "Cannot automatically maintain linked list because field\n", | ||
| 115 | "configuration variables are not available:\n", | ||
| 116 | join( "\n", @bad_params ) ); | ||
| 117 | return 1; | ||
| 118 | } | ||
| 119 | |||
| 120 | # Add framework parameters (resources, etc) that need to be passed | ||
| 121 | # into the save() commands here (this is dirty) | ||
| 122 | |||
| 123 | my %temp_params = ( db => $p->{db} ); | ||
| 124 | |||
| 125 | my $this_id = $self->id; | ||
| 126 | my ( $previous ); | ||
| 127 | if ( $head_type eq 'null' ) { | ||
| 128 | $previous = _linklist_fetch_previous_null( $self, $field_next, \%temp_params ); | ||
| 129 | } | ||
| 130 | elsif ( $head_type eq 'order' ) { | ||
| 131 | $previous = _linklist_fetch_previous_order( $self, $order_by, \%temp_params ); | ||
| 132 | } | ||
| 133 | elsif ( $head_type eq 'value' ) { | ||
| 134 | $previous = _linklist_fetch_previous_value( $self, $field_next, $head_value, \%temp_params ); | ||
| 135 | } | ||
| 136 | return 1 unless ( $previous ); | ||
| 137 | my $previous_id = $previous->id; | ||
| 138 | |||
| 139 | $log->is_debug && | ||
| 140 | $log->debug( "Creating linked list entries for [", ref( $self ), "]", | ||
| 141 | "with ID [$this_id] to previous [$previous_id]" ); | ||
| 142 | |||
| 143 | $previous->{ $field_next } = $this_id; | ||
| 144 | $self->{ $field_prev } = $previous_id; | ||
| 145 | eval { | ||
| 146 | $previous->save( \%temp_params ); | ||
| 147 | $self->save( \%temp_params ); | ||
| 148 | }; | ||
| 149 | if ( $@ ) { | ||
| 150 | $log->warn( "Error maintaining linked list: $@" ); | ||
| 151 | return undef; | ||
| 152 | } | ||
| 153 | $log->is_debug && | ||
| 154 | $log->debug( "Created links ok" ); | ||
| 155 | return 1; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub _linklist_fetch_previous_null { | ||
| 159 | my ( $self, $head_field, $p ) = @_; | ||
| 160 | my $id_field = $self->id_field; | ||
| 161 | return ( $self->fetch_group({ %{ $p }, | ||
| 162 | where => "$id_field != ? and $head_field IS NULL", | ||
| 163 | value => [ $self->id ], | ||
| 164 | limit => 1 }) )->[0]; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub _linklist_fetch_previous_order { | ||
| 168 | my ( $self, $order_by, $p ) = @_; | ||
| 169 | my $id_field = $self->id_field; | ||
| 170 | return ( $self->fetch_group({ %{ $p }, | ||
| 171 | where => "$id_field != ?", | ||
| 172 | value => [ $self->id ], | ||
| 173 | order => $order_by, | ||
| 174 | limit => 1 }) )->[0]; | ||
| 175 | } | ||
| 176 | |||
| 177 | sub _linklist_fetch_previous_value { | ||
| 178 | my ( $self, $head_field, $head_value, $p ) = @_; | ||
| 179 | my $id_field = $self->id_field; | ||
| 180 | return ( $self->fetch_group({ %{ $p }, | ||
| 181 | where => "$id_field != ? AND $head_field = ?", | ||
| 182 | value => [ $self->id, $head_value ], | ||
| 183 | limit => 1 }) )->[0]; | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | sub remove_links { | ||
| 188 | my ( $self, $p ) = @_; | ||
| 189 | return 1 if ( $p->{skip_linklist} ); | ||
| 190 | |||
| 191 | my $field_prev = $self->CONFIG->{linklist_previous}; | ||
| 192 | my $field_next = $self->CONFIG->{linklist_next}; | ||
| 193 | unless ( $field_prev and $field_next ) { | ||
| 194 | $log->warn( "Cannot automatically maintain linked list because certain\n", | ||
| 195 | "configuration variables are not available:\n", | ||
| 196 | "[linklist_previous: $field_prev]\n", | ||
| 197 | "[linklist_next: $field_next]" ); | ||
| 198 | return 1; | ||
| 199 | } | ||
| 200 | |||
| 201 | my $previous = $self->fetch( $self->{ $field_prev }, $p ); | ||
| 202 | my $next = $self->fetch( $self->{ $field_next }, $p ); | ||
| 203 | if ( $previous and $next ) { | ||
| 204 | $previous->{ $field_next } = $next->id; | ||
| 205 | $next->{ $field_prev } = $previous->id; | ||
| 206 | $log->is_debug && | ||
| 207 | $log->debug( "Linking [", ref( $self ), "] ", | ||
| 208 | "[previous: ", $previous->id, "] ", | ||
| 209 | "[next: ", $next->id, "]" ); | ||
| 210 | } | ||
| 211 | elsif ( $previous ) { | ||
| 212 | $previous->{ $field_next } = undef; | ||
| 213 | $log->is_debug && | ||
| 214 | $log->debug( "Linking [", ref( $self ), "] ", | ||
| 215 | "[previous: ", $previous->id, "] ", | ||
| 216 | "[next: n/a]" ); | ||
| 217 | } | ||
| 218 | elsif ( $next ) { | ||
| 219 | $next->{ $field_prev } = undef; | ||
| 220 | $log->is_debug && | ||
| 221 | $log->debug( "Linking [", ref( $self ), "] ", | ||
| 222 | "[previous: n/a] ", | ||
| 223 | "[next: ", $next->id, "]" ); | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | $log->is_debug && | ||
| 227 | $log->debug( "Linking [", ref( $self ), "] ", | ||
| 228 | "[previous: n/a] ", | ||
| 229 | "[next: n/a]" ); | ||
| 230 | } | ||
| 231 | eval { | ||
| 232 | $previous->save( $p ) if ( $previous ); | ||
| 233 | $next->save( $p ) if ( $next );; | ||
| 234 | }; | ||
| 235 | if ( $@ ) { | ||
| 236 | warn "Error maintaining linked list: $@\n"; | ||
| 237 | return undef; | ||
| 238 | } | ||
| 239 | return 1; | ||
| 240 | } | ||
| 241 | |||
| 242 | 1; | ||
| 243 | |||
| 244 | __END__ |