]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Library.pm
LP2061136 - Stamping 1405 DB upgrade script
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Library.pm
1 package OpenILS::WWW::EGCatLoader;
2 use strict; use warnings;
3 use Apache2::Const -compile => qw(OK DECLINED FORBIDDEN HTTP_INTERNAL_SERVER_ERROR REDIRECT HTTP_BAD_REQUEST);
4 use OpenSRF::Utils::JSON;
5 use OpenSRF::Utils::Logger qw/$logger/;
6 use DateTime;
7 use DateTime::Format::ISO8601;
8 use OpenILS::Utils::CStoreEditor qw/:funcs/;
9 use OpenILS::Utils::Fieldmapper;
10 use OpenILS::Application::AppUtils;
11 my $U = 'OpenILS::Application::AppUtils';
12 my $library_cache;
13
14 # context additions: 
15 #   library : aou object
16 #   parent: aou object
17 sub load_library {
18     my $self = shift;
19     my %kwargs = @_;
20     my $ctx = $self->ctx;
21     $ctx->{page} = 'library';  
22
23     $self->timelog("load_library() began");
24
25     my $lib_id = $ctx->{page_args}->[0];
26     $lib_id = $self->_resolve_org_id_or_shortname($lib_id);
27
28     return Apache2::Const::HTTP_BAD_REQUEST unless $lib_id;
29
30     my $aou = $ctx->{get_aou}->($lib_id);
31     my $sname = $aou->parent_ou;
32
33     $ctx->{library} = $aou;
34     if ($aou->parent_ou) {
35         $ctx->{parent} = $ctx->{get_aou}->($aou->parent_ou);
36     }
37
38     $self->timelog("got basic lib info");
39
40     # Get mailing address from the cache
41     $library_cache ||= OpenSRF::Utils::Cache->new('global');
42     my $address_cache_key = "TPAC_aou_address_cache_$lib_id";
43     my $address = OpenSRF::Utils::JSON->JSON2perl($library_cache->get_cache($address_cache_key));
44
45     if ($address) {
46         $ctx->{mailing_address} = $address;
47     } elsif (!$address && $aou->mailing_address) {
48         # We didn't get cached hours, so hit the database
49         my $session = OpenSRF::AppSession->create("open-ils.actor");
50         $ctx->{mailing_address} =
51             $session->request('open-ils.actor.org_unit.address.retrieve',
52             $aou->mailing_address)->gather(1);
53         $library_cache->put_cache($address_cache_key, OpenSRF::Utils::JSON->perl2JSON($ctx->{mailing_address}), 360);
54     }
55
56     # Get current hours of operation
57     my $hours_cache_key = "TPAC_aouhoo_cache_$lib_id";
58     my $hours = OpenSRF::Utils::JSON->JSON2perl($library_cache->get_cache($hours_cache_key));
59
60     # If we don't have cached hours, try the database
61     if (!$hours) {
62         $hours = $self->editor->retrieve_actor_org_unit_hours_of_operation($lib_id);
63         # If we got hours from the database, cache them
64         if ($hours) {
65             $library_cache->put_cache($hours_cache_key, OpenSRF::Utils::JSON->perl2JSON($hours), 360);
66         }
67     }
68
69     # After all that, if we have hours, pass them to the context object
70     if ($hours) {
71         $ctx->{hours} = $hours;
72     }
73
74     # Get upcoming closed dates
75     my $dt = DateTime->now(time_zone => 'local');
76     my $start = $dt->year .'-'. $dt->month .'-'. $dt->day;
77
78     my $dates = $self->editor->search_actor_org_unit_closed_date([
79         {close_end => { ">=" => $start },
80             org_unit => $lib_id
81         },
82         {order_by => {aoucd => 'close_start'},
83             limit => 10
84         }
85     ]);
86
87     $ctx->{closed_dates} = $dates;
88
89     return Apache2::Const::OK;
90 }
91
92 1;