]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
Merge remote-tracking branch 'eg-working/collab/phasefx/merged_bill_and_receipt_fixes'
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / WWW / EGCatLoader / Util.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::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';
10
11 my $ro_object_subs; # cached subs
12 our %cache = ( # cached data
13     map => {aou => {}}, # others added dynamically as needed
14     list => {},
15     search => {},
16     org_settings => {}
17 );
18
19 sub init_ro_object_cache {
20     my $self = shift;
21     my $e = $self->editor;
22     my $ctx = $self->ctx;
23
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} = {};
27
28     if($ro_object_subs) {
29         # subs have been built.  insert into the context then move along.
30         $ctx->{$_} = $ro_object_subs->{$_} for keys %$ro_object_subs;
31         return;
32     }
33
34     # make all "field_safe" classes accesible by default in the template context
35     my @classes = grep {
36         ($Fieldmapper::fieldmap->{$_}->{field_safe} || '') =~ /true/i
37     } keys %{ $Fieldmapper::fieldmap };
38
39     for my $class (@classes) {
40
41         my $hint = $Fieldmapper::fieldmap->{$class}->{hint};
42         next if $hint eq 'aou'; # handled separately
43
44         my $ident_field =  $Fieldmapper::fieldmap->{$class}->{identity};
45         (my $eclass = $class) =~ s/Fieldmapper:://o;
46         $eclass =~ s/::/_/g;
47
48         my $list_key = "${hint}_list";
49         my $get_key = "get_$hint";
50         my $search_key = "search_$hint";
51
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};
57         };
58     
59         # locate object of class $hint with Ident field $id
60         $cache{map}{$hint} = {};
61         $ro_object_subs->{$get_key} = sub {
62             my $id = shift;
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};
66         };
67
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};
77         };
78     }
79
80     $ro_object_subs->{aou_tree} = sub {
81
82         # fetch the org unit tree
83         unless($cache{aou_tree}) {
84             my $tree = $e->search_actor_org_unit([
85                             {   parent_ou => undef},
86                             {   flesh            => -1,
87                                     flesh_fields    => {aou =>  ['children']},
88                                     order_by        => {aou => 'name'}
89                             }
90                     ])->[0];
91
92             # flesh the org unit type for each org unit
93             # and simultaneously set the id => aou map cache
94             sub flesh_aout {
95                 my $node = shift;
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};
100             };
101             flesh_aout($tree, $ro_object_subs);
102
103             $cache{aou_tree} = $tree;
104         }
105
106         return $cache{aou_tree};
107     };
108
109     # Add a special handler for the tree-shaped org unit cache
110     $ro_object_subs->{get_aou} = sub {
111         my $org_id = shift;
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};
115     };
116
117     # turns an ISO date into something TT can understand
118     $ro_object_subs->{parse_datetime} = sub {
119         my $date = shift;
120         $date = DateTime::Format::ISO8601->new->parse_datetime(cleanse_ISO8601($date));
121         return sprintf(
122             "%0.2d:%0.2d:%0.2d %0.2d-%0.2d-%0.4d",
123             $date->hour,
124             $date->minute,
125             $date->second,
126             $date->day,
127             $date->month,
128             $date->year
129         );
130     };
131
132     # retrieve and cache org unit setting values
133     $ro_object_subs->{get_org_setting} = sub {
134         my($org_id, $setting) = @_;
135
136         $cache{org_settings}{$org_id} = {} 
137             unless $cache{org_settings}{$org_id};
138
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};
142
143         return $cache{org_settings}{$org_id}{$setting};
144     };
145
146     $ctx->{$_} = $ro_object_subs->{$_} for keys %$ro_object_subs;
147 }
148
149 sub generic_redirect {
150     my $self = shift;
151     my $url = shift;
152     my $cookie = shift; # can be an array of cgi.cookie's
153
154     $self->apache->print(
155         $self->cgi->redirect(
156             -url => $url || 
157                 $self->cgi->param('redirect_to') || 
158                 $self->ctx->{referer} || 
159                 $self->ctx->{home_page},
160             -cookie => $cookie
161         )
162     );
163
164     return Apache2::Const::REDIRECT;
165 }
166
167 sub get_records_and_facets {
168     my ($self, $rec_ids, $facet_key, $unapi_args) = @_;
169
170     $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;
174
175     my @data;
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});
185         }
186     );
187
188     $ses->request(
189         'open-ils.cstore.json_query',
190          {from => [
191             'unapi.bre', $_, 'marcxml','record', 
192             $unapi_args->{flesh}, 
193             $unapi_args->{site}, 
194             $unapi_args->{depth}, 
195             $unapi_args->{flesh_depth}, 
196         ]}
197     ) for @$rec_ids;
198
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
203     ) if $facet_key;
204
205     # gather up the unapi recs
206     $ses->session_wait(1);
207
208     my $facets = {};
209     if ($facet_key) {
210         my $tmp_facets = $facet_req->gather(1);
211         for my $cmf_id (keys %$tmp_facets) {
212
213             # sort highest to lowest match count
214             my @entries;
215             my $entries = $tmp_facets->{$cmf_id};
216             for my $ent (keys %$entries) {
217                 push(@entries, {value => $ent, count => $$entries{$ent}});
218             };
219             @entries = sort { $b->{count} <=> $a->{count} } @entries;
220             $facets->{$cmf_id} = {
221                 cmf => $self->ctx->{get_cmf}->($cmf_id),
222                 data => \@entries
223             }
224         }
225     } else {
226         $facets = undef;
227     }
228
229     $search->kill_me;
230
231     return ($facets, @data);
232 }
233
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);
238
239     {
240         no warnings qw/numeric/;
241         $id_list = [map { int $_ } @$id_list];
242         $id_list = [grep { $_ > 0} @$id_list];
243     };
244
245     return {} if scalar(@$id_list) < 1;
246
247     # I'm just sure there needs to be some more efficient way to get all of
248     # this.
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;
254
255     my $marc_xml = {};
256     for my $r (@$results) {
257         $marc_xml->{$r->{"id"}} =
258             (new XML::LibXML)->parse_string($r->{"marc"});
259     }
260
261     return $marc_xml;
262 }
263
264 1;