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 # turns an ISO date into something TT can understand
118 $ro_object_subs->{parse_datetime} = sub {
120 $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
122 "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
132 # retrieve and cache org unit setting values
133 $ro_object_subs->{get_org_setting} = sub {
134 my($org_id, $setting) = @_;
136 $cache{org_settings}{$org_id} = {}
137 unless $cache{org_settings}{$org_id};
139 $cache{org_settings}{$org_id}{$setting} =
140 $U->ou_ancestor_setting_value($org_id, $setting)
141 unless exists $cache{org_settings}{$org_id}{$setting};
143 return $cache{org_settings}{$org_id}{$setting};
146 $ctx->{$_} = $ro_object_subs->{$_} for keys %$ro_object_subs;
149 sub generic_redirect {
152 my $cookie = shift; # can be an array of cgi.cookie's
154 $self->apache->print(
155 $self->cgi->redirect(
157 $self->cgi->param('redirect_to') ||
158 $self->ctx->{referer} ||
159 $self->ctx->{home_page},
164 return Apache2::Const::REDIRECT;
167 sub get_records_and_facets {
168 my ($self, $rec_ids, $facet_key, $unapi_args) = @_;
171 $unapi_args->{site} ||= $self->ctx->{aou_tree}->()->shortname;
172 $unapi_args->{depth} ||= $self->ctx->{aou_tree}->()->ou_type->depth;
173 $unapi_args->{flesh_depth} ||= 5;
176 my $ses = OpenSRF::MultiSession->new(
177 app => 'open-ils.cstore',
178 cap => 10, # XXX config
179 success_handler => sub {
180 my($self, $req) = @_;
181 my $data = $req->{response}->[0]->content;
182 my $xml = XML::LibXML->new->parse_string($data->{'unapi.bre'})->documentElement;
183 my $bre_id = $xml->find('*[@tag="901"]/*[@code="c"]')->[0]->textContent;
184 push(@data, {id => $bre_id, marc_xml => $xml});
189 'open-ils.cstore.json_query',
191 'unapi.bre', $_, 'marcxml','record',
192 $unapi_args->{flesh},
194 $unapi_args->{depth},
195 $unapi_args->{flesh_depth},
199 # collect the facet data
200 my $search = OpenSRF::AppSession->create('open-ils.search');
201 my $facet_req = $search->request(
202 'open-ils.search.facet_cache.retrieve', $facet_key, 10
205 # gather up the unapi recs
206 $ses->session_wait(1);
210 my $tmp_facets = $facet_req->gather(1);
211 for my $cmf_id (keys %$tmp_facets) {
213 # sort highest to lowest match count
215 my $entries = $tmp_facets->{$cmf_id};
216 for my $ent (keys %$entries) {
217 push(@entries, {value => $ent, count => $$entries{$ent}});
219 @entries = sort { $b->{count} <=> $a->{count} } @entries;
220 $facets->{$cmf_id} = {
221 cmf => $self->ctx->{get_cmf}->($cmf_id),
231 return ($facets, @data);
234 # TODO: blend this code w/ ^-- get_records_and_facets
235 sub fetch_marc_xml_by_id {
236 my ($self, $id_list) = @_;
237 $id_list = [$id_list] unless ref($id_list);
240 no warnings qw/numeric/;
241 $id_list = [map { int $_ } @$id_list];
242 $id_list = [grep { $_ > 0} @$id_list];
245 return {} if scalar(@$id_list) < 1;
247 # I'm just sure there needs to be some more efficient way to get all of
249 my $results = $self->editor->json_query({
250 "select" => {"bre" => ["id", "marc"]},
251 "from" => {"bre" => {}},
252 "where" => {"id" => $id_list}
253 }, {substream => 1}) or return $self->editor->die_event;
256 for my $r (@$results) {
257 $marc_xml->{$r->{"id"}} =
258 (new XML::LibXML)->parse_string($r->{"marc"});