| 1 | package SPOPS::Loopback; | ||
| 2 | |||
| 3 | # $Id: Loopback.pm,v 3.11 2004/06/02 00:48:21 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( SPOPS ); | ||
| 7 | use Data::Dumper qw( Dumper ); | ||
| 8 | use Log::Log4perl qw( get_logger ); | ||
| 9 | use SPOPS::Secure qw( :level ); | ||
| 10 | |||
| 11 | $SPOPS::Loopback::VERSION = sprintf("%d.%02d", q$Revision: 3.11 $ =~ /(\d+)\.(\d+)/); | ||
| 12 | |||
| 13 | my $log = get_logger(); | ||
| 14 | |||
| 15 | # Save objects here, indexed by ID. | ||
| 16 | |||
| 17 | my %BY_ID = (); | ||
| 18 | |||
| 19 | sub fetch { | ||
| 20 | my ( $class, $id, $p ) = @_; | ||
| 21 | $log->is_info && | ||
| 22 | $log->info( "Trying to fetch '$class' with ID '$id'" ); | ||
| 23 | return undef unless ( $class->pre_fetch_action( $id ) ); | ||
| 24 | $log->is_info && | ||
| 25 | $log->info( "The 'pre_fetch_action' check ran ok" ); | ||
| 26 | my $level = SEC_LEVEL_WRITE; | ||
| 27 | if ( ! $p->{skip_security} and $class->isa( 'SPOPS::Secure' ) ) { | ||
| 28 | $level = $class->check_action_security({ id => $id, | ||
| 29 | required => SEC_LEVEL_READ }); | ||
| 30 | } | ||
| 31 | $log->is_info && | ||
| 32 | $log->info( "The security check ran ok" ); | ||
| 33 | |||
| 34 | my ( $object ); | ||
| 35 | if ( exists $BY_ID{ $class }->{ $id } ) { | ||
| 36 | $log->is_info && | ||
| 37 | $log->info( "Object exists, creating from data" ); | ||
| 38 | $object = $class->new( $BY_ID{ $class }->{ $id } ) | ||
| 39 | } | ||
| 40 | else { | ||
| 41 | $log->is_info && | ||
| 42 | $log->info( "Object doesn't exist, creating an empty one" ); | ||
| 43 | $object = $class->new({ id => $id }); | ||
| 44 | } | ||
| 45 | $object->{tmp_security_level} = $level; | ||
| 46 | return undef unless ( $object->post_fetch_action ); | ||
| 47 | $log->is_info && | ||
| 48 | $log->info( "The 'post_fetch_action' check ran ok" ); | ||
| 49 | $object->has_save; | ||
| 50 | $object->clear_change; | ||
| 51 | $log->is_info && | ||
| 52 | $log->info( "Set object saved and changed flags ok" ); | ||
| 53 | return $object; | ||
| 54 | } | ||
| 55 | |||
| 56 | |||
| 57 | sub fetch_group { | ||
| 58 | my ( $class, $params ) = @_; | ||
| 59 | $params ||= {}; | ||
| 60 | my @id_list = (); | ||
| 61 | if ( scalar keys %{ $BY_ID{ $class } } == 0 ) { | ||
| 62 | @id_list = ( 1 .. 15 ); # make up some stuff... | ||
| 63 | } | ||
| 64 | elsif ( $params->{where} ) { | ||
| 65 | $log->is_info && | ||
| 66 | $log->info( "Fetching objects with [$params->{where}]" ); | ||
| 67 | my @values = ( ref $params->{value} ) ? @{ $params->{value} } : (); | ||
| 68 | my ( $field, $value ) = split /\s*=\s*/, $params->{where}; | ||
| 69 | if ( $value eq '?' ) { | ||
| 70 | $value = shift @values; | ||
| 71 | } | ||
| 72 | else { | ||
| 73 | $value =~ s/[\'\"]//g; | ||
| 74 | } | ||
| 75 | my $id_field = $class->id_field; | ||
| 76 | foreach my $id ( sort keys %{ $BY_ID{ $class } } ) { | ||
| 77 | my $data = $BY_ID{ $class }->{ $id }; | ||
| 78 | if ( exists $data->{ $field } and $data->{ $field } eq $value ) { | ||
| 79 | push @id_list, $id; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | @id_list = sort keys %{ $BY_ID{ $class } } | ||
| 85 | } | ||
| 86 | $log->is_info && | ||
| 87 | $log->info( "Trying to fetch objects: ", join( ', ', @id_list ) ); | ||
| 88 | return [ map { $class->fetch( $_ ) } @id_list ]; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub fetch_iterator { | ||
| 93 | my ( $class, $params ) = @_; | ||
| 94 | my $items = $class->fetch_group( $params ); | ||
| 95 | require SPOPS::Iterator::WrapList; | ||
| 96 | return SPOPS::Iterator::WrapList->new({ object_list => $items }); | ||
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | sub save { | ||
| 101 | my ( $self, $p ) = @_; | ||
| 102 | $log->is_info && | ||
| 103 | $log->info( "Trying to save object '", $self->id, "'" ); | ||
| 104 | $p ||= {}; | ||
| 105 | unless ( $self->pre_save_action({ is_add => $self->is_saved }) ) { | ||
| 106 | $log->error( "Failed the 'pre_save_action'" ); | ||
| 107 | return undef; | ||
| 108 | } | ||
| 109 | if ( $self->is_saved ) { | ||
| 110 | if ( ! $p->{skip_security} and $self->isa( 'SPOPS::Secure' ) ) { | ||
| 111 | $self->check_action_security({ required => SEC_LEVEL_WRITE }); | ||
| 112 | } | ||
| 113 | $log->is_info && | ||
| 114 | $log->info( "Passed security check ok" ); | ||
| 115 | |||
| 116 | } | ||
| 117 | else { | ||
| 118 | $self->id( $self->pre_fetch_id ) unless ( $self->id ); | ||
| 119 | $self->id( $self->post_fetch_id ) unless ( $self->id ); | ||
| 120 | $log->is_info && | ||
| 121 | $log->info( "Assigned ID '", $self->id, "'" ); | ||
| 122 | } | ||
| 123 | |||
| 124 | my %data = %{ $self->as_data_only }; | ||
| 125 | $BY_ID{ ref( $self ) }->{ $self->id } = \%data; | ||
| 126 | $log->is_debug && | ||
| 127 | $log->debug( "Saved new object: ", Dumper( \%data ) ); | ||
| 128 | unless ( $self->is_saved or $p->{skip_security} ) { | ||
| 129 | #warn "Calling create_initial_security()\n"; | ||
| 130 | $self->create_initial_security; | ||
| 131 | } | ||
| 132 | unless ( $self->post_save_action ) { | ||
| 133 | $log->is_info && | ||
| 134 | $log->info( "Failed the 'post_save_action'" ); | ||
| 135 | return undef; | ||
| 136 | } | ||
| 137 | $self->has_save; | ||
| 138 | $self->clear_change; | ||
| 139 | return $self; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Other items in ISA will override, otherwise we want to prevent a | ||
| 143 | # warning when doing strict field checking | ||
| 144 | |||
| 145 | sub pre_fetch_id { return undef } | ||
| 146 | sub post_fetch_id { return undef } | ||
| 147 | |||
| 148 | sub remove { | ||
| 149 | my ( $self ) = @_; | ||
| 150 | return undef unless ( $self->pre_remove_action ); | ||
| 151 | delete $BY_ID{ ref( $self ) }->{ $self->id }; | ||
| 152 | return undef unless ( $self->post_remove_action ); | ||
| 153 | return 1 | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | sub peek { | ||
| 158 | my ( $class, $id, $field ) = @_; | ||
| 159 | return undef unless ( exists $BY_ID{ $class }->{ $id } ); | ||
| 160 | return $BY_ID{ $class }->{ $id }{ $field } | ||
| 161 | } | ||
| 162 | |||
| 163 | 1; | ||
| 164 | |||
| 165 | __END__ |