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