]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/CStoreEditor.pm
abcb515692d1e4c4df92c5f09119ce3b105f9d41
[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         return $self->event;
145 }
146
147
148 # -----------------------------------------------------------------------------
149 # Clears the last caught event
150 # -----------------------------------------------------------------------------
151 sub clear_event {
152         my $self = shift;
153         $self->{event} = undef;
154 }
155
156 sub authtoken {
157         my( $self, $auth ) = @_;
158         $self->{authtoken} = $auth if $auth;
159         return $self->{authtoken};
160 }
161
162 sub timeout {
163     my($self, $to) = @_;
164     $self->{timeout} = $to if defined $to;
165     return defined($self->{timeout}) ? $self->{timeout} : 60;
166 }
167
168 # -----------------------------------------------------------------------------
169 # fetches the session, creating if necessary.  If 'xact' is true on this
170 # object, a db session is created
171 # -----------------------------------------------------------------------------
172 sub session {
173         my( $self, $session ) = @_;
174         $self->{session} = $session if $session;
175
176         if(!$self->{session}) {
177                 $self->{session} = OpenSRF::AppSession->create($self->app);
178
179                 if( ! $self->{session} ) {
180                         my $str = "Error creating cstore session with OpenSRF::AppSession->create()!";
181                         $self->log(E, $str);
182                         throw OpenSRF::EX::ERROR ($str);
183                 }
184
185                 $self->{session}->connect if $self->{xact} or $self->{connect} or $always_xact;
186                 $self->xact_start if $self->{xact} or $always_xact;
187         }
188
189     $xact_ed_cache{$self->{xact_id}} = $self if $always_xact;
190         return $self->{session};
191 }
192
193
194 # -----------------------------------------------------------------------------
195 # Starts a storage transaction
196 # -----------------------------------------------------------------------------
197 sub xact_start {
198         my $self = shift;
199         $self->log(D, "starting new db session");
200         my $stat = $self->request($self->app . '.transaction.begin') unless $self->{xact_id};
201         $self->log(E, "error starting database transaction") unless $stat;
202     $self->{xact_id} = $stat;
203         return $stat;
204 }
205
206 # -----------------------------------------------------------------------------
207 # Commits a storage transaction
208 # -----------------------------------------------------------------------------
209 sub xact_commit {
210         my $self = shift;
211         $self->log(D, "comitting db session");
212         my $stat = $self->request($self->app.'.transaction.commit') if $self->{xact_id};
213         $self->log(E, "error comitting database transaction") unless $stat;
214     delete $self->{xact_id};
215         return $stat;
216 }
217
218 # -----------------------------------------------------------------------------
219 # Rolls back a storage stransaction
220 # -----------------------------------------------------------------------------
221 sub xact_rollback {
222         my $self = shift;
223    return unless $self->{session};
224         $self->log(I, "rolling back db session");
225         my $stat = $self->request($self->app.".transaction.rollback") if $self->{xact_id};
226         $self->log(E, "error rolling back database transaction") unless $stat;
227     delete $self->{xact_id};
228         return $stat;
229 }
230
231
232
233 # -----------------------------------------------------------------------------
234 # Rolls back the transaction and disconnects
235 # -----------------------------------------------------------------------------
236 sub rollback {
237         my $self = shift;
238         $self->xact_rollback;
239    delete $self->{xact};
240         $self->disconnect;
241 }
242
243 sub disconnect {
244         my $self = shift;
245         $self->session->disconnect if $self->{session};
246    delete $self->{session};
247 }
248
249
250 # -----------------------------------------------------------------------------
251 # commits the db session and destroys the session
252 # -----------------------------------------------------------------------------
253 sub commit {
254         my $self = shift;
255         return unless $self->{xact_id};
256         $self->xact_commit;
257         $self->session->disconnect;
258         $self->{session} = undef;
259 }
260
261 # -----------------------------------------------------------------------------
262 # clears all object data. Does not commit the db transaction.
263 # -----------------------------------------------------------------------------
264 sub reset {
265         my $self = shift;
266         $self->disconnect;
267         $$self{$_} = undef for (keys %$self);
268 }
269
270
271 # -----------------------------------------------------------------------------
272 # commits and resets
273 # -----------------------------------------------------------------------------
274 sub finish {
275         my $self = shift;
276         $self->commit;
277         $self->reset;
278 }
279
280
281
282 # -----------------------------------------------------------------------------
283 # Does a simple storage request
284 # -----------------------------------------------------------------------------
285 sub request {
286         my( $self, $method, @params ) = @_;
287
288     my $val;
289         my $err;
290         my $argstr = __arg_to_string( (scalar(@params)) == 1 ? $params[0] : \@params);
291         my $locale = $self->session->session_locale;
292
293         $self->log(I, "request $locale $method $argstr");
294
295         if( ($self->{xact} or $always_xact) and 
296                         $self->session->state != OpenSRF::AppSession::CONNECTED() ) {
297                 #$logger->error("CStoreEditor lost it's connection!!");
298                 throw OpenSRF::EX::ERROR ("CStore connection timed out - transaction cannot continue");
299         }
300
301
302         try {
303
304         my $req = $self->session->request($method, @params);
305
306         if($self->substream) {
307             $self->log(D,"running in substream mode");
308             $val = [];
309             while( my $resp = $req->recv(timeout => $self->timeout) ) {
310                 push(@$val, $resp->content) if $resp->content;
311             }
312
313         } else {
314             my $resp = $req->recv(timeout => $self->timeout);
315             if($req->failed) {
316                 $err = $resp;
317                         $self->log(E, "request error $method : $argstr : $err");
318             } else {
319                 $val = $resp->content;
320             }
321         }
322
323         $req->finish;
324
325         } catch Error with {
326                 $err = shift;
327                 $self->log(E, "request error $method : $argstr : $err");
328         };
329
330         throw $err if $err;
331         return $val;
332 }
333
334 sub substream {
335    my( $self, $bool ) = @_;
336    $self->{substream} = $bool if defined $bool;
337    return $self->{substream};
338 }
339
340
341 # -----------------------------------------------------------------------------
342 # Sets / Returns the requestor object.  This is set when checkauth succeeds.
343 # -----------------------------------------------------------------------------
344 sub requestor {
345         my($self, $requestor) = @_;
346         $self->{requestor} = $requestor if $requestor;
347         return $self->{requestor};
348 }
349
350
351
352 # -----------------------------------------------------------------------------
353 # Holds the last data received from a storage call
354 # -----------------------------------------------------------------------------
355 sub data {
356         my( $self, $data ) = @_;
357         $self->{data} = $data if defined $data;
358         return $self->{data};
359 }
360
361
362 # -----------------------------------------------------------------------------
363 # True if this perm has already been checked at this org
364 # -----------------------------------------------------------------------------
365 sub perm_checked {
366         my( $self, $perm, $org ) = @_;
367         $self->{checked_perms}->{$org} = {}
368                 unless $self->{checked_perms}->{$org};
369         my $checked = $self->{checked_perms}->{$org}->{$perm};
370         if(!$checked) {
371                 $self->{checked_perms}->{$org}->{$perm} = 1;
372                 return 0;
373         }
374         return 1;
375 }
376
377
378
379 # -----------------------------------------------------------------------------
380 # Returns true if the requested perm is allowed.  If the perm check fails,
381 # $e->event is set and undef is returned
382 # The perm user is $e->requestor->id and perm org defaults to the requestor's
383 # ws_ou
384 # if perm is an array of perms, method will return true at the first allowed
385 # permission.  If none of the perms are allowed, the perm_failure event
386 # is created with the last perm to fail
387 # -----------------------------------------------------------------------------
388 my $PERM_QUERY = {
389     select => {
390         au => [ {
391             transform => 'permission.usr_has_perm',
392             alias => 'has_perm',
393             column => 'id',
394             params => []
395         } ]
396     },
397     from => 'au',
398     where => {},
399 };
400
401 my $OBJECT_PERM_QUERY = {
402     select => {
403         au => [ {
404             transform => 'permission.usr_has_object_perm',
405             alias => 'has_perm',
406             column => 'id',
407             params => []
408         } ]
409     },
410     from => 'au',
411     where => {},
412 };
413
414 sub allowed {
415         my( $self, $perm, $org, $object, $hint ) = @_;
416         my $uid = $self->requestor->id;
417         $org ||= $self->requestor->ws_ou;
418
419     my $perms = (ref($perm) eq 'ARRAY') ? $perm : [$perm];
420
421     for $perm (@$perms) {
422             $self->log(I, "checking perms user=$uid, org=$org, perm=$perm");
423     
424         if($object) {
425             my $params;
426             if(ref $object) {
427                 # determine the ID field and json_hint from the object
428                 my $id_field = $object->Identity;
429                 $params = [$perm, $object->json_hint, $object->$id_field];
430             } else {
431                 # we were passed an object-id and json_hint
432                 $params = [$perm, $hint, $object];
433             }
434             push(@$params, $org) if $org;
435             $OBJECT_PERM_QUERY->{select}->{au}->[0]->{params} = $params;
436             $OBJECT_PERM_QUERY->{where}->{id} = $uid;
437             return 1 if $U->is_true($self->json_query($OBJECT_PERM_QUERY)->[0]->{has_perm});
438
439         } else {
440             $PERM_QUERY->{select}->{au}->[0]->{params} = [$perm, $org];
441             $PERM_QUERY->{where}->{id} = $uid;
442             return 1 if $U->is_true($self->json_query($PERM_QUERY)->[0]->{has_perm});
443         }
444     }
445
446     # set the perm failure event if the permission check returned false
447         my $e = OpenILS::Event->new('PERM_FAILURE', ilsperm => $perm, ilspermloc => $org);
448         $self->event($e);
449         return undef;
450 }
451
452
453 # -----------------------------------------------------------------------------
454 # Returns the list of object IDs this user has object-specific permissions for
455 # -----------------------------------------------------------------------------
456 sub objects_allowed {
457     my($self, $perm, $obj_type) = @_;
458
459     my $perms = (ref($perm) eq 'ARRAY') ? $perm : [$perm];
460     my @ids;
461
462     for $perm (@$perms) {
463         my $query = {
464             select => {puopm => ['object_id']},
465             from => {
466                 puopm => {
467                     ppl => {field => 'id',fkey => 'perm'}
468                 }
469             },
470             where => {
471                 '+puopm' => {usr => $self->requestor->id, object_type => $obj_type},
472                 '+ppl' => {code => $perm}
473             }
474         };
475     
476         my $list = $self->json_query($query);
477         push(@ids, 0+$_->{object_id}) for @$list;
478     }
479
480    my %trim;
481    $trim{$_} = 1 for @ids;
482    return [ keys %trim ];
483 }
484
485
486 # -----------------------------------------------------------------------------
487 # checks the appropriate perm for the operation
488 # -----------------------------------------------------------------------------
489 sub _checkperm {
490         my( $self, $ptype, $action, $org ) = @_;
491         $org ||= $self->requestor->ws_ou;
492         my $perm = $PERMS{$ptype}{$action};
493         if( $perm ) {
494                 return undef if $self->perm_checked($perm, $org);
495                 return $self->event unless $self->allowed($perm, $org);
496         } else {
497                 $self->log(I, "no perm provided for $ptype.$action");
498         }
499         return undef;
500 }
501
502
503
504 # -----------------------------------------------------------------------------
505 # Logs update actions to the activity log
506 # -----------------------------------------------------------------------------
507 sub log_activity {
508         my( $self, $type, $action, $arg ) = @_;
509         my $str = "$type.$action";
510         $str .= _prop_string($arg);
511         $self->log(A, $str);
512 }
513
514
515
516 sub _prop_string {
517         my $obj = shift;
518         my @props = $obj->properties;
519         my $str = "";
520         for(@props) {
521                 my $prop = $obj->$_() || "";
522                 $prop = substr($prop, 0, 128) . "..." if length $prop > 131;
523                 $str .= " $_=$prop";
524         }
525         return $str;
526 }
527
528
529 sub __arg_to_string {
530         my $arg = shift;
531         return "" unless defined $arg;
532         if( UNIVERSAL::isa($arg, "Fieldmapper") ) {
533                 return (defined $arg->id) ? $arg->id : '<new object>';
534         }
535         return OpenSRF::Utils::JSON->perl2JSON($arg);
536         return "";
537 }
538
539
540 # -----------------------------------------------------------------------------
541 # This does the actual storage query.
542 #
543 # 'search' calls become search_where calls and $arg can be a search hash or
544 # an array-ref of storage search options.  
545 #
546 # 'retrieve' expects an id
547 # 'update' expects an object
548 # 'create' expects an object
549 # 'delete' expects an object
550 #
551 # All methods return true on success and undef on failure.  On failure, 
552 # $e->event is set to the generated event.  
553 # Note: this method assumes that updating a non-changed object and 
554 # thereby receiving a 0 from storage, is a successful update.  
555 #
556 # The method will therefore return true so the caller can just do 
557 # $e->update_blah($x) or return $e->event;
558 # The true value returned from storage for all methods will be stored in 
559 # $e->data, until the next method is called.
560 #
561 # not-found events are generated on retrieve and serach methods.
562 # action=search methods will return [] (==true) if no data is found.  If the
563 # caller is interested in the not found event, they can do:  
564 # return $e->event unless @$results; 
565 # -----------------------------------------------------------------------------
566 sub runmethod {
567         my( $self, $action, $type, $arg, $options ) = @_;
568
569    $options ||= {};
570
571         if( $action eq 'retrieve' ) {
572                 if(! defined($arg) ) {
573                         $self->log(W,"$action $type called with no ID...");
574                         $self->event(_mk_not_found($type, $arg));
575                         return undef;
576                 } elsif( ref($arg) =~ /Fieldmapper/ ) {
577                         $self->log(E,"$action $type called with an object.. attempting ID retrieval..");
578                         $arg = $arg->id;
579                 }
580         }
581
582         my @arg = ( ref($arg) eq 'ARRAY' ) ? @$arg : ($arg);
583         my $method = $self->app.".direct.$type.$action";
584
585         if( $action eq 'search' ) {
586                 $method .= '.atomic';
587
588         } elsif( $action eq 'batch_retrieve' ) {
589                 $action = 'search';
590                 @arg = ( { id => $arg } );
591                 $method =~ s/batch_retrieve/search/o;
592                 $method .= '.atomic';
593
594         } elsif( $action eq 'retrieve_all' ) {
595                 $action = 'search';
596                 $method =~ s/retrieve_all/search/o;
597                 my $tt = $type;
598                 $tt =~ s/\./::/og;
599                 my $fmobj = "Fieldmapper::$tt";
600                 @arg = ( { $fmobj->Identity => { '!=' => undef } } );
601                 $method .= '.atomic';
602         }
603
604         $method =~ s/search/id_list/o if $options->{idlist};
605
606     $method =~ s/\.atomic$//o if $self->substream($$options{substream} || 0);
607     $self->timeout($$options{timeout});
608
609         # remove any stale events
610         $self->clear_event;
611
612         if( $action eq 'update' or $action eq 'delete' or $action eq 'create' ) {
613                 if(!($self->{xact} or $always_xact)) {
614                         $logger->error("Attempt to update DB while not in a transaction : $method");
615                         throw OpenSRF::EX::ERROR ("Attempt to update DB while not in a transaction : $method");
616                 }
617                 $self->log_activity($type, $action, $arg);
618         }
619
620         if($$options{checkperm}) {
621                 my $a = ($action eq 'search') ? 'retrieve' : $action;
622                 my $e = $self->_checkperm($type, $a, $$options{permorg});
623                 if($e) {
624                         $self->event($e);
625                         return undef;
626                 }
627         }
628
629         my $obj; 
630         my $err;
631
632         try {
633                 $obj = $self->request($method, @arg);
634         } catch Error with { $err = shift; };
635         
636
637         if(!defined $obj) {
638                 $self->log(I, "request returned no data : $method");
639
640                 if( $action eq 'retrieve' ) {
641                         $self->event(_mk_not_found($type, $arg));
642
643                 } elsif( $action eq 'update' or 
644                                 $action eq 'delete' or $action eq 'create' ) {
645                         my $evt = OpenILS::Event->new(
646                                 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
647                         $self->event($evt);
648                 }
649
650                 if( $err ) {
651                         $self->event( 
652                                 OpenILS::Event->new( 'DATABASE_QUERY_FAILED', 
653                                         payload => $arg, debug => "$err" ));
654                         return undef;
655                 }
656
657                 return undef;
658         }
659
660         if( $action eq 'create' and $obj == 0 ) {
661                 my $evt = OpenILS::Event->new(
662                         'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
663                 $self->event($evt);
664                 return undef;
665         }
666
667         # If we havn't dealt with the error in a nice way, go ahead and throw it
668         if( $err ) {
669                 $self->event( 
670                         OpenILS::Event->new( 'DATABASE_QUERY_FAILED', 
671                                 payload => $arg, debug => "$err" ));
672                 return undef;
673         }
674
675         if( $action eq 'search' ) {
676                 $self->log(I, "$type.$action : returned ".scalar(@$obj). " result(s)");
677                 $self->event(_mk_not_found($type, $arg)) unless @$obj;
678         }
679
680         if( $action eq 'create' ) {
681                 $self->log(I, "created a new $type object with ID " . $obj->id);
682                 $arg->id($obj->id);
683         }
684
685         $self->data($obj); # cache the data for convenience
686
687         return ($obj) ? $obj : 1;
688 }
689
690
691 sub _mk_not_found {
692         my( $type, $arg ) = @_;
693         (my $t = $type) =~ s/\./_/og;
694         $t = uc($t);
695         return OpenILS::Event->new("${t}_NOT_FOUND", payload => $arg);
696 }
697
698
699
700 # utility method for loading
701 sub __fm2meth { 
702         my $str = shift;
703         my $sep = shift;
704         $str =~ s/Fieldmapper:://o;
705         $str =~ s/::/$sep/g;
706         return $str;
707 }
708
709
710 # -------------------------------------------------------------
711 # Load up the methods from the FM classes
712 # -------------------------------------------------------------
713 my $map = $Fieldmapper::fieldmap;
714 for my $object (keys %$map) {
715         my $obj = __fm2meth($object,'_');
716         my $type = __fm2meth($object, '.');
717
718         my $update = "update_$obj";
719         my $updatef = 
720                 "sub $update {return shift()->runmethod('update', '$type', \@_);}";
721         eval $updatef;
722
723         my $retrieve = "retrieve_$obj";
724         my $retrievef = 
725                 "sub $retrieve {return shift()->runmethod('retrieve', '$type', \@_);}";
726         eval $retrievef;
727
728         my $search = "search_$obj";
729         my $searchf = 
730                 "sub $search {return shift()->runmethod('search', '$type', \@_);}";
731         eval $searchf;
732
733         my $create = "create_$obj";
734         my $createf = 
735                 "sub $create {return shift()->runmethod('create', '$type', \@_);}";
736         eval $createf;
737
738         my $delete = "delete_$obj";
739         my $deletef = 
740                 "sub $delete {return shift()->runmethod('delete', '$type', \@_);}";
741         eval $deletef;
742
743         my $bretrieve = "batch_retrieve_$obj";
744         my $bretrievef = 
745                 "sub $bretrieve {return shift()->runmethod('batch_retrieve', '$type', \@_);}";
746         eval $bretrievef;
747
748         my $retrieveall = "retrieve_all_$obj";
749         my $retrieveallf = 
750                 "sub $retrieveall {return shift()->runmethod('retrieve_all', '$type', \@_);}";
751         eval $retrieveallf;
752 }
753
754 sub json_query {
755     my( $self, $arg, $options ) = @_;
756     $options ||= {};
757         my @arg = ( ref($arg) eq 'ARRAY' ) ? @$arg : ($arg);
758     my $method = $self->app.'.json_query.atomic';
759     $method =~ s/\.atomic$//o if $self->substream($$options{substream} || 0);
760         $self->clear_event;
761     my $obj;
762     my $err;
763     
764     try {
765         $obj = $self->request($method, @arg);
766     } catch Error with { $err = shift; };
767
768     if( $err ) {
769         $self->event(
770             OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
771             payload => $arg, debug => "$err" ));
772         return undef;
773     }
774
775     $self->log(I, "json_query : returned ".scalar(@$obj). " result(s)") if (ref($obj));
776     return $obj;
777 }
778
779
780
781 1;
782
783