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 my $storage_session = undef;
17 # ---------------------------------------------------------------------------
18 # Pile of utilty methods used accross applications.
19 # ---------------------------------------------------------------------------
22 # ---------------------------------------------------------------------------
23 # on sucess, returns the created session, on failure throws ERROR exception
24 # ---------------------------------------------------------------------------
25 sub start_db_session {
28 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
29 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
31 my $trans_resp = $trans_req->recv();
32 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
33 if( ! $trans_resp->content() ) {
35 ("Unable to Begin Transaction with database" );
39 $logger->debug("Setting global storage session to ".
40 "session: " . $session->session_id . " : " . $session->app );
42 $storage_session = $session;
47 # returns undef if user has all of the perms provided
48 # returns the first failed perm on failure
49 sub check_user_perms {
50 my($self, $user_id, $org_id, @perm_types ) = @_;
51 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
52 for my $type (@perm_types) {
53 return $type unless ($self->storagereq(
54 "open-ils.storage.permission.user_has_perm",
55 $user_id, $type, $org_id ));
60 # checks the list of user perms. The first one that fails returns a new
61 # OpenILS::Perm object of that type. Returns undef if all perms are allowed
63 my( $self, $user_id, $org_id, @perm_types ) = @_;
64 my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
65 return OpenILS::Event->new('PERM_FAILURE', ilsperm => $t, ilspermloc => $org_id ) if $t;
71 # ---------------------------------------------------------------------------
72 # commits and destroys the session
73 # ---------------------------------------------------------------------------
74 sub commit_db_session {
75 my( $self, $session ) = @_;
77 my $req = $session->request( "open-ils.storage.transaction.commit" );
78 my $resp = $req->recv();
81 throw OpenSRF::EX::ERROR ("Unable to commit db session");
84 if(UNIVERSAL::isa($resp,"Error")) {
85 throw $resp ($resp->stringify);
89 throw OpenSRF::EX::ERROR ("Unable to commit db session");
93 $session->disconnect();
95 $storage_session = undef;
98 sub rollback_db_session {
99 my( $self, $session ) = @_;
101 my $req = $session->request("open-ils.storage.transaction.rollback");
102 my $resp = $req->recv();
103 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
106 $session->disconnect();
108 $storage_session = undef;
112 # returns undef it the event is not an ILS event
113 # returns the event code otherwise
115 my( $self, $evt ) = @_;
116 return $evt->{ilsevent} if( ref($evt) eq 'HASH' and defined($evt->{ilsevent})) ;
120 # ---------------------------------------------------------------------------
121 # Checks to see if a user is logged in. Returns the user record on success,
122 # throws an exception on error.
123 # ---------------------------------------------------------------------------
124 sub check_user_session {
126 my( $self, $user_session ) = @_;
128 my $content = $self->simplereq(
130 'open-ils.auth.session.retrieve', $user_session );
133 if(! $content or $self->event_code($content)) {
134 throw OpenSRF::EX::ERROR
135 ("Session [$user_session] cannot be authenticated" );
138 $logger->debug("Fetch user session $user_session found user " . $content->id );
143 # generic simple request returning a scalar value
145 my($self, $service, $method, @params) = @_;
146 return $self->simple_scalar_request($service, $method, @params);
149 sub get_storage_session {
151 if( $storage_session and
152 $storage_session->connected and
153 $storage_session->transport_connected and
154 $storage_session->app eq 'open-ils.storage' ) {
156 $logger->debug("get_storage_session(): returning existing session");
157 return $storage_session;
159 $logger->debug("get_storage_session(): returning undef");
160 $storage_session = undef;
165 sub simple_scalar_request {
166 my($self, $service, $method, @params) = @_;
169 if( $service eq 'open-ils.storage' ) {
170 if( $session = get_storage_session() ) {
171 $logger->debug("simple request using existing storage session ".$session->session_id);
172 } else { $session = undef; }
176 $session = OpenSRF::AppSession->create( $service );
179 $logger->debug("simple request for service $service using session " .$session->app);
181 my $request = $session->request( $method, @params );
182 my $response = $request->recv(30);
184 $request->wait_complete;
186 if(!$request->complete) {
187 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" .
188 "\n did not complete successfully");
192 warn "No response from $service for method $method with params @params";
195 if(UNIVERSAL::isa($response,"Error")) {
196 throw $response ("Call to $service for method $method with params @params" .
197 "\n failed with exception: " . $response->stringify );
203 if($service ne 'open-ils.storage' or !get_storage_session() ) {
205 $session->disconnect();
210 if($response) { $value = $response->content; }
211 else { $value = undef; }
222 my $org_typelist = undef;
223 my $org_typelist_hash = {};
228 if($tree) { return $tree; }
230 # see if it's in the cache
231 $tree = $cache_client->new()->get_cache('_orgtree');
232 if($tree) { return $tree; }
235 warn "Retrieving Org Tree\n";
236 $orglist = $self->simple_scalar_request(
238 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
241 if( ! $org_typelist ) {
242 warn "Retrieving org types\n";
243 $org_typelist = $self->simple_scalar_request(
245 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
246 $self->build_org_type($org_typelist);
249 $tree = $self->build_org_tree($orglist,1);
250 $cache_client->new()->put_cache('_orgtree', $tree);
255 my $slimtree = undef;
256 sub get_slim_org_tree {
259 if($slimtree) { return $slimtree; }
261 # see if it's in the cache
262 $slimtree = $cache_client->new()->get_cache('slimorgtree');
263 if($slimtree) { return $slimtree; }
266 warn "Retrieving Org Tree\n";
267 $orglist = $self->simple_scalar_request(
269 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
272 $slimtree = $self->build_org_tree($orglist);
273 $cache_client->new->put_cache('slimorgtree', $slimtree);
280 my($self, $org_typelist) = @_;
281 for my $type (@$org_typelist) {
282 $org_typelist_hash->{$type->id()} = $type;
290 my( $self, $orglist, $add_types ) = @_;
292 return $orglist unless (
293 ref($orglist) and @$orglist > 1 );
296 $a->ou_type <=> $b->ou_type ||
297 $a->name cmp $b->name } @$orglist;
299 for my $org (@list) {
303 if(!ref($org->ou_type()) and $add_types) {
304 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
307 next unless (defined($org->parent_ou));
309 my ($parent) = grep { $_->id == $org->parent_ou } @list;
311 $parent->children([]) unless defined($parent->children);
312 push( @{$parent->children}, $org );
320 my( $self, $userid ) = @_;
323 $logger->debug("Fetching user $userid from storage");
325 $user = $self->simplereq(
327 'open-ils.storage.direct.actor.user.retrieve', $userid );
330 $logger->info("User $userid not found in the db");
331 $evt = OpenILS::Event->new('USER_NOT_FOUND');
334 return ($user, $evt);
338 my( $self, $session ) = @_;
339 my $user; my $evt; my $e;
341 $logger->debug("Checking user session $session");
344 $user = $self->check_user_session($session);
345 } catch Error with { $e = 1; };
347 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
348 return ( $user, $evt );
352 # verifiese the session and checks the permissions agains the
353 # session user and the user's home_ou as the org id
355 my( $self, $session, @perms ) = @_;
356 my $user; my $evt; my $e;
357 $logger->debug("Checking user session $session and perms @perms");
358 ($user, $evt) = $self->checkses($session);
359 return (undef, $evt) if $evt;
360 $evt = $self->check_perms($user->id, $user->home_ou, @perms);
361 return ($user, $evt);
366 my( $self, $staffobj, $userid, @perms ) = @_;
368 $userid = $staffobj->id unless defined $userid;
370 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
372 if( $userid ne $staffobj->id ) {
373 ($user, $evt) = $self->fetch_user($userid);
374 return (undef, $evt) if $evt;
375 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
381 return ($user, $evt);
384 sub checkses_requestor {
385 my( $self, $authtoken, $targetid, @perms ) = @_;
386 my( $requestor, $target, $evt );
388 ($requestor, $evt) = $self->checkses($authtoken);
389 return (undef, undef, $evt) if $evt;
391 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
392 return( $requestor, $target, $evt);
396 my( $self, $copyid ) = @_;
399 $logger->debug("Fetching copy $copyid from storage");
401 $copy = $self->simplereq(
403 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
405 if(!$copy) { $evt = OpenILS::Event->new('COPY_NOT_FOUND'); }
407 return( $copy, $evt );
411 # retrieves a circ object by id
412 sub fetch_circulation {
413 my( $self, $circid ) = @_;
416 $logger->debug("Fetching circ $circid from storage");
418 $circ = $self->simplereq(
420 "open-ils.storage.direct.action.circulation.retrieve", $circid );
423 $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND', circid => $circid );
426 return ( $circ, $evt );
429 sub fetch_record_by_copy {
430 my( $self, $copyid ) = @_;
433 $logger->debug("Fetching record by copy $copyid from storage");
435 $record = $self->simplereq(
437 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy', $copyid );
440 $evt = OpenILS::Event->new('BIBLIO_RECORD_NOT_FOUND');
443 return ($record, $evt);
446 # turns a record object into an mvr (mods) object
448 my( $self, $record ) = @_;
449 my $u = OpenILS::Utils::ModsParser->new();
450 $u->start_mods_batch( $record->marc );
451 my $mods = $u->finish_mods_batch();
452 $mods->doc_id($record->id);
457 my( $self, $holdid ) = @_;
460 $logger->debug("Fetching hold $holdid from storage");
462 $hold = $self->simplereq(
464 'open-ils.storage.direct.action.hold_request.retrieve', $holdid);
466 $evt = OpenILS::Event->new('HOLD_NOT_FOUND', holdid => $holdid) unless $hold;
468 return ($hold, $evt);
472 sub fetch_hold_transit_by_hold {
473 my( $self, $holdid ) = @_;
474 my( $transit, $evt );
476 $logger->debug("Fetching transit by hold $holdid from storage");
478 $transit = $self->simplereq(
480 'open-ils.storage.direct.action.hold_transit_copy.search.hold', $holdid );
482 $evt = OpenILS::Event->new('HOLD_TRANSIT_NOT_FOUND', holdid => $holdid) unless $transit;
484 return ($transit, $evt );
487 # fetches the captured, but not fulfilled hold attached to a given copy
488 sub fetch_open_hold_by_copy {
489 my( $self, $copyid ) = @_;
490 $logger->debug("Searching for active hold for copy $copyid");
493 $hold = $self->storagereq(
494 'open-ils.storage.direct.action.hold_request.search_where',
496 current_copy => $copyid ,
497 capture_time => { "!=" => undef },
498 fulfillment_time => undef
501 $evt = OpenILS::Event->new('HOLD_NOT_FOUND', copyid => $copyid) unless $hold;
502 return ($hold, $evt);
505 sub fetch_hold_transit {
506 my( $self, $transid ) = @_;
507 my( $htransit, $evt );
508 $logger->debug("Fetching hold transit with hold id $transid");
509 $htransit = $self->storagereq(
510 'open-ils.storage.direct.action.hold_transit_copy.retrieve', $transid );
511 $evt = OpenILS::Event->new('HOLD_TRANSIT_NOT_FOUND', id => $transid) unless $htransit;
512 return ($htransit, $evt);
515 sub fetch_copy_by_barcode {
516 my( $self, $barcode ) = @_;
519 $logger->debug("Fetching copy by barcode $barcode from storage");
521 $copy = $self->simplereq( 'open-ils.storage',
522 'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
524 $evt = OpenILS::Event->new('COPY_NOT_FOUND', barcode => $barcode) unless $copy;
526 return ($copy, $evt);
529 sub fetch_open_billable_transaction {
530 my( $self, $transid ) = @_;
531 my( $transaction, $evt );
533 $logger->debug("Fetching open billable transaction $transid from storage");
535 $transaction = $self->simplereq(
537 'open-ils.storage.direct.money.open_billable_transaction_summary.retrieve', $transid);
539 $evt = OpenILS::Event->new(
540 'TRANSACTION_NOT_FOUND', transid => $transid ) unless $transaction;
542 return ($transaction, $evt);
548 $buckets{'biblio'} = 'biblio_record_entry_bucket';
549 $buckets{'callnumber'} = 'call_number_bucket';
550 $buckets{'copy'} = 'copy_bucket';
551 $buckets{'user'} = 'user_bucket';
553 sub fetch_container {
554 my( $self, $id, $type ) = @_;
557 $logger->debug("Fetching container $id with type $type");
559 my $meth = $buckets{$type};
560 $bucket = $self->simplereq(
562 "open-ils.storage.direct.container.$meth.retrieve", $id );
564 $evt = OpenILS::Event->new(
565 'CONTAINER_NOT_FOUND', container => $id,
566 container_type => $type ) unless $bucket;
568 return ($bucket, $evt);
572 sub fetch_container_item {
573 my( $self, $id, $type ) = @_;
576 $logger->debug("Fetching container item $id with type $type");
578 my $meth = $buckets{$type} . "_item";
580 $bucket = $self->simplereq(
582 "open-ils.storage.direct.container.$meth.retrieve", $id );
584 $evt = OpenILS::Event->new(
585 'CONTAINER_ITEM_NOT_FOUND', itemid => $id,
586 container_type => $type ) unless $bucket;
588 return ($bucket, $evt);
592 sub fetch_patron_standings {
594 $logger->debug("Fetching patron standings");
595 return $self->simplereq(
597 'open-ils.storage.direct.config.standing.retrieve.all.atomic');
601 sub fetch_permission_group_tree {
603 $logger->debug("Fetching patron profiles");
604 return $self->simplereq(
606 'open-ils.actor.groups.tree.retrieve' );
610 sub fetch_patron_circ_summary {
611 my( $self, $userid ) = @_;
612 $logger->debug("Fetching patron summary for $userid");
613 my $summary = $self->simplereq(
615 "open-ils.storage.action.circulation.patron_summary", $userid );
619 $summary->[1] ||= 0.0;
626 sub fetch_copy_statuses {
628 $logger->debug("Fetching copy statuses");
629 return $self->simplereq(
631 'open-ils.storage.direct.config.copy_status.retrieve.all.atomic' );
634 sub fetch_copy_location {
635 my( $self, $id ) = @_;
637 my $cl = $self->storagereq(
638 'open-ils.storage.direct.asset.copy_location.retrieve', $id );
639 $evt = OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless $cl;
643 sub fetch_copy_locations {
645 return $self->simplereq(
647 'open-ils.storage.direct.asset.copy_location.retrieve.all.atomic');
650 sub fetch_copy_location_by_name {
651 my( $self, $name, $org ) = @_;
653 my $cl = $self->storagereq(
654 'open-ils.storage.direct.asset.copy_location.search_where',
655 { name => $name, owning_lib => $org } );
656 $evt = OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless $cl;
660 sub fetch_callnumber {
661 my( $self, $id ) = @_;
663 $logger->debug("Fetching callnumber $id");
665 my $cn = $self->simplereq(
667 'open-ils.storage.direct.asset.call_number.retrieve', $id );
668 $evt = OpenILS::Event->new( 'VOLUME_NOT_FOUND', id => $id ) unless $cn;
670 return ( $cn, $evt );
674 my( $self, $id ) = @_;
675 return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
676 $logger->debug("Fetching org unit $id");
679 my $org = $self->simplereq(
681 'open-ils.storage.direct.actor.org_unit.retrieve', $id );
682 $evt = OpenILS::Event->new( 'ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
688 my( $self, $type, $id ) = @_;
690 $logger->debug("Fetching $type stat cat: $id");
691 $cat = $self->simplereq(
693 "open-ils.storage.direct.$type.stat_cat.retrieve", $id );
694 $evt = OpenILS::Event->new( 'STAT_CAT_NOT_FOUND', id => $id ) unless $cat;
695 return ( $cat, $evt );
698 sub fetch_stat_cat_entry {
699 my( $self, $type, $id ) = @_;
701 $logger->debug("Fetching $type stat cat entry: $id");
702 $entry = $self->simplereq(
704 "open-ils.storage.direct.$type.stat_cat_entry.retrieve", $id );
705 $evt = OpenILS::Event->new( 'STAT_CAT_ENTRY_NOT_FOUND', id => $id ) unless $entry;
706 return ( $entry, $evt );
711 my( $self, $org_tree, $orgid ) = @_;
712 return $org_tree if ( $org_tree->id eq $orgid );
713 return undef unless ref($org_tree->children);
714 for my $c (@{$org_tree->children}) {
715 my $o = $self->find_org($c, $orgid);
721 sub fetch_non_cat_type_by_name_and_org {
722 my( $self, $name, $orgId ) = @_;
723 $logger->debug("Fetching non cat type $name at org $orgId");
724 my $types = $self->simplereq(
726 'open-ils.storage.direct.config.non_cataloged_type.search_where.atomic',
727 { name => $name, owning_lib => $orgId } );
728 return ($types->[0], undef) if($types and @$types);
729 return (undef, OpenILS::Event->new('NON_CAT_TYPE_NOT_FOUND') );
732 sub fetch_non_cat_type {
733 my( $self, $id ) = @_;
734 $logger->debug("Fetching non cat type $id");
736 $type = $self->simplereq(
738 'open-ils.storage.direct.config.non_cataloged_type.retrieve', $id );
739 $evt = OpenILS::Event->new('NON_CAT_TYPE_NOT_FOUND') unless $type;
740 return ($type, $evt);
743 sub DB_UPDATE_FAILED {
744 my( $self, $payload ) = @_;
745 return OpenILS::Event->new('DATABASE_UPDATE_FAILED',
746 payload => ($payload) ? $payload : undef );
749 sub fetch_circ_duration_by_name {
750 my( $self, $name ) = @_;
752 $dur = $self->simplereq(
754 'open-ils.storage.direct.config.rules.circ_duration.search.name.atomic', $name );
756 $evt = OpenILS::Event->new('CIRC_DURATION_NOT_FOUND') unless $dur;
760 sub fetch_recurring_fine_by_name {
761 my( $self, $name ) = @_;
763 $obj = $self->simplereq(
765 'open-ils.storage.direct.config.rules.recuring_fine.search.name.atomic', $name );
767 $evt = OpenILS::Event->new('RECURRING_FINE_NOT_FOUND') unless $obj;
771 sub fetch_max_fine_by_name {
772 my( $self, $name ) = @_;
774 $obj = $self->simplereq(
776 'open-ils.storage.direct.config.rules.max_fine.search.name.atomic', $name );
778 $evt = OpenILS::Event->new('MAX_FINE_NOT_FOUND') unless $obj;
783 my( $self, $method, @params ) = @_;
784 return $self->simplereq(
785 'open-ils.storage', $method, @params );
789 my( $self, $e, $name ) = @_;
790 if( $e and ref($e) eq 'HASH' and
791 defined($e->{textcode}) and $e->{textcode} eq $name ) {
798 my( undef, $f, $l ) = caller(0);
799 my( undef, undef, undef, $s ) = caller(1);
802 $logger->debug("LOGMARK: $f:$l:$s");
806 sub fetch_open_circulation {
807 my( $self, $cid ) = @_;
810 my $circ = $self->storagereq(
811 'open-ils.storage.direct.action.open_circulation.search_where',
812 { target_copy => $cid, stop_fines_time => undef } );
813 $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND') unless $circ;
814 return ($circ, $evt);
818 sub copy_status_from_name {
819 my( $self, $name ) = @_;
820 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
821 for my $status (@$copy_statuses) {
822 return $status if( $status->name =~ /$name/i );
827 sub copy_status_to_name {
828 my( $self, $sid ) = @_;
829 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
830 for my $status (@$copy_statuses) {
831 return $status->name if( $status->id == $sid );
836 sub fetch_open_transit_by_copy {
837 my( $self, $copyid ) = @_;
839 $transit = $self->storagereq(
840 'open-ils.storage.direct.action.transit_copy.search_where',
841 { target_copy => $copyid, dest_recv_time => undef });
842 $evt = OpenILS::Event->new('TRANSIT_NOT_FOUND') unless $transit;
843 return ($transit, $evt);
847 my( $self, $copy ) = @_;
848 return undef unless $copy;
849 $copy->status( $copy->status->id ) if ref($copy->status);
850 $copy->location( $copy->location->id ) if ref($copy->location);
851 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
855 # un-fleshes a copy and updates it in the DB
856 # returns a DB_UPDATE_FAILED event on error
857 # returns undef on success
859 my( $self, %params ) = @_;
861 my $copy = $params{copy} || die "update_copy(): copy required";
862 my $editor = $params{editor} || die "update_copy(): copy editor required";
863 my $session = $params{session};
865 $logger->debug("Updating copy in the database: " . $copy->id);
867 $self->unflesh_copy($copy);
868 $copy->editor( $editor );
869 $copy->edit_date( 'now' );
872 my $meth = 'open-ils.storage.direct.asset.copy.update';
874 $s = $session->request( $meth, $copy )->gather(1) if $session;
875 $s = $self->storagereq( $meth, $copy );
877 $logger->debug("Update of copy ".$copy->id." returned: $s");
879 return $self->DB_UPDATE_FAILED($copy) unless $s;
883 sub fetch_billable_xact {
884 my( $self, $id ) = @_;
886 $logger->debug("Fetching billable transaction %id");
887 $xact = $self->storagereq(
888 'open-ils.storage.direct.money.billable_transaction.retrieve', $id );
889 $evt = OpenILS::Event->new('TRANSACTION_NOT_FOUND') unless $xact;
890 return ($xact, $evt);
894 sub fetch_fleshed_copy {
895 my( $self, $id ) = @_;
897 $logger->info("Fetching fleshed copy $id");
898 $copy = $self->storagereq(
899 "open-ils.storage.fleshed.asset.copy.retrieve", $id );
900 $evt = OpenILS::Event->new('COPY_NOT_FOUND', id => $id) unless $copy;
901 return ($copy, $evt);