]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Library.pm
LP#1452352: fix generation of locale-specific RO object fetchers
[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 OpenILS::Utils::CStoreEditor qw/:funcs/;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Application::AppUtils;
9 my $U = 'OpenILS::Application::AppUtils';
10 my $library_cache;
11
12 # context additions: 
13 #   library : aou object
14 #   parent: aou object
15 sub load_library {
16     my $self = shift;
17     my %kwargs = @_;
18     my $ctx = $self->ctx;
19     $ctx->{page} = 'library';  
20
21     $self->timelog("load_library() began");
22
23     my $lib_id = $ctx->{page_args}->[0];
24     $lib_id = $self->_resolve_org_id_or_shortname($lib_id);
25
26     return Apache2::Const::HTTP_BAD_REQUEST unless $lib_id;
27
28     my $aou = $ctx->{get_aou}->($lib_id);
29     my $sname = $aou->parent_ou;
30
31     $ctx->{library} = $aou;
32     if ($aou->parent_ou) {
33         $ctx->{parent} = $ctx->{get_aou}->($aou->parent_ou);
34     }
35
36     $self->timelog("got basic lib info");
37
38     # Get mailing address from the cache
39     $library_cache ||= OpenSRF::Utils::Cache->new('global');
40     my $address_cache_key = "TPAC_aou_address_cache_$lib_id";
41     my $address = OpenSRF::Utils::JSON->JSON2perl($library_cache->get_cache($address_cache_key));
42
43     if ($address) {
44         $ctx->{mailing_address} = $address;
45     } elsif (!$address && $aou->mailing_address) {
46         # We didn't get cached hours, so hit the database
47         my $session = OpenSRF::AppSession->create("open-ils.actor");
48         $ctx->{mailing_address} =
49             $session->request('open-ils.actor.org_unit.address.retrieve',
50             $aou->mailing_address)->gather(1);
51         $library_cache->put_cache($address_cache_key, OpenSRF::Utils::JSON->perl2JSON($ctx->{mailing_address}), 360);
52     }
53
54     # Get current hours of operation
55     my $hours_cache_key = "TPAC_aouhoo_cache_$lib_id";
56     my $hours = OpenSRF::Utils::JSON->JSON2perl($library_cache->get_cache($hours_cache_key));
57
58     # If we don't have cached hours, try the database
59     if (!$hours) {
60         $hours = $self->editor->retrieve_actor_org_unit_hours_of_operation($lib_id);
61         # If we got hours from the database, cache them
62         if ($hours) {
63             $library_cache->put_cache($hours_cache_key, OpenSRF::Utils::JSON->perl2JSON($hours), 360);
64         }
65     }
66
67     # After all that, if we have hours, pass them to the context object
68     if ($hours) {
69         $ctx->{hours} = $hours;
70     }
71
72     return Apache2::Const::OK;
73 }
74
75 1;