moved script building to a generic framework using cstore as the base - standardizing...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 28 Jun 2006 13:23:53 +0000 (13:23 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 28 Jun 2006 13:23:53 +0000 (13:23 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@4811 dcc99617-32d9-48b4-a31d-7c20da2025e4

14 files changed:
Open-ILS/src/javascript/backend/circ/circ_permit_copy.js
Open-ILS/src/javascript/backend/circ/circ_permit_hold.js
Open-ILS/src/javascript/backend/circ/circ_permit_patron.js
Open-ILS/src/javascript/backend/penalty/patron_penalty.js
Open-ILS/src/perlmods/OpenILS/Application/Actor.pm
Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
Open-ILS/src/perlmods/OpenILS/Application/Circ/Circulate.pm
Open-ILS/src/perlmods/OpenILS/Application/Circ/Holds.pm
Open-ILS/src/perlmods/OpenILS/Application/Circ/ScriptBuilder.pm
Open-ILS/src/perlmods/OpenILS/Application/Circ/Transit.pm
Open-ILS/src/perlmods/OpenILS/Application/Penalty.pm
Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
Open-ILS/src/perlmods/OpenILS/Utils/CStoreEditor.pm
Open-ILS/src/perlmods/OpenILS/Utils/PermitHold.pm

index 95d3d6f..6f3843e 100644 (file)
@@ -6,19 +6,33 @@ load_lib('circ_lib.js');
 /* collect some useful variables */
 var copy                                       = environment.copy;
 var patron                             = environment.patron;
+var volume                             = environment.volume;
+var title                              = environment.title;
+var recDescriptor              = environment.titleDescriptor;
 var patronProfile              = patron.profile.name.toLowerCase();
 var copyStatus                 = copy.status.name.toLowerCase();
 var isRenewal                  = environment.isRenewal;
 
+/*
+- at some point we should add a library of objects that map 
+codes to names (item_form, item_type, etc.)
+load_lib('item_form_map.js');
+var form_name = item_form_map[env.record_descriptor.item_form];
+*/
+
 
 log_debug('circ_permit_copy: permit circ on ' +
-       ' Copy: '                                       + copy.id + 
+       '  Copy: '                                      + copy.id + 
        ', Patron:'                                     + patron.id +
        ', Patron Username:'            + patron.usrname +
        ', Patron Library: '            + patron.home_ou.name +
        ', Copy status: '                       + copyStatus +
        ', Copy location: '             + copy.location.name +
-       ', Is Renewal: '                        + ( (isRenewal) ? "yes" : "no" ) +
+       ', Is Renewal: '                        + ( (isTrue(isRenewal)) ? "yes" : "no" ) +
+       ', Item Type: '                 + recDescriptor.item_type +
+       ', Item Form: '                 + recDescriptor.item_form +
+       ', Item Lang: '                 + recDescriptor.item_lang +
+       ', Item Audience: '             + recDescriptor.audience +
        '');
 
 
@@ -35,7 +49,14 @@ if(copyStatus != 'available' &&
        copyStatus != 'on holds shelf' && copyStatus != 'reshelving' ) {
                result.events.push('COPY_NOT_AVAILABLE');
 }
-       
+
+/* this should happen very rarely .. but it needs to be protected */
+if( recDescriptor.item_type == 'g'  /* projected medium */
+       && copy.circ_lib != patron.home_ou.id )
+       result.events.push('CIRC_EXCEEDS_COPY_RANGE');
+
+
+
        
 } go();
 
index bc727df..c46d557 100644 (file)
@@ -3,10 +3,32 @@ function go() {
 var patron                             = environment.patron;
 var title                              = environment.title;
 var copy                                       = environment.copy;
+var volume                             = environment.volume;
+var title                              = environment.title;
 var requestor                  = environment.requestor;
 var requestLib                 = environment.requestLib;
 var titleDescriptor    = environment.titleDescriptor;
 
+log_debug('circ_permit_hold: permit circ on ' +
+       ' Copy: '                                       + copy.id + 
+       ', Patron:'                                     + patron.id +
+       ', Patron Username:'            + patron.usrname +
+       ', Patron Library: '            + patron.home_ou.name +
+       ', Copy location: '             + copy.location.name +
+       ', Item Type: '                 + titleDescriptor.item_type +
+       ', Item Form: '                 + titleDescriptor.item_form +
+       ', Item Lang: '                 + titleDescriptor.item_lang +
+       ', Item Audience: '             + titleDescriptor.audience +
+       '');
+
+
+
+
+
+if( titleDescriptor.item_type == 'g'  /* projected medium */
+       && copy.circ_lib != patron.home_ou.id )
+       return result.event = 'CIRC_EXCEEDS_COPY_RANGE';
+
 
 return result.event = 'SUCCESS';
 
index d4d4d70..9c97f14 100644 (file)
@@ -6,7 +6,6 @@ load_lib('circ_lib.js');
 
 /* collect some useful variables */
 var patron                             = environment.patron;
-//var patronStanding   = patron.standing.value.toLowerCase();
 var patronProfile              = patron.profile.name.toLowerCase();
 var patronItemsOut     = environment.patronItemsOut;
 var patronFines                = environment.patronFines;
@@ -17,7 +16,6 @@ log_debug('circ_permit_patron: permit circ on ' +
        ', Patron:'                                     + patron.id +
        ', Patron Username:'            + patron.usrname +
        ', Patron Profile: '            + patronProfile +
-//     ', Patron Standing: '   + patronStanding +
        ', Patron copies: '             + patronItemsOut +
        ', Patron Library: '            + patron.home_ou.name +
        ', Patron fines: '              + patronFines +
@@ -25,32 +23,40 @@ log_debug('circ_permit_patron: permit circ on ' +
        '');
 
 
-log_debug("BARRED: " + patron.barred );
-
 if( isTrue(patron.barred) ) 
        result.events.push('PATRON_BARRED');
 
 
 /* define the items out limits */
 var PROFILES = {
+
        restricted : {
                itemsOutLimit : 2,
        },
        patrons : {
                itemsOutLimit : 10,
        },
-       class : {
+       'class' : {
                itemsOutLimit : 10,
+       },
+       'local system administrator' : {
+               itemsOut : -1,
+       },
+       circulators : {
+               itemsOut : -1,
        }
 
+
        /* Add profiles as necessary ... */
 }
 
 
 var profile = PROFILES[patronProfile];
 if( profile ) {
-       if( patronItemsOut > profile.itemsOutLimit )
+       if( patronItemsOut > 0 && patronItemsOut > profile.itemsOutLimit )
                result.events.push('PATRON_EXCEEDS_CHECKOUT_COUNT');
+} else {
+       log_warn("profile has no configured information: " + patronProfile);
 }
 
 
index e7d990d..6530ae3 100644 (file)
@@ -30,10 +30,14 @@ var PROFILES = {
                fineLimit : 10,
                overdueLimit : 10,
        },
-       class : {
+       'class' : {
                fineLimit : 10,
                overdueLimit : 10,
        },
+       circulators : {
+               fineLimit : -1,
+               overdueLimit : -1,
+       },
        'local system administrator' : {
                fineLimit : -1,
                overdueLimit : -1,
index 682cfcc..d23b37f 100644 (file)
@@ -19,6 +19,7 @@ use OpenSRF::Utils qw/:datetime/;
 
 use OpenSRF::Utils::Cache;
 
+use JSON;
 use DateTime;
 use DateTime::Format::ISO8601;
 
@@ -58,12 +59,10 @@ sub set_user_settings {
                $apputils->checkses_requestor( $user_session, $uid, 'UPDATE_USER' );    
        return $evt if $evt;
        
-
-       #my ($params) = map { 
-       #       [{ usr => $user->id, name => $_}, {value => $$settings{$_}}] } keys %$settings;
-
        my @params = map { 
                [{ usr => $user->id, name => $_}, {value => $$settings{$_}}] } keys %$settings;
+               
+       $_->[1]->{value} = JSON->perl2JSON($_->[1]->{value}) for @params;
 
        $logger->activity("User " . $staff->id . " updating user $uid settings with: " . Dumper(\@params));
 
@@ -88,14 +87,16 @@ sub set_ou_settings {
        return $evt if $evt;
 
 
-       my ($params) = 
+       my @params = 
                map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings;
 
-       $logger->activity("Updating org unit [$ouid] settings with: " . Dumper($params));
+       $_->[1]->{value} = JSON->perl2JSON($_->[1]->{value}) for @params;
+
+       $logger->activity("Updating org unit [$ouid] settings with: " . Dumper(\@params));
 
        return $apputils->simplereq(
                'open-ils.storage',
-               'open-ils.storage.direct.actor.org_unit_setting.merge', @$params );
+               'open-ils.storage.direct.actor.org_unit_setting.merge', @params );
 }
 
 
@@ -118,7 +119,7 @@ sub user_settings {
                'open-ils.storage',
                'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
 
-       return { map { ($_->name,$_->value) } @$s };
+       return { map { ( $_->name => JSON->JSON2perl($_->value) ) } @$s };
 }
 
 
@@ -136,7 +137,7 @@ sub ou_settings {
                'open-ils.storage',
                'open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic', $ouid);
 
-       return { map { ($_->name,$_->value) } @$s };
+       return { map { ( $_->name => JSON->JSON2perl($_->value) ) } @$s };
 }
 
 __PACKAGE__->register_method (
index ac0f8f7..351228d 100644 (file)
@@ -169,6 +169,7 @@ sub simple_scalar_request {
        my($self, $service, $method, @params) = @_;
 
        my $session = undef;
+
        if( $service eq 'open-ils.storage' ) {
                if( $session = get_storage_session() ) {
                        $logger->debug("simple request using existing storage session ".$session->session_id);
@@ -182,17 +183,20 @@ sub simple_scalar_request {
        my $request = $session->request( $method, @params );
        my $response = $request->recv(60);
 
-       $request->wait_complete;
+#      $request->wait_complete;
+
+#      if(!$request->complete) {
+#              warn "request did not complete : service=$service : method=$method\n";
+#              throw OpenSRF::EX::ERROR 
+#                      ("Call to $service for method $method with params ". Dumper(\@params) . 
+#                              "\n did not complete successfully");
+#      }
 
-       if(!$request->complete) {
-               warn "request did not complete : service=$service : method=$method\n";
-               throw OpenSRF::EX::ERROR 
-                       ("Call to $service for method $method with params ". Dumper(\@params) . 
-                               "\n did not complete successfully");
-       }
 
        if(!$response) {
-               warn "No response from $service for method $method with params @params";
+               warn "No response from $service for method $method with params " . Dumper(\@params);
+               $logger->error("No response from $service for method $method with params " . Dumper(\@params));
+               return undef;
        }
 
        if(UNIVERSAL::isa($response,"Error")) {
@@ -209,12 +213,7 @@ sub simple_scalar_request {
                $session->disconnect();
        }
 
-       my $value;
-
-       if($response) { $value = $response->content; }
-       else { $value = undef; }
-
-       return $value;
+       return $response->content;
 }
 
 
index 79a6c89..c63a277 100644 (file)
@@ -12,26 +12,26 @@ use OpenILS::Application::Circ::Holds;
 use OpenILS::Application::Circ::Transit;
 use OpenILS::Utils::PermitHold;
 use OpenSRF::Utils::Logger qw(:logger);
+use OpenILS::Utils::Editor qw/:funcs/;
 use DateTime;
 use DateTime::Format::ISO8601;
 use OpenSRF::Utils qw/:datetime/;
+use OpenILS::Application::Circ::ScriptBuilder;
 
 $Data::Dumper::Indent = 0;
-my $apputils   = "OpenILS::Application::AppUtils";
-my $U                          = $apputils;
+my $U                          = "OpenILS::Application::AppUtils";
 my $holdcode   = "OpenILS::Application::Circ::Holds";
 my $transcode  = "OpenILS::Application::Circ::Transit";
 
 my %scripts;                   # - circulation script filenames
 my $script_libs;               # - any additional script libraries
-my %cache;                             # - db objects cache
-my %contexts;                  # - Script runner contexts
+#my %cache;                            # - db objects cache
 my $cache_handle;              # - memcache handle
 
 sub PRECAT_FINE_LEVEL { return 2; }
 sub PRECAT_LOAN_DURATION { return 2; }
 
-my %RECORD_FROM_COPY_CACHE;
+#my %RECORD_FROM_COPY_CACHE;
 
 
 # for security, this is a process-defined and not
@@ -70,10 +70,15 @@ sub initialize {
        $lb = [ $lb ] unless ref($lb);
        $script_libs = $lb;
 
-       $logger->debug("Loaded rules scripts for circ: " .
-               "circ permit patron: $p, circ permit copy: $c, ".
-               "circ duration :$d , circ recurring fines : $f, " .
-               "circ max fines : $m, circ renew permit : $pr");
+       $logger->debug(
+               "Loaded rules scripts for circ: " .
+               "circ permit patron = $p, ".
+               "circ permit copy = $c, ".
+               "circ duration = $d, ".
+               "circ recurring fines = $f, " .
+               "circ max fines = $m, ".
+               "circ renew permit = $pr.  ".
+               "lib paths = @$lb");
 }
 
 
@@ -89,191 +94,202 @@ sub create_circ_ctx {
        my $evt;
        my $ctx = \%params;
 
-       $evt = _ctx_add_patron_objects($ctx, %params);
-       return (undef,$evt) if $evt;
+       $ctx->{copy_id}         = $ctx->{copyid};
+       $ctx->{patron_id}               = $ctx->{patronid};
+       $ctx->{copy_barcode} = $ctx->{barcode};
+       $ctx->{fetch_patron_circ_info} = 1;
+
+       my @evts = OpenILS::Application::Circ::ScriptBuilder->build($ctx);
 
        if(!$params{noncat}) {
-               if( $evt = _ctx_add_copy_objects($ctx, %params) ) {
-                       $ctx->{precat} = 1 if($evt->{textcode} eq 'ASSET_COPY_NOT_FOUND')
+               if( @evts and grep { $_->{textcode} eq 'ASSET_COPY_NOT_FOUND' } @evts) {
+                       $ctx->{precat} = 1;
                } else {
                        $ctx->{precat} = 1 if ( $ctx->{copy}->call_number == -1 ); # special case copy
                }
        }
 
-       _doctor_patron_object($ctx) if $ctx->{patron};
-       _doctor_copy_object($ctx) if $ctx->{copy};
-
-       if(!$ctx->{no_runner}) {
-               _build_circ_script_runner($ctx);
-               _add_script_runner_methods($ctx);
-       }
-
-       return $ctx;
-}
-
-sub _ctx_add_patron_objects {
-       my( $ctx, %params) = @_;
-       $U->logmark;
-
-       # - patron standings are now handled in the penalty server...
-
-       #if(!defined($cache{patron_standings})) {
-       #       $cache{patron_standings} = $U->fetch_patron_standings();
-       #}
-       #$ctx->{patron_standings} = $cache{patron_standings};
-
-       $cache{group_tree} = $U->fetch_permission_group_tree() unless $cache{group_tree};
-       $ctx->{group_tree} = $cache{group_tree};
-
-       $ctx->{patron_circ_summary} = 
-               $U->fetch_patron_circ_summary($ctx->{patron}->id) 
-               if $params{fetch_patron_circsummary};
-
-       return undef;
-}
-
-
-sub _find_copy_by_attr {
-       my %params = @_;
-       $U->logmark;
-       my $evt;
-
-       my $copy = $params{copy} || undef;
-
-       if(!$copy) {
-
-               ( $copy, $evt ) = 
-                       $U->fetch_copy($params{copyid}) if $params{copyid};
-               return (undef,$evt) if $evt;
-
-               if(!$copy) {
-                       ( $copy, $evt ) = 
-                               $U->fetch_copy_by_barcode( $params{barcode} ) if $params{barcode};
-                       return (undef,$evt) if $evt;
-               }
-       }
-       return ( $copy, $evt );
-}
-
-sub _ctx_add_copy_objects {
-       my($ctx, %params)  = @_;
-       $U->logmark;
-       my $evt;
-       my $copy;
-
-       $cache{copy_statuses} = $U->fetch_copy_statuses 
-               if( $params{fetch_copy_statuses} and !defined($cache{copy_statuses}) );
-
-       $cache{copy_locations} = $U->fetch_copy_locations 
-               if( $params{fetch_copy_locations} and !defined($cache{copy_locations}));
-
-       $ctx->{copy_statuses} = $cache{copy_statuses};
-       $ctx->{copy_locations} = $cache{copy_locations};
-
-       ($copy, $evt) = _find_copy_by_attr(%params);
-       return $evt if $evt;
-
-       if( $copy and !$ctx->{title} ) {
-               $logger->debug("Copy status: " . $copy->status);
-
-               my $r = $RECORD_FROM_COPY_CACHE{$copy->id};
-               ($r, $evt) = $U->fetch_record_by_copy( $copy->id ) unless $r;
-               return $evt if $evt;
-               $RECORD_FROM_COPY_CACHE{$copy->id} = $r;
-
-               $ctx->{title} = $r;
-               $ctx->{copy} = $copy;
-       }
-
-       return undef;
-}
-
-
-# ------------------------------------------------------------------------------
-# Fleshes parts of the patron object
-# ------------------------------------------------------------------------------
-sub _doctor_copy_object {
-       my $ctx = shift;
-       $U->logmark;
-       my $copy = $ctx->{copy} || return undef;
-
-       $logger->debug("Doctoring copy object...");
-
-       # set the copy status to a status name
-       $copy->status( _get_copy_status( $copy, $ctx->{copy_statuses} ) );
-
-       # set the copy location to the location object
-       $copy->location( _get_copy_location( $copy, $ctx->{copy_locations} ) );
-
-       $copy->circ_lib( $U->fetch_org_unit($copy->circ_lib) );
-}
-
+       _build_circ_script_runner($ctx);
+       return ($ctx);
 
-# ------------------------------------------------------------------------------
-# Fleshes parts of the patron object
-# ------------------------------------------------------------------------------
-sub _doctor_patron_object {
-       my $ctx = shift;
-       $U->logmark;
-       my $patron = $ctx->{patron} || return undef;
-
-       # push the standing object into the patron
-#      if(ref($ctx->{patron_standings})) {
-#              for my $s (@{$ctx->{patron_standings}}) {
-#                      if( $s->id eq $ctx->{patron}->standing ) {
-#                              $patron->standing($s);
-#                              $logger->debug("Set patron standing to ". $s->value);
-#                      }
+#      # XXX XXX
+#
+#      $evt = _ctx_add_patron_objects($ctx, %params);
+#      return (undef,$evt) if $evt;
+#
+#      if(!$params{noncat}) {
+#              if( $evt = _ctx_add_copy_objects($ctx, %params) ) {
+#                      $ctx->{precat} = 1 if($evt->{textcode} eq 'ASSET_COPY_NOT_FOUND')
+#              } else {
+#                      $ctx->{precat} = 1 if ( $ctx->{copy}->call_number == -1 ); # special case copy
 #              }
 #      }
+#
+#      _doctor_patron_object($ctx) if $ctx->{patron};
+#      _doctor_copy_object($ctx) if $ctx->{copy};
+#
+#      if(!$ctx->{no_runner}) {
+#              _build_circ_script_runner($ctx);
+#              _add_script_runner_methods($ctx);
+#      }
+#
+#      return $ctx;
 
-       # set the patron ptofile to the profile name
-       $patron->profile( _get_patron_profile( 
-               $patron, $ctx->{group_tree} ) ) if $ctx->{group_tree};
-
-       # flesh the org unit
-       $patron->home_ou( 
-               $U->fetch_org_unit( $patron->home_ou ) ) if $patron;
-
-}
-
-# recurse and find the patron profile name from the tree
-# another option would be to grab the groups for the patron
-# and cycle through those until the "profile" group has been found
-sub _get_patron_profile { 
-       my( $patron, $group_tree ) = @_;
-       return $group_tree if ($group_tree->id eq $patron->profile);
-       return undef unless ($group_tree->children);
-
-       for my $child (@{$group_tree->children}) {
-               my $ret = _get_patron_profile( $patron, $child );
-               return $ret if $ret;
-       }
-       return undef;
-}
-
-sub _get_copy_status {
-       my( $copy, $cstatus ) = @_;
-       $U->logmark;
-       my $s = undef;
-       for my $status (@$cstatus) {
-               $s = $status if( $status->id eq $copy->status ) 
-       }
-       $logger->debug("Retrieving copy status: " . $s->name) if $s;
-       return $s;
-}
-
-sub _get_copy_location {
-       my( $copy, $locations ) = @_;
-       $U->logmark;
-       my $l = undef;
-       for my $loc (@$locations) {
-               $l = $loc if $loc->id eq $copy->location;
-       }
-       $logger->debug("Retrieving copy location: " . $l->name ) if $l;
-       return $l;
 }
 
+#sub _ctx_add_patron_objects {
+#      my( $ctx, %params) = @_;
+#      $U->logmark;
+#
+#      $cache{group_tree} = $U->fetch_permission_group_tree() unless $cache{group_tree};
+#      $ctx->{group_tree} = $cache{group_tree};
+#
+#      $ctx->{patron_circ_summary} = 
+#              $U->fetch_patron_circ_summary($ctx->{patron}->id) 
+#              if $params{fetch_patron_circsummary};
+#
+#      return undef;
+#}
+#
+#
+#sub _find_copy_by_attr {
+#      my %params = @_;
+#      $U->logmark;
+#      my $evt;
+#
+#      my $copy = $params{copy} || undef;
+#
+#      if(!$copy) {
+#
+#              ( $copy, $evt ) = 
+#                      $U->fetch_copy($params{copyid}) if $params{copyid};
+#              return (undef,$evt) if $evt;
+#
+#              if(!$copy) {
+#                      ( $copy, $evt ) = 
+#                              $U->fetch_copy_by_barcode( $params{barcode} ) if $params{barcode};
+#                      return (undef,$evt) if $evt;
+#              }
+#      }
+#      return ( $copy, $evt );
+#}
+#
+#sub _ctx_add_copy_objects {
+#      my($ctx, %params)  = @_;
+#      $U->logmark;
+#      my $evt;
+#      my $copy;
+#
+#      $cache{copy_statuses} = $U->fetch_copy_statuses 
+#              if( $params{fetch_copy_statuses} and !defined($cache{copy_statuses}) );
+#
+#      $cache{copy_locations} = $U->fetch_copy_locations 
+#              if( $params{fetch_copy_locations} and !defined($cache{copy_locations}));
+#
+#      $ctx->{copy_statuses} = $cache{copy_statuses};
+#      $ctx->{copy_locations} = $cache{copy_locations};
+#
+#      ($copy, $evt) = _find_copy_by_attr(%params);
+#      return $evt if $evt;
+#
+#      if( $copy and !$ctx->{title} ) {
+#
+#              my $r = $RECORD_FROM_COPY_CACHE{$copy->id};
+#              ($r, $evt) = $U->fetch_record_by_copy( $copy->id ) unless $r;
+#              return $evt if $evt;
+#              $RECORD_FROM_COPY_CACHE{$copy->id} = $r;
+#
+#              $ctx->{title} = $r;
+#              $ctx->{copy} = $copy;
+#
+#              ($ctx->{volume}) = $U->fetch_callnumber($copy->call_number);
+#              $ctx->{recordDescriptor} = $U->storagereq(
+#                      'open-ils.storage.direct.metabib.record_descriptor.search_where', 
+#                      { record => $ctx->{title}->id });
+#
+#
+#      }
+#
+#      return undef;
+#}
+#
+#
+## ------------------------------------------------------------------------------
+## Fleshes parts of the patron object
+## ------------------------------------------------------------------------------
+#sub _doctor_copy_object {
+#      my $ctx = shift;
+#      $U->logmark;
+#      my $copy = $ctx->{copy} || return undef;
+#
+#      $logger->debug("Doctoring copy object...");
+#
+#      # set the copy status to a status name
+#      $copy->status( _get_copy_status( $copy, $ctx->{copy_statuses} ) );
+#
+#      # set the copy location to the location object
+#      $copy->location( _get_copy_location( $copy, $ctx->{copy_locations} ) );
+#
+#      $copy->circ_lib( $U->fetch_org_unit($copy->circ_lib) );
+#
+#}
+#
+#
+## ------------------------------------------------------------------------------
+## Fleshes parts of the patron object
+## ------------------------------------------------------------------------------
+#sub _doctor_patron_object {
+#      my $ctx = shift;
+#      $U->logmark;
+#      my $patron = $ctx->{patron} || return undef;
+#
+#      # set the patron ptofile to the profile name
+#      $patron->profile( _get_patron_profile( 
+#              $patron, $ctx->{group_tree} ) ) if $ctx->{group_tree};
+#
+#      # flesh the org unit
+#      $patron->home_ou( 
+#              $U->fetch_org_unit( $patron->home_ou ) ) if $patron;
+#
+#}
+#
+## recurse and find the patron profile name from the tree
+## another option would be to grab the groups for the patron
+## and cycle through those until the "profile" group has been found
+#sub _get_patron_profile { 
+#      my( $patron, $group_tree ) = @_;
+#      return $group_tree if ($group_tree->id eq $patron->profile);
+#      return undef unless ($group_tree->children);
+#
+#      for my $child (@{$group_tree->children}) {
+#              my $ret = _get_patron_profile( $patron, $child );
+#              return $ret if $ret;
+#      }
+#      return undef;
+#}
+#
+#sub _get_copy_status {
+#      my( $copy, $cstatus ) = @_;
+#      $U->logmark;
+#      my $s = undef;
+#      for my $status (@$cstatus) {
+#              $s = $status if( $status->id eq $copy->status ) 
+#      }
+#      $logger->debug("Retrieving copy status: " . $s->name) if $s;
+#      return $s;
+#}
+#
+#sub _get_copy_location {
+#      my( $copy, $locations ) = @_;
+#      $U->logmark;
+#      my $l = undef;
+#      for my $loc (@$locations) {
+#              $l = $loc if $loc->id eq $copy->location;
+#      }
+#      $logger->debug("Retrieving copy location: " . $l->name ) if $l;
+#      return $l;
+#}
+#
 
 # ------------------------------------------------------------------------------
 # Constructs and shoves data into the script environment
@@ -284,31 +300,8 @@ sub _build_circ_script_runner {
 
        $logger->debug("Loading script environment for circulation");
 
-       my $runner;
-       if( $runner = $contexts{$ctx->{type}} ) {
-               $runner->refresh_context;
-       } else {
-               $runner = OpenILS::Utils::ScriptRunner->new;
-               $contexts{type} = $runner;
-       }
-
-       for(@$script_libs) {
-               $logger->debug("Loading circ script lib path $_");
-               $runner->add_path( $_ );
-       }
-
-       # Note: inserting the number 0 into the script turns into the
-       # string "0", and thus evaluates to true in JS land
-       # inserting undef will insert "", which evaluates to false
-
-       $runner->insert( 'environment.patron',  $ctx->{patron}, 1);
-       $runner->insert( 'environment.title',   $ctx->{title}, 1);
-       $runner->insert( 'environment.copy',    $ctx->{copy}, 1);
 
-       # circ script result
-       $runner->insert( 'result', {} );
-       #$runner->insert( 'result.event', 'SUCCESS' );
-       $runner->insert( 'result.events', [] );
+       my $runner = $ctx->{runner};
 
        if($__isrenewal) {
                $runner->insert('environment.isRenewal', 1);
@@ -329,36 +322,97 @@ sub _build_circ_script_runner {
                $runner->insert('environment.isNonCat', undef);
        }
 
-       if(ref($ctx->{patron_circ_summary})) {
-               $runner->insert( 'environment.patronItemsOut', $ctx->{patron_circ_summary}->[0], 1 );
+       for(@$script_libs) {
+               $logger->debug("Loading circ script lib path $_");
+               $runner->add_path( $_ );
        }
 
-       $ctx->{runner} = $runner;
+
        return $runner;
-}
 
 
-sub _add_script_runner_methods {
-       my $ctx = shift;
-       $U->logmark;
-       my $runner = $ctx->{runner};
+#      # XXX XXX
+#
+#
+#
+#
+#      for(@$script_libs) {
+#              $logger->debug("Loading circ script lib path $_");
+#              $runner->add_path( $_ );
+#      }
+#
+#      # Note: inserting the number 0 into the script turns into the
+#      # string "0", and thus evaluates to true in JS land
+#      # inserting undef will insert "", which evaluates to false
+#
+#      $runner->insert( 'environment.patron',  $ctx->{patron}, 1);
+#      $runner->insert( 'environment.record',  $ctx->{title}, 1);
+#      $runner->insert( 'environment.copy',    $ctx->{copy}, 1);
+#      $runner->insert( 'environment.volume',  $ctx->{volume}, 1);
+#      $runner->insert( 'environment.recordDescriptor', $ctx->{recordDescriptor}, 1);
+#      $runner->insert( 'environment.requestor', $ctx->{requestor}, 1);
+#
+#      # circ script result
+#      $runner->insert( 'result', {} );
+#      #$runner->insert( 'result.event', 'SUCCESS' );
+#      $runner->insert( 'result.events', [] );
+#
+#      if($__isrenewal) {
+#              $runner->insert('environment.isRenewal', 1);
+#      } else {
+#              $runner->insert('environment.isRenewal', undef);
+#      }
+#
+#      if($ctx->{ishold} ) { 
+#              $runner->insert('environment.isHold', 1); 
+#      } else{ 
+#              $runner->insert('environment.isHold', undef) 
+#      }
+#
+#      if( $ctx->{noncat} ) {
+#              $runner->insert('environment.isNonCat', 1);
+#              $runner->insert('environment.nonCatType', $ctx->{noncat_type});
+#      } else {
+#              $runner->insert('environment.isNonCat', undef);
+#      }
+#
+#      if(ref($ctx->{patron_circ_summary})) {
+#              $runner->insert( 'environment.patronItemsOut', $ctx->{patron_circ_summary}->[0], 1 );
+#      }
+#
+#      $ctx->{runner} = $runner;
+#      return $runner;
+
+
 
-       if( $ctx->{copy} ) {
-               
-               # allows a script to fetch a hold that is currently targeting the
-               # copy in question
-               $runner->insert_method( 'environment.copy', '__OILS_FUNC_fetch_hold', sub {
-                               my $key = shift;
-                               my $hold = $holdcode->fetch_related_holds($ctx->{copy}->id);
-                               $hold = undef unless $hold;
-                               $runner->insert( $key, $hold, 1 );
-                       }
-               );
-       }
 }
 
+
+
+#
+#
+#sub _add_script_runner_methods {
+#      my $ctx = shift;
+#      $U->logmark;
+#      my $runner = $ctx->{runner};
+#
+#      if( $ctx->{copy} ) {
+#              
+#              # allows a script to fetch a hold that is currently targeting the
+#              # copy in question
+#              $runner->insert_method( 'environment.copy', '__OILS_FUNC_fetch_hold', sub {
+#                              my $key = shift;
+#                              my $hold = $holdcode->fetch_related_holds($ctx->{copy}->id);
+#                              $hold = undef unless $hold;
+#                              $runner->insert( $key, $hold, 1 );
+#                      }
+#              );
+#      }
+#}
+#
 # ------------------------------------------------------------------------------
 
+
 __PACKAGE__->register_method(
        method  => "permit_circ",
        api_name        => "open-ils.circ.checkout.permit",
@@ -407,6 +461,7 @@ sub permit_circ {
                return $evt if $evt;
        }
 
+
        my $copy = $ctx->{copy};
        if($copy) {
                my $stat = (ref $copy->status) ? $copy->status->id : $copy->status;
@@ -466,81 +521,6 @@ sub override_events {
 }
 
 
-__PACKAGE__->register_method(
-       method  => "check_title_hold",
-       api_name        => "open-ils.circ.title_hold.is_possible",
-       notes           => q/
-               Determines if a hold were to be placed by a given user,
-               whether or not said hold would have any potential copies
-               to fulfill it.
-               @param authtoken The login session key
-               @param params A hash of named params including:
-                       patronid  - the id of the hold recipient
-                       titleid (brn) - the id of the title to be held
-                       depth   - the hold range depth (defaults to 0)
-       /);
-
-# XXX add pickup lib to the call to test for perms
-
-sub check_title_hold {
-       my( $self, $client, $authtoken, $params ) = @_;
-       my %params = %$params;
-       my $titleid = $params{titleid};
-
-       my ( $requestor, $patron, $evt ) = $U->checkses_requestor( 
-               $authtoken, $params{patronid}, 'VIEW_HOLD_PERMIT' );
-       return $evt if $evt;
-
-       my $rangelib    = $patron->home_ou;
-       my $depth               = $params{depth} || 0;
-       my $pickup              = $params{pickup_lib};
-
-       $logger->debug("Fetching ranged title tree for title $titleid, org $rangelib, depth $depth");
-
-       my $org = $U->simplereq(
-               'open-ils.actor', 
-               'open-ils.actor.org_unit.retrieve', 
-               $authtoken, $requestor->home_ou );
-
-       my $limit       = 10;
-       my $offset      = 0;
-       my $title;
-
-       while( $title = $U->storagereq(
-                               'open-ils.storage.biblio.record_entry.ranged_tree', 
-                               $titleid, $rangelib, $depth, $limit, $offset ) ) {
-
-               last unless 
-                       ref($title) and 
-                       ref($title->call_numbers) and 
-                       @{$title->call_numbers};
-
-               for my $cn (@{$title->call_numbers}) {
-       
-                       $logger->debug("Checking callnumber ".$cn->id." for hold fulfillment possibility");
-       
-                       for my $copy (@{$cn->copies}) {
-       
-                               $logger->debug("Checking copy ".$copy->id." for hold fulfillment possibility");
-       
-                               return 1 if OpenILS::Utils::PermitHold::permit_copy_hold(
-                                       {       patron                          => $patron, 
-                                               requestor                       => $requestor, 
-                                               copy                                    => $copy,
-                                               title                                   => $title, 
-                                               title_descriptor        => $title->fixed_fields, # this is fleshed into the title object
-                                               pickup_lib                      => $pickup,
-                                               request_lib                     => $org } );
-       
-                               $logger->debug("Copy ".$copy->id." for hold fulfillment possibility failed...");
-                       }
-               }
-
-               $offset += $limit;
-       }
-
-       return 0;
-}
 
 
 # Runs the patron and copy permit scripts
@@ -555,7 +535,6 @@ sub _run_permit_scripts {
        my $key                 = $ctx->{permit_key};
 
 
-
        # ---------------------------------------------------------------------
        # Find all of the fatal penalties currently set on the user
        # ---------------------------------------------------------------------
@@ -571,8 +550,11 @@ sub _run_permit_scripts {
        # ---------------------------------------------------------------------
        # Now run the patron permit script 
        # ---------------------------------------------------------------------
-       $runner->load($scripts{circ_permit_patron});
-       $runner->run or throw OpenSRF::EX::ERROR ("Circ Permit Patron Script Died: $@");
+       $logger->debug("Running circ script: " . $scripts{circ_permit_patron});
+
+       #$runner->load($scripts{circ_permit_patron});
+       $runner->run($scripts{circ_permit_patron}) or 
+               throw OpenSRF::EX::ERROR ("Circ Permit Patron Script Died: $@");
 
        my $patron_events = $runner->retrieve('result.events');
        $patron_events = [ split(/,/, $patron_events) ]; 
@@ -580,8 +562,10 @@ sub _run_permit_scripts {
        $logger->activity("circ_permit_patron for returned @$patron_events") if @$patron_events;
 
        my @evts_so_far = (@$penalties, @$patron_events);
+       my @allevents; 
+       push( @allevents, OpenILS::Event->new($_)) for @evts_so_far;
 
-       return \@evts_so_far if @evts_so_far;
+       return \@allevents if @allevents;
 
 
        if( $ctx->{noncat} ) {
@@ -620,8 +604,6 @@ sub _run_permit_scripts {
        # Now collect all of the events together
        # ---------------------------------------------------------------------
 
-       my @allevents; #= ( @evts_so_far, @$copy_events );
-       push( @allevents, OpenILS::Event->new($_)) for @evts_so_far;
        push( @allevents, OpenILS::Event->new($_)) for @$copy_events;
 
        my $ae = _check_copy_alert($ctx->{copy});
@@ -744,7 +726,6 @@ sub checkout {
                        requestor                                               => $requestor, 
                        session                                                 => $U->start_db_session(),
                        type                                                            => 'circ',
-                       #fetch_patron_circ_summary      => 1,
                        fetch_copy_statuses                     => 1, 
                        fetch_copy_locations                    => 1, 
                        );
@@ -1184,6 +1165,10 @@ sub generic_receive {
                $authtoken, 'COPY_CHECKIN' ) unless $__isrenewal;
        return $evt if $evt;
 
+
+       my ($patron) = _find_patron_from_params($params);
+       $ctx->{patron} = $patron if $patron;
+
        # load up the circ objects
        if( !( $ctx = $params->{_ctx}) ) {
                ( $ctx, $evt ) = create_circ_ctx( %$params, 
@@ -1217,7 +1202,7 @@ sub generic_receive {
        $U->update_patron_penalties( 
                authtoken => $authtoken, 
                patron    => $ctx->{patron},
-               background => 1
+               background => 1,
        );
 
        return $val;
@@ -1315,10 +1300,6 @@ sub checkin_do_receive {
 
        # If it's a renewal, we're done
        if($__isrenewal) {
-               #$$ctx{force} = 1;
-               #my ($cc, $ee) = _reshelve_copy($ctx);
-               #return $ee if $ee;
-               #delete $$ctx{force};
                $U->commit_db_session($session);
                return OpenILS::Event->new('SUCCESS');
        }
@@ -1400,7 +1381,7 @@ sub checkin_do_receive {
 
 sub _reshelve_copy {
 
-       my $ctx = shift;
+       my $ctx         = shift;
        my $copy                = $ctx->{copy};
        my $reqr                = $ctx->{requestor};
        my $session     = $ctx->{session};
@@ -1475,32 +1456,6 @@ sub _fetch_open_claims_returned {
        return undef;
 }
 
-# - if the copy is has the 'in process' status, set it to reshelving
-#sub _check_in_process {
-       #my $ctx = shift;
-#
-       #my $copy = $ctx->{copy};
-       #my $reqr       = $ctx->{requestor};
-       #my $ses        = $ctx->{session};
-##
-       #my $stat = $U->copy_status_from_name('in process');
-       #my $rstat = $U->copy_status_from_name('reshelving');
-#
-       #if( $stat->id == $copy->status->id ) {
-               #$logger->info("marking 'in-process' copy ".$copy->id." as 'reshelving'");
-               #$copy->status( $rstat->id );
-               #my $evt = $U->update_copy( 
-                       #copy           => $copy,
-                       #editor => $reqr->id,
-                       #session        => $ses
-                       #);
-               #return $evt if $evt;
-#
-               #$copy->status( $rstat ); # - reflesh the copy status
-       #}
-       #return undef;
-#}
-
 
 # returns (ITEM_NOT_CATALOGED, change_occurred, $error_event) where necessary
 sub _checkin_handle_precat {
@@ -1565,63 +1520,6 @@ sub _checkin_check_copy_status {
 
 
 
-#      my $rstat = $U->copy_status_from_name('reshelving');
-#      my $stat = (ref($copy->status)) ? $copy->status->id : $copy->status;
-#
-#      if( $stat == $U->copy_status_from_name('lost')->id ) {
-#              $islost = 1;
-#              $evt = OpenILS::Event->new('COPY_STATUS_LOST', payload => $copy );
-#
-#      } elsif( $stat == $U->copy_status_from_name('missing')->id) {
-#              $ismissing = 1;
-#              $evt = OpenILS::Event->new('COPY_STATUS_MISSING', payload => $copy );
-#      }
-#
-#      return (undef,$evt) if(!$ctx->{override});
-#
-#      # we're are now going to attempt to override the failure 
-#      # and set the copy to reshelving
-#      my $e;
-#      my $copyid = $copy->id;
-#      my $userid = $reqr->id;
-#      if( $islost ) {
-#
-#              # - make sure we have permission
-#              $e = $U->check_perms( $reqr->id, 
-#                      $copy->circ_lib, 'COPY_STATUS_LOST.override');
-#              return (undef,$e) if $e;
-#              $copy->status( $rstat->id );
-#
-#              # XXX if no fines are owed in the circ, close it out - will this happen later anyway?
-#              #my $circ = $U->storagereq(
-#              #       'open-ils.storage.direct.action.circulation
-#
-#              $logger->activity("user $userid overriding 'lost' copy status for copy $copyid");
-#
-#      } elsif( $ismissing ) {
-#
-#              # - make sure we have permission
-#              $e = $U->check_perms( $reqr->id, 
-#                      $copy->circ_lib, 'COPY_STATUS_MISSING.override');
-#              return (undef,$e) if $e;
-#              $copy->status( $rstat->id );
-#              $logger->activity("user $userid overriding 'missing' copy status for copy $copyid");
-#      }
-#
-#      if( $islost or $ismissing ) {
-#
-#              # - update the copy with the new status
-#              $evt = $U->update_copy(
-#                      copy            => $copy,
-#                      editor  => $reqr->id,
-#                      session => $ses
-#              );
-#              return (undef,$evt) if $evt;
-#              $copy->status( $rstat );
-#      }
-#
-#      return (1);
-
 
 }
 
@@ -1765,11 +1663,6 @@ sub _checkin_handle_circ {
        $evt = _set_copy_reshelving($copy, $requestor->id, $ctx->{session}); 
        return $evt if $evt;
 
-#      $copy->status($U->copy_status_from_name('reshelving')->id);
-#      $evt = $U->update_copy( session => $session, 
-#              copy => $copy, editor => $requestor->id );
-#      return $evt if $evt;
-
        $ctx->{session}->request(
                'open-ils.storage.direct.action.circulation.update', $circ )->gather(1);
 
@@ -1889,7 +1782,7 @@ sub renew {
 
        } else {
                ($patron, $copy, $evt) = _find_patron_from_params($params);
-               return $evt if $evt;
+               if($evt) { $__isrenewal = 0; return $evt; }
                $params->{copy} = $copy;
        }
 
@@ -1910,7 +1803,6 @@ sub renew {
                requestor                                               => $requestor, 
                patron                                                  => $patron, 
                type                                                            => 'circ',
-               #fetch_patron_circ_summary      => 1,
                fetch_copy_statuses                     => 1, 
                fetch_copy_locations                    => 1, 
                );
@@ -1937,12 +1829,12 @@ sub renew {
        }
 
        # re-fetch the context since objects have changed in the checkin
+       # XXX Do we really need to do this - what changes that we don't control??
        ( $ctx, $evt ) = create_circ_ctx( %$params, 
                patron                                                  => $patron, 
                requestor                                               => $requestor, 
                patron                                                  => $patron, 
                type                                                            => 'circ',
-               #fetch_patron_circ_summary      => 1,
                fetch_copy_statuses                     => 1, 
                fetch_copy_locations                    => 1, 
                );
@@ -1959,7 +1851,6 @@ sub renew {
        } else {
                $evt = $self->permit_circ( $client, $authtoken, $params );
                if( $U->event_equals($evt, 'ITEM_NOT_CATALOGED')) {
-                       #$ctx->{precat} = 1;
                        $params->{precat} = 1;
 
                } else {
index 1df5a39..b0756c9 100644 (file)
@@ -24,6 +24,7 @@ use OpenILS::Perm;
 use OpenILS::Event;
 use OpenSRF::Utils::Logger qw(:logger);
 use OpenILS::Utils::Editor q/:funcs/;
+use OpenILS::Utils::PermitHold;
 
 my $apputils = "OpenILS::Application::AppUtils";
 my $U = $apputils;
@@ -861,4 +862,106 @@ sub fetch_captured_holds {
 
 
 
+
+
+__PACKAGE__->register_method(
+       method  => "check_title_hold",
+       api_name        => "open-ils.circ.title_hold.is_possible",
+       notes           => q/
+               Determines if a hold were to be placed by a given user,
+               whether or not said hold would have any potential copies
+               to fulfill it.
+               @param authtoken The login session key
+               @param params A hash of named params including:
+                       patronid  - the id of the hold recipient
+                       titleid (brn) - the id of the title to be held
+                       depth   - the hold range depth (defaults to 0)
+       /);
+
+sub check_title_hold {
+       my( $self, $client, $authtoken, $params ) = @_;
+
+       my %params              = %$params;
+       my $titleid             = $params{titleid} ||"";
+       my $mrid                        = $params{mrid} ||"";
+       my $depth               = $params{depth} || 0;
+       my $pickup_lib  = $params{pickup_lib};
+       my $hold_type   = $params{hold_type} || 'T';
+
+       my $e = new_editor(authtoken=>$authtoken);
+       return $e->event unless $e->checkauth;
+       my $patron = $e->retrieve_actor_user($params{patronid})
+               or return $e->event;
+       return $e->event unless $e->allowed('VIEW_HOLD_PERMIT', $patron->home_ou);
+
+       my $rangelib    = $params{range_lib} || $patron->home_ou;
+
+       my $request_lib = $e->retrieve_actor_org_unit($e->requestor->ws_ou)
+               or return $e->event;
+
+       if( $hold_type eq 'T' ) {
+               return _check_title_hold_is_possible(
+                       $titleid, $rangelib, $depth, $request_lib, $patron, $e->requestor, $pickup_lib);
+       }
+
+       if( $hold_type eq 'M' ) {
+               my $maps = $e->search_metabib_source_map({metarecord=>$mrid});
+               my @recs = map { $_->source } @$maps;
+               for my $rec (@recs) {
+                       return 1 if (_check_title_hold_is_possible(
+                               $rec, $rangelib, $depth, $request_lib, $patron, $e->requestor, $pickup_lib));
+               }
+       }
+}
+
+
+
+sub _check_title_hold_is_possible {
+       my( $titleid, $rangelib, $depth, $request_lib, $patron, $requestor, $pickup_lib ) = @_;
+
+       my $limit       = 10;
+       my $offset      = 0;
+       my $title;
+
+       $logger->debug("Fetching ranged title tree for title $titleid, org $rangelib, depth $depth");
+
+       while( $title = $U->storagereq(
+                               'open-ils.storage.biblio.record_entry.ranged_tree', 
+                               $titleid, $rangelib, $depth, $limit, $offset ) ) {
+
+               last unless 
+                       ref($title) and 
+                       ref($title->call_numbers) and 
+                       @{$title->call_numbers};
+
+               for my $cn (@{$title->call_numbers}) {
+       
+                       $logger->debug("Checking callnumber ".$cn->id." for hold fulfillment possibility");
+       
+                       for my $copy (@{$cn->copies}) {
+       
+                               $logger->debug("Checking copy ".$copy->id." for hold fulfillment possibility");
+       
+                               return 1 if OpenILS::Utils::PermitHold::permit_copy_hold(
+                                       {       patron                          => $patron, 
+                                               requestor                       => $requestor, 
+                                               copy                                    => $copy,
+                                               title                                   => $title, 
+                                               title_descriptor        => $title->fixed_fields, # this is fleshed into the title object
+                                               pickup_lib                      => $pickup_lib,
+                                               request_lib                     => $request_lib 
+                                       } 
+                               );
+       
+                               $logger->debug("Copy ".$copy->id." for hold fulfillment possibility failed...");
+                       }
+               }
+
+               $offset += $limit;
+       }
+       return 0;
+}
+
+
+
 1;
index 0431fcd..beee99c 100644 (file)
@@ -5,6 +5,7 @@ use OpenILS::Utils::CStoreEditor qw/:funcs/;
 use OpenILS::Application::AppUtils;
 use OpenILS::Application::Actor;
 my $U = "OpenILS::Application::AppUtils";
+use Data::Dumper;
 
 my $evt = "environment";
 my @COPY_STATUSES;
@@ -30,12 +31,18 @@ my @GROUP_LIST;
 sub build {
        my( $class, $args ) = @_;
 
+       my $evt;
+       my @evts;
+
        my $editor = $$args{editor} || new_editor();
 
        $args->{_direct} = {} unless $args->{_direct};
        
-       fetch_bib_data($editor, $args);
-       fetch_user_data($editor, $args);
+       $evt = fetch_bib_data($editor, $args);
+       push(@evts, $evt) if $evt;
+       $evt = fetch_user_data($editor, $args);
+       push(@evts, $evt) if $evt;
+       $args->{_event} = \@evts;
        return build_runner($editor, $args);
 }
 
@@ -48,9 +55,9 @@ sub build_runner {
        $runner->insert( "$evt.patron",         $ctx->{patron}, 1);
        $runner->insert( "$evt.copy",                   $ctx->{copy}, 1);
        $runner->insert( "$evt.volume",         $ctx->{volume}, 1);
-       $runner->insert( "$evt.record",         $ctx->{title}, 1);
+       $runner->insert( "$evt.title",          $ctx->{title}, 1);
        $runner->insert( "$evt.requestor",      $ctx->{requestor}, 1);
-       $runner->insert( "$evt.recordDescriptor", $ctx->{recordDescriptor}, 1);
+       $runner->insert( "$evt.titleDescriptor", $ctx->{titleDescriptor}, 1);
 
        $runner->insert( "$evt.patronItemsOut", $ctx->{patronItemsOut} );
        $runner->insert( "$evt.patronOverdueCount", $ctx->{patronOverdue} );
@@ -58,6 +65,7 @@ sub build_runner {
 
        # circ script result
        $runner->insert("result", {});
+       $runner->insert("result.event", 'SUCCESS');
        $runner->insert("result.events", []);
        $runner->insert('result.fatalEvents', []);
        $runner->insert('result.infoEvents', []);
@@ -96,24 +104,33 @@ sub fetch_bib_data {
                @COPY_STATUSES = @$s;
                $s = $e->retrieve_all_asset_copy_location();
                @COPY_LOCATIONS = @$s;
-               $s = $e->retrieve_all_permission_grp_tree();
-               @GROUP_LIST = @$s;
        }
 
        # Flesh the status and location
-       $copy->status( grep { $_->id == $copy->status } @COPY_STATUSES );
-       $copy->location( grep { $_->id == $copy->location } @COPY_LOCATIONS );
+       $copy->status( 
+               grep { $_->id == $copy->status } @COPY_STATUSES ) 
+               unless ref $copy->status;
+
+       $copy->location( 
+               grep { $_->id == $copy->location } @COPY_LOCATIONS ) 
+               unless ref $copy->location;
+
+       $copy->circ_lib( 
+               $e->retrieve_actor_org_unit($copy->circ_lib)) 
+               unless ref $copy->circ_lib;
 
        $ctx->{volume} = $e->retrieve_asset_call_number(
                $ctx->{copy}->call_number) or return $e->event;
 
-       $ctx->{record} = $e->retrieve_biblio_record_entry(
+       $ctx->{title} = $e->retrieve_biblio_record_entry(
                $ctx->{volume}->record) or return $e->event;
 
-       $ctx->{recordDescriptor} = $e->search_metabib_record_descriptor( 
-               { record => $ctx->{record}->id }) or return $e->event;
+       if(!$ctx->{titleDescriptor}) {
+               $ctx->{titleDescriptor} = $e->search_metabib_record_descriptor( 
+                       { record => $ctx->{title}->id }) or return $e->event;
 
-       $ctx->{recordDescriptor} = $ctx->{recordDescriptor}->[0];
+               $ctx->{titleDescriptor} = $ctx->{titleDescriptor}->[0];
+       }
 
        return undef;
 }
@@ -141,8 +158,21 @@ sub fetch_user_data {
 
        return undef unless my $patron = $ctx->{patron};
 
-       $patron->home_ou( $e->retrieve_actor_org_unit($patron->home_ou) );      
-       $patron->profile( grep { $_->id == $patron->profile } @GROUP_LIST );
+       $patron->home_ou( 
+               $e->retrieve_actor_org_unit($patron->home_ou) ) 
+               unless ref $patron->home_ou;
+
+
+       if(!@GROUP_LIST) {
+               my $s = $e->retrieve_all_permission_grp_tree();
+               @GROUP_LIST = @$s;
+       }
+
+       warn 'patron profile = ' . $patron->profile . "\n";
+       $patron->profile( 
+               grep { $_->id == $patron->profile } @GROUP_LIST ) 
+               unless ref $patron->profile;
+       warn 'patron profile = ' . $patron->profile->name . "\n";
 
        $ctx->{requestor} = $ctx->{requestor} || $e->requestor;
 
index b1c0464..0485abb 100644 (file)
@@ -6,7 +6,6 @@ use Data::Dumper;
 use OpenSRF::Utils;
 use OpenSRF::Utils::Cache;
 use Digest::MD5 qw(md5_hex);
-use OpenILS::Utils::ScriptRunner;
 use OpenILS::Utils::Editor;
 use OpenILS::Application::AppUtils;
 use OpenILS::Application::Circ::Holds;
index e38dd4d..7f035d3 100644 (file)
@@ -5,13 +5,11 @@ use Data::Dumper;
 use OpenSRF::EX qw(:try);
 use OpenSRF::Utils::Cache;
 use OpenSRF::Utils qw/:datetime/;
-use OpenILS::Utils::ScriptRunner;
+use OpenILS::Application::Circ::ScriptBuilder;
 use OpenSRF::Utils::SettingsClient;
 use OpenILS::Application::AppUtils;
 use OpenSRF::Utils::Logger qw(:logger);
 use base 'OpenSRF::Application';
-use OpenILS::Utils::Editor q/:funcs/;
-use OpenILS::Application::Actor;
 
 my $U = "OpenILS::Application::AppUtils";
 my $script;
@@ -43,51 +41,6 @@ sub initialize {
 }
 
 
-# --------------------------------------------------------------
-# Builds the script runner and shoves data into the script 
-# context
-# --------------------------------------------------------------
-sub build_runner {
-
-       my %args = @_;
-       my $patron = $args{patron};
-       my $fines = $args{fines};
-       my $circ_counts = $args{circ_counts};
-
-       my $pgroup = find_profile($patron);
-       $patron->profile( $pgroup );
-
-       if($runner) {
-               $runner->refresh_context if $runner;
-
-       } else {
-               $runner = OpenILS::Utils::ScriptRunner->new unless $runner;
-               $runner->add_path( $_ );
-       }
-
-       $runner->insert( 'environment.patron',  $patron, 1);
-       $runner->insert( $fatal_key, [] );
-       $runner->insert( $info_key, [] );
-       $runner->insert( 'environment.patronOverdueCount', $circ_counts->{overdue});
-       $runner->insert( 'environment.patronFines', $fines );
-
-       return $runner;
-}
-
-
-sub find_profile {
-       my $patron = shift;
-
-       if(!%groups) {
-               my $groups = $U->storagereq(
-                       'open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic');
-               %groups = map { $_->id => $_ } @$groups;
-       }
-
-       return $groups{$patron->profile};
-}
-
-
 
 __PACKAGE__->register_method (
        method   => 'patron_penalty',
@@ -120,40 +73,17 @@ sub patron_penalty {
        my( $patron, $evt );
 
        $conn->respond_complete(1) if $$args{background};
-       
-       my $e = new_editor(xact => 1);
 
-       if( $patron = $$args{patron} ) { # - unflesh if necessary
-               $patron->home_ou( $patron->home_ou->id ) if ref($patron->home_ou);
-               $patron->profile( $patron->profile->id ) if ref($patron->profile);
+       return { fatal_penalties => [], info_penalties => [] }
+               unless ($args->{patron} || $args->{patronid});
 
-       } else {
-               $patron = $e->retrieve_actor_user($$args{patronid})
-                       or return $e->event;
-       }
-
-       # - fetch the circulation summary info for the user
-       my $summary = $U->fetch_patron_circ_summary($patron->id);
-
-       # Note, that this ignores any negative balances
-       my $fxacts = $e->search_money_open_billable_transaction_summary(
-               { usr => $patron->id, balance_owed => { ">" => 0 } });
-       my $fines = 0;
-       $fines += $_->balance_owed for @$fxacts;
-
-       # - retrieve the number of open circulations the user has by type
-       # - we have to call this method directly because we don't have an auth session
-       my $circ_counts = OpenILS::Application::Actor::_checked_out(1, $e, $patron->id);
-
-       # - build the script runner
-       my $runner = build_runner( 
-               patron          => $patron, 
-               fines                   => $fines,
-               circ_counts     => $circ_counts,
-               );
+       $args->{patron_id} = $args->{patronid};
+       $args->{fetch_patron_circ_info} = 1;
+       my $runner = OpenILS::Application::Circ::ScriptBuilder->build($args);
 
        # - Load up the script and run it
        $runner->add_path($path);
+
        $runner->run($script) or 
                throw OpenSRF::EX::ERROR ("Patron Penalty Script Died: $@");
 
@@ -191,14 +121,14 @@ sub update_patron_penalties {
        my $penalties = $args{penalties};
 
        my $session   = $U->start_db_session();
+       my $pid = ($patron) ? $patron->id : $args{patronid};
 
        # - fetch the current penalties
        my $existing = $session->request(
                'open-ils.storage.direct.actor.'.
-               'user_standing_penalty.search.usr.atomic', $patron->id )->gather(1);
+               'user_standing_penalty.search.usr.atomic', $pid )->gather(1);
 
        my @deleted;
-       my $patronid = $patron->id;
 
        # If an existing penalty is not in the newly generated 
        # list of penalties, remove it from the DB
@@ -206,7 +136,7 @@ sub update_patron_penalties {
                if( ! grep { $_ eq $e->penalty_type } @$penalties ) {
 
                        $logger->activity("penalty: removing user penalty ".
-                               $e->penalty_type . " from user $patronid");
+                               $e->penalty_type . " from user $pid");
 
                        my $s = $session->request(
                                'open-ils.storage.direct.actor.user_standing_penalty.delete', $e->id )->gather(1);
@@ -218,11 +148,11 @@ sub update_patron_penalties {
        for my $p (@$penalties) {
                if( ! grep { $_->penalty_type eq $p } @$existing ) {
 
-                       $logger->activity("penalty: adding user penalty $p to user $patronid");
+                       $logger->activity("penalty: adding user penalty $p to user $pid");
 
                        my $newp = Fieldmapper::actor::user_standing_penalty->new;
                        $newp->penalty_type( $p );
-                       $newp->usr( $patronid );
+                       $newp->usr( $pid );
 
                        my $s = $session->request(
                                'open-ils.storage.direct.actor.user_standing_penalty.create', $newp )->gather(1);
index 509faff..9203e5f 100644 (file)
@@ -7,7 +7,8 @@ use JSON;
 use OpenILS::Utils::Fieldmapper;
 use OpenILS::Utils::ModsParser;
 use OpenSRF::Utils::SettingsClient;
-use OpenILS::Utils::Editor q/:funcs/;
+#use OpenILS::Utils::Editor q/:funcs/;
+use OpenILS::Utils::CStoreEditor q/:funcs/;
 use OpenSRF::Utils::Cache;
 
 use OpenSRF::Utils::Logger qw/:logger/;
@@ -376,7 +377,7 @@ sub the_quest_for_knowledge {
                my $errstr;
 
                try {
-                       $result = new_editor()->request( $method, %$searchhash );
+                       $result = $U->storagereq( $method, %$searchhash );
 
                } catch Error with {
                        my $err = shift;
@@ -544,15 +545,9 @@ sub biblio_mrid_check_mvr {
 
 sub _grab_metarecord {
        my $mrid = shift;
-       my $e = OpenILS::Utils::Editor->new;
+       #my $e = OpenILS::Utils::Editor->new;
+       my $e = new_editor();
        my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
-
-       # XXX - test
-#      my $mr = $U->simplereq(
-#              'open-ils.cstore', 
-#              'open-ils.cstore.direct.metabib.metarecord.retrieve', 
-#              $mrid);
-
        return ($mr);
 }
 
@@ -569,7 +564,8 @@ __PACKAGE__->register_method(
 sub biblio_mrid_make_modsbatch {
        my( $self, $client, $mrid ) = @_;
 
-       my $e = OpenILS::Utils::Editor->new;
+       #my $e = OpenILS::Utils::Editor->new;
+       my $e = new_editor();
 
        my $mr;
        if( ref($mrid) ) {
@@ -583,7 +579,7 @@ sub biblio_mrid_make_modsbatch {
        my $masterid = $mr->master_record;
        $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
 
-       my $ids = $e->request(
+       my $ids = $U->storagereq(
                'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
        return undef unless @$ids;
 
@@ -614,7 +610,8 @@ sub biblio_mrid_make_modsbatch {
        my $string = JSON->perl2JSON($mods->decast);
        $mr->mods($string);
 
-       $e = OpenILS::Utils::Editor->new(xact => 1);
+       #$e = OpenILS::Utils::Editor->new(xact => 1);
+       $e = new_editor(xact => 1);
        $e->update_metabib_metarecord($mr) 
                or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
        $e->finish;
@@ -918,7 +915,7 @@ sub biblio_search_isbn {
        my( $self, $client, $isbn ) = @_;
        $logger->debug("Searching ISBN $isbn");
        my $e = new_editor();
-       my $recs = $e->request(
+       my $recs = $U->storagereq(
                'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
        return { ids => $recs, count => scalar(@$recs) };
 }
@@ -933,7 +930,7 @@ sub biblio_search_issn {
        my( $self, $client, $issn ) = @_;
        $logger->debug("Searching ISSN $issn");
        my $e = new_editor();
-       my $recs = $e->request(
+       my $recs = $U->storagereq(
                'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
        return { ids => $recs, count => scalar(@$recs) };
 }
@@ -1094,7 +1091,8 @@ __PACKAGE__->register_method(
 sub fetch_slim_record {
        my( $self, $conn, $ids ) = @_;
 
-       my $editor = OpenILS::Utils::Editor->new;
+       #my $editor = OpenILS::Utils::Editor->new;
+       my $editor = new_editor();
        my @res;
        for( @$ids ) {
                return $editor->event unless
index 77949ba..307d1e8 100644 (file)
@@ -377,7 +377,7 @@ sub runmethod {
        } elsif( $action eq 'retrieve_all' ) {
                $action = 'search';
                $method =~ s/retrieve_all/search/o;
-               @arg = ( { id => { '!=' => 0 } } );
+               @arg = ( { id => { '!=' => undef } } );
                $method = "$method.atomic";
        }
 
index 60430c2..bc303c0 100644 (file)
@@ -5,6 +5,7 @@ use OpenSRF::Utils;
 use OpenSRF::Utils::SettingsClient;
 use OpenILS::Utils::ScriptRunner;
 use OpenILS::Application::AppUtils;
+use OpenILS::Application::Circ::ScriptBuilder;
 use OpenSRF::Utils::Logger qw(:logger);
 my $U  = "OpenILS::Application::AppUtils";
 
@@ -20,19 +21,33 @@ sub permit_copy_hold {
 
        my $params      = shift;
        my $k                   = 'environment';
-       my $runner      = OpenILS::Utils::ScriptRunner->new;
-
-       $runner->insert( "$k.patron",                           $$params{patron},                               1);
-       $runner->insert( "$k.title",                            $$params{title},                                1);
-       $runner->insert( "$k.copy",                             $$params{copy},                         1);
-       $runner->insert( "$k.requestor",                        $$params{requestor},                    1);
-       $runner->insert( "$k.requestLib",               $$params{request_lib},          1);
-       $runner->insert( "$k.pickupLib",                        $$params{pickup_lib},           1);
-       $runner->insert( "$k.titleDescriptor",  $$params{title_descriptor},1);
-
-       # we get the script result from the event 
-       $runner->insert( "result.event",        'SUCCESS' );
-       $runner->insert( "result.events", [] );
+
+       my $runner = OpenILS::Application::Circ::ScriptBuilder->build(
+               {
+                       patron          => $$params{patron},
+                       copy                    => $$params{copy},
+                       requestor       => $$params{requestor},
+                       titleDescriptor => $$params{title_descriptor},
+                       _direct => {
+                               requestLib      => $$params{request_lib},
+                               pickupLib       => $$params{pickup_lib},
+                       }
+               }
+       );
+
+#      my $runner      = OpenILS::Utils::ScriptRunner->new;
+#
+#      $runner->insert( "$k.patron",                           $$params{patron},                               1);
+#      $runner->insert( "$k.title",                            $$params{title},                                1);
+#      $runner->insert( "$k.copy",                             $$params{copy},                         1);
+#      $runner->insert( "$k.requestor",                        $$params{requestor},                    1);
+#      $runner->insert( "$k.requestLib",               $$params{request_lib},          1);
+#      $runner->insert( "$k.pickupLib",                        $$params{pickup_lib},           1);
+#      $runner->insert( "$k.titleDescriptor",  $$params{title_descriptor},1);
+
+#      # we get the script result from the event 
+#      $runner->insert( "result.event",        'SUCCESS' );
+#      $runner->insert( "result.events", [] );
 
        $logger->debug("Running permit_copy_hold on copy " . $$params{copy}->id);