]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
added some NOT_FOUND events for record, copy, and circ (more to come)
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / AppUtils.pm
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);
6 use OpenILS::Perm;
7 use OpenSRF::Utils::Logger;
8 use OpenILS::Utils::ModsParser;
9 my $logger = "OpenSRF::Utils::Logger";
10
11
12 my $cache_client = "OpenSRF::Utils::Cache";
13
14 # ---------------------------------------------------------------------------
15 # Pile of utilty methods used accross applications.
16 # ---------------------------------------------------------------------------
17
18
19 # ---------------------------------------------------------------------------
20 # on sucess, returns the created session, on failure throws ERROR exception
21 # ---------------------------------------------------------------------------
22 sub start_db_session {
23
24         my $self = shift;
25         my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
26         my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
27
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() ) {
31                 throw OpenSRF::ERROR 
32                         ("Unable to Begin Transaction with database" );
33         }
34         $trans_req->finish();
35         return $session;
36 }
37
38
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 ) = @_;
43
44         $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
45
46         throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
47                 unless( defined($user_id) and defined($org_id) and @perm_types); 
48
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);
55                 if(!$resp) { 
56                         $session->disconnect();
57                         return $type; 
58                 }
59         }
60
61         $session->disconnect();
62         return undef;
63 }
64
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
67 sub check_perms {
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;
71         return undef;
72 }
73
74
75
76 # ---------------------------------------------------------------------------
77 # commits and destroys the session
78 # ---------------------------------------------------------------------------
79 sub commit_db_session {
80         my( $self, $session ) = @_;
81
82         my $req = $session->request( "open-ils.storage.transaction.commit" );
83         my $resp = $req->recv();
84
85         if(!$resp) {
86                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
87         }
88
89         if(UNIVERSAL::isa($resp,"Error")) { 
90                 throw $resp ($resp->stringify); 
91         }
92
93         if(!$resp->content) {
94                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
95         }
96
97         $session->finish();
98         $session->disconnect();
99         $session->kill_me();
100 }
101
102 sub rollback_db_session {
103         my( $self, $session ) = @_;
104
105         my $req = $session->request("open-ils.storage.transaction.rollback");
106         my $resp = $req->recv();
107         if(UNIVERSAL::isa($resp,"Error")) { throw $resp;  }
108
109         $session->finish();
110         $session->disconnect();
111         $session->kill_me();
112 }
113
114 # ---------------------------------------------------------------------------
115 # Checks to see if a user is logged in.  Returns the user record on success,
116 # throws an exception on error.
117 # ---------------------------------------------------------------------------
118
119
120 sub check_user_session {
121
122         my( $self, $user_session ) = @_;
123
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();
127
128         if(!$response) {
129                 throw OpenSRF::EX::User 
130                         ("Error communication with storage server");
131         }
132
133         if(ref($response) and $response->isa("OpenSRF::EX")) {
134                 throw $response ($response->stringify);
135         }
136
137
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" );
143                 }
144         }
145
146         my $user = $content;
147         if(!$user) {
148                 throw OpenSRF::EX::ERROR 
149                         ("Session [$user_session] cannot be authenticated" );
150         }
151
152         $session->disconnect();
153         $session->kill_me();
154
155         return $user;
156 }
157
158 # generic simple request returning a scalar value
159 sub simplereq {
160         my($self, $service, $method, @params) = @_;
161         return $self->simple_scalar_request($service, $method, @params);
162 }
163
164
165 sub simple_scalar_request {
166         my($self, $service, $method, @params) = @_;
167
168         my $session = OpenSRF::AppSession->create( $service );
169         my $request = $session->request( $method, @params );
170         my $response = $request->recv(30);
171
172         $request->wait_complete;
173
174         if(!$request->complete) {
175                 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" . 
176                                 "\n did not complete successfully");
177         }
178
179         if(!$response) {
180                 warn "No response from $service for method $method with params @params";
181         }
182
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 );
186         }
187
188
189         $request->finish();
190         $session->finish();
191         $session->disconnect();
192
193         my $value;
194
195         if($response) { $value = $response->content; }
196         else { $value = undef; }
197
198         return $value;
199 }
200
201
202
203
204
205 my $tree                                                = undef;
206 my $orglist                                     = undef;
207 my $org_typelist                        = undef;
208 my $org_typelist_hash   = {};
209
210 sub get_org_tree {
211
212         my $self = shift;
213         if($tree) { return $tree; }
214
215         # see if it's in the cache
216         $tree = $cache_client->new()->get_cache('_orgtree');
217         if($tree) { return $tree; }
218
219         if(!$orglist) {
220                 warn "Retrieving Org Tree\n";
221                 $orglist = $self->simple_scalar_request( 
222                         "open-ils.storage", 
223                         "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
224         }
225
226         if( ! $org_typelist ) {
227                 warn "Retrieving org types\n";
228                 $org_typelist = $self->simple_scalar_request( 
229                         "open-ils.storage", 
230                         "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
231                 $self->build_org_type($org_typelist);
232         }
233
234         $tree = $self->build_org_tree($orglist,1);
235         $cache_client->new()->put_cache('_orgtree', $tree);
236         return $tree;
237
238 }
239
240 my $slimtree = undef;
241 sub get_slim_org_tree {
242
243         my $self = shift;
244         if($slimtree) { return $slimtree; }
245
246         # see if it's in the cache
247         $slimtree = $cache_client->new()->get_cache('slimorgtree');
248         if($slimtree) { return $slimtree; }
249
250         if(!$orglist) {
251                 warn "Retrieving Org Tree\n";
252                 $orglist = $self->simple_scalar_request( 
253                         "open-ils.storage", 
254                         "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
255         }
256
257         $slimtree = $self->build_org_tree($orglist);
258         $cache_client->new->put_cache('slimorgtree', $slimtree);
259         return $slimtree;
260
261 }
262
263
264 sub build_org_type { 
265         my($self, $org_typelist)  = @_;
266         for my $type (@$org_typelist) {
267                 $org_typelist_hash->{$type->id()} = $type;
268         }
269 }
270
271
272
273 sub build_org_tree {
274
275         my( $self, $orglist, $add_types ) = @_;
276
277         return $orglist unless ( 
278                         ref($orglist) and @$orglist > 1 );
279
280         my @list = sort { 
281                 $a->ou_type <=> $b->ou_type ||
282                 $a->name cmp $b->name } @$orglist;
283
284         for my $org (@list) {
285
286                 next unless ($org);
287
288                 if(!ref($org->ou_type()) and $add_types) {
289                         $org->ou_type( $org_typelist_hash->{$org->ou_type()});
290                 }
291
292                 next unless (defined($org->parent_ou));
293
294                 my ($parent) = grep { $_->id == $org->parent_ou } @list;
295                 next unless $parent;
296                 $parent->children([]) unless defined($parent->children); 
297                 push( @{$parent->children}, $org );
298         }
299
300         return $list[0];
301
302 }
303
304 sub fetch_user {
305         my $self = shift;
306         my $id = shift;
307         return $self->simple_scalar_request(
308                 'open-ils.storage',
309                 'open-ils.storage.direct.actor.user.retrieve', $id );
310 }
311
312 sub checkses {
313         my( $self, $session ) = @_;
314         my $user; my $evt; my $e; 
315
316         $logger->debug("Checking user session $session");
317
318         try {
319                 $user = $self->check_user_session($session);
320         } catch Error with { $e = 1; };
321
322         if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
323         return ( $user, $evt );
324 }
325
326
327 sub checkrequestor {
328         my( $self, $staffobj, $userid, @perms ) = @_;
329         my $user; my $evt;
330         $userid = $staffobj->id unless defined $userid;
331
332         $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
333
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);
338                 }
339                 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
340
341         } else {
342                 $user = $staffobj;
343         }
344
345         return ($user, $evt);
346 }
347
348 sub checkses_requestor {
349         my( $self, $authtoken, $targetid, @perms ) = @_;
350         my( $requestor, $target, $evt );
351
352         ($requestor, $evt) = $self->checkses($authtoken);
353         return (undef, undef, $evt) if $evt;
354
355         ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
356         return( $requestor, $target, $evt);
357 }
358
359 sub fetch_copy {
360         my( $self, $copyid ) = @_;
361         my( $copy, $evt );
362
363         $copy = $self->simplereq(
364                 'open-ils.storage',
365                 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
366
367         if(!$copy) { $evt = OpenILS::Event->new('COPY_NOT_FOUND'); }
368
369         return( $copy, $evt );
370 }
371
372
373 # retrieves a circ object by id
374 sub fetch_circulation {
375         my( $self, $circid ) = @_;
376         my $circ; my $evt;
377         
378         $circ = $self->simplereq(
379                 'open-ils.storage',
380                 "open-ils.storage.direct.action.circulation.retrieve", $circid );
381
382         if(!$circ) {
383                 $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND', circid => $circid );
384         }
385
386         return ( $circ, $evt );
387 }
388
389 sub fetch_record_by_copy {
390         my( $self, $copyid ) = @_;
391         my( $record, $evt );
392
393         $record = $self->simplereq(
394                 'open-ils.storage',
395                 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy', $copyid );
396
397         if(!$record) {
398                 $evt = OpenILS::Event->new('BIBLIO_RECORD_NOT_FOUND');
399         }
400
401         return ($record, $evt);
402 }
403
404 # turns a record object into an mvr (mods) object
405 sub record_to_mvr {
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);
411         return $mods;
412 }
413
414
415
416 1;