moved some stuff out to config
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 14 Jul 2006 20:14:42 +0000 (20:14 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Fri, 14 Jul 2006 20:14:42 +0000 (20:14 +0000)
implementing more methods

git-svn-id: svn://svn.open-ils.org/ILS/trunk@5014 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/SIP.pm
Open-ILS/src/perlmods/OpenILS/SIP/Item.pm
Open-ILS/src/perlmods/OpenILS/SIP/Patron.pm
Open-ILS/src/perlmods/OpenILS/SIP/Transaction.pm
Open-ILS/src/perlmods/OpenILS/SIP/Transaction/Checkin.pm

index 53b767b..e062297 100644 (file)
@@ -12,11 +12,7 @@ use OpenILS::SIP::Patron;
 use OpenILS::SIP::Transaction;
 use OpenILS::SIP::Transaction::Checkout;
 use OpenILS::SIP::Transaction::Checkin;
-#use OpenILS::SIP::Transaction::FeePayment;
-#use OpenILS::SIP::Transaction::Hold;
 use OpenILS::SIP::Transaction::Renew;
-#use OpenILS::SIP::Transaction::RenewAll;
-
 
 use OpenSRF::System;
 use OpenILS::Utils::Fieldmapper;
@@ -24,30 +20,26 @@ use OpenSRF::Utils::SettingsClient;
 use OpenILS::Application::AppUtils;
 my $U = 'OpenILS::Application::AppUtils';
 
-use Digest::MD5 qw(md5_hex);
-
-# PUT ME IN THE CONFIG XXX
-my %supports = (
-       'magnetic media'                => 1,
-       'security inhibit'      => 0,
-       'offline operation'     => 0
-       );
+my $editor;
+my $config;
 
+use Digest::MD5 qw(md5_hex);
 
 sub new {
        my ($class, $institution, $login) = @_;
        my $type = ref($class) || $class;
        my $self = {};
 
+       $config = $institution;
        syslog("LOG_DEBUG", "new ILS '%s'", $institution->{id});
        $self->{institution} = $institution;
 
-       my $config = $institution->{implementation_config}->{bootstrap};
+       my $bsconfig = $institution->{implementation_config}->{bootstrap};
 
-       syslog('LOG_DEBUG', "loading bootstrap config: $config");
+       syslog('LOG_DEBUG', "loading bootstrap config: $bsconfig");
        
        local $/ = "\n";
-       OpenSRF::System->bootstrap_client(config_file => $config);
+       OpenSRF::System->bootstrap_client(config_file => $bsconfig);
        syslog('LOG_DEBUG', "bootstrap loaded..");
 
        $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
@@ -62,6 +54,37 @@ sub new {
        return $self;
 }
 
+sub to_bool {
+       my $val = shift;
+       return ($val and $val =~ /true/io);
+}
+
+sub editor {
+       $editor = make_editor() unless $editor;
+       return $editor;
+}
+sub reset_editor {
+       $editor = undef;
+       return editor();
+}
+
+
+# Creates the global editor object
+sub make_editor {
+       require OpenILS::Utils::CStoreEditor;
+       my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
+       # gnarly cstore hack to re-gen autogen methods after IDL is loaded
+       if(!UNIVERSAL::can($e, 'search_actor_card')) {
+               syslog("LOG_WARNING", "Reloading CStoreEditor...");
+               delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
+               require OpenILS::Utils::CStoreEditor;
+               $e = OpenILS::Utils::CStoreEditor->new(xact =>1);
+       }
+       return $e;
+}
+
+
+
 sub login {
        my( $self, $username, $password ) = @_;
        syslog('LOG_DEBUG', "OpenILS: Logging in with username $username");
@@ -80,15 +103,14 @@ sub login {
                }
        );
 
-       my $key;
-       if( ref($response) eq 'HASH' and $response->{payload} 
-               and $key = $response->{payload}->{authtoken} ) {
-               syslog('LOG_INFO', "OpenILS: Login succeeded for $username : authkey = $key");
-
-       } else {
-               syslog('LOG_WARN', "OpenILS: Login failed for $username");
+       if( my $code = $U->event_code($response) ) {
+               my $txt = $response->{textcode};
+               syslog('LOG_WARN', "OpenILS: Login failed for $username.  $txt:$code");
+               return undef;
        }
 
+       my $key = $response->{payload}->{authtoken};
+       syslog('LOG_INFO', "OpenILS: Login succeeded for $username : authkey = $key");
        return $self->{authtoken} = $key;
 }
 
@@ -107,45 +129,48 @@ sub find_item {
 
 sub institution {
     my $self = shift;
-
     return $self->{institution}->{id};
 }
 
-
-# XXX Get me from the config?
 sub supports {
-    my ($self, $op) = @_;
-    return exists($supports{$op}) ? $supports{$op} : 0;
+       my ($self, $op) = @_;
+       my ($i) = grep { $_->{name} eq $op }  
+               @{$config->{implementation_config}->{supports}->{item}};
+       return to_bool($i->{value});
 }
 
 sub check_inst_id {
-    my ($self, $id, $whence) = @_;
-
-    if ($id ne $self->{institution}->{id}) {
-       syslog("LOG_WARNING", "%s: received institution '%s', expected '%s'",
-              $whence, $id, $self->{institution}->{id});
-    }
+       my ($self, $id, $whence) = @_;
+       if ($id ne $self->{institution}->{id}) {
+               syslog("LOG_WARNING", 
+                       "%s: received institution '%s', expected '%s'",
+                       $whence, $id, $self->{institution}->{id});
+       }
 }
 
-
-# XXX by default, these should come from the config
 sub checkout_ok {
-    return 1;
+       return to_bool($config->{policy}->{checkout});
 }
 
 sub checkin_ok {
+       return to_bool($config->{policy}->{checkin});
     return 0;
 }
 
+sub renew_ok {
+       return to_bool($config->{policy}->{renew});
+}
+
 sub status_update_ok {
-    return 1;
+       return to_bool($config->{policy}->{status_update});
 }
 
 sub offline_ok {
-    return 0;
+       return to_bool($config->{policy}->{offline});
 }
 
 
+
 ##
 ## Checkout(patron_id, item_id, sc_renew):
 ##    patron_id & item_id are the identifiers send by the terminal
@@ -161,8 +186,8 @@ sub checkout {
        syslog('LOG_DEBUG', "OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
        
        my $xact                = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
-       my $patron      = OpenILS::SIP::Patron->new($patron_id);
-       my $item                = OpenILS::SIP::Item->new($item_id);
+       my $patron      = $self->find_patron($patron_id);
+       my $item                = $self->find_item($item_id);
        
        $xact->patron($patron);
        $xact->item($item);
@@ -196,13 +221,13 @@ sub checkout {
 
        if( $xact->ok ) {
 
-               $xact->editor->commit;
+               #editor()->commit;
                syslog("LOG_DEBUG", "OpenILS::Checkout: " .
                        "patron %s checkout %s succeeded", $patron_id, $item_id);
 
        } else {
 
-               $xact->editor->xact_rollback;
+               #editor()->xact_rollback;
                syslog("LOG_DEBUG", "OpenILS::Checkout: " .
                        "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
        }
@@ -219,23 +244,29 @@ sub checkin {
        
        my $patron;
        my $xact                = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
-       my $item                = OpenILS::SIP::Item->new($item_id);
+       my $item                = $self->find_item($item_id);
 
        $xact->item($item);
 
+       if(!$xact->item) {
+               $xact->screen_msg("Invalid item barcode: $item_id");
+               $xact->ok(0);
+               return $xact;
+       }
+
        $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
        
        if ($xact->ok) {
 
-               $xact->patron($patron = OpenILS::SIP::Patron->new($item->{patron}));
+               $xact->patron($patron = $self->find_patron($item->{patron}));
                delete $item->{patron};
                delete $item->{due_date};
                syslog('LOG_INFO', "OpenILS: Checkin succeeded");
-               $xact->editor->commit;
+               #editor()->commit;
 
        } else {
 
-               $xact->editor->xact_rollback;
+               #editor()->xact_rollback;
                syslog('LOG_WARNING', "OpenILS: Checkin failed");
        }
        # END TRANSACTION
@@ -243,9 +274,6 @@ sub checkin {
        return $xact;
 }
 
-
-
-
 ## If the ILS caches patron information, this lets it free it up
 sub end_patron_session {
     my ($self, $patron_id) = @_;
@@ -435,8 +463,8 @@ sub renew {
                $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
 
        my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
-       $trans->patron(OpenILS::SIP::Patron->new($patron_id));
-       $trans->item(OpenILS::SIP::Item->new($item_id));
+       $trans->patron($self->find_patron($patron_id));
+       $trans->item($self->find_item($item_id));
 
        if(!$trans->patron) {
                $trans->screen_msg("Invalid patron barcode.");
index 199bda1..85e7f18 100644 (file)
@@ -5,12 +5,11 @@
 #
 
 package OpenILS::SIP::Item;
-
-use strict;
-use warnings;
+use strict; use warnings;
 
 use Sys::Syslog qw(syslog);
 
+use OpenILS::SIP;
 use OpenILS::SIP::Transaction;
 use OpenILS::Application::AppUtils;
 my $U = 'OpenILS::Application::AppUtils';
@@ -20,22 +19,15 @@ my %item_db;
 sub new {
     my ($class, $item_id) = @_;
     my $type = ref($class) || $class;
-    my $self = {};
-    bless $self, $type;
-
-       require OpenILS::Utils::CStoreEditor;
-       my $e = OpenILS::Utils::CStoreEditor->new;
+    my $self = bless( {}, $type );
 
-       if(!UNIVERSAL::can($e, 'search_actor_card')) {
-               syslog("LOG_WARNING", "Reloading CStoreEditor...");
-               delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
-               require OpenILS::Utils::CStoreEditor;
-               $e = OpenILS::Utils::CStoreEditor->new;
-       }
+       syslog('LOG_DEBUG', "Loading item $item_id...");
+       return undef unless $item_id;
 
+       my $e = OpenILS::SIP->editor();
 
         # FLESH ME
-        my $copy = $e->search_asset_copy(
+       my $copy = $e->search_asset_copy(
                [
                        { barcode => $item_id },
                        {
@@ -48,25 +40,20 @@ sub new {
                ]
        );
 
-       if(!@$copy) {
+       $copy = $$copy[0];
+
+       if(!$copy) {
                syslog("LOG_DEBUG", "OpenILS: Item '%s' : not found", $item_id);
                return undef;
-    }
-
-       $copy = $$copy[0];
+       }
 
-        # XXX See if i am checked out, if so set $self->{patron} to the user's barcode
        my ($circ) = $U->fetch_open_circulation($copy->id);
        if($circ) {
+               # if i am checked out, set $self->{patron} to the user's barcode
                my $user = $e->retrieve_actor_user(
                        [
                                $circ->usr,
-                               {
-                                       flesh => 1,
-                                       flesh_fields => {
-                                               "au" => [ 'card' ],
-                                       }
-                               }
+                               { flesh => 1, flesh_fields => { "au" => [ 'card' ] } }
                        ]
                );
 
@@ -81,13 +68,11 @@ sub new {
        $self->{copy}           = $copy;
        $self->{volume} = $copy->call_number;
        $self->{record} = $copy->call_number->record;
-       
-       $self->{mods}   = $U->record_to_mvr($self->{record}) if $self->{record}->marc;
+       $self->{mods}           = $U->record_to_mvr($self->{record}) if $self->{record}->marc;
 
-    syslog("LOG_DEBUG", "new OpenILS Item('%s'): found with title '%s'",
-          $item_id, $self->title_id);
+       syslog("LOG_DEBUG", "Item('$item_id'): found with title '%s'", $self->title_id);
 
-    return $self;
+       return $self;
 }
 
 sub magnetic {
@@ -107,7 +92,7 @@ sub sip_item_properties {
 
 sub status_update {
     my ($self, $props) = @_;
-    my $status = new OpenILS::SIP::Transaction;
+    my $status = OpenILS::SIP::Transaction->new;
     $self->{sip_item_properties} = $props;
     $status->{ok} = 1;
     return $status;
index af42062..194a73b 100644 (file)
@@ -14,6 +14,7 @@ use Sys::Syslog qw(syslog);
 use Data::Dumper;
 use Digest::MD5 qw(md5_hex);
 
+use OpenILS::SIP;
 use OpenILS::Application::AppUtils;
 use OpenILS::Application::Actor;
 my $U = 'OpenILS::Application::AppUtils';
@@ -31,21 +32,12 @@ sub new {
 
        syslog("LOG_DEBUG", "new OpenILS Patron(%s): searching...", $patron_id);
 
-       require OpenILS::Utils::CStoreEditor;
-       my $e = OpenILS::Utils::CStoreEditor->new;
+       my $e = OpenILS::SIP->editor();
 
-       if(!UNIVERSAL::can($e, 'search_actor_card')) {
-               syslog("LOG_WARNING", "Reloading CStoreEditor...");
-               delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
-               require OpenILS::Utils::CStoreEditor;
-               $e = OpenILS::Utils::CStoreEditor->new;
-       }
-
-
-        my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
-        my $user;
+       my $c = $e->search_actor_card({barcode => $patron_id}, {idlist=>1});
+       my $user;
 
-        if( @$c ) {
+       if( @$c ) {
 
                $user = $e->search_actor_user(
                        [
@@ -311,6 +303,7 @@ sub __hold_to_title {
 
 sub __copy_to_title {
        my( $e, $copy ) = @_;
+       syslog('LOG_DEBUG', "copy_to_title(%s)", $copy->id);
        return $copy->dummy_title if $copy->call_number == -1;  
        my $vol = $e->retrieve_asset_call_number($copy->call_number);
        return __volume_to_title($e, $vol);
@@ -319,12 +312,14 @@ sub __copy_to_title {
 
 sub __volume_to_title {
        my( $e, $volume ) = @_;
+       syslog('LOG_DEBUG', "volume_to_title(%s)", $volume->id);
        return __record_to_title($e, $volume->record);
 }
 
 
 sub __record_to_title {
        my( $e, $title_id ) = @_;
+       syslog('LOG_DEBUG', "record_to_title($title_id)");
        my $mods = $U->simplereq(
                'open-ils.search',
                'open-ils.search.biblio.record.mods_slim.retrieve', $title_id );
@@ -333,6 +328,7 @@ sub __record_to_title {
 
 sub __metarecord_to_title {
        my( $e, $m_id ) = @_;
+       syslog('LOG_DEBUG', "metarecord_to_title($m_id)");
        my $mods = $U->simplereq(
                'open-ils.search',
                'open-ils.search.biblio.metarecord.mods_slim.retrieve', $m_id);
@@ -464,7 +460,8 @@ sub block {
        # stay in synch
        $self->{user}->alert_message( $note );
 
-       $e->finish; # commits and resets
+       $e->commit; # commits and resets
+       $self->{editor} = OpenILS::SIP->reset_editor();
        return $self;
 }
 
index 1d76939..e2999f8 100644 (file)
@@ -8,6 +8,8 @@ use Carp;
 use strict; use warnings;
 use Sys::Syslog qw(syslog);
 
+use OpenILS::SIP;
+
 
 my %fields = (
              ok            => 0,
@@ -27,55 +29,6 @@ my %fields = (
 
 our $AUTOLOAD;
 
-# returns the global transaction pointer
-#sub get_xact {
-#      my $class = shift;
-#      return $XACT;
-#}
-#
-#sub session {
-#      my( $self, $session ) = @_;
-#      $self->{session} = $session if $session;
-#      return $self->{session};
-#}
-#
-#
-#sub create_session {
-#      my( $self, $patron ) = @_;
-#      $self->commit_session if $self->session_is_alive;
-#      require OpenILS::Utils::CStoreEditor;
-#      return $self->{session} = {
-#              editor => OpenILS::Utils::CStoreEditor->new(xact=>1),
-#              patron => $patron
-#      }
-#}
-#
-#sub commit_session {
-#      my $self = shift;
-#      if( my $session = $self->session ) {
-#              $session->{editor}->commit;
-#              delete $$session{editor};
-#              delete $$session{patron};
-#      }
-#}
-#
-#
-#sub rollback_session {
-#      my $self = shift;
-#      if( my $session = $self->session ) {
-#              $session->{editor}->xact_rollback;
-#              delete $$session{editor};
-#              delete $$session{patron};
-#      }
-#}
-#
-#sub session_is_alive {
-#      my $self = shift;
-#      return $self->session and $self->session->{editor};
-#}
-
-
-
 sub new {
     my( $class, %args ) = @_;
 
@@ -86,33 +39,32 @@ sub new {
 
        syslog('LOG_DEBUG', "OpenILS: Created new transaction with authtoken %s", $self->authtoken);
 
-       require OpenILS::Utils::CStoreEditor;
-       $self->editor(OpenILS::Utils::CStoreEditor->new(
-               xact=>1, authtoken => $self->authtoken));
+       my $e = OpenILS::SIP->editor();
+       $e->{authtoken} = $self->authtoken;
 
        return $self;
 }
 
-sub DESTROY {
-    # be cool
+sub DESTROY { 
+       # be cool
 }
 
 sub AUTOLOAD {
-    my $self = shift;
-    my $class = ref($self) or croak "$self is not an object";
-    my $name = $AUTOLOAD;
+       my $self = shift;
+       my $class = ref($self) or croak "$self is not an object";
+       my $name = $AUTOLOAD;
 
-    $name =~ s/.*://;
+       $name =~ s/.*://;
 
-    unless (exists $self->{_permitted}->{$name}) {
-       croak "Can't access '$name' field of class '$class'";
-    }
+       unless (exists $self->{_permitted}->{$name}) {
+               croak "Can't access '$name' field of class '$class'";
+       }
 
-    if (@_) {
-       return $self->{$name} = shift;
-    } else {
-       return $self->{$name};
-    }
+       if (@_) {
+               return $self->{$name} = shift;
+       } else {
+               return $self->{$name};
+       }
 }
 
 1;
index 5744046..d19b152 100644 (file)
@@ -12,6 +12,7 @@ use POSIX qw(strftime);
 use OpenILS::SIP;
 use OpenILS::SIP::Transaction;
 use Data::Dumper;
+use Sys::Syslog qw(syslog);
 
 use OpenILS::Application::AppUtils;
 my $U = 'OpenILS::Application::AppUtils';
@@ -61,7 +62,7 @@ sub do_checkin {
        my $circ = $resp->{payload}->{circ};
 
        $self->{item}->{patron} = 
-               $self->editor->search_actor_card(
+               OpenILS::SIP->editor->search_actor_card(
                { usr => $circ->usr, active => 't' } )->[0]->barcode;
 
        $self->ok(1);