]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/CStoreEditor.pm
change log level warning for non-ID retrieval
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / CStoreEditor.pm
1 use strict; use warnings;
2 package OpenILS::Utils::CStoreEditor;
3 use OpenILS::Application::AppUtils;
4 use OpenSRF::AppSession;
5 use OpenSRF::EX qw(:try);
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Event;
8 use Data::Dumper;
9 use OpenSRF::Utils::JSON;
10 use OpenSRF::Utils::Logger qw($logger);
11 my $U = "OpenILS::Application::AppUtils";
12 my %PERMS;
13 my $cache;
14 my %xact_ed_cache;
15
16 our $always_xact = 0;
17 our $_loaded = 1;
18
19 #my %PERMS = (
20 #       'biblio.record_entry'   => { update => 'UPDATE_MARC' },
21 #       'asset.copy'                            => { update => 'UPDATE_COPY'},
22 #       'asset.call_number'             => { update => 'UPDATE_VOLUME'},
23 #       'action.circulation'            => { retrieve => 'VIEW_CIRCULATIONS'},
24 #);
25
26 sub flush_forced_xacts {
27     for my $k ( keys %xact_ed_cache ) {
28         $xact_ed_cache{$k}->rollback;
29         delete $xact_ed_cache{$k};
30     }
31 }
32
33 # -----------------------------------------------------------------------------
34 # Export some useful functions
35 # -----------------------------------------------------------------------------
36 use vars qw(@EXPORT_OK %EXPORT_TAGS);
37 use Exporter;
38 use base qw/Exporter/;
39 push @EXPORT_OK, ( 'new_editor', 'new_rstore_editor' );
40 %EXPORT_TAGS = ( funcs => [ qw/ new_editor new_rstore_editor / ] );
41
42 sub new_editor { return OpenILS::Utils::CStoreEditor->new(@_); }
43
44 sub new_rstore_editor { 
45         my $e = OpenILS::Utils::CStoreEditor->new(@_); 
46         $e->app('open-ils.reporter-store');
47         return $e;
48 }
49
50
51 # -----------------------------------------------------------------------------
52 # Log levels
53 # -----------------------------------------------------------------------------
54 use constant E => 'error';
55 use constant W => 'warn';
56 use constant I => 'info';
57 use constant D => 'debug';
58 use constant A => 'activity';
59
60
61
62 # -----------------------------------------------------------------------------
63 # Params include:
64 #       xact=><true> : creates a storage transaction
65 #       authtoken=>$token : the login session key
66 # -----------------------------------------------------------------------------
67 sub new {
68         my( $class, %params ) = @_;
69         $class = ref($class) || $class;
70         my $self = bless( \%params, $class );
71         $self->{checked_perms} = {};
72         return $self;
73 }
74
75
76 sub app {
77         my( $self, $app ) = @_;
78         $self->{app} = $app if $app;
79         $self->{app} = 'open-ils.cstore' unless $self->{app};
80         return $self->{app};
81 }
82
83
84 # -----------------------------------------------------------------------------
85 # Log the editor metadata along with the log string
86 # -----------------------------------------------------------------------------
87 sub log {
88         my( $self, $lev, $str ) = @_;
89         my $s = "editor[";
90     if ($always_xact) {
91         $s .= "!|";
92     } elsif ($self->{xact}) {
93         $s .= "1|";
94     } else {
95             $s .= "0|";
96     }
97         $s .= "0" unless $self->requestor;
98         $s .= $self->requestor->id if $self->requestor;
99         $s .= "]";
100         $logger->$lev("$s $str");
101 }
102
103 # -----------------------------------------------------------------------------
104 # Verifies the auth token and fetches the requestor object
105 # -----------------------------------------------------------------------------
106 sub checkauth {
107         my $self = shift;
108         $self->log(D, "checking auth token ".$self->authtoken);
109         my ($reqr, $evt) = $U->checkses($self->authtoken);
110         $self->event($evt) if $evt;
111         return $self->{requestor} = $reqr;
112 }
113
114
115 =head test
116 sub checkauth {
117         my $self = shift;
118         $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
119         $self->log(D, "checking cached auth token ".$self->authtoken);
120         my $user = $cache->get_cache("oils_auth_".$self->authtoken);
121         return $self->{requestor} = $user->{userobj} if $user;
122         $self->event(OpenILS::Event->new('NO_SESSION'));
123         return undef;
124 }
125 =cut
126
127
128 # -----------------------------------------------------------------------------
129 # Returns the last generated event
130 # -----------------------------------------------------------------------------
131 sub event {
132         my( $self, $evt ) = @_;
133         $self->{event} = $evt if $evt;
134         return $self->{event};
135 }
136
137 # -----------------------------------------------------------------------------
138 # Destroys the transaction and disconnects where necessary,
139 # then returns the last event that occurred
140 # -----------------------------------------------------------------------------
141 sub die_event {
142         my $self = shift;
143         $self->rollback;
144     $self->died(1);
145         return $self->event;
146 }
147
148
149 # -----------------------------------------------------------------------------
150 # Clears the last caught event
151 # -----------------------------------------------------------------------------
152 sub clear_event {
153         my $self = shift;
154         $self->{event} = undef;
155 }
156
157 sub died {
158     my($self, $died) = @_;
159     $self->{died} = $died if defined $died;
160     return $self->{died};
161 }
162
163 sub authtoken {
164         my( $self, $auth ) = @_;
165         $self->{authtoken} = $auth if $auth;
166         return $self->{authtoken};
167 }
168
169 sub timeout {
170     my($self, $to) = @_;
171     $self->{timeout} = $to if defined $to;
172     return defined($self->{timeout}) ? $self->{timeout} : 60;
173 }
174
175 # -----------------------------------------------------------------------------
176 # fetches the session, creating if necessary.  If 'xact' is true on this
177 # object, a db session is created
178 # -----------------------------------------------------------------------------
179 sub session {
180         my( $self, $session ) = @_;
181         $self->{session} = $session if $session;
182
183         if(!$self->{session}) {
184                 $self->{session} = OpenSRF::AppSession->create($self->app);
185
186                 if( ! $self->{session} ) {
187                         my $str = "Error creating cstore session with OpenSRF::AppSession->create()!";
188                         $self->log(E, $str);
189                         throw OpenSRF::EX::ERROR ($str);
190                 }
191
192                 $self->{session}->connect if $self->{xact} or $self->{connect} or $always_xact;
193                 $self->xact_begin if $self->{xact} or $always_xact;
194         }
195
196     $xact_ed_cache{$self->{xact_id}} = $self if $always_xact;
197         return $self->{session};
198 }
199
200
201 # -----------------------------------------------------------------------------
202 # Starts a storage transaction
203 # -----------------------------------------------------------------------------
204 sub xact_begin {
205     my $self = shift;
206     return $self->{xact_id} if $self->{xact_id};
207     $self->session->connect unless $self->session->state == OpenSRF::AppSession::CONNECTED();
208         $self->log(D, "starting new database transaction");
209         my $stat = $self->request($self->app . '.transaction.begin') unless $self->{xact_id};
210         $self->log(E, "error starting database transaction") unless $stat;
211     $self->{xact_id} = $stat;
212     $self->{xact} = 1;
213         return $stat;
214 }
215
216 # -----------------------------------------------------------------------------
217 # Commits a storage transaction
218 # -----------------------------------------------------------------------------
219 sub xact_commit {
220         my $self = shift;
221     return unless $self->{xact_id};
222         $self->log(D, "comitting db session");
223         my $stat = $self->request($self->app.'.transaction.commit');
224         $self->log(E, "error comitting database transaction") unless $stat;
225     delete $self->{xact_id};
226     delete $self->{xact};
227         return $stat;
228 }
229
230 # -----------------------------------------------------------------------------
231 # Rolls back a storage stransaction
232 # -----------------------------------------------------------------------------
233 sub xact_rollback {
234         my $self = shift;
235     return unless $self->{session} and $self->{xact_id};
236         $self->log(I, "rolling back db session");
237         my $stat = $self->request($self->app.".transaction.rollback");
238         $self->log(E, "error rolling back database transaction") unless $stat;
239     delete $self->{xact_id};
240     delete $self->{xact};
241         return $stat;
242 }
243
244
245 # -----------------------------------------------------------------------------
246 # Savepoint functions.  If no savepoint name is provided, the same name is used 
247 # for each successive savepoint, in which case only the last savepoint set can 
248 # be released or rolled back.
249 # -----------------------------------------------------------------------------
250 sub set_savepoint {
251     my $self = shift;
252     my $name = shift || 'savepoint';
253     return unless $self->{session} and $self->{xact_id};
254         $self->log(I, "setting savepoint '$name'");
255         my $stat = $self->request($self->app.".savepoint.set")
256             or $self->log(E, "error setting savepoint '$name'");
257     return $stat;
258 }
259
260 sub release_savepoint {
261     my $self = shift;
262     my $name = shift || 'savepoint';
263     return unless $self->{session} and $self->{xact_id};
264         $self->log(I, "releasing savepoint '$name'");
265         my $stat = $self->request($self->app.".savepoint.release")
266         or $self->log(E, "error releasing savepoint '$name'");
267     return $stat;
268 }
269
270 sub rollback_savepoint {
271     my $self = shift;
272     my $name = shift || 'savepoint';
273     return unless $self->{session} and $self->{xact_id};
274         $self->log(I, "rollback savepoint '$name'");
275         my $stat = $self->request($self->app.".savepoint.rollback")
276         or $self->log(E, "error rolling back savepoint '$name'");
277     return $stat;
278 }
279
280
281 # -----------------------------------------------------------------------------
282 # Rolls back the transaction and disconnects
283 # -----------------------------------------------------------------------------
284 sub rollback {
285         my $self = shift;
286         $self->xact_rollback;
287         $self->disconnect;
288 }
289
290 sub disconnect {
291         my $self = shift;
292         $self->session->disconnect if $self->{session};
293     delete $self->{session};
294 }
295
296
297 # -----------------------------------------------------------------------------
298 # commits the db session and destroys the session
299 # -----------------------------------------------------------------------------
300 sub commit {
301         my $self = shift;
302         return unless $self->{xact_id};
303         $self->xact_commit;
304         $self->session->disconnect;
305         $self->{session} = undef;
306 }
307
308 # -----------------------------------------------------------------------------
309 # clears all object data. Does not commit the db transaction.
310 # -----------------------------------------------------------------------------
311 sub reset {
312         my $self = shift;
313         $self->disconnect;
314         $$self{$_} = undef for (keys %$self);
315 }
316
317
318 # -----------------------------------------------------------------------------
319 # commits and resets
320 # -----------------------------------------------------------------------------
321 sub finish {
322         my $self = shift;
323         $self->commit;
324         $self->reset;
325 }
326
327
328
329 # -----------------------------------------------------------------------------
330 # Does a simple storage request
331 # -----------------------------------------------------------------------------
332 sub request {
333         my( $self, $method, @params ) = @_;
334
335     my $val;
336         my $err;
337         my $argstr = __arg_to_string( (scalar(@params)) == 1 ? $params[0] : \@params);
338         my $locale = $self->session->session_locale;
339
340         $self->log(I, "request $locale $method $argstr");
341
342         if( ($self->{xact} or $always_xact) and 
343                         $self->session->state != OpenSRF::AppSession::CONNECTED() ) {
344                 #$logger->error("CStoreEditor lost it's connection!!");
345                 throw OpenSRF::EX::ERROR ("CStore connection timed out - transaction cannot continue");
346         }
347
348
349         try {
350
351         my $req = $self->session->request($method, @params);
352
353         if($self->substream) {
354             $self->log(D,"running in substream mode");
355             $val = [];
356             while( my $resp = $req->recv(timeout => $self->timeout) ) {
357                 push(@$val, $resp->content) if $resp->content;
358             }
359
360         } else {
361             my $resp = $req->recv(timeout => $self->timeout);
362             if($req->failed) {
363                 $err = $resp;
364                         $self->log(E, "request error $method : $argstr : $err");
365             } else {
366                 $val = $resp->content;
367             }
368         }
369
370         $req->finish;
371
372         } catch Error with {
373                 $err = shift;
374                 $self->log(E, "request error $method : $argstr : $err");
375         };
376
377         throw $err if $err;
378         return $val;
379 }
380
381 sub substream {
382    my( $self, $bool ) = @_;
383    $self->{substream} = $bool if defined $bool;
384    return $self->{substream};
385 }
386
387
388 # -----------------------------------------------------------------------------
389 # Sets / Returns the requestor object.  This is set when checkauth succeeds.
390 # -----------------------------------------------------------------------------
391 sub requestor {
392         my($self, $requestor) = @_;
393         $self->{requestor} = $requestor if $requestor;
394         return $self->{requestor};
395 }
396
397
398
399 # -----------------------------------------------------------------------------
400 # Holds the last data received from a storage call
401 # -----------------------------------------------------------------------------
402 sub data {
403         my( $self, $data ) = @_;
404         $self->{data} = $data if defined $data;
405         return $self->{data};
406 }
407
408
409 # -----------------------------------------------------------------------------
410 # True if this perm has already been checked at this org
411 # -----------------------------------------------------------------------------
412 sub perm_checked {
413         my( $self, $perm, $org ) = @_;
414         $self->{checked_perms}->{$org} = {}
415                 unless $self->{checked_perms}->{$org};
416         my $checked = $self->{checked_perms}->{$org}->{$perm};
417         if(!$checked) {
418                 $self->{checked_perms}->{$org}->{$perm} = 1;
419                 return 0;
420         }
421         return 1;
422 }
423
424
425
426 # -----------------------------------------------------------------------------
427 # Returns true if the requested perm is allowed.  If the perm check fails,
428 # $e->event is set and undef is returned
429 # The perm user is $e->requestor->id and perm org defaults to the requestor's
430 # ws_ou
431 # if perm is an array of perms, method will return true at the first allowed
432 # permission.  If none of the perms are allowed, the perm_failure event
433 # is created with the last perm to fail
434 # -----------------------------------------------------------------------------
435 my $PERM_QUERY = {
436     select => {
437         au => [ {
438             transform => 'permission.usr_has_perm',
439             alias => 'has_perm',
440             column => 'id',
441             params => []
442         } ]
443     },
444     from => 'au',
445     where => {},
446 };
447
448 my $OBJECT_PERM_QUERY = {
449     select => {
450         au => [ {
451             transform => 'permission.usr_has_object_perm',
452             alias => 'has_perm',
453             column => 'id',
454             params => []
455         } ]
456     },
457     from => 'au',
458     where => {},
459 };
460
461 sub allowed {
462         my( $self, $perm, $org, $object, $hint ) = @_;
463         my $uid = $self->requestor->id;
464         $org ||= $self->requestor->ws_ou;
465
466     my $perms = (ref($perm) eq 'ARRAY') ? $perm : [$perm];
467
468     for $perm (@$perms) {
469             $self->log(I, "checking perms user=$uid, org=$org, perm=$perm");
470     
471         if($object) {
472             my $params;
473             if(ref $object) {
474                 # determine the ID field and json_hint from the object
475                 my $id_field = $object->Identity;
476                 $params = [$perm, $object->json_hint, $object->$id_field];
477             } else {
478                 # we were passed an object-id and json_hint
479                 $params = [$perm, $hint, $object];
480             }
481             push(@$params, $org) if $org;
482             $OBJECT_PERM_QUERY->{select}->{au}->[0]->{params} = $params;
483             $OBJECT_PERM_QUERY->{where}->{id} = $uid;
484             return 1 if $U->is_true($self->json_query($OBJECT_PERM_QUERY)->[0]->{has_perm});
485
486         } else {
487             $PERM_QUERY->{select}->{au}->[0]->{params} = [$perm, $org];
488             $PERM_QUERY->{where}->{id} = $uid;
489             return 1 if $U->is_true($self->json_query($PERM_QUERY)->[0]->{has_perm});
490         }
491     }
492
493     # set the perm failure event if the permission check returned false
494         my $e = OpenILS::Event->new('PERM_FAILURE', ilsperm => $perm, ilspermloc => $org);
495         $self->event($e);
496         return undef;
497 }
498
499
500 # -----------------------------------------------------------------------------
501 # Returns the list of object IDs this user has object-specific permissions for
502 # -----------------------------------------------------------------------------
503 sub objects_allowed {
504     my($self, $perm, $obj_type) = @_;
505
506     my $perms = (ref($perm) eq 'ARRAY') ? $perm : [$perm];
507     my @ids;
508
509     for $perm (@$perms) {
510         my $query = {
511             select => {puopm => ['object_id']},
512             from => {
513                 puopm => {
514                     ppl => {field => 'id',fkey => 'perm'}
515                 }
516             },
517             where => {
518                 '+puopm' => {usr => $self->requestor->id, object_type => $obj_type},
519                 '+ppl' => {code => $perm}
520             }
521         };
522     
523         my $list = $self->json_query($query);
524         push(@ids, 0+$_->{object_id}) for @$list;
525     }
526
527    my %trim;
528    $trim{$_} = 1 for @ids;
529    return [ keys %trim ];
530 }
531
532
533 # -----------------------------------------------------------------------------
534 # checks the appropriate perm for the operation
535 # -----------------------------------------------------------------------------
536 sub _checkperm {
537         my( $self, $ptype, $action, $org ) = @_;
538         $org ||= $self->requestor->ws_ou;
539         my $perm = $PERMS{$ptype}{$action};
540         if( $perm ) {
541                 return undef if $self->perm_checked($perm, $org);
542                 return $self->event unless $self->allowed($perm, $org);
543         } else {
544                 $self->log(I, "no perm provided for $ptype.$action");
545         }
546         return undef;
547 }
548
549
550
551 # -----------------------------------------------------------------------------
552 # Logs update actions to the activity log
553 # -----------------------------------------------------------------------------
554 sub log_activity {
555         my( $self, $type, $action, $arg ) = @_;
556         my $str = "$type.$action";
557         $str .= _prop_string($arg);
558         $self->log(A, $str);
559 }
560
561
562
563 sub _prop_string {
564         my $obj = shift;
565         my @props = $obj->properties;
566         my $str = "";
567         for(@props) {
568                 my $prop = $obj->$_() || "";
569                 $prop = substr($prop, 0, 128) . "..." if length $prop > 131;
570                 $str .= " $_=$prop";
571         }
572         return $str;
573 }
574
575
576 sub __arg_to_string {
577         my $arg = shift;
578         return "" unless defined $arg;
579         if( UNIVERSAL::isa($arg, "Fieldmapper") ) {
580                 return (defined $arg->id) ? $arg->id : '<new object>';
581         }
582         return OpenSRF::Utils::JSON->perl2JSON($arg);
583         return "";
584 }
585
586
587 # -----------------------------------------------------------------------------
588 # This does the actual storage query.
589 #
590 # 'search' calls become search_where calls and $arg can be a search hash or
591 # an array-ref of storage search options.  
592 #
593 # 'retrieve' expects an id
594 # 'update' expects an object
595 # 'create' expects an object
596 # 'delete' expects an object
597 #
598 # All methods return true on success and undef on failure.  On failure, 
599 # $e->event is set to the generated event.  
600 # Note: this method assumes that updating a non-changed object and 
601 # thereby receiving a 0 from storage, is a successful update.  
602 #
603 # The method will therefore return true so the caller can just do 
604 # $e->update_blah($x) or return $e->event;
605 # The true value returned from storage for all methods will be stored in 
606 # $e->data, until the next method is called.
607 #
608 # not-found events are generated on retrieve and serach methods.
609 # action=search methods will return [] (==true) if no data is found.  If the
610 # caller is interested in the not found event, they can do:  
611 # return $e->event unless @$results; 
612 # -----------------------------------------------------------------------------
613 sub runmethod {
614         my( $self, $action, $type, $arg, $options ) = @_;
615
616    $options ||= {};
617
618         if( $action eq 'retrieve' ) {
619                 if(! defined($arg) ) {
620                         $self->log(W,"$action $type called with no ID...");
621                         $self->event(_mk_not_found($type, $arg));
622                         return undef;
623                 } elsif( ref($arg) =~ /Fieldmapper/ ) {
624                         $self->log(D,"$action $type called with an object.. attempting ID retrieval..");
625                         $arg = $arg->id;
626                 }
627         }
628
629         my @arg = ( ref($arg) eq 'ARRAY' ) ? @$arg : ($arg);
630         my $method = $self->app.".direct.$type.$action";
631
632         if( $action eq 'search' ) {
633                 $method .= '.atomic';
634
635         } elsif( $action eq 'batch_retrieve' ) {
636                 $action = 'search';
637                 @arg = ( { id => $arg } );
638                 $method =~ s/batch_retrieve/search/o;
639                 $method .= '.atomic';
640
641         } elsif( $action eq 'retrieve_all' ) {
642                 $action = 'search';
643                 $method =~ s/retrieve_all/search/o;
644                 my $tt = $type;
645                 $tt =~ s/\./::/og;
646                 my $fmobj = "Fieldmapper::$tt";
647                 @arg = ( { $fmobj->Identity => { '!=' => undef } } );
648                 $method .= '.atomic';
649         }
650
651         $method =~ s/search/id_list/o if $options->{idlist};
652
653     $method =~ s/\.atomic$//o if $self->substream($$options{substream} || 0);
654     $self->timeout($$options{timeout});
655
656         # remove any stale events
657         $self->clear_event;
658
659         if( $action eq 'update' or $action eq 'delete' or $action eq 'create' ) {
660                 if(!($self->{xact} or $always_xact)) {
661                         $logger->error("Attempt to update DB while not in a transaction : $method");
662                         throw OpenSRF::EX::ERROR ("Attempt to update DB while not in a transaction : $method");
663                 }
664                 $self->log_activity($type, $action, $arg);
665         }
666
667         if($$options{checkperm}) {
668                 my $a = ($action eq 'search') ? 'retrieve' : $action;
669                 my $e = $self->_checkperm($type, $a, $$options{permorg});
670                 if($e) {
671                         $self->event($e);
672                         return undef;
673                 }
674         }
675
676         my $obj; 
677         my $err;
678
679         try {
680                 $obj = $self->request($method, @arg);
681         } catch Error with { $err = shift; };
682         
683
684         if(!defined $obj) {
685                 $self->log(I, "request returned no data : $method");
686
687                 if( $action eq 'retrieve' ) {
688                         $self->event(_mk_not_found($type, $arg));
689
690                 } elsif( $action eq 'update' or 
691                                 $action eq 'delete' or $action eq 'create' ) {
692                         my $evt = OpenILS::Event->new(
693                                 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
694                         $self->event($evt);
695                 }
696
697                 if( $err ) {
698                         $self->event( 
699                                 OpenILS::Event->new( 'DATABASE_QUERY_FAILED', 
700                                         payload => $arg, debug => "$err" ));
701                         return undef;
702                 }
703
704                 return undef;
705         }
706
707         if( $action eq 'create' and $obj == 0 ) {
708                 my $evt = OpenILS::Event->new(
709                         'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
710                 $self->event($evt);
711                 return undef;
712         }
713
714         # If we havn't dealt with the error in a nice way, go ahead and throw it
715         if( $err ) {
716                 $self->event( 
717                         OpenILS::Event->new( 'DATABASE_QUERY_FAILED', 
718                                 payload => $arg, debug => "$err" ));
719                 return undef;
720         }
721
722         if( $action eq 'search' ) {
723                 $self->log(I, "$type.$action : returned ".scalar(@$obj). " result(s)");
724                 $self->event(_mk_not_found($type, $arg)) unless @$obj;
725         }
726
727         if( $action eq 'create' ) {
728                 $self->log(I, "created a new $type object with ID " . $obj->id);
729                 $arg->id($obj->id);
730         }
731
732         $self->data($obj); # cache the data for convenience
733
734         return ($obj) ? $obj : 1;
735 }
736
737
738 sub _mk_not_found {
739         my( $type, $arg ) = @_;
740         (my $t = $type) =~ s/\./_/og;
741         $t = uc($t);
742         return OpenILS::Event->new("${t}_NOT_FOUND", payload => $arg);
743 }
744
745
746
747 # utility method for loading
748 sub __fm2meth { 
749         my $str = shift;
750         my $sep = shift;
751         $str =~ s/Fieldmapper:://o;
752         $str =~ s/::/$sep/g;
753         return $str;
754 }
755
756
757 # -------------------------------------------------------------
758 # Load up the methods from the FM classes
759 # -------------------------------------------------------------
760 my $map = $Fieldmapper::fieldmap;
761 for my $object (keys %$map) {
762         my $obj = __fm2meth($object,'_');
763         my $type = __fm2meth($object, '.');
764
765         my $update = "update_$obj";
766         my $updatef = 
767                 "sub $update {return shift()->runmethod('update', '$type', \@_);}";
768         eval $updatef;
769
770         my $retrieve = "retrieve_$obj";
771         my $retrievef = 
772                 "sub $retrieve {return shift()->runmethod('retrieve', '$type', \@_);}";
773         eval $retrievef;
774
775         my $search = "search_$obj";
776         my $searchf = 
777                 "sub $search {return shift()->runmethod('search', '$type', \@_);}";
778         eval $searchf;
779
780         my $create = "create_$obj";
781         my $createf = 
782                 "sub $create {return shift()->runmethod('create', '$type', \@_);}";
783         eval $createf;
784
785         my $delete = "delete_$obj";
786         my $deletef = 
787                 "sub $delete {return shift()->runmethod('delete', '$type', \@_);}";
788         eval $deletef;
789
790         my $bretrieve = "batch_retrieve_$obj";
791         my $bretrievef = 
792                 "sub $bretrieve {return shift()->runmethod('batch_retrieve', '$type', \@_);}";
793         eval $bretrievef;
794
795         my $retrieveall = "retrieve_all_$obj";
796         my $retrieveallf = 
797                 "sub $retrieveall {return shift()->runmethod('retrieve_all', '$type', \@_);}";
798         eval $retrieveallf;
799 }
800
801 sub json_query {
802     my( $self, $arg, $options ) = @_;
803     $options ||= {};
804         my @arg = ( ref($arg) eq 'ARRAY' ) ? @$arg : ($arg);
805     my $method = $self->app.'.json_query.atomic';
806     $method =~ s/\.atomic$//o if $self->substream($$options{substream} || 0);
807         $self->clear_event;
808     my $obj;
809     my $err;
810     
811     try {
812         $obj = $self->request($method, @arg);
813     } catch Error with { $err = shift; };
814
815     if( $err ) {
816         $self->event(
817             OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
818             payload => $arg, debug => "$err" ));
819         return undef;
820     }
821
822     $self->log(I, "json_query : returned ".scalar(@$obj). " result(s)") if (ref($obj));
823     return $obj;
824 }
825
826
827
828 1;
829
830