1 package OpenILS::Application::AppUtils;
3 use strict; use warnings;
4 use OpenILS::Application;
5 use base qw/OpenILS::Application/;
6 use OpenSRF::Utils::Cache;
7 use OpenSRF::Utils::Logger qw/$logger/;
8 use OpenILS::Utils::ModsParser;
9 use OpenSRF::EX qw(:try);
12 use OpenILS::Utils::CStoreEditor;
13 use OpenILS::Const qw/:const/;
14 use Unicode::Normalize;
15 use OpenSRF::Utils::SettingsClient;
19 # ---------------------------------------------------------------------------
20 # Pile of utilty methods used accross applications.
21 # ---------------------------------------------------------------------------
22 my $cache_client = "OpenSRF::Utils::Cache";
25 # ---------------------------------------------------------------------------
26 # on sucess, returns the created session, on failure throws ERROR exception
27 # ---------------------------------------------------------------------------
28 sub start_db_session {
31 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
32 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
34 my $trans_resp = $trans_req->recv();
35 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
36 if( ! $trans_resp->content() ) {
38 ("Unable to Begin Transaction with database" );
42 $logger->debug("Setting global storage session to ".
43 "session: " . $session->session_id . " : " . $session->app );
51 transform => 'permission.usr_has_perm',
62 # returns undef if user has all of the perms provided
63 # returns the first failed perm on failure
64 sub check_user_perms {
65 my($self, $user_id, $org_id, @perm_types ) = @_;
66 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
68 for my $type (@perm_types) {
69 $PERM_QUERY->{select}->{au}->[0]->{params} = [$type, $org_id];
70 $PERM_QUERY->{where}->{id} = $user_id;
71 return $type unless $self->is_true(OpenILS::Utils::CStoreEditor->new->json_query($PERM_QUERY)->[0]->{has_perm});
76 # checks the list of user perms. The first one that fails returns a new
78 my( $self, $user_id, $org_id, @perm_types ) = @_;
79 my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
80 return OpenILS::Event->new('PERM_FAILURE', ilsperm => $t, ilspermloc => $org_id ) if $t;
86 # ---------------------------------------------------------------------------
87 # commits and destroys the session
88 # ---------------------------------------------------------------------------
89 sub commit_db_session {
90 my( $self, $session ) = @_;
92 my $req = $session->request( "open-ils.storage.transaction.commit" );
93 my $resp = $req->recv();
96 throw OpenSRF::EX::ERROR ("Unable to commit db session");
99 if(UNIVERSAL::isa($resp,"Error")) {
100 throw $resp ($resp->stringify);
103 if(!$resp->content) {
104 throw OpenSRF::EX::ERROR ("Unable to commit db session");
108 $session->disconnect();
112 sub rollback_db_session {
113 my( $self, $session ) = @_;
115 my $req = $session->request("open-ils.storage.transaction.rollback");
116 my $resp = $req->recv();
117 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
120 $session->disconnect();
125 # returns undef it the event is not an ILS event
126 # returns the event code otherwise
128 my( $self, $evt ) = @_;
129 return $evt->{ilsevent} if( ref($evt) eq 'HASH' and defined($evt->{ilsevent})) ;
133 # ---------------------------------------------------------------------------
134 # Checks to see if a user is logged in. Returns the user record on success,
135 # throws an exception on error.
136 # ---------------------------------------------------------------------------
137 sub check_user_session {
138 my( $self, $user_session ) = @_;
140 my $content = $self->simplereq(
142 'open-ils.auth.session.retrieve', $user_session);
144 return undef if (!$content) or $self->event_code($content);
148 # generic simple request returning a scalar value
150 my($self, $service, $method, @params) = @_;
151 return $self->simple_scalar_request($service, $method, @params);
155 sub simple_scalar_request {
156 my($self, $service, $method, @params) = @_;
158 my $session = OpenSRF::AppSession->create( $service );
160 my $request = $session->request( $method, @params );
166 $val = $request->gather(1);
173 warn "received error : service=$service : method=$method : params=".Dumper(\@params) . "\n $err";
174 throw $err ("Call to $service for method $method \n failed with exception: $err : " );
186 my $org_typelist = undef;
187 my $org_typelist_hash = {};
191 # can we throw this version away??
194 if($tree) { return $tree; }
196 # see if it's in the cache
197 $tree = $cache_client->new()->get_cache('_orgtree');
198 if($tree) { return $tree; }
201 warn "Retrieving Org Tree\n";
202 $orglist = $self->simple_scalar_request(
204 "open-ils.cstore.direct.actor.org_unit.search.atomic",
205 { id => { '!=' => undef } }
209 if( ! $org_typelist ) {
210 warn "Retrieving org types\n";
211 $org_typelist = $self->simple_scalar_request(
213 "open-ils.cstore.direct.actor.org_unit_type.search.atomic",
214 { id => { '!=' => undef } }
216 $self->build_org_type($org_typelist);
219 $tree = $self->build_org_tree($orglist,1);
220 $cache_client->new()->put_cache('_orgtree', $tree);
225 my $slimtree = undef;
226 sub get_slim_org_tree {
229 if($slimtree) { return $slimtree; }
231 # see if it's in the cache
232 $slimtree = $cache_client->new()->get_cache('slimorgtree');
233 if($slimtree) { return $slimtree; }
236 warn "Retrieving Org Tree\n";
237 $orglist = $self->simple_scalar_request(
239 "open-ils.cstore.direct.actor.org_unit.search.atomic",
240 { id => { '!=' => undef } }
244 $slimtree = $self->build_org_tree($orglist);
245 $cache_client->new->put_cache('slimorgtree', $slimtree);
252 my($self, $org_typelist) = @_;
253 for my $type (@$org_typelist) {
254 $org_typelist_hash->{$type->id()} = $type;
262 my( $self, $orglist, $add_types ) = @_;
264 return $orglist unless ref $orglist;
265 return $$orglist[0] if @$orglist == 1;
268 $a->ou_type <=> $b->ou_type ||
269 $a->name cmp $b->name } @$orglist;
271 for my $org (@list) {
275 if(!ref($org->ou_type()) and $add_types) {
276 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
279 next if (!defined($org->parent_ou) || $org->parent_ou eq "");
281 my ($parent) = grep { $_->id == $org->parent_ou } @list;
283 $parent->children([]) unless defined($parent->children);
284 push( @{$parent->children}, $org );
290 sub fetch_closed_date {
291 my( $self, $cd ) = @_;
294 $logger->debug("Fetching closed_date $cd from cstore");
296 my $cd_obj = $self->simplereq(
298 'open-ils.cstore.direct.actor.org_unit.closed_date.retrieve', $cd );
301 $logger->info("closed_date $cd not found in the db");
302 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
305 return ($cd_obj, $evt);
309 my( $self, $userid ) = @_;
312 $logger->debug("Fetching user $userid from cstore");
314 $user = $self->simplereq(
316 'open-ils.cstore.direct.actor.user.retrieve', $userid );
319 $logger->info("User $userid not found in the db");
320 $evt = OpenILS::Event->new('ACTOR_USER_NOT_FOUND');
323 return ($user, $evt);
327 my( $self, $session ) = @_;
328 my $user = $self->check_user_session($session) or
329 return (undef, OpenILS::Event->new('NO_SESSION'));
334 # verifiese the session and checks the permissions agains the
335 # session user and the user's home_ou as the org id
337 my( $self, $session, @perms ) = @_;
338 my $user; my $evt; my $e;
339 $logger->debug("Checking user session $session and perms @perms");
340 ($user, $evt) = $self->checkses($session);
341 return (undef, $evt) if $evt;
342 $evt = $self->check_perms($user->id, $user->home_ou, @perms);
343 return ($user, $evt);
348 my( $self, $staffobj, $userid, @perms ) = @_;
350 $userid = $staffobj->id unless defined $userid;
352 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
354 if( $userid ne $staffobj->id ) {
355 ($user, $evt) = $self->fetch_user($userid);
356 return (undef, $evt) if $evt;
357 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
363 return ($user, $evt);
366 sub checkses_requestor {
367 my( $self, $authtoken, $targetid, @perms ) = @_;
368 my( $requestor, $target, $evt );
370 ($requestor, $evt) = $self->checkses($authtoken);
371 return (undef, undef, $evt) if $evt;
373 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
374 return( $requestor, $target, $evt);
378 my( $self, $copyid ) = @_;
381 $logger->debug("Fetching copy $copyid from cstore");
383 $copy = $self->simplereq(
385 'open-ils.cstore.direct.asset.copy.retrieve', $copyid );
387 if(!$copy) { $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND'); }
389 return( $copy, $evt );
393 # retrieves a circ object by id
394 sub fetch_circulation {
395 my( $self, $circid ) = @_;
398 $logger->debug("Fetching circ $circid from cstore");
400 $circ = $self->simplereq(
402 "open-ils.cstore.direct.action.circulation.retrieve", $circid );
405 $evt = OpenILS::Event->new('ACTION_CIRCULATION_NOT_FOUND', circid => $circid );
408 return ( $circ, $evt );
411 sub fetch_record_by_copy {
412 my( $self, $copyid ) = @_;
415 $logger->debug("Fetching record by copy $copyid from cstore");
417 $record = $self->simplereq(
419 'open-ils.cstore.direct.asset.copy.retrieve', $copyid,
421 flesh_fields => { bre => [ 'fixed_fields' ],
423 acp => [ 'call_number' ],
429 $evt = OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND');
431 $record = $record->call_number->record;
434 return ($record, $evt);
437 # turns a record object into an mvr (mods) object
439 my( $self, $record ) = @_;
440 return undef unless $record and $record->marc;
441 my $u = OpenILS::Utils::ModsParser->new();
442 $u->start_mods_batch( $record->marc );
443 my $mods = $u->finish_mods_batch();
444 $mods->doc_id($record->id);
445 $mods->tcn($record->tcn_value);
450 my( $self, $holdid ) = @_;
453 $logger->debug("Fetching hold $holdid from cstore");
455 $hold = $self->simplereq(
457 'open-ils.cstore.direct.action.hold_request.retrieve', $holdid);
459 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', holdid => $holdid) unless $hold;
461 return ($hold, $evt);
465 sub fetch_hold_transit_by_hold {
466 my( $self, $holdid ) = @_;
467 my( $transit, $evt );
469 $logger->debug("Fetching transit by hold $holdid from cstore");
471 $transit = $self->simplereq(
473 'open-ils.cstore.direct.action.hold_transit_copy.search', { hold => $holdid } );
475 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', holdid => $holdid) unless $transit;
477 return ($transit, $evt );
480 # fetches the captured, but not fulfilled hold attached to a given copy
481 sub fetch_open_hold_by_copy {
482 my( $self, $copyid ) = @_;
483 $logger->debug("Searching for active hold for copy $copyid");
486 $hold = $self->cstorereq(
487 'open-ils.cstore.direct.action.hold_request.search',
489 current_copy => $copyid ,
490 capture_time => { "!=" => undef },
491 fulfillment_time => undef,
492 cancel_time => undef,
495 $evt = OpenILS::Event->new('ACTION_HOLD_REQUEST_NOT_FOUND', copyid => $copyid) unless $hold;
496 return ($hold, $evt);
499 sub fetch_hold_transit {
500 my( $self, $transid ) = @_;
501 my( $htransit, $evt );
502 $logger->debug("Fetching hold transit with hold id $transid");
503 $htransit = $self->cstorereq(
504 'open-ils.cstore.direct.action.hold_transit_copy.retrieve', $transid );
505 $evt = OpenILS::Event->new('ACTION_HOLD_TRANSIT_COPY_NOT_FOUND', id => $transid) unless $htransit;
506 return ($htransit, $evt);
509 sub fetch_copy_by_barcode {
510 my( $self, $barcode ) = @_;
513 $logger->debug("Fetching copy by barcode $barcode from cstore");
515 $copy = $self->simplereq( 'open-ils.cstore',
516 'open-ils.cstore.direct.asset.copy.search', { barcode => $barcode, deleted => 'f'} );
517 #'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
519 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', barcode => $barcode) unless $copy;
521 return ($copy, $evt);
524 sub fetch_open_billable_transaction {
525 my( $self, $transid ) = @_;
526 my( $transaction, $evt );
528 $logger->debug("Fetching open billable transaction $transid from cstore");
530 $transaction = $self->simplereq(
532 'open-ils.cstore.direct.money.open_billable_transaction_summary.retrieve', $transid);
534 $evt = OpenILS::Event->new(
535 'MONEY_OPEN_BILLABLE_TRANSACTION_SUMMARY_NOT_FOUND', transid => $transid ) unless $transaction;
537 return ($transaction, $evt);
543 $buckets{'biblio'} = 'biblio_record_entry_bucket';
544 $buckets{'callnumber'} = 'call_number_bucket';
545 $buckets{'copy'} = 'copy_bucket';
546 $buckets{'user'} = 'user_bucket';
548 sub fetch_container {
549 my( $self, $id, $type ) = @_;
552 $logger->debug("Fetching container $id with type $type");
554 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_NOT_FOUND';
555 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_NOT_FOUND' if $type eq 'biblio';
556 $e = 'CONTAINER_USER_BUCKET_NOT_FOUND' if $type eq 'user';
557 $e = 'CONTAINER_COPY_BUCKET_NOT_FOUND' if $type eq 'copy';
559 my $meth = $buckets{$type};
560 $bucket = $self->simplereq(
562 "open-ils.cstore.direct.container.$meth.retrieve", $id );
564 $evt = OpenILS::Event->new(
565 $e, container => $id, container_type => $type ) unless $bucket;
567 return ($bucket, $evt);
571 sub fetch_container_e {
572 my( $self, $editor, $id, $type ) = @_;
575 $bucket = $editor->retrieve_container_copy_bucket($id) if $type eq 'copy';
576 $bucket = $editor->retrieve_container_call_number_bucket($id) if $type eq 'callnumber';
577 $bucket = $editor->retrieve_container_biblio_record_entry_bucket($id) if $type eq 'biblio';
578 $bucket = $editor->retrieve_container_user_bucket($id) if $type eq 'user';
580 $evt = $editor->event unless $bucket;
581 return ($bucket, $evt);
584 sub fetch_container_item_e {
585 my( $self, $editor, $id, $type ) = @_;
588 $bucket = $editor->retrieve_container_copy_bucket_item($id) if $type eq 'copy';
589 $bucket = $editor->retrieve_container_call_number_bucket_item($id) if $type eq 'callnumber';
590 $bucket = $editor->retrieve_container_biblio_record_entry_bucket_item($id) if $type eq 'biblio';
591 $bucket = $editor->retrieve_container_user_bucket_item($id) if $type eq 'user';
593 $evt = $editor->event unless $bucket;
594 return ($bucket, $evt);
601 sub fetch_container_item {
602 my( $self, $id, $type ) = @_;
605 $logger->debug("Fetching container item $id with type $type");
607 my $meth = $buckets{$type} . "_item";
609 $bucket = $self->simplereq(
611 "open-ils.cstore.direct.container.$meth.retrieve", $id );
614 my $e = 'CONTAINER_CALL_NUMBER_BUCKET_ITEM_NOT_FOUND';
615 $e = 'CONTAINER_BIBLIO_RECORD_ENTRY_BUCKET_ITEM_NOT_FOUND' if $type eq 'biblio';
616 $e = 'CONTAINER_USER_BUCKET_ITEM_NOT_FOUND' if $type eq 'user';
617 $e = 'CONTAINER_COPY_BUCKET_ITEM_NOT_FOUND' if $type eq 'copy';
619 $evt = OpenILS::Event->new(
620 $e, itemid => $id, container_type => $type ) unless $bucket;
622 return ($bucket, $evt);
626 sub fetch_patron_standings {
628 $logger->debug("Fetching patron standings");
629 return $self->simplereq(
631 'open-ils.cstore.direct.config.standing.search.atomic', { id => { '!=' => undef } });
635 sub fetch_permission_group_tree {
637 $logger->debug("Fetching patron profiles");
638 return $self->simplereq(
640 'open-ils.actor.groups.tree.retrieve' );
643 sub fetch_permission_group_descendants {
644 my( $self, $profile ) = @_;
645 my $group_tree = $self->fetch_permission_group_tree();
649 # FIXME: okay, so it's not an org tree, but it is compatible
650 $self->walk_org_tree($group_tree, sub {
652 if ($g->id == $profile) {
657 $self->walk_org_tree($start_here, sub {
659 push(@groups,$g->id);
665 sub fetch_patron_circ_summary {
666 my( $self, $userid ) = @_;
667 $logger->debug("Fetching patron summary for $userid");
668 my $summary = $self->simplereq(
670 "open-ils.storage.action.circulation.patron_summary", $userid );
674 $summary->[1] ||= 0.0;
681 sub fetch_copy_statuses {
683 $logger->debug("Fetching copy statuses");
684 return $self->simplereq(
686 'open-ils.cstore.direct.config.copy_status.search.atomic', { id => { '!=' => undef } });
689 sub fetch_copy_location {
690 my( $self, $id ) = @_;
692 my $cl = $self->cstorereq(
693 'open-ils.cstore.direct.asset.copy_location.retrieve', $id );
694 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
698 sub fetch_copy_locations {
700 return $self->simplereq(
702 'open-ils.cstore.direct.asset.copy_location.search.atomic', { id => { '!=' => undef } });
705 sub fetch_copy_location_by_name {
706 my( $self, $name, $org ) = @_;
708 my $cl = $self->cstorereq(
709 'open-ils.cstore.direct.asset.copy_location.search',
710 { name => $name, owning_lib => $org } );
711 $evt = OpenILS::Event->new('ASSET_COPY_LOCATION_NOT_FOUND') unless $cl;
715 sub fetch_callnumber {
716 my( $self, $id, $flesh ) = @_;
719 my $e = OpenILS::Event->new( 'ASSET_CALL_NUMBER_NOT_FOUND', id => $id );
720 return( undef, $e ) unless $id;
722 $logger->debug("Fetching callnumber $id");
724 my $cn = $self->simplereq(
726 'open-ils.cstore.direct.asset.call_number.retrieve', $id );
727 $evt = $e unless $cn;
733 'open-ils.cstore.direct.asset.call_number_prefix.retrieve', $cn->prefix
739 'open-ils.cstore.direct.asset.call_number_suffix.retrieve', $cn->suffix
745 'open-ils.cstore.direct.asset.call_number_class.retrieve', $cn->label_class
750 return ( $cn, $evt );
753 my %ORG_CACHE; # - these rarely change, so cache them..
755 my( $self, $id ) = @_;
756 return undef unless $id;
757 return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
758 return $ORG_CACHE{$id} if $ORG_CACHE{$id};
759 $logger->debug("Fetching org unit $id");
762 my $org = $self->simplereq(
764 'open-ils.cstore.direct.actor.org_unit.retrieve', $id );
765 $evt = OpenILS::Event->new( 'ACTOR_ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
766 $ORG_CACHE{$id} = $org;
772 my( $self, $type, $id ) = @_;
774 $logger->debug("Fetching $type stat cat: $id");
775 $cat = $self->simplereq(
777 "open-ils.cstore.direct.$type.stat_cat.retrieve", $id );
779 my $e = 'ASSET_STAT_CAT_NOT_FOUND';
780 $e = 'ACTOR_STAT_CAT_NOT_FOUND' if $type eq 'actor';
782 $evt = OpenILS::Event->new( $e, id => $id ) unless $cat;
783 return ( $cat, $evt );
786 sub fetch_stat_cat_entry {
787 my( $self, $type, $id ) = @_;
789 $logger->debug("Fetching $type stat cat entry: $id");
790 $entry = $self->simplereq(
792 "open-ils.cstore.direct.$type.stat_cat_entry.retrieve", $id );
794 my $e = 'ASSET_STAT_CAT_ENTRY_NOT_FOUND';
795 $e = 'ACTOR_STAT_CAT_ENTRY_NOT_FOUND' if $type eq 'actor';
797 $evt = OpenILS::Event->new( $e, id => $id ) unless $entry;
798 return ( $entry, $evt );
803 my( $self, $org_tree, $orgid ) = @_;
804 return undef unless $org_tree and defined $orgid;
805 return $org_tree if ( $org_tree->id eq $orgid );
806 return undef unless ref($org_tree->children);
807 for my $c (@{$org_tree->children}) {
808 my $o = $self->find_org($c, $orgid);
814 sub fetch_non_cat_type_by_name_and_org {
815 my( $self, $name, $orgId ) = @_;
816 $logger->debug("Fetching non cat type $name at org $orgId");
817 my $types = $self->simplereq(
819 'open-ils.cstore.direct.config.non_cataloged_type.search.atomic',
820 { name => $name, owning_lib => $orgId } );
821 return ($types->[0], undef) if($types and @$types);
822 return (undef, OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') );
825 sub fetch_non_cat_type {
826 my( $self, $id ) = @_;
827 $logger->debug("Fetching non cat type $id");
829 $type = $self->simplereq(
831 'open-ils.cstore.direct.config.non_cataloged_type.retrieve', $id );
832 $evt = OpenILS::Event->new('CONFIG_NON_CATALOGED_TYPE_NOT_FOUND') unless $type;
833 return ($type, $evt);
836 sub DB_UPDATE_FAILED {
837 my( $self, $payload ) = @_;
838 return OpenILS::Event->new('DATABASE_UPDATE_FAILED',
839 payload => ($payload) ? $payload : undef );
842 sub fetch_booking_reservation {
843 my( $self, $id ) = @_;
846 $res = $self->simplereq(
848 'open-ils.cstore.direct.booking.reservation.retrieve', $id
851 # simplereq doesn't know how to flesh so ...
856 'open-ils.cstore.direct.actor.user.retrieve', $res->usr
860 $res->target_resource_type(
863 'open-ils.cstore.direct.booking.resource_type.retrieve', $res->target_resource_type
867 if ($res->current_resource) {
868 $res->current_resource(
871 'open-ils.cstore.direct.booking.resource.retrieve', $res->current_resource
875 if ($self->is_true( $res->target_resource_type->catalog_item )) {
876 $res->current_resource->catalog_item( $self->fetch_copy_by_barcode( $res->current_resource->barcode ) );
880 if ($res->target_resource) {
881 $res->target_resource(
884 'open-ils.cstore.direct.booking.resource.retrieve', $res->target_resource
888 if ($self->is_true( $res->target_resource_type->catalog_item )) {
889 $res->target_resource->catalog_item( $self->fetch_copy_by_barcode( $res->target_resource->barcode ) );
894 $evt = OpenILS::Event->new('RESERVATION_NOT_FOUND');
900 sub fetch_circ_duration_by_name {
901 my( $self, $name ) = @_;
903 $dur = $self->simplereq(
905 'open-ils.cstore.direct.config.rules.circ_duration.search.atomic', { name => $name } );
907 $evt = OpenILS::Event->new('CONFIG_RULES_CIRC_DURATION_NOT_FOUND') unless $dur;
911 sub fetch_recurring_fine_by_name {
912 my( $self, $name ) = @_;
914 $obj = $self->simplereq(
916 'open-ils.cstore.direct.config.rules.recurring_fine.search.atomic', { name => $name } );
918 $evt = OpenILS::Event->new('CONFIG_RULES_RECURRING_FINE_NOT_FOUND') unless $obj;
922 sub fetch_max_fine_by_name {
923 my( $self, $name ) = @_;
925 $obj = $self->simplereq(
927 'open-ils.cstore.direct.config.rules.max_fine.search.atomic', { name => $name } );
929 $evt = OpenILS::Event->new('CONFIG_RULES_MAX_FINE_NOT_FOUND') unless $obj;
933 sub fetch_hard_due_date_by_name {
934 my( $self, $name ) = @_;
936 $obj = $self->simplereq(
938 'open-ils.cstore.direct.config.hard_due_date.search.atomic', { name => $name } );
940 $evt = OpenILS::Event->new('CONFIG_RULES_HARD_DUE_DATE_NOT_FOUND') unless $obj;
945 my( $self, $method, @params ) = @_;
946 return $self->simplereq(
947 'open-ils.storage', $method, @params );
950 sub storagereq_xact {
951 my($self, $method, @params) = @_;
952 my $ses = $self->start_db_session();
953 my $val = $ses->request($method, @params)->gather(1);
954 $self->rollback_db_session($ses);
959 my( $self, $method, @params ) = @_;
960 return $self->simplereq(
961 'open-ils.cstore', $method, @params );
965 my( $self, $e, $name ) = @_;
966 if( $e and ref($e) eq 'HASH' and
967 defined($e->{textcode}) and $e->{textcode} eq $name ) {
974 my( undef, $f, $l ) = caller(0);
975 my( undef, undef, undef, $s ) = caller(1);
978 $logger->debug("LOGMARK: $f:$l:$s");
982 sub fetch_open_circulation {
983 my( $self, $cid ) = @_;
986 my $e = OpenILS::Utils::CStoreEditor->new;
987 my $circ = $e->search_action_circulation({
989 stop_fines_time => undef,
990 checkin_time => undef
993 return ($circ, $e->event);
997 sub copy_status_from_name {
998 my( $self, $name ) = @_;
999 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
1000 for my $status (@$copy_statuses) {
1001 return $status if( $status->name =~ /$name/i );
1006 sub copy_status_to_name {
1007 my( $self, $sid ) = @_;
1008 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
1009 for my $status (@$copy_statuses) {
1010 return $status->name if( $status->id == $sid );
1017 my( $self, $arg ) = @_;
1018 return $arg if ref $arg;
1019 $copy_statuses = $self->fetch_copy_statuses unless $copy_statuses;
1020 my ($stat) = grep { $_->id == $arg } @$copy_statuses;
1024 sub fetch_open_transit_by_copy {
1025 my( $self, $copyid ) = @_;
1027 $transit = $self->cstorereq(
1028 'open-ils.cstore.direct.action.transit_copy.search',
1029 { target_copy => $copyid, dest_recv_time => undef });
1030 $evt = OpenILS::Event->new('ACTION_TRANSIT_COPY_NOT_FOUND') unless $transit;
1031 return ($transit, $evt);
1035 my( $self, $copy ) = @_;
1036 return undef unless $copy;
1037 $copy->status( $copy->status->id ) if ref($copy->status);
1038 $copy->location( $copy->location->id ) if ref($copy->location);
1039 $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
1043 sub unflesh_reservation {
1044 my( $self, $reservation ) = @_;
1045 return undef unless $reservation;
1046 $reservation->usr( $reservation->usr->id ) if ref($reservation->usr);
1047 $reservation->target_resource_type( $reservation->target_resource_type->id ) if ref($reservation->target_resource_type);
1048 $reservation->target_resource( $reservation->target_resource->id ) if ref($reservation->target_resource);
1049 $reservation->current_resource( $reservation->current_resource->id ) if ref($reservation->current_resource);
1050 return $reservation;
1053 # un-fleshes a copy and updates it in the DB
1054 # returns a DB_UPDATE_FAILED event on error
1055 # returns undef on success
1057 my( $self, %params ) = @_;
1059 my $copy = $params{copy} || die "update_copy(): copy required";
1060 my $editor = $params{editor} || die "update_copy(): copy editor required";
1061 my $session = $params{session};
1063 $logger->debug("Updating copy in the database: " . $copy->id);
1065 $self->unflesh_copy($copy);
1066 $copy->editor( $editor );
1067 $copy->edit_date( 'now' );
1070 my $meth = 'open-ils.storage.direct.asset.copy.update';
1072 $s = $session->request( $meth, $copy )->gather(1) if $session;
1073 $s = $self->storagereq( $meth, $copy ) unless $session;
1075 $logger->debug("Update of copy ".$copy->id." returned: $s");
1077 return $self->DB_UPDATE_FAILED($copy) unless $s;
1081 sub update_reservation {
1082 my( $self, %params ) = @_;
1084 my $reservation = $params{reservation} || die "update_reservation(): reservation required";
1085 my $editor = $params{editor} || die "update_reservation(): copy editor required";
1086 my $session = $params{session};
1088 $logger->debug("Updating copy in the database: " . $reservation->id);
1090 $self->unflesh_reservation($reservation);
1093 my $meth = 'open-ils.cstore.direct.booking.reservation.update';
1095 $s = $session->request( $meth, $reservation )->gather(1) if $session;
1096 $s = $self->cstorereq( $meth, $reservation ) unless $session;
1098 $logger->debug("Update of copy ".$reservation->id." returned: $s");
1100 return $self->DB_UPDATE_FAILED($reservation) unless $s;
1104 sub fetch_billable_xact {
1105 my( $self, $id ) = @_;
1107 $logger->debug("Fetching billable transaction %id");
1108 $xact = $self->cstorereq(
1109 'open-ils.cstore.direct.money.billable_transaction.retrieve', $id );
1110 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
1111 return ($xact, $evt);
1114 sub fetch_billable_xact_summary {
1115 my( $self, $id ) = @_;
1117 $logger->debug("Fetching billable transaction summary %id");
1118 $xact = $self->cstorereq(
1119 'open-ils.cstore.direct.money.billable_transaction_summary.retrieve', $id );
1120 $evt = OpenILS::Event->new('MONEY_BILLABLE_TRANSACTION_NOT_FOUND') unless $xact;
1121 return ($xact, $evt);
1124 sub fetch_fleshed_copy {
1125 my( $self, $id ) = @_;
1127 $logger->info("Fetching fleshed copy $id");
1128 $copy = $self->cstorereq(
1129 "open-ils.cstore.direct.asset.copy.retrieve", $id,
1131 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
1134 $evt = OpenILS::Event->new('ASSET_COPY_NOT_FOUND', id => $id) unless $copy;
1135 return ($copy, $evt);
1139 # returns the org that owns the callnumber that the copy
1141 sub fetch_copy_owner {
1142 my( $self, $copyid ) = @_;
1143 my( $copy, $cn, $evt );
1144 $logger->debug("Fetching copy owner $copyid");
1145 ($copy, $evt) = $self->fetch_copy($copyid);
1146 return (undef,$evt) if $evt;
1147 ($cn, $evt) = $self->fetch_callnumber($copy->call_number);
1148 return (undef,$evt) if $evt;
1149 return ($cn->owning_lib);
1152 sub fetch_copy_note {
1153 my( $self, $id ) = @_;
1155 $logger->debug("Fetching copy note $id");
1156 $note = $self->cstorereq(
1157 'open-ils.cstore.direct.asset.copy_note.retrieve', $id );
1158 $evt = OpenILS::Event->new('ASSET_COPY_NOTE_NOT_FOUND', id => $id ) unless $note;
1159 return ($note, $evt);
1162 sub fetch_call_numbers_by_title {
1163 my( $self, $titleid ) = @_;
1164 $logger->info("Fetching call numbers by title $titleid");
1165 return $self->cstorereq(
1166 'open-ils.cstore.direct.asset.call_number.search.atomic',
1167 { record => $titleid, deleted => 'f' });
1168 #'open-ils.storage.direct.asset.call_number.search.record.atomic', $titleid);
1171 sub fetch_copies_by_call_number {
1172 my( $self, $cnid ) = @_;
1173 $logger->info("Fetching copies by call number $cnid");
1174 return $self->cstorereq(
1175 'open-ils.cstore.direct.asset.copy.search.atomic', { call_number => $cnid, deleted => 'f' } );
1176 #'open-ils.storage.direct.asset.copy.search.call_number.atomic', $cnid );
1179 sub fetch_user_by_barcode {
1180 my( $self, $bc ) = @_;
1181 my $cardid = $self->cstorereq(
1182 'open-ils.cstore.direct.actor.card.id_list', { barcode => $bc } );
1183 return (undef, OpenILS::Event->new('ACTOR_CARD_NOT_FOUND', barcode => $bc)) unless $cardid;
1184 my $user = $self->cstorereq(
1185 'open-ils.cstore.direct.actor.user.search', { card => $cardid } );
1186 return (undef, OpenILS::Event->new('ACTOR_USER_NOT_FOUND', card => $cardid)) unless $user;
1192 my( $self, $billid ) = @_;
1193 $logger->debug("Fetching billing $billid");
1194 my $bill = $self->cstorereq(
1195 'open-ils.cstore.direct.money.billing.retrieve', $billid );
1196 my $evt = OpenILS::Event->new('MONEY_BILLING_NOT_FOUND') unless $bill;
1197 return($bill, $evt);
1201 sub fetch_org_tree {
1203 return $ORG_TREE if $ORG_TREE;
1204 return $ORG_TREE = OpenILS::Utils::CStoreEditor->new->search_actor_org_unit(
1206 {"parent_ou" => undef },
1209 flesh_fields => { aou => ['children'] },
1210 order_by => { aou => 'name'}
1217 my( $self, $node, $callback ) = @_;
1218 return unless $node;
1220 if( $node->children ) {
1221 $self->walk_org_tree($_, $callback) for @{$node->children};
1226 my( $self, $item ) = @_;
1227 return 1 if $item and $item !~ /^f$/i;
1233 my ($self, $client, $service, $method, @params) = @_;
1234 my ($response, $err);
1236 my $session = create OpenSRF::AppSession($service);
1237 my $request = $session->request($method, @params);
1240 my $give_up = time + 1000;
1243 while (time < $give_up) {
1244 $response = $request->recv("timeout" => $spurt);
1245 last if $request->complete;
1247 $client->status(new OpenSRF::DomainObject::oilsContinueStatus);
1249 } catch Error with {
1254 warn "received error : service=$service : method=$method : params=".Dumper(\@params) . "\n $err";
1255 throw $err ("Call to $service for method $method \n failed with exception: $err : " );
1258 return $response->content;
1261 # This logic now lives in storage
1262 sub __patron_money_owed {
1263 my( $self, $patronid ) = @_;
1264 my $ses = OpenSRF::AppSession->create('open-ils.storage');
1265 my $req = $ses->request(
1266 'open-ils.storage.money.billable_transaction.summary.search',
1267 { usr => $patronid, xact_finish => undef } );
1271 while( $data = $req->recv ) {
1272 $data = $data->content;
1273 $total += $data->balance_owed;
1278 sub patron_money_owed {
1279 my( $self, $userid ) = @_;
1280 my $ses = $self->start_db_session();
1281 my $val = $ses->request(
1282 'open-ils.storage.actor.user.total_owed', $userid)->gather(1);
1283 $self->rollback_db_session($ses);
1287 sub patron_total_items_out {
1288 my( $self, $userid ) = @_;
1289 my $ses = $self->start_db_session();
1290 my $val = $ses->request(
1291 'open-ils.storage.actor.user.total_out', $userid)->gather(1);
1292 $self->rollback_db_session($ses);
1299 #---------------------------------------------------------------------
1300 # Returns ($summary, $event)
1301 #---------------------------------------------------------------------
1305 my $e = shift || OpenILS::Utils::CStoreEditor->new;
1306 $id = $id->id if ref($id);
1308 my $xact = $e->retrieve_money_billable_transaction_summary($id)
1309 or return (undef, $e->event);
1315 #---------------------------------------------------------------------
1316 # Given a list of money.billable_transaction objects, this creates
1317 # transaction summary objects for each
1318 #--------------------------------------------------------------------
1323 return () if (!@xacts);
1324 return @{$e->search_money_billable_transaction_summary({id => [ map { $_->id } @xacts ]})};
1328 sub ou_ancestor_setting_value {
1329 my($self, $org_id, $name, $e) = @_;
1330 $e = $e || OpenILS::Utils::CStoreEditor->new;
1331 my $set = $self->ou_ancestor_setting($org_id, $name, $e);
1332 return $set->{value} if $set;
1337 # If an authentication token is provided AND this org unit setting has a
1338 # view_perm, then make sure the user referenced by the auth token has
1339 # that permission. This means that if you call this method without an
1340 # authtoken param, you can get whatever org unit setting values you want.
1343 # NOTE: If you supply an editor ($e) arg AND an auth token arg, the editor's
1344 # authtoken is checked, but the $auth arg is NOT checked. To say that another
1345 # way, be sure NOT to pass an editor argument if you want your token checked.
1346 # Otherwise the auth arg is just a flag saying "check the editor".
1348 sub ou_ancestor_setting {
1349 my( $self, $orgid, $name, $e, $auth ) = @_;
1350 $e = $e || OpenILS::Utils::CStoreEditor->new(
1351 (defined $auth) ? (authtoken => $auth) : ()
1353 my $coust = $e->retrieve_config_org_unit_setting_type([
1354 $name, {flesh => 1, flesh_fields => {coust => ['view_perm']}}
1357 if ($auth && $coust && $coust->view_perm) {
1358 # And you can't have permission if you don't have a valid session.
1359 return undef if not $e->checkauth;
1360 # And now that we know you MIGHT have permission, we check it.
1361 return undef if not $e->allowed($coust->view_perm->code, $orgid);
1364 my $query = {from => ['actor.org_unit_ancestor_setting', $name, $orgid]};
1365 my $setting = $e->json_query($query)->[0];
1366 return undef unless $setting;
1367 return {org => $setting->{org_unit}, value => OpenSRF::Utils::JSON->JSON2perl($setting->{value})};
1371 # returns the ISO8601 string representation of the requested epoch in GMT
1373 my( $self, $epoch ) = @_;
1374 my ($sec,$min,$hour,$mday,$mon,$year) = gmtime($epoch);
1375 $year += 1900; $mon += 1;
1377 '%s-%0.2d-%0.2dT%0.2d:%0.2d:%0.2d-00',
1378 $year, $mon, $mday, $hour, $min, $sec);
1382 sub find_highest_perm_org {
1383 my ( $self, $perm, $userid, $start_org, $org_tree ) = @_;
1384 my $org = $self->find_org($org_tree, $start_org );
1388 last if ($self->check_perms( $userid, $org->id, $perm )); # perm failed
1390 $org = $self->find_org( $org_tree, $org->parent_ou() );
1397 # returns the org_unit ID's
1398 sub user_has_work_perm_at {
1399 my($self, $e, $perm, $options, $user_id) = @_;
1401 $user_id = (defined $user_id) ? $user_id : $e->requestor->id;
1403 my $func = 'permission.usr_has_perm_at';
1404 $func = $func.'_all' if $$options{descendants};
1406 my $orgs = $e->json_query({from => [$func, $user_id, $perm]});
1407 $orgs = [map { $_->{ (keys %$_)[0] } } @$orgs];
1409 return $orgs unless $$options{objects};
1411 return $e->search_actor_org_unit({id => $orgs});
1414 sub get_user_work_ou_ids {
1415 my($self, $e, $userid) = @_;
1416 my $work_orgs = $e->json_query({
1417 select => {puwoum => ['work_ou']},
1419 where => {usr => $e->requestor->id}});
1421 return [] unless @$work_orgs;
1423 push(@work_orgs, $_->{work_ou}) for @$work_orgs;
1431 my($self, $client) = @_;
1432 return $org_types if $org_types;
1433 return $org_types = OpenILS::Utils::CStoreEditor->new->retrieve_all_actor_org_unit_type();
1438 my $locale = shift || '';
1439 my $cache = OpenSRF::Utils::Cache->new("global", 0);
1440 my $tree = $cache->get_cache("orgtree.$locale");
1441 return $tree if $tree;
1443 my $ses = OpenILS::Utils::CStoreEditor->new;
1444 $ses->session->session_locale($locale);
1445 $tree = $ses->search_actor_org_unit(
1447 {"parent_ou" => undef },
1450 flesh_fields => { aou => ['children'] },
1451 order_by => { aou => 'name'}
1456 $cache->put_cache("orgtree.$locale", $tree);
1460 sub get_org_descendants {
1461 my($self, $org_id, $depth) = @_;
1464 transform => 'actor.org_unit_descendants',
1466 result_field => 'id',
1468 $select->{params} = [$depth] if defined $depth;
1470 my $org_list = OpenILS::Utils::CStoreEditor->new->json_query({
1471 select => {aou => [$select]},
1473 where => {id => $org_id}
1476 push(@orgs, $_->{id}) for @$org_list;
1480 sub get_org_ancestors {
1481 my($self, $org_id) = @_;
1483 my $org_list = OpenILS::Utils::CStoreEditor->new->json_query({
1486 transform => 'actor.org_unit_ancestors',
1488 result_field => 'id',
1493 where => {id => $org_id}
1497 push(@orgs, $_->{id}) for @$org_list;
1501 sub get_org_full_path {
1502 my($self, $org_id, $depth) = @_;
1507 transform => 'actor.org_unit_full_path',
1509 result_field => 'id',
1513 where => {id => $org_id}
1516 $query->{select}->{aou}->[0]->{params} = [$depth] if defined $depth;
1517 my $org_list = OpenILS::Utils::CStoreEditor->new->json_query($query);
1518 return [ map {$_->{id}} @$org_list ];
1521 # returns the ID of the org unit ancestor at the specified depth
1522 sub org_unit_ancestor_at_depth {
1523 my($class, $org_id, $depth) = @_;
1524 my $resp = OpenILS::Utils::CStoreEditor->new->json_query(
1525 {from => ['actor.org_unit_ancestor_at_depth', $org_id, $depth]})->[0];
1526 return ($resp) ? $resp->{id} : undef;
1529 # returns the user's configured locale as a string. Defaults to en-US if none is configured.
1530 sub get_user_locale {
1531 my($self, $user_id, $e) = @_;
1532 $e ||= OpenILS::Utils::CStoreEditor->new;
1534 # first, see if the user has an explicit locale set
1535 my $setting = $e->search_actor_user_setting(
1536 {usr => $user_id, name => 'global.locale'})->[0];
1537 return OpenSRF::Utils::JSON->JSON2perl($setting->value) if $setting;
1539 my $user = $e->retrieve_actor_user($user_id) or return $e->event;
1540 return $self->get_org_locale($user->home_ou, $e);
1543 # returns org locale setting
1544 sub get_org_locale {
1545 my($self, $org_id, $e) = @_;
1546 $e ||= OpenILS::Utils::CStoreEditor->new;
1549 if(defined $org_id) {
1550 $locale = $self->ou_ancestor_setting_value($org_id, 'global.default_locale', $e);
1551 return $locale if $locale;
1554 # system-wide default
1555 my $sclient = OpenSRF::Utils::SettingsClient->new;
1556 $locale = $sclient->config_value('default_locale');
1557 return $locale if $locale;
1559 # if nothing else, fallback to locale=cowboy
1564 # xml-escape non-ascii characters
1566 my($self, $string, $form) = @_;
1569 # If we're going to convert non-ASCII characters to XML entities,
1570 # we had better be dealing with a UTF8 string to begin with
1571 $string = decode_utf8($string);
1574 $string = NFD($string);
1576 $string = NFC($string);
1579 # Convert raw ampersands to entities
1580 $string =~ s/&(?!\S+;)/&/gso;
1582 # Convert Unicode characters to entities
1583 $string =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
1588 # x0000-x0008 isn't legal in XML documents
1589 # XXX Perhaps this should just go into our standard entityize method
1590 sub strip_ctrl_chars {
1591 my ($self, $string) = @_;
1593 $string =~ s/([\x{0000}-\x{0008}])//sgoe;
1597 sub get_copy_price {
1598 my($self, $e, $copy, $volume) = @_;
1600 $copy->price(0) if $copy->price and $copy->price < 0;
1602 return $copy->price if $copy->price and $copy->price > 0;
1607 if($volume->id == OILS_PRECAT_CALL_NUMBER) {
1608 $owner = $copy->circ_lib;
1610 $owner = $volume->owning_lib;
1613 if($copy->call_number == OILS_PRECAT_CALL_NUMBER) {
1614 $owner = $copy->circ_lib;
1616 $owner = $e->retrieve_asset_call_number($copy->call_number)->owning_lib;
1620 my $default_price = $self->ou_ancestor_setting_value(
1621 $owner, OILS_SETTING_DEF_ITEM_PRICE, $e) || 0;
1623 return $default_price unless defined $copy->price;
1625 # price is 0. Use the default?
1626 my $charge_on_0 = $self->ou_ancestor_setting_value(
1627 $owner, OILS_SETTING_CHARGE_LOST_ON_ZERO, $e) || 0;
1629 return $default_price if $charge_on_0;
1633 # given a transaction ID, this returns the context org_unit for the transaction
1635 my($self, $xact_id, $e) = @_;
1636 $e ||= OpenILS::Utils::CStoreEditor->new;
1638 my $loc = $e->json_query({
1639 "select" => {circ => ["circ_lib"]},
1641 "where" => {id => $xact_id},
1644 return $loc->[0]->{circ_lib} if @$loc;
1646 $loc = $e->json_query({
1647 "select" => {bresv => ["request_lib"]},
1649 "where" => {id => $xact_id},
1652 return $loc->[0]->{request_lib} if @$loc;
1654 $loc = $e->json_query({
1655 "select" => {mg => ["billing_location"]},
1657 "where" => {id => $xact_id},
1660 return $loc->[0]->{billing_location};
1664 sub find_event_def_by_hook {
1665 my($self, $hook, $context_org, $e) = @_;
1667 $e ||= OpenILS::Utils::CStoreEditor->new;
1669 my $orgs = $self->get_org_ancestors($context_org);
1671 # search from the context org up
1672 for my $org_id (reverse @$orgs) {
1674 my $def = $e->search_action_trigger_event_definition(
1675 {hook => $hook, owner => $org_id})->[0];
1677 return $def if $def;
1685 # If an event_def ID is not provided, use the hook and context org to find the
1686 # most appropriate event. create the event, fire it, then return the resulting
1687 # event with fleshed template_output and error_output
1688 sub fire_object_event {
1689 my($self, $event_def, $hook, $object, $context_org, $granularity, $user_data, $client) = @_;
1691 my $e = OpenILS::Utils::CStoreEditor->new;
1694 my $auto_method = "open-ils.trigger.event.autocreate.by_definition";
1697 $def = $e->retrieve_action_trigger_event_definition($event_def)
1698 or return $e->event;
1700 $auto_method .= '.include_inactive';
1704 # find the most appropriate event def depending on context org
1705 $def = $self->find_event_def_by_hook($hook, $context_org, $e)
1706 or return $e->event;
1711 if($def->group_field) {
1712 # we have a list of objects
1713 $object = [$object] unless ref $object eq 'ARRAY';
1717 for my $i (0..$#$object) {
1718 my $obj = $$object[$i];
1719 my $udata = $$user_data[$i];
1720 my $event_id = $self->simplereq(
1721 'open-ils.trigger', $auto_method, $def->id, $obj, $context_org, $udata);
1722 push(@event_ids, $event_id);
1725 $logger->info("EVENTS = " . OpenSRF::Utils::JSON->perl2JSON(\@event_ids));
1728 if (not defined $client) {
1729 $resp = $self->simplereq(
1731 'open-ils.trigger.event_group.fire',
1734 $resp = $self->patientreq(
1736 "open-ils.trigger", "open-ils.trigger.event_group.fire",
1741 if($resp and $resp->{events} and @{$resp->{events}}) {
1744 $final_resp = $e->retrieve_action_trigger_event([
1745 $resp->{events}->[0]->id,
1746 {flesh => 1, flesh_fields => {atev => ['template_output', 'error_output']}}
1753 $object = $$object[0] if ref $object eq 'ARRAY';
1758 if (not defined $client) {
1759 $event_id = $self->simplereq(
1761 $auto_method, $def->id, $object, $context_org, $user_data
1764 $resp = $self->simplereq(
1766 'open-ils.trigger.event.fire',
1770 $event_id = $self->patientreq(
1773 $auto_method, $def->id, $object, $context_org, $user_data
1776 $resp = $self->patientreq(
1779 'open-ils.trigger.event.fire',
1784 if($resp and $resp->{event}) {
1786 $final_resp = $e->retrieve_action_trigger_event([
1788 {flesh => 1, flesh_fields => {atev => ['template_output', 'error_output']}}
1798 sub create_events_for_hook {
1799 my($self, $hook, $obj, $org_id, $granularity, $user_data, $wait) = @_;
1800 my $ses = OpenSRF::AppSession->create('open-ils.trigger');
1801 my $req = $ses->request('open-ils.trigger.event.autocreate',
1802 $hook, $obj, $org_id, $granularity, $user_data);
1803 return undef unless $wait;
1804 my $resp = $req->recv;
1805 return $resp->content if $resp;
1808 sub create_uuid_string {
1809 return create_UUID_as_string();
1812 sub create_circ_chain_summary {
1813 my($class, $e, $circ_id) = @_;
1814 my $sum = $e->json_query({from => ['action.summarize_circ_chain', $circ_id]})->[0];
1815 return undef unless $sum;
1816 my $obj = Fieldmapper::action::circ_chain_summary->new;
1817 $obj->$_($sum->{$_}) for keys %$sum;
1822 # Returns "mra" attribute key/value pairs for a set of bre's
1823 # Takes a list of bre IDs, returns a hash of hashes,
1824 # {bre_id1 => {key1 => {code => value1, label => label1}, ...}...}
1827 my ($class, $bre_ids, $e) = @_;
1828 $e = $e || OpenILS::Utils::CStoreEditor->new;
1831 return $attrs unless defined $bre_ids;
1832 $bre_ids = [$bre_ids] unless ref $bre_ids;
1834 my $mra = $e->json_query({
1842 transform => 'each',
1843 result_field => 'key',
1847 transform => 'each',
1848 result_field => 'value',
1854 where => {id => $bre_ids}
1857 return $attrs unless $mra;
1859 $ccvm_cache = $ccvm_cache || $e->search_config_coded_value_map({id => {'!=' => undef}});
1861 for my $id (@$bre_ids) {
1863 for my $mra (grep { $_->{bre} eq $id } @$mra) {
1864 my $ctype = $mra->{key};
1865 my $code = $mra->{value};
1866 $attrs->{$id}->{$ctype} = {code => $code};
1868 my ($ccvm) = grep { $_->ctype eq $ctype and $_->code eq $code } @$ccvm_cache;
1869 $attrs->{$id}->{$ctype}->{label} = $ccvm->value if $ccvm;