updated circ code to run with updated script runner
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 19 Jan 2006 21:11:20 +0000 (21:11 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 19 Jan 2006 21:11:20 +0000 (21:11 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@2756 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Circ/Circulate.pm

index c88ec73..0b4b020 100644 (file)
@@ -7,6 +7,7 @@ use OpenSRF::Utils::Logger qw(:logger);
 use OpenILS::Utils::ScriptRunner;
 use OpenILS::Application::AppUtils;
 use OpenILS::Application::Circ::Holds;
+$Data::Dumper::Indent = 0;
 my $apputils = "OpenILS::Application::AppUtils";
 my $holdcode = "OpenILS::Application::Circ::Holds";
 
@@ -17,6 +18,13 @@ my $script_libs;             # - any additional script libraries
 my $copy_statuses;     # - copy status objects
 my $copy_locations;    # - shelving locations
 
+my $cur_copy;
+my $cur_patron;
+my $cur_title;
+my $cur_standings;
+
+my %contexts;                  # - Script runner contexts
+
 # ------------------------------------------------------------------------------
 # Load the circ script from the config
 # ------------------------------------------------------------------------------
@@ -32,7 +40,7 @@ sub initialize {
        my $m           = $conf->config_value(  @pfx, 'max_fines' );
        my $pr  = $conf->config_value(  @pfx, 'permit_renew' );
        my $ph  = $conf->config_value(  @pfx, 'permit_hold' );
-       my $lb  = $conf->config_value(  @pfx, 'script_lib' );
+       my $lb  = $conf->config_value(  'apps', 'open-ils.circ', 'app_settings', 'script_path' );
 
        $logger->error( "Missing circ script(s)" ) 
                unless( $p and $d and $f and $m and $pr and $ph );
@@ -69,15 +77,14 @@ sub create_circ_env {
 
        my ( $copy, $title, $evt );
 
-       if(!$standings) {
-               $standings = $apputils->fetch_patron_standings();
+       if(!$cur_standings) {
+               $cur_standings = $apputils->fetch_patron_standings();
                $group_tree = $apputils->fetch_permission_group_tree();
        }
 
-       # XXX must decide if caching is "right"
-       my $cstatus = $apputils->fetch_copy_statuses if( $fetch_cstatus and !$copy_statuses );
-       my $clocs = $apputils->fetch_copy_locations if( $fetch_clocs and !$copy_locations);
-       my $summary = $apputils->fetch_patron_circ_summary($patron->id) if $fetch_summary;
+       my $cstatus             = $apputils->fetch_copy_statuses if( $fetch_cstatus and !$copy_statuses );
+       my $clocs               = $apputils->fetch_copy_locations if( $fetch_clocs and !$copy_locations);
+       my $summary             = $apputils->fetch_patron_circ_summary($patron->id) if $fetch_summary;
 
        ( $copy, $evt ) = $apputils->fetch_copy_by_barcode( $barcode );
        return ( undef, $evt ) if $evt;
@@ -87,7 +94,11 @@ sub create_circ_env {
 
        _doctor_circ_objects( $patron, $title, $copy, $summary, $cstatus, $clocs );
 
-       my $runner = _build_circ_script_runner( $patron, $title, $copy, $summary );
+       my $runner = _build_circ_script_runner( $patron, $title, $copy, $summary, $params{type} );
+
+       $cur_patron = $patron;
+       $cur_copy       = $copy;
+       $cur_title      = $title;
 
        return { 
                runner                  => $runner, 
@@ -107,7 +118,7 @@ sub _doctor_circ_objects {
        my( $patron, $title, $copy, $summary, $cstatus, $clocs ) = @_;
 
        # set the patron standing to the standing name
-       for my $s (@$standings) {
+       for my $s (@$cur_standings) {
                $patron->standing( $s->value ) if( $s->id eq $patron->standing);
        }
 
@@ -155,12 +166,22 @@ sub _get_copy_location {
 # Constructs and shoves data into the script environment
 # ------------------------------------------------------------------------------
 sub _build_circ_script_runner {
-       my( $patron, $title, $copy, $summary ) = @_;
+       my( $patron, $title, $copy, $summary, $type ) = @_;
 
        $logger->debug("Loading script environment for circulation");
 
-       my $runner = OpenILS::Utils::ScriptRunner->new( 
-               type => 'js', libs => $script_libs );
+       my $runner;
+       if( $runner = $contexts{$type} ) {
+               $runner->refresh_context;
+       } else {
+               $runner = OpenILS::Utils::ScriptRunner->new unless $runner;
+               $contexts{$type} = $runner;
+       }
+
+       for(@$script_libs) {
+               $logger->debug("Loading circ script lib path $_");
+               $runner->add_path( $_ );
+       }
 
        $runner->insert( 'patron',              $patron );
        $runner->insert( 'title',               $title );
@@ -183,15 +204,97 @@ sub _build_circ_script_runner {
 sub _add_script_runner_methods {
        my $runner = shift;     
 
-       $runner->context->function_set(
-               'fetch_hold_by_copy', sub {
-                       my $copyid = shift;
-                       my $hold = $holdcode->fetch_open_hold_by_current_copy($copyid);
-                       $runner->insert( 'hold', $hold ) if $hold;
+       $runner->insert_method( 'copy', '__OILS_FUNC_fetch_hold', sub {
+                       my $key = shift;
+                       my $hold = $holdcode->fetch_open_hold_by_current_copy($cur_copy->id);
+                       $hold = undef unless $hold;
+                       $runner->insert( $key, $hold );
                }
        );
+
+#      $runner->insert_method( 'patron', '__OILS_FUNC_get_standing', sub {
+#                      my $key = shift;
+#                      my $standing = "";
+#                      for my $s (@$cur_standings) {
+#                              $standing = $s->value if ( $s->id eq $cur_patron->standing );
+#                      }
+#                      $runner->insert( $key, $standing );
+#              }
+#      );
+
+
+}
+
+=head blah
+sub _insert_event {
+       my $runner = shift;
+       my $evt = shift;
+       $runner->insert('result.event', $evt->{textcode} );
 }
 
+sub _add_script_methods {
+       my $runner = shift;
+
+       $runner->insert( 'fetch_patron', sub {
+                       my ( $key, $id ) = @_;  
+                       my ( $user, $evt ) = $apputils->fetch_user($id);
+                       _insert_event( $runner, $evt ) if $evt;
+                       $runner->insert( $key, $user );
+               }
+       );
+
+       $runner->insert( 'fetch_copy_by_barcode', sub {
+                       my( $key, $barcode ) = @_;
+                       my( $copy, $evt ) = $apputils->fetch_copy_by_barcode( $barcode );
+                       _insert_event( $runner, $evt ) if $evt;
+                       $runner->insert( $key, $copy );
+               }
+       );
+
+       $runner->insert( 'fetch_copy_statuses', sub {
+               my $key = shift;
+               $runner->insert( $key, $apputils->fetch_copy_statuses ); 
+       });
+
+       $runner->insert( 'fetch_copy_locations', sub {
+               my $key = shift;
+               $runner->insert( $key, $apputils->fetch_copy_locations ); });
+
+       $runner->insert( 'fetch_patron_circ_summary', sub {
+               my( $key, $patron_id ) = @_;
+               $runner->insert( $key, $apputils->fetch_patron_circ_summary($patron_id)); });
+
+       $runner->insert( 'fetch_group_tree', sub { 
+               my $key = shift;
+               $runner->insert( $key, $apputils->fetch_permission_group_tree ); });
+
+       $runner->insert( 'fetch_patron_standings', sub { 
+               my $key = shift;
+               $runner->insert( $key, $apputils->fetch_patron_standings ); } );
+}
+
+
+sub _build_script_runner {
+       my %params = @_;
+
+       my $runner = OpenILS::Utils::ScriptRunner->new( 
+               type => 'js', libs => $script_libs );
+       
+       # return status event
+       $runner->insert( 'result', {} );
+       $runner->insert( 'result.event', 'SUCCESS' );
+
+       $runner->insert('env.patron_id', $params{patron_id} ) if defined $params{patron_id};
+       $runner->insert('env.copy_barcode', $params{copy_barcode} ) if defined $params{copy_barcode};
+
+       $runner->insert( 'arr', [ 1, 5, 10 ] );
+
+
+       return $runner;
+}
+
+=cut
+
 
 # ------------------------------------------------------------------------------
 
@@ -206,21 +309,27 @@ __PACKAGE__->register_method(
 
 sub permit_circ {
        my( $self, $client, $authtoken, %params ) = @_;
+
        my $barcode             = $params{barcode};
        my $patronid    = $params{patron};
        my $isrenew             = $params{renew};
        my ( $requestor, $patron, $env, $evt );
 
+
        # check permisson of the requestor
        ( $requestor, $patron, $evt ) = 
                $apputils->checkses_requestor( 
                $authtoken, $patronid, 'VIEW_PERMIT_CHECKOUT' );
        return $evt if $evt;
 
+       $logger->info("Checking circulation permission for staff: " . $requestor->id .
+               ", patron " . $patron->id . ", and barcode $barcode" );
+
        # fetch and build the circulation environment
        ( $env, $evt ) = create_circ_env( 
                barcode                                                 => $barcode, 
                patron                                                  => $patron, 
+               type                                                            => 'permit',
                fetch_patron_circ_summary       => 1,
                fetch_copy_statuses                     => 1, 
                fetch_copy_locations                    => 1, 
@@ -229,8 +338,12 @@ sub permit_circ {
 
        # run the script
        my $runner = $env->{runner};
+
+#      my $runner = _build_script_runner( patron_id => $patronid, copy_barcode => $barcode );
+#      _add_script_methods( $runner );
+
        $runner->load($scripts{circ_permit});
-       $runner->run or throw OpenSRF::EX::ERROR ("Circ Permit Script Died");
+       $runner->run or throw OpenSRF::EX::ERROR ("Circ Permit Script Died: $@");
 
        my $evtname = $runner->retrieve('result.event');
        $logger->activity("Permit Circ for user $patronid and barcode $barcode returned event: $evtname");