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 use OpenSRF::MultiSession;
9 my $U = 'OpenILS::Application::AppUtils';
11 my $ro_object_subs; # cached subs
12 our %cache = ( # cached data
13 map => {aou => {}}, # others added dynamically as needed
19 sub init_ro_object_cache {
21 my $e = $self->editor;
24 # reset org unit setting cache on each page load to avoid the
25 # requirement of reloading apache with each org-setting change
26 $cache{org_settings} = {};
29 # subs have been built. insert into the context then move along.
30 $ctx->{$_} = $ro_object_subs->{$_} for keys %$ro_object_subs;
34 # make all "field_safe" classes accesible by default in the template context
36 ($Fieldmapper::fieldmap->{$_}->{field_safe} || '') =~ /true/i
37 } keys %{ $Fieldmapper::fieldmap };
39 for my $class (@classes) {
41 my $hint = $Fieldmapper::fieldmap->{$class}->{hint};
42 next if $hint eq 'aou'; # handled separately
44 my $ident_field = $Fieldmapper::fieldmap->{$class}->{identity};
45 (my $eclass = $class) =~ s/Fieldmapper:://o;
48 my $list_key = "${hint}_list";
49 my $get_key = "get_$hint";
50 my $search_key = "search_$hint";
52 # Retrieve the full set of objects with class $hint
53 $ro_object_subs->{$list_key} = sub {
54 my $method = "retrieve_all_$eclass";
55 $cache{list}{$hint} = $e->$method() unless $cache{list}{$hint};
56 return $cache{list}{$hint};
59 # locate object of class $hint with Ident field $id
60 $cache{map}{$hint} = {};
61 $ro_object_subs->{$get_key} = sub {
63 return $cache{map}{$hint}{$id} if $cache{map}{$hint}{$id};
64 ($cache{map}{$hint}{$id}) = grep { $_->$ident_field eq $id } @{$ro_object_subs->{$list_key}->()};
65 return $cache{map}{$hint}{$id};
68 # search for objects of class $hint where field=value
69 $cache{search}{$hint} = {};
70 $ro_object_subs->{$search_key} = sub {
71 my ($field, $val) = @_;
72 my $method = "search_$eclass";
73 $cache{search}{$hint}{$field} = {} unless $cache{search}{$hint}{$field};
74 $cache{search}{$hint}{$field}{$val} = $e->$method({$field => $val})
75 unless $cache{search}{$hint}{$field}{$val};
76 return $cache{search}{$hint}{$field}{$val};
80 $ro_object_subs->{aou_tree} = sub {
82 # fetch the org unit tree
83 unless($cache{aou_tree}) {
84 my $tree = $e->search_actor_org_unit([
85 { parent_ou => undef},
87 flesh_fields => {aou => ['children']},
88 order_by => {aou => 'name'}
92 # flesh the org unit type for each org unit
93 # and simultaneously set the id => aou map cache
96 my $ro_object_subs = shift;
97 $node->ou_type( $ro_object_subs->{get_aout}->($node->ou_type) );
98 $cache{map}{aou}{$node->id} = $node;
99 flesh_aout($_, $ro_object_subs) foreach @{$node->children};
101 flesh_aout($tree, $ro_object_subs);
103 $cache{aou_tree} = $tree;
106 return $cache{aou_tree};
109 # Add a special handler for the tree-shaped org unit cache
110 $ro_object_subs->{get_aou} = sub {
112 return undef unless defined $org_id;
113 $ro_object_subs->{aou_tree}->(); # force the org tree to load
114 return $cache{map}{aou}{$org_id};
117 # Returns a flat list of aou objects. often easier to manage than a tree.
118 $ro_object_subs->{aou_list} = sub {
119 $ro_object_subs->{aou_tree}->(); # force the org tree to load
120 return [ values %{$cache{map}{aou}} ];
124 # turns an ISO date into something TT can understand
125 $ro_object_subs->{parse_datetime} = sub {
127 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
129 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
139 # retrieve and cache org unit setting values
140 $ro_object_subs->{get_org_setting} = sub {
141 my($org_id, $setting) = @_;
143 $cache{org_settings}{$org_id} = {}
144 unless $cache{org_settings}{$org_id};
146 $cache{org_settings}{$org_id}{$setting} =
147 $U->ou_ancestor_setting_value($org_id, $setting)
148 unless exists $cache{org_settings}{$org_id}{$setting};
150 return $cache{org_settings}{$org_id}{$setting};
153 $ctx->{$_} = $ro_object_subs->{$_} for keys %$ro_object_subs;
156 sub generic_redirect {
159 my $cookie = shift; # can be an array of cgi.cookie's
161 $self->apache->print(
162 $self->cgi->redirect(
164 $self->cgi->param('redirect_to') ||
165 $self->ctx->{referer} ||
166 $self->ctx->{home_page},
171 return Apache2::Const::REDIRECT;
174 sub get_records_and_facets {
175 my ($self, $rec_ids, $facet_key, $unapi_args) = @_;
178 $unapi_args->{site} ||= $self->ctx->{aou_tree}->()->shortname;
179 $unapi_args->{depth} ||= $self->ctx->{aou_tree}->()->ou_type->depth;
180 $unapi_args->{flesh_depth} ||= 5;
183 my $ses = OpenSRF::MultiSession->new(
184 app => 'open-ils.cstore',
185 cap => 10, # XXX config
186 success_handler => sub {
187 my($self, $req) = @_;
188 my $data = $req->{response}->[0]->content;
189 my $xml = XML::LibXML->new->parse_string($data->{'unapi.bre'})->documentElement;
191 # Protect against legacy invalid MARCXML that might not have a 901c
193 my $bre_id_nodes = $xml->find('*[@tag="901"]/*[@code="c"]');
195 $bre_id = $bre_id_nodes->[0]->textContent;
197 $logger->warn("Missing 901 subfield 'c' in " . $xml->toString());
199 push(@data, {id => $bre_id, marc_xml => $xml});
204 'open-ils.cstore.json_query',
206 'unapi.bre', $_, 'marcxml','record',
207 $unapi_args->{flesh},
209 $unapi_args->{depth},
210 $unapi_args->{flesh_depth},
214 # collect the facet data
215 my $search = OpenSRF::AppSession->create('open-ils.search');
216 my $facet_req = $search->request(
217 'open-ils.search.facet_cache.retrieve', $facet_key, 10
220 # gather up the unapi recs
221 $ses->session_wait(1);
225 my $tmp_facets = $facet_req->gather(1);
226 for my $cmf_id (keys %$tmp_facets) {
228 # sort highest to lowest match count
230 my $entries = $tmp_facets->{$cmf_id};
231 for my $ent (keys %$entries) {
232 push(@entries, {value => $ent, count => $$entries{$ent}});
234 @entries = sort { $b->{count} <=> $a->{count} } @entries;
235 $facets->{$cmf_id} = {
236 cmf => $self->ctx->{get_cmf}->($cmf_id),
246 return ($facets, @data);
249 # TODO: blend this code w/ ^-- get_records_and_facets
250 sub fetch_marc_xml_by_id {
251 my ($self, $id_list) = @_;
252 $id_list = [$id_list] unless ref($id_list);
255 no warnings qw/numeric/;
256 $id_list = [map { int $_ } @$id_list];
257 $id_list = [grep { $_ > 0} @$id_list];
260 return {} if scalar(@$id_list) < 1;
262 # I'm just sure there needs to be some more efficient way to get all of
264 my $results = $self->editor->json_query({
265 "select" => {"bre" => ["id", "marc"]},
266 "from" => {"bre" => {}},
267 "where" => {"id" => $id_list}
268 }, {substream => 1}) or return $self->editor->die_event;
271 for my $r (@$results) {
272 $marc_xml->{$r->{"id"}} =
273 (new XML::LibXML)->parse_string($r->{"marc"});
279 sub _get_search_lib {
282 # loc param takes precedence
283 my $loc = $self->cgi->param('loc');
286 if ($self->ctx->{user}) {
287 # See if the user has a search library preference
288 my $lset = $self->editor->search_actor_user_setting({
289 usr => $self->ctx->{user}->id,
290 name => 'opac.default_search_location'
292 return OpenSRF::Utils::JSON->JSON2perl($lset->value) if $lset;
294 # Otherwise return the user's home library
295 return $self->ctx->{user}->home_ou;
298 if ($self->cgi->param('physical_loc')) {
299 return $self->cgi->param('physical_loc');
302 return $self->ctx->{aou_tree}->()->id;