]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
basic circulation (checkout, renew, checkin) is now functioning
[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 use OpenILS::Event;
10 my $logger = "OpenSRF::Utils::Logger";
11
12
13 my $cache_client = "OpenSRF::Utils::Cache";
14
15 my $storage_session = undef;
16
17 # ---------------------------------------------------------------------------
18 # Pile of utilty methods used accross applications.
19 # ---------------------------------------------------------------------------
20
21
22 # ---------------------------------------------------------------------------
23 # on sucess, returns the created session, on failure throws ERROR exception
24 # ---------------------------------------------------------------------------
25 sub start_db_session {
26
27         my $self = shift;
28         my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
29         my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
30
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() ) {
34                 throw OpenSRF::ERROR 
35                         ("Unable to Begin Transaction with database" );
36         }
37         $trans_req->finish();
38
39         $logger->debug("Setting global storage session to ".
40                 "session: " . $session->session_id . " : " . $session->app );
41
42         $storage_session = $session;
43         return $session;
44 }
45
46
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 ));
56         }
57         return undef;
58 }
59
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
62 sub check_perms {
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;
66         return undef;
67 }
68
69
70
71 # ---------------------------------------------------------------------------
72 # commits and destroys the session
73 # ---------------------------------------------------------------------------
74 sub commit_db_session {
75         my( $self, $session ) = @_;
76
77         my $req = $session->request( "open-ils.storage.transaction.commit" );
78         my $resp = $req->recv();
79
80         if(!$resp) {
81                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
82         }
83
84         if(UNIVERSAL::isa($resp,"Error")) { 
85                 throw $resp ($resp->stringify); 
86         }
87
88         if(!$resp->content) {
89                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
90         }
91
92         $session->finish();
93         $session->disconnect();
94         $session->kill_me();
95         $storage_session = undef;
96 }
97
98 sub rollback_db_session {
99         my( $self, $session ) = @_;
100
101         my $req = $session->request("open-ils.storage.transaction.rollback");
102         my $resp = $req->recv();
103         if(UNIVERSAL::isa($resp,"Error")) { throw $resp;  }
104
105         $session->finish();
106         $session->disconnect();
107         $session->kill_me();
108         $storage_session = undef;
109 }
110
111
112 # returns undef it the event is not an ILS event
113 # returns the event code otherwise
114 sub event_code {
115         my( $self, $evt ) = @_;
116         return $evt->{ilsevent} if( ref($evt) eq 'HASH' and defined($evt->{ilsevent})) ;
117         return undef;
118 }
119
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 {
125
126         my( $self, $user_session ) = @_;
127
128         my $content = $self->simplereq( 
129                 'open-ils.auth', 
130                 'open-ils.auth.session.retrieve', $user_session );
131
132
133         if(! $content or $self->event_code($content)) {
134                 throw OpenSRF::EX::ERROR 
135                         ("Session [$user_session] cannot be authenticated" );
136         }
137
138         $logger->debug("Fetch user session $user_session found user " . $content->id );
139
140         return $content;
141 }
142
143 # generic simple request returning a scalar value
144 sub simplereq {
145         my($self, $service, $method, @params) = @_;
146         return $self->simple_scalar_request($service, $method, @params);
147 }
148
149 sub get_storage_session {
150         if(     $storage_session and 
151                         $storage_session->connected and
152                         $storage_session->app eq 'open-ils.storage' ) {
153                 $logger->debug("get_storage_session(): returning existing session");
154                 return $storage_session;
155         }
156         $logger->debug("get_storage_session(): returning undef");
157         $storage_session = undef;
158         return undef;
159 }
160
161
162 sub simple_scalar_request {
163         my($self, $service, $method, @params) = @_;
164
165         my $session = undef;
166         if( $service eq 'open-ils.storage' ) {
167                 if( $session = get_storage_session() ) {
168                         $logger->debug("simple request using existing storage session ".$session->session_id);
169                 } else { $session = undef; }
170         }
171
172         if(!$session) {
173                 $session = OpenSRF::AppSession->create( $service );
174         }
175
176         $logger->debug("simple request for service $service using session " .$session->app);
177
178         my $request = $session->request( $method, @params );
179         my $response = $request->recv(30);
180
181         $request->wait_complete;
182
183         if(!$request->complete) {
184                 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" . 
185                                 "\n did not complete successfully");
186         }
187
188         if(!$response) {
189                 warn "No response from $service for method $method with params @params";
190         }
191
192         if(UNIVERSAL::isa($response,"Error")) {
193                 throw $response ("Call to $service for method $method with params @params" . 
194                                 "\n failed with exception: " . $response->stringify );
195         }
196
197
198         $request->finish();
199
200         if($service ne 'open-ils.storage' or !get_storage_session() ) {
201                 $session->finish();
202                 $session->disconnect();
203         }
204
205         my $value;
206
207         if($response) { $value = $response->content; }
208         else { $value = undef; }
209
210         return $value;
211 }
212
213
214
215
216
217 my $tree                                                = undef;
218 my $orglist                                     = undef;
219 my $org_typelist                        = undef;
220 my $org_typelist_hash   = {};
221
222 sub get_org_tree {
223
224         my $self = shift;
225         if($tree) { return $tree; }
226
227         # see if it's in the cache
228         $tree = $cache_client->new()->get_cache('_orgtree');
229         if($tree) { return $tree; }
230
231         if(!$orglist) {
232                 warn "Retrieving Org Tree\n";
233                 $orglist = $self->simple_scalar_request( 
234                         "open-ils.storage", 
235                         "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
236         }
237
238         if( ! $org_typelist ) {
239                 warn "Retrieving org types\n";
240                 $org_typelist = $self->simple_scalar_request( 
241                         "open-ils.storage", 
242                         "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
243                 $self->build_org_type($org_typelist);
244         }
245
246         $tree = $self->build_org_tree($orglist,1);
247         $cache_client->new()->put_cache('_orgtree', $tree);
248         return $tree;
249
250 }
251
252 my $slimtree = undef;
253 sub get_slim_org_tree {
254
255         my $self = shift;
256         if($slimtree) { return $slimtree; }
257
258         # see if it's in the cache
259         $slimtree = $cache_client->new()->get_cache('slimorgtree');
260         if($slimtree) { return $slimtree; }
261
262         if(!$orglist) {
263                 warn "Retrieving Org Tree\n";
264                 $orglist = $self->simple_scalar_request( 
265                         "open-ils.storage", 
266                         "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
267         }
268
269         $slimtree = $self->build_org_tree($orglist);
270         $cache_client->new->put_cache('slimorgtree', $slimtree);
271         return $slimtree;
272
273 }
274
275
276 sub build_org_type { 
277         my($self, $org_typelist)  = @_;
278         for my $type (@$org_typelist) {
279                 $org_typelist_hash->{$type->id()} = $type;
280         }
281 }
282
283
284
285 sub build_org_tree {
286
287         my( $self, $orglist, $add_types ) = @_;
288
289         return $orglist unless ( 
290                         ref($orglist) and @$orglist > 1 );
291
292         my @list = sort { 
293                 $a->ou_type <=> $b->ou_type ||
294                 $a->name cmp $b->name } @$orglist;
295
296         for my $org (@list) {
297
298                 next unless ($org);
299
300                 if(!ref($org->ou_type()) and $add_types) {
301                         $org->ou_type( $org_typelist_hash->{$org->ou_type()});
302                 }
303
304                 next unless (defined($org->parent_ou));
305
306                 my ($parent) = grep { $_->id == $org->parent_ou } @list;
307                 next unless $parent;
308                 $parent->children([]) unless defined($parent->children); 
309                 push( @{$parent->children}, $org );
310         }
311
312         return $list[0];
313
314 }
315
316 sub fetch_user {
317         my( $self, $userid ) = @_;
318         my( $user, $evt );
319
320         $logger->debug("Fetching user $userid from storage");
321
322         $user = $self->simplereq(
323                 'open-ils.storage',
324                 'open-ils.storage.direct.actor.user.retrieve', $userid );
325
326         if(!$user) {
327                 $logger->info("User $userid not found in the db");
328                 $evt = OpenILS::Event->new('USER_NOT_FOUND');
329         }
330
331         return ($user, $evt);
332 }
333
334 sub checkses {
335         my( $self, $session ) = @_;
336         my $user; my $evt; my $e; 
337
338         $logger->debug("Checking user session $session");
339
340         try {
341                 $user = $self->check_user_session($session);
342         } catch Error with { $e = 1; };
343
344         if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
345         return ( $user, $evt );
346 }
347
348
349 # verifiese the session and checks the permissions agains the
350 # session user and the user's home_ou as the org id
351 sub checksesperm {
352         my( $self, $session, @perms ) = @_;
353         my $user; my $evt; my $e; 
354         $logger->debug("Checking user session $session and perms @perms");
355         ($user, $evt) = $self->checkses($session);
356         return (undef, $evt) if $evt;
357         $evt = $self->check_perms($user->id, $user->home_ou, @perms);
358         return ($user, $evt);
359 }
360
361
362 sub checkrequestor {
363         my( $self, $staffobj, $userid, @perms ) = @_;
364         my $user; my $evt;
365         $userid = $staffobj->id unless defined $userid;
366
367         $logger->debug("checkrequestor(): requestor => " . $staffobj->id . ", target => $userid");
368
369         if( $userid ne $staffobj->id ) {
370                 ($user, $evt) = $self->fetch_user($userid);
371                 return (undef, $evt) if $evt;
372                 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
373
374         } else {
375                 $user = $staffobj;
376         }
377
378         return ($user, $evt);
379 }
380
381 sub checkses_requestor {
382         my( $self, $authtoken, $targetid, @perms ) = @_;
383         my( $requestor, $target, $evt );
384
385         ($requestor, $evt) = $self->checkses($authtoken);
386         return (undef, undef, $evt) if $evt;
387
388         ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
389         return( $requestor, $target, $evt);
390 }
391
392 sub fetch_copy {
393         my( $self, $copyid ) = @_;
394         my( $copy, $evt );
395
396         $logger->debug("Fetching copy $copyid from storage");
397
398         $copy = $self->simplereq(
399                 'open-ils.storage',
400                 'open-ils.storage.direct.asset.copy.retrieve', $copyid );
401
402         if(!$copy) { $evt = OpenILS::Event->new('COPY_NOT_FOUND'); }
403
404         return( $copy, $evt );
405 }
406
407
408 # retrieves a circ object by id
409 sub fetch_circulation {
410         my( $self, $circid ) = @_;
411         my $circ; my $evt;
412         
413         $logger->debug("Fetching circ $circid from storage");
414
415         $circ = $self->simplereq(
416                 'open-ils.storage',
417                 "open-ils.storage.direct.action.circulation.retrieve", $circid );
418
419         if(!$circ) {
420                 $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND', circid => $circid );
421         }
422
423         return ( $circ, $evt );
424 }
425
426 sub fetch_record_by_copy {
427         my( $self, $copyid ) = @_;
428         my( $record, $evt );
429
430         $logger->debug("Fetching record by copy $copyid from storage");
431
432         $record = $self->simplereq(
433                 'open-ils.storage',
434                 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy', $copyid );
435
436         if(!$record) {
437                 $evt = OpenILS::Event->new('BIBLIO_RECORD_NOT_FOUND');
438         }
439
440         return ($record, $evt);
441 }
442
443 # turns a record object into an mvr (mods) object
444 sub record_to_mvr {
445         my( $self, $record ) = @_;
446         my $u = OpenILS::Utils::ModsParser->new();
447         $u->start_mods_batch( $record->marc );
448         my $mods = $u->finish_mods_batch();
449         $mods->doc_id($record->id);
450         return $mods;
451 }
452
453 sub fetch_hold {
454         my( $self, $holdid ) = @_;
455         my( $hold, $evt );
456
457         $logger->debug("Fetching hold $holdid from storage");
458
459         $hold = $self->simplereq(
460                 'open-ils.storage',
461                 'open-ils.storage.direct.action.hold_request.retrieve', $holdid);
462
463         $evt = OpenILS::Event->new('HOLD_NOT_FOUND', holdid => $holdid) unless $hold;
464
465         return ($hold, $evt);
466 }
467
468
469 sub fetch_hold_transit_by_hold {
470         my( $self, $holdid ) = @_;
471         my( $transit, $evt );
472
473         $logger->debug("Fetching transit by hold $holdid from storage");
474
475         $transit = $self->simplereq(
476                 'open-ils.storage',
477                 'open-ils.storage.direct.action.hold_transit_copy.search.hold', $holdid );
478
479         $evt = OpenILS::Event->new('HOLD_TRANSIT_NOT_FOUND', holdid => $holdid) unless $transit;
480
481         return ($transit, $evt );
482 }
483
484 sub fetch_hold_transit {
485         my( $self, $transid ) = @_;
486         my( $htransit, $evt );
487         $logger->debug("Fetching hold transit with hold id $transid");
488         $htransit = $self->storagereq(
489                 'open-ils.storage.direct.action.hold_transit_copy.retrieve', $transid );
490         $evt = OpenILS::Event->new('HOLD_TRANSIT_NOT_FOUND', id => $transid) unless $htransit;
491         return ($htransit, $evt);
492 }
493
494 sub fetch_copy_by_barcode {
495         my( $self, $barcode ) = @_;
496         my( $copy, $evt );
497
498         $logger->debug("Fetching copy by barcode $barcode from storage");
499
500         $copy = $self->simplereq( 'open-ils.storage',
501                 'open-ils.storage.direct.asset.copy.search.barcode', $barcode );
502
503         $evt = OpenILS::Event->new('COPY_NOT_FOUND', barcode => $barcode) unless $copy;
504
505         return ($copy, $evt);
506 }
507
508 sub fetch_open_billable_transaction {
509         my( $self, $transid ) = @_;
510         my( $transaction, $evt );
511
512         $logger->debug("Fetching open billable transaction $transid from storage");
513
514         $transaction = $self->simplereq(
515                 'open-ils.storage',
516                 'open-ils.storage.direct.money.open_billable_transaction_summary.retrieve',  $transid);
517
518         $evt = OpenILS::Event->new(
519                 'TRANSACTION_NOT_FOUND', transid => $transid ) unless $transaction;
520
521         return ($transaction, $evt);
522 }
523
524
525
526 my %buckets;
527 $buckets{'biblio'} = 'biblio_record_entry_bucket';
528 $buckets{'callnumber'} = 'call_number_bucket';
529 $buckets{'copy'} = 'copy_bucket';
530 $buckets{'user'} = 'user_bucket';
531
532 sub fetch_container {
533         my( $self, $id, $type ) = @_;
534         my( $bucket, $evt );
535
536         $logger->debug("Fetching container $id with type $type");
537
538         my $meth = $buckets{$type};
539         $bucket = $self->simplereq(
540                 'open-ils.storage',
541                 "open-ils.storage.direct.container.$meth.retrieve", $id );
542
543         $evt = OpenILS::Event->new(
544                 'CONTAINER_NOT_FOUND', container => $id, 
545                         container_type => $type ) unless $bucket;
546
547         return ($bucket, $evt);
548 }
549
550
551 sub fetch_container_item {
552         my( $self, $id, $type ) = @_;
553         my( $bucket, $evt );
554
555         $logger->debug("Fetching container item $id with type $type");
556
557         my $meth = $buckets{$type} . "_item";
558
559         $bucket = $self->simplereq(
560                 'open-ils.storage',
561                 "open-ils.storage.direct.container.$meth.retrieve", $id );
562
563         $evt = OpenILS::Event->new(
564                 'CONTAINER_ITEM_NOT_FOUND', itemid => $id, 
565                         container_type => $type ) unless $bucket;
566
567         return ($bucket, $evt);
568 }
569
570
571 sub fetch_patron_standings {
572         my $self = shift;
573         $logger->debug("Fetching patron standings");    
574         return $self->simplereq(
575                 'open-ils.storage', 
576                 'open-ils.storage.direct.config.standing.retrieve.all.atomic');
577 }
578
579
580 sub fetch_permission_group_tree {
581         my $self = shift;
582         $logger->debug("Fetching patron profiles");     
583         return $self->simplereq(
584                 'open-ils.actor', 
585                 'open-ils.actor.groups.tree.retrieve' );
586 }
587
588
589 sub fetch_patron_circ_summary {
590         my( $self, $userid ) = @_;
591         $logger->debug("Fetching patron summary for $userid");
592         my $summary = $self->simplereq(
593                 'open-ils.storage', 
594                 "open-ils.storage.action.circulation.patron_summary", $userid );
595
596         if( $summary ) {
597                 $summary->[0] ||= 0;
598                 $summary->[1] ||= 0.0;
599                 return $summary;
600         }
601         return undef;
602 }
603
604
605 sub fetch_copy_statuses {
606         my( $self ) = @_;
607         $logger->debug("Fetching copy statuses");
608         return $self->simplereq(
609                 'open-ils.storage', 
610                 'open-ils.storage.direct.config.copy_status.retrieve.all.atomic' );
611 }
612
613 sub fetch_copy_location {
614         my( $self, $id ) = @_;
615         my $evt;
616         my $cl = $self->storagereq(
617                 'open-ils.storage.direct.asset.copy_location.retrieve', $id );
618         $evt = OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless $cl;
619         return ($cl, $evt);
620 }
621
622 sub fetch_copy_locations {
623         my $self = shift; 
624         return $self->simplereq(
625                 'open-ils.storage', 
626                 'open-ils.storage.direct.asset.copy_location.retrieve.all.atomic');
627 }
628
629 sub fetch_copy_location_by_name {
630         my( $self, $name, $org ) = @_;
631         my $evt;
632         my $cl = $self->storagereq(
633                 'open-ils.storage.direct.asset.copy_location.search_where',
634                         { name => $name, owning_lib => $org } );
635         $evt = OpenILS::Event->new('COPY_LOCATION_NOT_FOUND') unless $cl;
636         return ($cl, $evt);
637 }
638
639 sub fetch_callnumber {
640         my( $self, $id ) = @_;
641         my $evt = undef;
642         $logger->debug("Fetching callnumber $id");
643
644         my $cn = $self->simplereq(
645                 'open-ils.storage',
646                 'open-ils.storage.direct.asset.call_number.retrieve', $id );
647         $evt = OpenILS::Event->new( 'VOLUME_NOT_FOUND', id => $id ) unless $cn;
648
649         return ( $cn, $evt );
650 }
651
652 sub fetch_org_unit {
653         my( $self, $id ) = @_;
654         return $id if( ref($id) eq 'Fieldmapper::actor::org_unit' );
655         $logger->debug("Fetching org unit $id");
656         my $evt = undef;
657
658         my $org = $self->simplereq(
659                 'open-ils.storage', 
660                 'open-ils.storage.direct.actor.org_unit.retrieve', $id );
661         $evt = OpenILS::Event->new( 'ORG_UNIT_NOT_FOUND', id => $id ) unless $org;
662
663         return ($org, $evt);
664 }
665
666 sub fetch_stat_cat {
667         my( $self, $type, $id ) = @_;
668         my( $cat, $evt );
669         $logger->debug("Fetching $type stat cat: $id");
670         $cat = $self->simplereq(
671                 'open-ils.storage', 
672                 "open-ils.storage.direct.$type.stat_cat.retrieve", $id );
673         $evt = OpenILS::Event->new( 'STAT_CAT_NOT_FOUND', id => $id ) unless $cat;
674         return ( $cat, $evt );
675 }
676
677 sub fetch_stat_cat_entry {
678         my( $self, $type, $id ) = @_;
679         my( $entry, $evt );
680         $logger->debug("Fetching $type stat cat entry: $id");
681         $entry = $self->simplereq(
682                 'open-ils.storage', 
683                 "open-ils.storage.direct.$type.stat_cat_entry.retrieve", $id );
684         $evt = OpenILS::Event->new( 'STAT_CAT_ENTRY_NOT_FOUND', id => $id ) unless $entry;
685         return ( $entry, $evt );
686 }
687
688
689 sub find_org {
690         my( $self, $org_tree, $orgid )  = @_;
691         return $org_tree if ( $org_tree->id eq $orgid );
692         return undef unless ref($org_tree->children);
693         for my $c (@{$org_tree->children}) {
694                 my $o = $self->find_org($c, $orgid);
695                 return $o if $o;
696         }
697         return undef;
698 }
699
700 sub fetch_non_cat_type_by_name_and_org {
701         my( $self, $name, $orgId ) = @_;
702         $logger->debug("Fetching non cat type $name at org $orgId");
703         my $types = $self->simplereq(
704                 'open-ils.storage',
705                 'open-ils.storage.direct.config.non_cataloged_type.search_where.atomic',
706                 { name => $name, owning_lib => $orgId } );
707         return ($types->[0], undef) if($types and @$types);
708         return (undef, OpenILS::Event->new('NON_CAT_TYPE_NOT_FOUND') );
709 }
710
711 sub fetch_non_cat_type {
712         my( $self, $id ) = @_;
713         $logger->debug("Fetching non cat type $id");
714         my( $type, $evt );
715         $type = $self->simplereq(
716                 'open-ils.storage', 
717                 'open-ils.storage.direct.config.non_cataloged_type.retrieve', $id );
718         $evt = OpenILS::Event->new('NON_CAT_TYPE_NOT_FOUND') unless $type;
719         return ($type, $evt);
720 }
721
722 sub DB_UPDATE_FAILED { 
723         my( $self, $payload ) = @_;
724         return OpenILS::Event->new('DATABASE_UPDATE_FAILED', 
725                 payload => ($payload) ? $payload : undef ); 
726 }
727
728 sub fetch_circ_duration_by_name {
729         my( $self, $name ) = @_;
730         my( $dur, $evt );
731         $dur = $self->simplereq(
732                 'open-ils.storage', 
733                 'open-ils.storage.direct.config.rules.circ_duration.search.name.atomic', $name );
734         $dur = $dur->[0];
735         $evt = OpenILS::Event->new('CIRC_DURATION_NOT_FOUND') unless $dur;
736         return ($dur, $evt);
737 }
738
739 sub fetch_recurring_fine_by_name {
740         my( $self, $name ) = @_;
741         my( $obj, $evt );
742         $obj = $self->simplereq(
743                 'open-ils.storage', 
744                 'open-ils.storage.direct.config.rules.recuring_fine.search.name.atomic', $name );
745         $obj = $obj->[0];
746         $evt = OpenILS::Event->new('RECURRING_FINE_NOT_FOUND') unless $obj;
747         return ($obj, $evt);
748 }
749
750 sub fetch_max_fine_by_name {
751         my( $self, $name ) = @_;
752         my( $obj, $evt );
753         $obj = $self->simplereq(
754                 'open-ils.storage', 
755                 'open-ils.storage.direct.config.rules.max_fine.search.name.atomic', $name );
756         $obj = $obj->[0];
757         $evt = OpenILS::Event->new('MAX_FINE_NOT_FOUND') unless $obj;
758         return ($obj, $evt);
759 }
760
761 sub storagereq {
762         my( $self, $method, @params ) = @_;
763         return $self->simplereq(
764                 'open-ils.storage', $method, @params );
765 }
766
767 sub event_equals {
768         my( $self, $e, $name ) =  @_;
769         if( $e and ref($e) eq 'HASH' and 
770                 defined($e->{textcode}) and $e->{textcode} eq $name ) {
771                 return 1 ;
772         }
773         return 0;
774 }
775
776 sub logmark {
777         my( undef, $f, $l ) = caller(0);
778         my( undef, undef, undef, $s ) = caller(1);
779         $s =~ s/.*:://g;
780         $f =~ s/.*\///g;
781         $logger->debug("LOGMARK: $f:$l:$s");
782 }
783
784 # takes a copy id 
785 sub fetch_open_circulation {
786         my( $self, $cid ) = @_;
787         my $evt;
788         $self->logmark;
789         my $circ = $self->storagereq(
790                 'open-ils.storage.direct.action.open_circulation.search_where',
791                 { target_copy => $cid, stop_fines_time => undef } );
792         $evt = OpenILS::Event->new('CIRCULATION_NOT_FOUND') unless $circ;       
793         return ($circ, $evt);
794 }
795
796 sub copy_status_from_name {
797         my( $self, $statuses, $name ) = @_;
798         for my $status (@$statuses) { 
799                 return $status if( $status->name =~ /$name/i );
800         }
801         return undef;
802 }
803
804 sub copy_status_to_name {
805         my( $self, $statuses, $sid ) = @_;
806         for my $status (@$statuses) { 
807                 return $status->name if( $status->id == $sid );
808         }
809         return undef;
810 }
811
812 sub fetch_open_transit_by_copy {
813         my( $self, $copyid ) = @_;
814         my($transit, $evt);
815         $transit = $self->storagereq(
816                 'open-ils.storage.direct.action.transit_copy.search_where',
817                 { target_copy => $copyid, dest_recv_time => undef });
818         $evt = OpenILS::Event->new('TRANSIT_NOT_FOUND') unless $transit;
819         return ($transit, $evt);
820 }
821
822
823 # un-fleshes a copy and updates it in the DB
824 # returns a DB_UPDATE_FAILED event on error
825 # returns undef on success
826 sub update_copy {
827         my( $self, %params ) = @_;
828
829         my $copy                = $params{copy} || die "update_copy(): copy required";
830         my $editor      = $params{editor} || die "update_copy(): copy editor required";
831         my $session = $params{session};
832
833         $logger->debug("Updating copy in the database: " . $copy->id);
834
835         $copy->status( $copy->status->id ) if ref($copy->status);
836         $copy->editor( $editor );
837         $copy->edit_date( 'now' );
838         $copy->location( $copy->location->id ) if ref($copy->location);
839         $copy->circ_lib( $copy->circ_lib->id ) if ref($copy->circ_lib);
840
841         my $s;
842         my $meth = 'open-ils.storage.direct.asset.copy.update';
843
844         $s = $session->request( $meth, $copy )->gather(1) if $session;
845         $s = $self->storagereq( $meth, $copy );
846
847         $logger->debug("Update of copy ".$copy->id." returned: $s");
848
849         return $self->DB_UPDATE_FAILED($copy) unless $s;
850         return undef;
851 }
852
853 1;
854