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::Logger qw/$logger/;
5 use OpenILS::Utils::CStoreEditor qw/:funcs/;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Application::AppUtils;
8 my $U = 'OpenILS::Application::AppUtils';
11 map => {aou => {}}, # others added dynamically as needed
16 sub init_ro_object_cache {
18 my $e = $self->editor;
21 # fetch-on-demand-and-cache subs for commonly used public data
22 my @public_classes = qw/ccs aout cifm citm clm cmf crahp/;
24 for my $hint (@public_classes) {
27 $Fieldmapper::fieldmap->{$_}->{hint} eq $hint
28 } keys %{ $Fieldmapper::fieldmap };
30 my $ident_field = $Fieldmapper::fieldmap->{$class}->{identity};
32 $class =~ s/Fieldmapper:://o;
36 my $list_key = $hint . '_list';
37 my $find_key = "find_$hint";
39 $ctx->{$list_key} = sub {
40 my $method = "retrieve_all_$class";
41 $cache{list}{$hint} = $e->$method() unless $cache{list}{$hint};
42 return $cache{list}{$hint};
45 $cache{map}{$hint} = {} unless $cache{map}{$hint};
47 $ctx->{$find_key} = sub {
49 return $cache{map}{$hint}{$id} if $cache{map}{$hint}{$id};
50 ($cache{map}{$hint}{$id}) = grep { $_->$ident_field eq $id } @{$ctx->{$list_key}->()};
51 return $cache{map}{$hint}{$id};
56 $ctx->{aou_tree} = sub {
58 # fetch the org unit tree
59 unless($cache{aou_tree}) {
60 my $tree = $e->search_actor_org_unit([
61 { parent_ou => undef},
63 flesh_fields => {aou => ['children']},
64 order_by => {aou => 'name'}
68 # flesh the org unit type for each org unit
69 # and simultaneously set the id => aou map cache
73 $node->ou_type( $ctx->{find_aout}->($node->ou_type) );
74 $cache{map}{aou}{$node->id} = $node;
75 flesh_aout($_, $ctx) foreach @{$node->children};
77 flesh_aout($tree, $ctx);
79 $cache{aou_tree} = $tree;
82 return $cache{aou_tree};
85 # Add a special handler for the tree-shaped org unit cache
86 $ctx->{find_aou} = sub {
88 $ctx->{aou_tree}->(); # force the org tree to load
89 return $cache{map}{aou}{$org_id};
92 # turns an ISO date into something TT can understand
93 $ctx->{parse_datetime} = sub {
95 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
97 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
107 # retrieve and cache org unit setting values
108 $ctx->{get_org_setting} = sub {
109 my($org_id, $setting) = @_;
111 $cache{org_settings}{$org_id} = {}
112 unless $cache{org_settings}{$org_id};
114 $cache{org_settings}{$org_id}{$setting} =
115 $U->ou_ancestor_setting_value($org_id, $setting)
116 unless exists $cache{org_settings}{$org_id}{$setting};
118 return $cache{org_settings}{$org_id}{$setting};
122 sub generic_redirect {
125 my $cookie = shift; # can be an array of cgi.cookie's
127 $self->apache->print(
128 $self->cgi->redirect(
130 $self->cgi->param('redirect_to') ||
131 $self->ctx->{referer} ||
132 $self->ctx->{home_page},
137 return Apache2::Const::REDIRECT;
140 sub get_records_and_facets {
141 my ($self, $rec_ids, $facet_key) = @_;
143 my $cstore = OpenSRF::AppSession->create('open-ils.cstore');
144 my $bre_req = $cstore->request(
145 'open-ils.cstore.direct.biblio.record_entry.search', {id => $rec_ids}
148 my $search = OpenSRF::AppSession->create('open-ils.search');
149 my $facet_req = $search->request(
150 'open-ils.search.facet_cache.retrieve', $facet_key, 10
154 while (my $resp = $bre_req->recv) {
155 my $bre = $resp->content;
157 # XXX farm out to multiple cstore sessions before loop,
159 my $copy_counts = $self->editor->json_query(
160 {from => ['asset.record_copy_count', 1, $bre->id, 0]}
165 marc_xml => XML::LibXML->new->parse_string($bre->marc),
166 copy_counts => $copy_counts
174 $facets = $facet_req->gather(1);
177 cmf => $self->ctx->{find_cmf}->($_),
178 data => $facets->{$_}
179 } for keys %$facets; # quick-n-dirty
184 return ($facets, @data);
187 sub fetch_marc_xml_by_id {
188 my ($self, $id_list) = @_;
189 $id_list = [$id_list] unless ref($id_list);
190 return {} if scalar(grep { defined $_ } @$id_list) < 1;
192 # I'm just sure there needs to be some more efficient way to get all of
194 my $results = $self->editor->json_query({
195 "select" => {"bre" => ["id", "marc"]},
196 "from" => {"bre" => {}},
197 "where" => {"id" => $id_list}
198 }) or return $self->editor->die_event;
201 for my $r (@$results) {
202 $marc_xml->{$r->{"id"}} =
203 (new XML::LibXML)->parse_string($r->{"marc"});