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;
25 # subs have been built. insert into the context then move along.
26 $ctx->{$_} = $ro_object_subs->{$_} for keys %$ro_object_subs;
30 # make all "field_safe" classes accesible by default in the template context
32 ($Fieldmapper::fieldmap->{$_}->{field_safe} || '') =~ /true/i
33 } keys %{ $Fieldmapper::fieldmap };
35 for my $class (@classes) {
37 my $hint = $Fieldmapper::fieldmap->{$class}->{hint};
38 next if $hint eq 'aou'; # handled separately
40 my $ident_field = $Fieldmapper::fieldmap->{$class}->{identity};
41 (my $eclass = $class) =~ s/Fieldmapper:://o;
44 my $list_key = "${hint}_list";
45 my $get_key = "get_$hint";
46 my $search_key = "search_$hint";
48 # Retrieve the full set of objects with class $hint
49 $ro_object_subs->{$list_key} = sub {
50 my $method = "retrieve_all_$eclass";
51 $cache{list}{$hint} = $e->$method() unless $cache{list}{$hint};
52 return $cache{list}{$hint};
55 # locate object of class $hint with Ident field $id
56 $cache{map}{$hint} = {};
57 $ro_object_subs->{$get_key} = sub {
59 return $cache{map}{$hint}{$id} if $cache{map}{$hint}{$id};
60 ($cache{map}{$hint}{$id}) = grep { $_->$ident_field eq $id } @{$ro_object_subs->{$list_key}->()};
61 return $cache{map}{$hint}{$id};
64 # search for objects of class $hint where field=value
65 $cache{search}{$hint} = {};
66 $ro_object_subs->{$search_key} = sub {
67 my ($field, $val) = @_;
68 my $method = "search_$eclass";
69 $cache{search}{$hint}{$field} = {} unless $cache{search}{$hint}{$field};
70 $cache{search}{$hint}{$field}{$val} = $e->$method({$field => $val})
71 unless $cache{search}{$hint}{$field}{$val};
72 return $cache{search}{$hint}{$field}{$val};
76 $ro_object_subs->{aou_tree} = sub {
78 # fetch the org unit tree
79 unless($cache{aou_tree}) {
80 my $tree = $e->search_actor_org_unit([
81 { parent_ou => undef},
83 flesh_fields => {aou => ['children']},
84 order_by => {aou => 'name'}
88 # flesh the org unit type for each org unit
89 # and simultaneously set the id => aou map cache
92 my $ro_object_subs = shift;
93 $node->ou_type( $ro_object_subs->{get_aout}->($node->ou_type) );
94 $cache{map}{aou}{$node->id} = $node;
95 flesh_aout($_, $ro_object_subs) foreach @{$node->children};
97 flesh_aout($tree, $ro_object_subs);
99 $cache{aou_tree} = $tree;
102 return $cache{aou_tree};
105 # Add a special handler for the tree-shaped org unit cache
106 $ro_object_subs->{get_aou} = sub {
108 $ro_object_subs->{aou_tree}->(); # force the org tree to load
109 return $cache{map}{aou}{$org_id};
112 # turns an ISO date into something TT can understand
113 $ro_object_subs->{parse_datetime} = sub {
115 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
117 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
127 # retrieve and cache org unit setting values
128 $ro_object_subs->{get_org_setting} = sub {
129 my($org_id, $setting) = @_;
131 $cache{org_settings}{$org_id} = {}
132 unless $cache{org_settings}{$org_id};
134 $cache{org_settings}{$org_id}{$setting} =
135 $U->ou_ancestor_setting_value($org_id, $setting)
136 unless exists $cache{org_settings}{$org_id}{$setting};
138 return $cache{org_settings}{$org_id}{$setting};
141 $ctx->{$_} = $ro_object_subs->{$_} for keys %$ro_object_subs;
144 sub generic_redirect {
147 my $cookie = shift; # can be an array of cgi.cookie's
149 $self->apache->print(
150 $self->cgi->redirect(
152 $self->cgi->param('redirect_to') ||
153 $self->ctx->{referer} ||
154 $self->ctx->{home_page},
159 return Apache2::Const::REDIRECT;
162 sub get_records_and_facets {
163 my ($self, $rec_ids, $facet_key, $unapi_args) = @_;
166 $unapi_args->{site} ||= $self->ctx->{aou_tree}->()->shortname;
167 $unapi_args->{depth} ||= $self->ctx->{aou_tree}->()->ou_type->depth;
168 $unapi_args->{flesh_depth} ||= 5;
171 my $ses = OpenSRF::MultiSession->new(
172 app => 'open-ils.cstore',
173 cap => 10, # XXX config
174 success_handler => sub {
175 my($self, $req) = @_;
176 my $data = $req->{response}->[0]->content;
177 my $xml = XML::LibXML->new->parse_string($data->{'unapi.bre'})->documentElement;
178 my $bre_id = $xml->find('*[@tag="901"]/*[@code="c"]')->[0]->textContent;
179 push(@data, {id => $bre_id, marc_xml => $xml});
184 'open-ils.cstore.json_query',
186 'unapi.bre', $_, 'marcxml','record',
187 $unapi_args->{flesh},
189 $unapi_args->{depth},
190 $unapi_args->{flesh_depth},
194 # collect the facet data
195 my $search = OpenSRF::AppSession->create('open-ils.search');
196 my $facet_req = $search->request(
197 'open-ils.search.facet_cache.retrieve', $facet_key, 10
200 # gather up the unapi recs
201 $ses->session_wait(1);
205 $facets = $facet_req->gather(1);
207 cmf => $self->ctx->{get_cmf}->($_),
208 data => $facets->{$_}
209 } for keys %$facets; # quick-n-dirty
214 return ($facets, @data);
217 # TODO: blend this code w/ ^-- get_records_and_facets
218 sub fetch_marc_xml_by_id {
219 my ($self, $id_list) = @_;
220 $id_list = [$id_list] unless ref($id_list);
223 no warnings qw/numeric/;
224 $id_list = [map { int $_ } @$id_list];
225 $id_list = [grep { $_ > 0} @$id_list];
228 return {} if scalar(@$id_list) < 1;
230 # I'm just sure there needs to be some more efficient way to get all of
232 my $results = $self->editor->json_query({
233 "select" => {"bre" => ["id", "marc"]},
234 "from" => {"bre" => {}},
235 "where" => {"id" => $id_list}
236 }) or return $self->editor->die_event;
239 for my $r (@$results) {
240 $marc_xml->{$r->{"id"}} =
241 (new XML::LibXML)->parse_string($r->{"marc"});