]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/PermitHold.pm
added cleanup method to script runner
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / PermitHold.pm
1 package OpenILS::Utils::PermitHold;
2 use strict; use warnings;
3 use Data::Dumper;
4 use OpenSRF::Utils;
5 use OpenSRF::Utils::SettingsClient;
6 use OpenILS::Utils::ScriptRunner;
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Application::Circ::ScriptBuilder;
9 use OpenSRF::Utils::Logger qw(:logger);
10 my $U   = "OpenILS::Application::AppUtils";
11
12 my $script;                     # - the permit script
13 my $script_libs;        # - extra script libs
14
15 # mental note:  open-ils.storage.biblio.record_entry.ranged_tree
16
17
18 # params within a hash are: copy, patron, 
19 # requestor, request_lib, title, title_descriptor
20 sub permit_copy_hold {
21         my $params      = shift;
22
23         my $runner = OpenILS::Application::Circ::ScriptBuilder->build(
24                 {
25                         patron          => $$params{patron},
26                         copy                    => $$params{copy},
27                         requestor       => $$params{requestor},
28                         titleDescriptor => $$params{title_descriptor},
29                         _direct => {
30                                 requestLib      => $$params{request_lib},
31                                 pickupLib       => $$params{pickup_lib},
32                         }
33                 }
34         );
35
36         $logger->debug("Running permit_copy_hold on copy " . $$params{copy}->id);
37
38         load_scripts($runner);
39         my $result = $runner->run or 
40                 throw OpenSRF::EX::ERROR ("Hold Copy Permit Script Died: $@");
41
42         $runner->cleanup;
43
44         # --------------------------------------------------------------
45         # Extract and uniquify the event list
46         # --------------------------------------------------------------
47         my $events = $result->{events};
48         $logger->debug("circ_permit_hold for user ".$params->{patron}->id." returned events: @$events");
49
50         my @allevents;
51         push( @allevents, OpenILS::Event->new($_)) for @$events;
52         my %hash = map { ($_->{ilsevent} => $_) } @allevents;
53         @allevents = values %hash;
54
55         return \@allevents if $$params{show_event_list};
56         return 1 unless @allevents;
57         return 0;
58 }
59
60
61 sub load_scripts {
62         my $runner = shift;
63
64         if(!$script) {
65                 my $conf = OpenSRF::Utils::SettingsClient->new;
66                 my @pfx = ( "apps", "open-ils.circ","app_settings" );
67                 my $libs        = $conf->config_value(@pfx, 'script_path');
68                 $script = $conf->config_value(@pfx, 'scripts', 'circ_permit_hold');
69                 $script_libs = (ref($libs)) ? $libs : [$libs];
70         }
71
72         $runner->add_path($_) for(@$script_libs);
73         $runner->load($script);
74 }
75
76
77
78 23;