]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
5410a567313aee9754f1b9761e82823159f2bff8
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / AppUtils.pm
1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
4 use OpenSRF::Utils::Cache;
5 use OpenSRF::EX qw(:try);
6 use OpenILS::Perm;
7 use OpenSRF::Utils::Logger;
8 use OpenILS::Utils::ModsParser;
9 use OpenILS::Event;
10 my $logger = "OpenSRF::Utils::Logger";
11
12
13 my $cache_client = "OpenSRF::Utils::Cache";
14
15 # ---------------------------------------------------------------------------
16 # Pile of utilty methods used accross applications.
17 # ---------------------------------------------------------------------------
18
19
20 # ---------------------------------------------------------------------------
21 # on sucess, returns the created session, on failure throws ERROR exception
22 # ---------------------------------------------------------------------------
23 sub start_db_session {
24
25         my $self = shift;
26         my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
27         my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
28
29         my $trans_resp = $trans_req->recv();
30         if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
31         if( ! $trans_resp->content() ) {
32                 throw OpenSRF::ERROR 
33                         ("Unable to Begin Transaction with database" );
34         }
35         $trans_req->finish();
36         return $session;
37 }
38
39
40 # returns undef if user has all of the perms provided
41 # returns the first failed perm on failure
42 sub check_user_perms {
43         my($self, $user_id, $org_id, @perm_types ) = @_;
44
45         $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
46
47         throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
48                 unless( defined($user_id) and defined($org_id) and @perm_types); 
49
50         my $session = OpenSRF::AppSession->create("open-ils.storage");
51         for my $type (@perm_types) {
52                 my $req = $session->request(
53                         "open-ils.storage.permission.user_has_perm", 
54                         $user_id, $type, $org_id );
55                 my $resp = $req->gather(1);
56                 if(!$resp) { 
57                         $session->disconnect();
58                         return $type; 
59                 }
60         }
61
62         $session->disconnect();
63         return undef;
64 }
65
66 # checks the list of user perms.  The first one that fails returns a new
67 # OpenILS::Perm object of that type.  Returns undef if all perms are allowed
68 sub check_perms {
69         my( $self, $user_id, $org_id, @perm_types ) = @_;
70         my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
71         return OpenILS::Event->new('PERM_FAILURE', ilsperm => $t, ilspermloc => $org_id ) if $t;
72         return undef;
73 }
74
75
76
77 # ---------------------------------------------------------------------------
78 # commits and destroys the session
79 # ---------------------------------------------------------------------------
80 sub commit_db_session {
81         my( $self, $session ) = @_;
82
83         my $req = $session->request( "open-ils.storage.transaction.commit" );
84         my $resp = $req->recv();
85
86         if(!$resp) {
87                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
88         }
89
90         if(UNIVERSAL::isa($resp,"Error")) { 
91                 throw $resp ($resp->stringify); 
92         }
93
94         if(!$resp->content) {
95                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
96         }
97
98         $session->finish();
99         $session->disconnect();
100         $session->kill_me();
101 }
102
103 sub rollback_db_session {
104         my( $self, $session ) = @_;
105
106         my $req = $session->request("open-ils.storage.transaction.rollback");
107         my $resp = $req->recv();
108         if(UNIVERSAL::isa($resp,"Error")) { throw $resp;  }
109
110         $session->finish();
111         $session->disconnect();
112         $session->kill_me();
113 }
114
115 # ---------------------------------------------------------------------------
116 # Checks to see if a user is logged in.  Returns the user record on success,
117 # throws an exception on error.
118 # ---------------------------------------------------------------------------
119
120
121 sub check_user_session {
122
123         my( $self, $user_session ) = @_;
124
125         my $session = OpenSRF::AppSession->create( "open-ils.auth" );
126         my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
127         my $response = $request->recv();
128
129         if(!$response) {
130                 throw OpenSRF::EX::User 
131                         ("Error communication with storage server");
132         }
133
134         if(ref($response) and $response->isa("OpenSRF::EX")) {
135                 throw $response ($response->stringify);
136         }
137
138
139         my $content = $response->content;
140         if( ref($content) eq 'HASH' ) {
141                 if(defined($content->{ilsevent}) and $content->{ilsevent} ne '0' ) {
142                         throw OpenSRF::EX::ERROR 
143                                 ("Session [$user_session] cannot be authenticated" );
144                 }
145         }
146
147         my $user = $content;
148         if(!$user) {
149                 throw OpenSRF::EX::ERROR 
150                         ("Session [$user_session] cannot be authenticated" );
151         }
152
153         $session->disconnect();
154         $session->kill_me();
155
156         return $user;
157 }
158
159 # generic simple request returning a scalar value
160 sub simplereq {
161         my($self, $service, $method, @params) = @_;
162         return $self->simple_scalar_request($service, $method, @params);
163 }
164
165
166 sub simple_scalar_request {
167         my($self, $service, $method, @params) = @_;
168
169         my $session = OpenSRF::AppSession->create( $service );
170         my $request = $session->request( $method, @params );
171         my $response = $request->recv(30);
172
173         $request->wait_complete;
174
175         if(!$request->complete) {
176                 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" . 
177                                 "\n did not complete successfully");
178         }
179
180         if(!$response) {
181                 warn "No response from $service for method $method with params @params";
182         }
183
184         if(UNIVERSAL::isa($response,"Error")) {
185                 throw $response ("Call to $service for method $method with params @params" . 
186                                 "\n failed with exception: " . $response->stringify );
187         }
188
189
190         $request->finish();
191         $session->finish();
192         $session->disconnect();
193
194         my $value;
195
196         if($response) { $value = $response->content; }
197         else { $value = undef; }
198
199         return $value;
200 }
201
202
203
204
205
206 my $tree                                                = undef;
207 my $orglist                                     = undef;
208 my $org_typelist                        = undef;
209 my $org_typelist_hash   = {};
210
211 sub get_org_tree {
212
213         my $self = shift;
214         if($tree) { return $tree; }
215
216         # see if it's in the cache
217         $tree = $cache_client->new()->get_cache('_orgtree');
218         if($tree) { return $tree; }
219
220         if(!$orglist) {
221                 warn "Retrieving Org Tree\n";
222                 $orglist = $self->simple_scalar_request( 
223                         "open-ils.storage", 
224                         "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
225         }
226
227         if( ! $org_typelist ) {
228                 warn "Retrieving org types\n";
229                 $org_typelist = $self->simple_scalar_request( 
230                         "open-ils.storage", 
231                         "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
232                 $self->build_org_type($org_typelist);
233         }
234
235         $tree = $self->build_org_tree($orglist,1);
236         $cache_client->new()->put_cache('_orgtree', $tree);
237         return $tree;
238
239 }
240
241 my $slimtree = undef;
242 sub get_slim_org_tree {
243
244         my $self = shift;
245         if($slimtree) { return $slimtree; }
246
247         # see if it's in the cache
248         $slimtree = $cache_client->new()->get_cache('slimorgtree');
249         if($slimtree) { return $slimtree; }
250
251         if(!$orglist) {
252                 warn "Retrieving Org Tree\n";
253                 $orglist = $self->simple_scalar_request( 
254                         "open-ils.storage", 
255                         "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
256         }
257
258         $slimtree = $self->build_org_tree($orglist);
259         $cache_client->new->put_cache('slimorgtree', $slimtree);
260         return $slimtree;
261
262 }
263
264
265 sub build_org_type { 
266         my($self, $org_typelist)  = @_;
267         for my $type (@$org_typelist) {
268                 $org_typelist_hash->{$type->id()} = $type;
269         }
270 }
271
272
273
274 sub build_org_tree {
275
276         my( $self, $orglist, $add_types ) = @_;
277
278         return $orglist unless ( 
279                         ref($orglist) and @$orglist > 1 );
280
281         my @list = sort { 
282                 $a->ou_type <=> $b->ou_type ||
283                 $a->name cmp $b->name } @$orglist;
284
285         for my $org (@list) {
286
287                 next unless ($org);
288
289                 if(!ref($org->ou_type()) and $add_types) {
290                         $org->ou_type( $org_typelist_hash->{$org->ou_type()});
291                 }
292
293                 next unless (defined($org->parent_ou));
294
295                 my ($parent) = grep { $_->id == $org->parent_ou } @list;
296                 next unless $parent;
297                 $parent->children([]) unless defined($parent->children); 
298                 push( @{$parent->children}, $org );
299         }
300
301         return $list[0];
302
303 }
304
305 sub fetch_user {
306         my( $self, $userid ) = @_;
307         my( $user, $evt );
308
309         $logger->debug("Fetching user $userid from storage");
310
311         $user = $self->simplereq(
312                 'open-ils.storage',
313                 'open-ils.storage.direct.actor.user.retrieve', $userid );
314
315         if(!$user) {
316                 $logger->info("User $userid not found in the db");
317                 $evt = OpenILS::Event->new('USER_NOT_FOUND');
318         }
319
320         return ($user, $evt);
321 }
322
323 sub checkses {
324         my( $self, $session ) = @_;
325         my $user; my $evt; my $e; 
326
327         $logger->debug("Checking user session $session");
328
329         try {
330                 $user = $self->check_user_session($session);
331         } catch Error with { $e = 1; };
332
333         if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
334         return ( $user, $evt );
335 }
336
337
338 # verifiese the session and checks the permissions agains the
339 # session user and the user's home_ou as the org id
340 sub checksesperm {
341         my( $self, $session, @perms ) = @_;
342         my $user; my $evt; my $e; 
343         $logger->debug("Checking user session $session and perms @perms");
344         ($user, $evt) = $self->checkses($session);
345         return (undef, $evt) if $evt;
346         $evt = $self->check_perms($user->id, $user->home_ou, @perms);
347         return ($user, $evt) if $evt;
348 }
349
350
351 sub checkrequestor {
352         my( $self, $staffobj, $userid, @perms ) = @_;
353         my $user; my $evt;
354         $userid = $staffobj->id unless defined $userid;
355
356         $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
357
358         if( $userid ne $staffobj->id ) {
359                 ($user, $evt) = $self->fetch_user($userid);
360                 return (undef, $evt) if $evt;
361                 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
362
363         } else {
364                 $user = $staffobj;
365         }
366
367         return ($user, $evt);
368 }
369
370 sub checkses_requestor {
371         my( $self, $authtoken, $targetid, @perms ) = @_;
372         my( $requestor, $target, $evt );
373
374         ($requestor, $evt) = $self->checkses($authtoken);
375         return (undef, undef, $evt) if $evt;
376
377         ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
378         return( $requestor, $target, $evt);
379 }
380
381 sub fetch_copy {
382         my( $self, $copyid ) = @_;
383         my( $copy, $evt );
384
385         $logger->debug("Fetching copy $copyid from storage");
386
387         $copy = $self->simplereq(
388                 'open-ils.storage',
389                 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
390
391         if(!$copy) { $evt = OpenILS::Event->new('COPY_NOT_FOUND'); }
392
393         return( $copy, $evt );
394 }
395
396
397 # retrieves a circ object by id
398 sub fetch_circulation {
399         my( $self, $circid ) = @_;
400         my $circ; my $evt;
401         
402         $logger->debug("Fetching circ $circid from storage");
403
404         $circ = $self->simplereq(
405                 'open-ils.storage',
406                 "open-ils.storage.direct.action.circulation.retrieve", $circid );
407
408         if(!$circ) {
409                 $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND', circid => $circid );
410         }
411
412         return ( $circ, $evt );
413 }
414
415 sub fetch_record_by_copy {
416         my( $self, $copyid ) = @_;
417         my( $record, $evt );
418
419         $logger->debug("Fetching record by copy $copyid from storage");
420
421         $record = $self->simplereq(
422                 'open-ils.storage',
423                 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy', $copyid );
424
425         if(!$record) {
426                 $evt = OpenILS::Event->new('BIBLIO_RECORD_NOT_FOUND');
427         }
428
429         return ($record, $evt);
430 }
431
432 # turns a record object into an mvr (mods) object
433 sub record_to_mvr {
434         my( $self, $record ) = @_;
435         my $u = OpenILS::Utils::ModsParser->new();
436         $u->start_mods_batch( $record->marc );
437         my $mods = $u->finish_mods_batch();
438         $mods->doc_id($record->id);
439         return $mods;
440 }
441
442 sub fetch_hold {
443         my( $self, $holdid ) = @_;
444         my( $hold, $evt );
445
446         $logger->debug("Fetching hold $holdid from storage");
447
448         $hold = $self->simplereq(
449                 'open-ils.storage',
450                 'open-ils.storage.direct.action.hold_request.retrieve', $holdid);
451
452         $evt = OpenILS::Event->new('HOLD_NOT_FOUND', holdid => $holdid) unless $hold;
453
454         return ($hold, $evt);
455 }
456
457
458 sub fetch_hold_transit_by_hold {
459         my( $self, $holdid ) = @_;
460         my( $transit, $evt );
461
462         $logger->debug("Fetching transit by hold $holdid from storage");
463
464         $transit = $self->simplereq(
465                 'open-ils.storage',
466                 'open-ils.storage.direct.action.hold_transit_copy.search.hold', $holdid );
467
468         $evt = OpenILS::Event->new('TRANSIT_NOT_FOUND', holdid => $holdid) unless $transit;
469
470         return ($transit, $evt );
471 }
472
473
474 sub fetch_copy_by_barcode {
475         my( $self, $barcode ) = @_;
476         my( $copy, $evt );
477
478         $logger->debug("Fetching copy by barcode $barcode from storage");
479
480         $copy = $self->simplereq( 'open-ils.storage',
481                 'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
482
483         $evt = OpenILS::Event->new('COPY_NOT_FOUND', barcode => $barcode) unless $copy;
484
485         return ($copy, $evt);
486 }
487
488 sub fetch_open_billable_transaction {
489         my( $self, $transid ) = @_;
490         my( $transaction, $evt );
491
492         $logger->debug("Fetching open billable transaction $transid from storage");
493
494         $transaction = $self->simplereq(
495                 'open-ils.storage',
496                 'open-ils.storage.direct.money.open_billable_transaction_summary.retrieve',  $transid);
497
498         $evt = OpenILS::Event->new(
499                 'TRANSACTION_NOT_FOUND', transid => $transid ) unless $transaction;
500
501         return ($transaction, $evt);
502 }
503
504
505
506 my %buckets;
507 $buckets{'biblio'} = 'biblio_record_entry_bucket';
508 $buckets{'callnumber'} = 'call_number_bucket';
509 $buckets{'copy'} = 'copy_bucket';
510 $buckets{'user'} = 'user_bucket';
511
512 sub fetch_container {
513         my( $self, $id, $type ) = @_;
514         my( $bucket, $evt );
515
516         $logger->debug("Fetching container $id with type $type");
517
518         my $meth = $buckets{$type};
519         $bucket = $self->simplereq(
520                 'open-ils.storage',
521                 "open-ils.storage.direct.container.$meth.retrieve", $id );
522
523         $evt = OpenILS::Event->new(
524                 'CONTAINER_NOT_FOUND', container => $id, 
525                         container_type => $type ) unless $bucket;
526
527         return ($bucket, $evt);
528 }
529
530
531 sub fetch_container_item {
532         my( $self, $id, $type ) = @_;
533         my( $bucket, $evt );
534
535         $logger->debug("Fetching container item $id with type $type");
536
537         my $meth = $buckets{$type} . "_item";
538
539         $bucket = $self->simplereq(
540                 'open-ils.storage',
541                 "open-ils.storage.direct.container.$meth.retrieve", $id );
542
543         $evt = OpenILS::Event->new(
544                 'CONTAINER_ITEM_NOT_FOUND', itemid => $id, 
545                         container_type => $type ) unless $bucket;
546
547         return ($bucket, $evt);
548 }
549
550
551 sub fetch_patron_standings {
552         my $self = shift;
553         $logger->debug("Fetching patron standings");    
554         return $self->simplereq(
555                 'open-ils.storage', 
556                 'open-ils.storage.direct.config.standing.retrieve.all.atomic');
557 }
558
559
560 sub fetch_permission_group_tree {
561         my $self = shift;
562         $logger->debug("Fetching patron profiles");     
563         return $self->simplereq(
564                 'open-ils.actor', 
565                 'open-ils.actor.groups.tree.retrieve' );
566 }
567
568
569 sub fetch_patron_circ_summary {
570         my( $self, $userid ) = @_;
571         $logger->debug("Fetching patron summary for $userid");
572         my $summary = $self->simplereq(
573                 'open-ils.storage', 
574                 "open-ils.storage.action.circulation.patron_summary", $userid );
575
576         if( $summary ) {
577                 $summary->[0] ||= 0;
578                 $summary->[1] ||= 0.0;
579                 return $summary;
580         }
581         return undef;
582 }
583
584
585 sub fetch_copy_statuses {
586         my( $self ) = @_;
587         $logger->debug("Fetching copy statuses");
588         return $self->simplereq(
589                 'open-ils.storage', 
590                 'open-ils.storage.direct.config.copy_status.retrieve.all.atomic' );
591 }
592
593 sub fetch_copy_locations {
594         my $self = shift; 
595         return $self->simplereq(
596                 'open-ils.storage', 
597                 'open-ils.storage.direct.asset.copy_location.retrieve.all.atomic');
598 }
599
600 sub fetch_callnumber {
601         my( $self, $id ) = @_;
602         my $evt = undef;
603         $logger->debug("Fetching callnumber $id");
604
605         my $cn = $self->simplereq(
606                 'open-ils.storage',
607                 'open-ils.storage.direct.asset.call_number.retrieve', $id );
608         $evt = OpenILS::Event->new( 'VOLUME_NOT_FOUND', id => $id ) unless $cn;
609
610         return ( $cn, $evt );
611 }
612
613 sub fetch_org_unit {
614         my( $self, $id ) = @_;
615         $logger->debug("Fetching org unit $id");
616         my $evt = undef;
617
618         my $org = $self->simplereq(
619                 'open-ils.storage', 
620                 'open-ils.storage.direct.actor.org_unit.retrieve', $id );
621         $evt = OpenILS::Event->new( 'ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
622
623         return ($org, $evt);
624 }
625
626 sub fetch_stat_cat {
627         my( $self, $type, $id ) = @_;
628         my( $cat, $evt );
629         $logger->debug("Fetching $type stat cat: $id");
630         $cat = $self->simplereq(
631                 'open-ils.storage', 
632                 "open-ils.storage.direct.$type.stat_cat.retrieve", $id );
633         $evt = OpenILS::Event->new( 'STAT_CAT_NOT_FOUND', id => $id ) unless $cat;
634         return ( $cat, $evt );
635 }
636
637 sub fetch_stat_cat_entry {
638         my( $self, $type, $id ) = @_;
639         my( $entry, $evt );
640         $logger->debug("Fetching $type stat cat entry: $id");
641         $entry = $self->simplereq(
642                 'open-ils.storage', 
643                 "open-ils.storage.direct.$type.stat_cat_entry.retrieve", $id );
644         $evt = OpenILS::Event->new( 'STAT_CAT_ENTRY_NOT_FOUND', id => $id ) unless $entry;
645         return ( $entry, $evt );
646 }
647
648 1;