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;
9 my $logger = "OpenSRF::Utils::Logger";
12 my $cache_client = "OpenSRF::Utils::Cache";
14 # ---------------------------------------------------------------------------
15 # Pile of utilty methods used accross applications.
16 # ---------------------------------------------------------------------------
19 # ---------------------------------------------------------------------------
20 # on sucess, returns the created session, on failure throws ERROR exception
21 # ---------------------------------------------------------------------------
22 sub start_db_session {
25 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
26 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
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() ) {
32 ("Unable to Begin Transaction with database" );
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 ) = @_;
44 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
46 throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
47 unless( defined($user_id) and defined($org_id) and @perm_types);
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);
56 $session->disconnect();
61 $session->disconnect();
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
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;
76 # ---------------------------------------------------------------------------
77 # commits and destroys the session
78 # ---------------------------------------------------------------------------
79 sub commit_db_session {
80 my( $self, $session ) = @_;
82 my $req = $session->request( "open-ils.storage.transaction.commit" );
83 my $resp = $req->recv();
86 throw OpenSRF::EX::ERROR ("Unable to commit db session");
89 if(UNIVERSAL::isa($resp,"Error")) {
90 throw $resp ($resp->stringify);
94 throw OpenSRF::EX::ERROR ("Unable to commit db session");
98 $session->disconnect();
102 sub rollback_db_session {
103 my( $self, $session ) = @_;
105 my $req = $session->request("open-ils.storage.transaction.rollback");
106 my $resp = $req->recv();
107 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
110 $session->disconnect();
114 # ---------------------------------------------------------------------------
115 # Checks to see if a user is logged in. Returns the user record on success,
116 # throws an exception on error.
117 # ---------------------------------------------------------------------------
120 sub check_user_session {
122 my( $self, $user_session ) = @_;
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();
129 throw OpenSRF::EX::User
130 ("Error communication with storage server");
133 if(ref($response) and $response->isa("OpenSRF::EX")) {
134 throw $response ($response->stringify);
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" );
148 throw OpenSRF::EX::ERROR
149 ("Session [$user_session] cannot be authenticated" );
152 $session->disconnect();
158 # generic simple request returning a scalar value
160 my($self, $service, $method, @params) = @_;
161 return $self->simple_scalar_request($service, $method, @params);
165 sub simple_scalar_request {
166 my($self, $service, $method, @params) = @_;
168 my $session = OpenSRF::AppSession->create( $service );
169 my $request = $session->request( $method, @params );
170 my $response = $request->recv(30);
172 $request->wait_complete;
174 if(!$request->complete) {
175 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" .
176 "\n did not complete successfully");
180 warn "No response from $service for method $method with params @params";
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 );
191 $session->disconnect();
195 if($response) { $value = $response->content; }
196 else { $value = undef; }
207 my $org_typelist = undef;
208 my $org_typelist_hash = {};
213 if($tree) { return $tree; }
215 # see if it's in the cache
216 $tree = $cache_client->new()->get_cache('_orgtree');
217 if($tree) { return $tree; }
220 warn "Retrieving Org Tree\n";
221 $orglist = $self->simple_scalar_request(
223 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
226 if( ! $org_typelist ) {
227 warn "Retrieving org types\n";
228 $org_typelist = $self->simple_scalar_request(
230 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
231 $self->build_org_type($org_typelist);
234 $tree = $self->build_org_tree($orglist,1);
235 $cache_client->new()->put_cache('_orgtree', $tree);
240 my $slimtree = undef;
241 sub get_slim_org_tree {
244 if($slimtree) { return $slimtree; }
246 # see if it's in the cache
247 $slimtree = $cache_client->new()->get_cache('slimorgtree');
248 if($slimtree) { return $slimtree; }
251 warn "Retrieving Org Tree\n";
252 $orglist = $self->simple_scalar_request(
254 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
257 $slimtree = $self->build_org_tree($orglist);
258 $cache_client->new->put_cache('slimorgtree', $slimtree);
265 my($self, $org_typelist) = @_;
266 for my $type (@$org_typelist) {
267 $org_typelist_hash->{$type->id()} = $type;
275 my( $self, $orglist, $add_types ) = @_;
277 return $orglist unless (
278 ref($orglist) and @$orglist > 1 );
281 $a->ou_type <=> $b->ou_type ||
282 $a->name cmp $b->name } @$orglist;
284 for my $org (@list) {
288 if(!ref($org->ou_type()) and $add_types) {
289 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
292 next unless (defined($org->parent_ou));
294 my ($parent) = grep { $_->id == $org->parent_ou } @list;
296 $parent->children([]) unless defined($parent->children);
297 push( @{$parent->children}, $org );
305 my( $self, $userid ) = @_;
308 $logger->debug("Fetching user $userid from storage");
310 $user = $self->simplereq(
312 'open-ils.storage.direct.actor.user.retrieve', $userid );
315 $logger->info("User $userid not found in the db");
316 $evt = OpenILS::Event->new('USER_NOT_FOUND');
319 return ($user, $evt);
323 my( $self, $session ) = @_;
324 my $user; my $evt; my $e;
326 $logger->debug("Checking user session $session");
329 $user = $self->check_user_session($session);
330 } catch Error with { $e = 1; };
332 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
333 return ( $user, $evt );
337 # verifiese the session and checks the permissions agains the
338 # session user and the user's home_ou as the org id
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;
351 my( $self, $staffobj, $userid, @perms ) = @_;
353 $userid = $staffobj->id unless defined $userid;
355 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
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 );
366 return ($user, $evt);
369 sub checkses_requestor {
370 my( $self, $authtoken, $targetid, @perms ) = @_;
371 my( $requestor, $target, $evt );
373 ($requestor, $evt) = $self->checkses($authtoken);
374 return (undef, undef, $evt) if $evt;
376 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
377 return( $requestor, $target, $evt);
381 my( $self, $copyid ) = @_;
384 $logger->debug("Fetching copy $copyid from storage");
386 $copy = $self->simplereq(
388 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
390 if(!$copy) { $evt = OpenILS::Event->new('COPY_NOT_FOUND'); }
392 return( $copy, $evt );
396 # retrieves a circ object by id
397 sub fetch_circulation {
398 my( $self, $circid ) = @_;
401 $logger->debug("Fetching circ $circid from storage");
403 $circ = $self->simplereq(
405 "open-ils.storage.direct.action.circulation.retrieve", $circid );
408 $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND', circid => $circid );
411 return ( $circ, $evt );
414 sub fetch_record_by_copy {
415 my( $self, $copyid ) = @_;
418 $logger->debug("Fetching record by copy $copyid from storage");
420 $record = $self->simplereq(
422 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy', $copyid );
425 $evt = OpenILS::Event->new('BIBLIO_RECORD_NOT_FOUND');
428 return ($record, $evt);
431 # turns a record object into an mvr (mods) object
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);
442 my( $self, $holdid ) = @_;
445 $logger->debug("Fetching hold $holdid from storage");
447 $hold = $self->simplereq(
449 'open-ils.storage.direct.action.hold_request.retrieve', $holdid);
451 $evt = OpenILS::Event->new('HOLD_NOT_FOUND', holdid => $holdid) unless $hold;
453 return ($hold, $evt);
457 sub fetch_hold_transit_by_hold {
458 my( $self, $holdid ) = @_;
459 my( $transit, $evt );
461 $logger->debug("Fetching transit by hold $holdid from storage");
463 $transit = $self->simplereq(
465 'open-ils.storage.direct.action.hold_transit_copy.search.hold', $holdid );
467 $evt = OpenILS::Event->new('TRANSIT_NOT_FOUND', holdid => $holdid) unless $transit;
469 return ($transit, $evt );
473 sub fetch_copy_by_barcode {
474 my( $self, $barcode ) = @_;
477 $logger->debug("Fetching copy by barcode $barcode from storage");
479 $copy = $self->simplereq( 'open-ils.storage',
480 'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
482 $evt = OpenILS::Event->new('COPY_NOT_FOUND', barcode => $barcode) unless $copy;
484 return ($copy, $evt);
487 sub fetch_open_billable_transaction {
488 my( $self, $transid ) = @_;
489 my( $transaction, $evt );
491 $logger->debug("Fetching open billable transaction $transid from storage");
493 $transaction = $self->simplereq(
495 'open-ils.storage.direct.money.open_billable_transaction_summary.retrieve', $transid);
497 $evt = OpenILS::Event->new(
498 'TRANSACTION_NOT_FOUND', transid => $transid ) unless $transaction;
500 return ($transaction, $evt);
506 $buckets{'biblio'} = 'biblio_record_entry_bucket';
507 $buckets{'callnumber'} = 'call_number_bucket';
508 $buckets{'copy'} = 'copy_bucket';
509 $buckets{'user'} = 'user_bucket';
511 sub fetch_container {
512 my( $self, $id, $type ) = @_;
515 $logger->debug("Fetching container $id with type $type");
517 my $meth = $buckets{$type};
518 $bucket = $self->simplereq(
520 "open-ils.storage.direct.container.$meth.retrieve", $id );
522 $evt = OpenILS::Event->new(
523 'CONTAINER_NOT_FOUND', container => $id,
524 container_type => $type ) unless $bucket;
526 return ($bucket, $evt);
530 sub fetch_container_item {
531 my( $self, $id, $type ) = @_;
534 $logger->debug("Fetching container item $id with type $type");
536 my $meth = $buckets{$type} . "_item";
538 $bucket = $self->simplereq(
540 "open-ils.storage.direct.container.$meth.retrieve", $id );
542 $evt = OpenILS::Event->new(
543 'CONTAINER_ITEM_NOT_FOUND', itemid => $id,
544 container_type => $type ) unless $bucket;
546 return ($bucket, $evt);