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);
7 use OpenSRF::Utils::Logger;
8 use OpenILS::Utils::ModsParser;
10 my $logger = "OpenSRF::Utils::Logger";
13 my $cache_client = "OpenSRF::Utils::Cache";
15 # ---------------------------------------------------------------------------
16 # Pile of utilty methods used accross applications.
17 # ---------------------------------------------------------------------------
20 # ---------------------------------------------------------------------------
21 # on sucess, returns the created session, on failure throws ERROR exception
22 # ---------------------------------------------------------------------------
23 sub start_db_session {
26 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
27 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
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() ) {
33 ("Unable to Begin Transaction with database" );
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 ) = @_;
45 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
47 throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
48 unless( defined($user_id) and defined($org_id) and @perm_types);
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);
57 $session->disconnect();
62 $session->disconnect();
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
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;
77 # ---------------------------------------------------------------------------
78 # commits and destroys the session
79 # ---------------------------------------------------------------------------
80 sub commit_db_session {
81 my( $self, $session ) = @_;
83 my $req = $session->request( "open-ils.storage.transaction.commit" );
84 my $resp = $req->recv();
87 throw OpenSRF::EX::ERROR ("Unable to commit db session");
90 if(UNIVERSAL::isa($resp,"Error")) {
91 throw $resp ($resp->stringify);
95 throw OpenSRF::EX::ERROR ("Unable to commit db session");
99 $session->disconnect();
103 sub rollback_db_session {
104 my( $self, $session ) = @_;
106 my $req = $session->request("open-ils.storage.transaction.rollback");
107 my $resp = $req->recv();
108 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
111 $session->disconnect();
115 # ---------------------------------------------------------------------------
116 # Checks to see if a user is logged in. Returns the user record on success,
117 # throws an exception on error.
118 # ---------------------------------------------------------------------------
121 sub check_user_session {
123 my( $self, $user_session ) = @_;
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();
130 throw OpenSRF::EX::User
131 ("Error communication with storage server");
134 if(ref($response) and $response->isa("OpenSRF::EX")) {
135 throw $response ($response->stringify);
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" );
149 throw OpenSRF::EX::ERROR
150 ("Session [$user_session] cannot be authenticated" );
153 $session->disconnect();
159 # generic simple request returning a scalar value
161 my($self, $service, $method, @params) = @_;
162 return $self->simple_scalar_request($service, $method, @params);
166 sub simple_scalar_request {
167 my($self, $service, $method, @params) = @_;
169 my $session = OpenSRF::AppSession->create( $service );
170 my $request = $session->request( $method, @params );
171 my $response = $request->recv(30);
173 $request->wait_complete;
175 if(!$request->complete) {
176 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" .
177 "\n did not complete successfully");
181 warn "No response from $service for method $method with params @params";
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 );
192 $session->disconnect();
196 if($response) { $value = $response->content; }
197 else { $value = undef; }
208 my $org_typelist = undef;
209 my $org_typelist_hash = {};
214 if($tree) { return $tree; }
216 # see if it's in the cache
217 $tree = $cache_client->new()->get_cache('_orgtree');
218 if($tree) { return $tree; }
221 warn "Retrieving Org Tree\n";
222 $orglist = $self->simple_scalar_request(
224 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
227 if( ! $org_typelist ) {
228 warn "Retrieving org types\n";
229 $org_typelist = $self->simple_scalar_request(
231 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
232 $self->build_org_type($org_typelist);
235 $tree = $self->build_org_tree($orglist,1);
236 $cache_client->new()->put_cache('_orgtree', $tree);
241 my $slimtree = undef;
242 sub get_slim_org_tree {
245 if($slimtree) { return $slimtree; }
247 # see if it's in the cache
248 $slimtree = $cache_client->new()->get_cache('slimorgtree');
249 if($slimtree) { return $slimtree; }
252 warn "Retrieving Org Tree\n";
253 $orglist = $self->simple_scalar_request(
255 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
258 $slimtree = $self->build_org_tree($orglist);
259 $cache_client->new->put_cache('slimorgtree', $slimtree);
266 my($self, $org_typelist) = @_;
267 for my $type (@$org_typelist) {
268 $org_typelist_hash->{$type->id()} = $type;
276 my( $self, $orglist, $add_types ) = @_;
278 return $orglist unless (
279 ref($orglist) and @$orglist > 1 );
282 $a->ou_type <=> $b->ou_type ||
283 $a->name cmp $b->name } @$orglist;
285 for my $org (@list) {
289 if(!ref($org->ou_type()) and $add_types) {
290 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
293 next unless (defined($org->parent_ou));
295 my ($parent) = grep { $_->id == $org->parent_ou } @list;
297 $parent->children([]) unless defined($parent->children);
298 push( @{$parent->children}, $org );
306 my( $self, $userid ) = @_;
309 $logger->debug("Fetching user $userid from storage");
311 $user = $self->simplereq(
313 'open-ils.storage.direct.actor.user.retrieve', $userid );
316 $logger->info("User $userid not found in the db");
317 $evt = OpenILS::Event->new('USER_NOT_FOUND');
320 return ($user, $evt);
324 my( $self, $session ) = @_;
325 my $user; my $evt; my $e;
327 $logger->debug("Checking user session $session");
330 $user = $self->check_user_session($session);
331 } catch Error with { $e = 1; };
333 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
334 return ( $user, $evt );
338 # verifiese the session and checks the permissions agains the
339 # session user and the user's home_ou as the org id
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;
352 my( $self, $staffobj, $userid, @perms ) = @_;
354 $userid = $staffobj->id unless defined $userid;
356 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
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 );
367 return ($user, $evt);
370 sub checkses_requestor {
371 my( $self, $authtoken, $targetid, @perms ) = @_;
372 my( $requestor, $target, $evt );
374 ($requestor, $evt) = $self->checkses($authtoken);
375 return (undef, undef, $evt) if $evt;
377 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
378 return( $requestor, $target, $evt);
382 my( $self, $copyid ) = @_;
385 $logger->debug("Fetching copy $copyid from storage");
387 $copy = $self->simplereq(
389 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
391 if(!$copy) { $evt = OpenILS::Event->new('COPY_NOT_FOUND'); }
393 return( $copy, $evt );
397 # retrieves a circ object by id
398 sub fetch_circulation {
399 my( $self, $circid ) = @_;
402 $logger->debug("Fetching circ $circid from storage");
404 $circ = $self->simplereq(
406 "open-ils.storage.direct.action.circulation.retrieve", $circid );
409 $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND', circid => $circid );
412 return ( $circ, $evt );
415 sub fetch_record_by_copy {
416 my( $self, $copyid ) = @_;
419 $logger->debug("Fetching record by copy $copyid from storage");
421 $record = $self->simplereq(
423 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy', $copyid );
426 $evt = OpenILS::Event->new('BIBLIO_RECORD_NOT_FOUND');
429 return ($record, $evt);
432 # turns a record object into an mvr (mods) object
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);
443 my( $self, $holdid ) = @_;
446 $logger->debug("Fetching hold $holdid from storage");
448 $hold = $self->simplereq(
450 'open-ils.storage.direct.action.hold_request.retrieve', $holdid);
452 $evt = OpenILS::Event->new('HOLD_NOT_FOUND', holdid => $holdid) unless $hold;
454 return ($hold, $evt);
458 sub fetch_hold_transit_by_hold {
459 my( $self, $holdid ) = @_;
460 my( $transit, $evt );
462 $logger->debug("Fetching transit by hold $holdid from storage");
464 $transit = $self->simplereq(
466 'open-ils.storage.direct.action.hold_transit_copy.search.hold', $holdid );
468 $evt = OpenILS::Event->new('TRANSIT_NOT_FOUND', holdid => $holdid) unless $transit;
470 return ($transit, $evt );
474 sub fetch_copy_by_barcode {
475 my( $self, $barcode ) = @_;
478 $logger->debug("Fetching copy by barcode $barcode from storage");
480 $copy = $self->simplereq( 'open-ils.storage',
481 'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
483 $evt = OpenILS::Event->new('COPY_NOT_FOUND', barcode => $barcode) unless $copy;
485 return ($copy, $evt);
488 sub fetch_open_billable_transaction {
489 my( $self, $transid ) = @_;
490 my( $transaction, $evt );
492 $logger->debug("Fetching open billable transaction $transid from storage");
494 $transaction = $self->simplereq(
496 'open-ils.storage.direct.money.open_billable_transaction_summary.retrieve', $transid);
498 $evt = OpenILS::Event->new(
499 'TRANSACTION_NOT_FOUND', transid => $transid ) unless $transaction;
501 return ($transaction, $evt);
507 $buckets{'biblio'} = 'biblio_record_entry_bucket';
508 $buckets{'callnumber'} = 'call_number_bucket';
509 $buckets{'copy'} = 'copy_bucket';
510 $buckets{'user'} = 'user_bucket';
512 sub fetch_container {
513 my( $self, $id, $type ) = @_;
516 $logger->debug("Fetching container $id with type $type");
518 my $meth = $buckets{$type};
519 $bucket = $self->simplereq(
521 "open-ils.storage.direct.container.$meth.retrieve", $id );
523 $evt = OpenILS::Event->new(
524 'CONTAINER_NOT_FOUND', container => $id,
525 container_type => $type ) unless $bucket;
527 return ($bucket, $evt);
531 sub fetch_container_item {
532 my( $self, $id, $type ) = @_;
535 $logger->debug("Fetching container item $id with type $type");
537 my $meth = $buckets{$type} . "_item";
539 $bucket = $self->simplereq(
541 "open-ils.storage.direct.container.$meth.retrieve", $id );
543 $evt = OpenILS::Event->new(
544 'CONTAINER_ITEM_NOT_FOUND', itemid => $id,
545 container_type => $type ) unless $bucket;
547 return ($bucket, $evt);
551 sub fetch_patron_standings {
553 $logger->debug("Fetching patron standings");
554 return $self->simplereq(
556 'open-ils.storage.direct.config.standing.retrieve.all.atomic');
560 sub fetch_permission_group_tree {
562 $logger->debug("Fetching patron profiles");
563 return $self->simplereq(
565 'open-ils.actor.groups.tree.retrieve' );
569 sub fetch_patron_circ_summary {
570 my( $self, $userid ) = @_;
571 $logger->debug("Fetching patron summary for $userid");
572 my $summary = $self->simplereq(
574 "open-ils.storage.action.circulation.patron_summary", $userid );
578 $summary->[1] ||= 0.0;
585 sub fetch_copy_statuses {
587 $logger->debug("Fetching copy statuses");
588 return $self->simplereq(
590 'open-ils.storage.direct.config.copy_status.retrieve.all.atomic' );
593 sub fetch_copy_locations {
595 return $self->simplereq(
597 'open-ils.storage.direct.asset.copy_location.retrieve.all.atomic');
600 sub fetch_callnumber {
601 my( $self, $id ) = @_;
603 $logger->debug("Fetching callnumber $id");
605 my $cn = $self->simplereq(
607 'open-ils.storage.direct.asset.call_number.retrieve', $id );
608 $evt = OpenILS::Event->new( 'VOLUME_NOT_FOUND', id => $id ) unless $cn;
610 return ( $cn, $evt );
614 my( $self, $id ) = @_;
615 $logger->debug("Fetching org unit $id");
618 my $org = $self->simplereq(
620 'open-ils.storage.direct.actor.org_unit.retrieve', $id );
621 $evt = OpenILS::Event->new( 'ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
627 my( $self, $type, $id ) = @_;
629 $logger->debug("Fetching $type stat cat: $id");
630 $cat = $self->simplereq(
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 );
637 sub fetch_stat_cat_entry {
638 my( $self, $type, $id ) = @_;
640 $logger->debug("Fetching $type stat cat entry: $id");
641 $entry = $self->simplereq(
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 );