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 );
307 return $self->simple_scalar_request(
309 'open-ils.storage.direct.actor.user.retrieve', $id );
313 my( $self, $session ) = @_;
314 my $user; my $evt; my $e;
316 $logger->debug("Checking user session $session");
319 $user = $self->check_user_session($session);
320 } catch Error with { $e = 1; };
322 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
323 return ( $user, $evt );
328 my( $self, $staffobj, $userid, @perms ) = @_;
330 $userid = $staffobj->id unless defined $userid;
332 $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
334 if( $userid ne $staffobj->id ) {
335 if( ! ($user = $self->fetch_user($userid)) ) {
336 $evt = OpenILS::Event->new('USER_NOT_FOUND');
337 return (undef, $evt);
339 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
345 return ($user, $evt);
348 sub checkses_requestor {
349 my( $self, $authtoken, $targetid, @perms ) = @_;
350 my( $requestor, $target, $evt );
352 ($requestor, $evt) = $self->checkses($authtoken);
353 return (undef, undef, $evt) if $evt;
355 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
356 return( $requestor, $target, $evt);
360 my( $self, $copyid ) = @_;
363 $copy = $self->simplereq(
365 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
367 if(!$copy) { $evt = OpenILS::Event->new('COPY_NOT_FOUND'); }
369 return( $copy, $evt );
373 # retrieves a circ object by id
374 sub fetch_circulation {
375 my( $self, $circid ) = @_;
378 $circ = $self->simplereq(
380 "open-ils.storage.direct.action.circulation.retrieve", $circid );
383 $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND', circid => $circid );
386 return ( $circ, $evt );
389 sub fetch_record_by_copy {
390 my( $self, $copyid ) = @_;
393 $record = $self->simplereq(
395 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy', $copyid );
398 $evt = OpenILS::Event->new('BIBLIO_RECORD_NOT_FOUND');
401 return ($record, $evt);
404 # turns a record object into an mvr (mods) object
406 my( $self, $record ) = @_;
407 my $u = OpenILS::Utils::ModsParser->new();
408 $u->start_mods_batch( $record->marc );
409 my $mods = $u->finish_mods_batch();
410 $mods->doc_id($record->id);