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;
9 use OpenSRF::Utils::JSON;
10 use OpenSRF::Utils::Logger qw($logger);
11 my $U = "OpenILS::Application::AppUtils";
16 # if set, we will use this locale for all new sessions
17 # if unset, we rely on the existing opensrf locale propagation
24 # 'biblio.record_entry' => { update => 'UPDATE_MARC' },
25 # 'asset.copy' => { update => 'UPDATE_COPY'},
26 # 'asset.call_number' => { update => 'UPDATE_VOLUME'},
27 # 'action.circulation' => { retrieve => 'VIEW_CIRCULATIONS'},
30 sub flush_forced_xacts {
31 for my $k ( keys %xact_ed_cache ) {
33 $xact_ed_cache{$k}->rollback;
37 delete $xact_ed_cache{$k};
41 # -----------------------------------------------------------------------------
42 # Export some useful functions
43 # -----------------------------------------------------------------------------
44 use vars qw(@EXPORT_OK %EXPORT_TAGS);
46 use base qw/Exporter/;
47 push @EXPORT_OK, ( 'new_editor', 'new_rstore_editor' );
48 %EXPORT_TAGS = ( funcs => [ qw/ new_editor new_rstore_editor / ] );
50 sub new_editor { return OpenILS::Utils::CStoreEditor->new(@_); }
52 sub new_rstore_editor {
53 my $e = OpenILS::Utils::CStoreEditor->new(@_);
54 $e->app('open-ils.reporter-store');
59 # -----------------------------------------------------------------------------
61 # -----------------------------------------------------------------------------
62 use constant E => 'error';
63 use constant W => 'warn';
64 use constant I => 'info';
65 use constant D => 'debug';
66 use constant A => 'activity';
70 # -----------------------------------------------------------------------------
72 # xact=><true> : creates a storage transaction
73 # authtoken=>$token : the login session key
74 # -----------------------------------------------------------------------------
76 my( $class, %params ) = @_;
77 $class = ref($class) || $class;
78 my $self = bless( \%params, $class );
79 $self->{checked_perms} = {};
90 my( $self, $app ) = @_;
91 $self->{app} = $app if $app;
92 $self->{app} = 'open-ils.cstore' unless $self->{app};
97 # -----------------------------------------------------------------------------
98 # Log the editor metadata along with the log string
99 # -----------------------------------------------------------------------------
101 my( $self, $lev, $str ) = @_;
105 } elsif ($self->{xact}) {
110 $s .= "0" unless $self->requestor;
111 $s .= $self->requestor->id if $self->requestor;
113 $logger->$lev("$s $str");
116 # -----------------------------------------------------------------------------
117 # Verifies the auth token and fetches the requestor object
118 # -----------------------------------------------------------------------------
121 $self->log(D, "checking auth token ".$self->authtoken);
123 my $content = $U->simplereq(
125 'open-ils.auth.session.retrieve', $self->authtoken, 1);
127 if(!$content or $U->event_code($content)) {
128 $self->event( ($content) ? $content : OpenILS::Event->new('NO_SESSION'));
132 $self->{authtime} = $content->{authtime};
133 return $self->{requestor} = $content->{userobj};
140 $cache = OpenSRF::Utils::Cache->new('global') unless $cache;
141 $self->log(D, "checking cached auth token ".$self->authtoken);
142 my $user = $cache->get_cache("oils_auth_".$self->authtoken);
143 return $self->{requestor} = $user->{userobj} if $user;
144 $self->event(OpenILS::Event->new('NO_SESSION'));
151 # -----------------------------------------------------------------------------
152 # Returns the last generated event
153 # -----------------------------------------------------------------------------
155 my( $self, $evt ) = @_;
156 $self->{event} = $evt if $evt;
157 return $self->{event};
160 # -----------------------------------------------------------------------------
161 # Destroys the transaction and disconnects where necessary,
162 # then returns the last event that occurred
163 # -----------------------------------------------------------------------------
174 # -----------------------------------------------------------------------------
175 # Clears the last caught event
176 # -----------------------------------------------------------------------------
179 $self->{event} = undef;
183 my($self, $died) = @_;
184 $self->{died} = $died if defined $died;
185 return $self->{died};
189 my( $self, $auth ) = @_;
190 $self->{authtoken} = $auth if $auth;
191 return $self->{authtoken};
195 my( $self, $auth ) = @_;
196 $self->{authtime} = $auth if $auth;
197 return $self->{authtime};
202 $self->{timeout} = $to if defined $to;
203 return defined($self->{timeout}) ? $self->{timeout} : 60;
206 # -----------------------------------------------------------------------------
207 # fetches the session, creating if necessary. If 'xact' is true on this
208 # object, a db session is created
209 # -----------------------------------------------------------------------------
211 my( $self, $session ) = @_;
212 $self->{session} = $session if $session;
214 # sessions can stick around longer than a single request/transaction.
215 # kill it if our default locale was altered since the last request
216 # and it does not match the locale of the existing session.
217 delete $self->{session} if
220 $self->{session}->session_locale ne $default_locale;
222 if(!$self->{session}) {
223 $self->{session} = OpenSRF::AppSession->create($self->app);
224 $self->{session}->session_locale($default_locale) if $default_locale;
226 if( ! $self->{session} ) {
227 my $str = "Error creating cstore session with OpenSRF::AppSession->create()!";
229 throw OpenSRF::EX::ERROR ($str);
232 $self->{session}->connect if $self->{xact} or $self->{connect} or $always_xact;
233 $self->xact_begin if $self->{xact} or $always_xact;
236 $xact_ed_cache{$self->{xact_id}} = $self if $always_xact and $self->{xact_id};
237 return $self->{session};
241 # -----------------------------------------------------------------------------
242 # Starts a storage transaction
243 # -----------------------------------------------------------------------------
246 return $self->{xact_id} if $self->{xact_id};
247 $self->session->connect unless $self->session->state == OpenSRF::AppSession::CONNECTED();
248 $self->log(D, "starting new database transaction");
249 unless($self->{xact_id}) {
250 my $stat = $self->request($self->app . '.transaction.begin');
251 $self->log(E, "error starting database transaction") unless $stat;
252 $self->{xact_id} = $stat;
253 if($self->authtoken) {
254 if(!$self->requestor) {
259 if($self->requestor) {
260 $user_id = $self->requestor->id;
261 $ws_id = $self->requestor->wsid;
263 $self->request($self->app . '.set_audit_info', $self->authtoken, $user_id, $ws_id);
267 return $self->{xact_id};
270 # -----------------------------------------------------------------------------
271 # Commits a storage transaction
272 # -----------------------------------------------------------------------------
275 return unless $self->{xact_id};
276 $self->log(D, "comitting db session");
277 my $stat = $self->request($self->app.'.transaction.commit');
278 $self->log(E, "error comitting database transaction") unless $stat;
279 delete $self->{xact_id};
280 delete $self->{xact};
284 # -----------------------------------------------------------------------------
285 # Rolls back a storage stransaction
286 # -----------------------------------------------------------------------------
289 return unless $self->{session} and $self->{xact_id};
290 $self->log(I, "rolling back db session");
291 my $stat = $self->request($self->app.".transaction.rollback");
292 $self->log(E, "error rolling back database transaction") unless $stat;
293 delete $self->{xact_id};
294 delete $self->{xact};
299 # -----------------------------------------------------------------------------
300 # Savepoint functions. If no savepoint name is provided, the same name is used
301 # for each successive savepoint, in which case only the last savepoint set can
302 # be released or rolled back.
303 # -----------------------------------------------------------------------------
306 my $name = shift || 'savepoint';
307 return unless $self->{session} and $self->{xact_id};
308 $self->log(I, "setting savepoint '$name'");
309 my $stat = $self->request($self->app.".savepoint.set", $name)
310 or $self->log(E, "error setting savepoint '$name'");
314 sub release_savepoint {
316 my $name = shift || 'savepoint';
317 return unless $self->{session} and $self->{xact_id};
318 $self->log(I, "releasing savepoint '$name'");
319 my $stat = $self->request($self->app.".savepoint.release", $name)
320 or $self->log(E, "error releasing savepoint '$name'");
324 sub rollback_savepoint {
326 my $name = shift || 'savepoint';
327 return unless $self->{session} and $self->{xact_id};
328 $self->log(I, "rollback savepoint '$name'");
329 my $stat = $self->request($self->app.".savepoint.rollback", $name)
330 or $self->log(E, "error rolling back savepoint '$name'");
335 # -----------------------------------------------------------------------------
336 # Rolls back the transaction and disconnects
337 # -----------------------------------------------------------------------------
343 $self->xact_rollback;
347 $ret = $self->disconnect
349 throw $err if ($err);
355 $self->session->disconnect if
357 $self->{session}->state == OpenSRF::AppSession::CONNECTED();
358 delete $self->{session};
362 # -----------------------------------------------------------------------------
363 # commits the db session and destroys the session
364 # returns the status of the commit call
365 # -----------------------------------------------------------------------------
368 return unless $self->{xact_id};
369 my $stat = $self->xact_commit;
374 # -----------------------------------------------------------------------------
375 # clears all object data. Does not commit the db transaction.
376 # -----------------------------------------------------------------------------
380 $$self{$_} = undef for (keys %$self);
384 # -----------------------------------------------------------------------------
386 # -----------------------------------------------------------------------------
398 throw $err if ($err);
404 # -----------------------------------------------------------------------------
405 # Does a simple storage request
406 # -----------------------------------------------------------------------------
408 my( $self, $method, @params ) = @_;
412 my $argstr = __arg_to_string( (scalar(@params)) == 1 ? $params[0] : \@params);
413 my $locale = $self->session->session_locale;
415 $self->log(I, "request $locale $method $argstr");
417 if( ($self->{xact} or $always_xact) and
418 $self->session->state != OpenSRF::AppSession::CONNECTED() ) {
419 #$logger->error("CStoreEditor lost it's connection!!");
420 throw OpenSRF::EX::ERROR ("CStore connection timed out - transaction cannot continue");
426 my $req = $self->session->request($method, @params);
428 if($self->substream) {
429 $self->log(D,"running in substream mode");
431 while( my $resp = $req->recv(timeout => $self->timeout) ) {
432 push(@$val, $resp->content) if $resp->content and not $self->discard;
436 my $resp = $req->recv(timeout => $self->timeout);
439 $self->log(E, "request error $method : $argstr : $err");
441 $val = $resp->content if $resp;
449 $self->log(E, "request error $method : $argstr : $err");
457 my( $self, $bool ) = @_;
458 $self->{substream} = $bool if defined $bool;
459 return $self->{substream};
462 # -----------------------------------------------------------------------------
463 # discard response data instead of returning it to the caller. currently only
464 # works in conjunction with substream mode.
465 # -----------------------------------------------------------------------------
467 my( $self, $bool ) = @_;
468 $self->{discard} = $bool if defined $bool;
469 return $self->{discard};
473 # -----------------------------------------------------------------------------
474 # Sets / Returns the requestor object. This is set when checkauth succeeds.
475 # -----------------------------------------------------------------------------
477 my($self, $requestor) = @_;
478 $self->{requestor} = $requestor if $requestor;
479 return $self->{requestor};
484 # -----------------------------------------------------------------------------
485 # Holds the last data received from a storage call
486 # -----------------------------------------------------------------------------
488 my( $self, $data ) = @_;
489 $self->{data} = $data if defined $data;
490 return $self->{data};
494 # -----------------------------------------------------------------------------
495 # True if this perm has already been checked at this org
496 # -----------------------------------------------------------------------------
498 my( $self, $perm, $org ) = @_;
499 $self->{checked_perms}->{$org} = {}
500 unless $self->{checked_perms}->{$org};
501 my $checked = $self->{checked_perms}->{$org}->{$perm};
503 $self->{checked_perms}->{$org}->{$perm} = 1;
511 # -----------------------------------------------------------------------------
512 # Returns true if the requested perm is allowed. If the perm check fails,
513 # $e->event is set and undef is returned
514 # The perm user is $e->requestor->id and perm org defaults to the requestor's
516 # if perm is an array of perms, method will return true at the first allowed
517 # permission. If none of the perms are allowed, the perm_failure event
518 # is created with the last perm to fail
519 # -----------------------------------------------------------------------------
523 transform => 'permission.usr_has_perm',
533 my $OBJECT_PERM_QUERY = {
536 transform => 'permission.usr_has_object_perm',
547 my( $self, $perm, $org, $object, $hint ) = @_;
548 my $uid = $self->requestor->id;
549 $org ||= $self->requestor->ws_ou;
551 my $perms = (ref($perm) eq 'ARRAY') ? $perm : [$perm];
553 for $perm (@$perms) {
554 $self->log(I, "checking perms user=$uid, org=$org, perm=$perm");
559 # determine the ID field and json_hint from the object
560 my $id_field = $object->Identity;
561 $params = [$perm, $object->json_hint, $object->$id_field];
563 # we were passed an object-id and json_hint
564 $params = [$perm, $hint, $object];
566 push(@$params, $org) if $org;
567 $OBJECT_PERM_QUERY->{select}->{au}->[0]->{params} = $params;
568 $OBJECT_PERM_QUERY->{where}->{id} = $uid;
569 return 1 if $U->is_true($self->json_query($OBJECT_PERM_QUERY)->[0]->{has_perm});
572 $PERM_QUERY->{select}->{au}->[0]->{params} = [$perm, $org];
573 $PERM_QUERY->{where}->{id} = $uid;
574 return 1 if $U->is_true($self->json_query($PERM_QUERY)->[0]->{has_perm});
578 # set the perm failure event if the permission check returned false
579 my $e = OpenILS::Event->new('PERM_FAILURE', ilsperm => $perm, ilspermloc => $org);
585 # -----------------------------------------------------------------------------
586 # Returns the list of object IDs this user has object-specific permissions for
587 # -----------------------------------------------------------------------------
588 sub objects_allowed {
589 my($self, $perm, $obj_type) = @_;
591 my $perms = (ref($perm) eq 'ARRAY') ? $perm : [$perm];
594 for $perm (@$perms) {
596 select => {puopm => ['object_id']},
599 ppl => {field => 'id',fkey => 'perm'}
603 '+puopm' => {usr => $self->requestor->id, object_type => $obj_type},
604 '+ppl' => {code => $perm}
608 my $list = $self->json_query($query);
609 push(@ids, 0+$_->{object_id}) for @$list;
613 $trim{$_} = 1 for @ids;
614 return [ keys %trim ];
618 # -----------------------------------------------------------------------------
619 # checks the appropriate perm for the operation
620 # -----------------------------------------------------------------------------
622 my( $self, $ptype, $action, $org ) = @_;
623 $org ||= $self->requestor->ws_ou;
624 my $perm = $PERMS{$ptype}{$action};
626 return undef if $self->perm_checked($perm, $org);
627 return $self->event unless $self->allowed($perm, $org);
629 $self->log(I, "no perm provided for $ptype.$action");
636 # -----------------------------------------------------------------------------
637 # Logs update actions to the activity log
638 # -----------------------------------------------------------------------------
640 my( $self, $type, $action, $arg ) = @_;
641 my $str = "$type.$action";
642 $str .= _prop_string($arg);
650 my @props = $obj->properties;
653 my $prop = $obj->$_() || "";
654 $prop = substr($prop, 0, 128) . "..." if length $prop > 131;
661 sub __arg_to_string {
663 return "" unless defined $arg;
664 if( UNIVERSAL::isa($arg, "Fieldmapper") ) {
665 my $idf = $arg->Identity;
666 return (defined $arg->$idf) ? $arg->$idf : '<new object>';
668 return OpenSRF::Utils::JSON->perl2JSON($arg);
673 # -----------------------------------------------------------------------------
674 # This does the actual storage query.
676 # 'search' calls become search_where calls and $arg can be a search hash or
677 # an array-ref of storage search options.
679 # 'retrieve' expects an id
680 # 'update' expects an object
681 # 'create' expects an object
682 # 'delete' expects an object
684 # All methods return true on success and undef on failure. On failure,
685 # $e->event is set to the generated event.
686 # Note: this method assumes that updating a non-changed object and
687 # thereby receiving a 0 from storage, is a successful update.
689 # The method will therefore return true so the caller can just do
690 # $e->update_blah($x) or return $e->event;
691 # The true value returned from storage for all methods will be stored in
692 # $e->data, until the next method is called.
694 # not-found events are generated on retrieve and serach methods.
695 # action=search methods will return [] (==true) if no data is found. If the
696 # caller is interested in the not found event, they can do:
697 # return $e->event unless @$results;
698 # -----------------------------------------------------------------------------
700 my( $self, $action, $type, $arg, $options ) = @_;
704 if( $action eq 'retrieve' ) {
705 if(! defined($arg) ) {
706 $self->log(W,"$action $type called with no ID...");
707 $self->event(_mk_not_found($type, $arg));
709 } elsif( ref($arg) =~ /Fieldmapper/ ) {
710 $self->log(D,"$action $type called with an object.. attempting Identity retrieval..");
711 my $idf = $arg->Identity;
716 my @arg = ( ref($arg) eq 'ARRAY' ) ? @$arg : ($arg);
717 my $method = $self->app.".direct.$type.$action";
719 if( $action eq 'search' ) {
720 $method .= '.atomic';
722 } elsif( $action eq 'batch_retrieve' ) {
724 $method =~ s/batch_retrieve/search/o;
725 $method .= '.atomic';
728 my $fmobj = "Fieldmapper::$tt";
729 my $ident_field = $fmobj->Identity;
731 if (ref $arg[0] eq 'ARRAY') {
732 # $arg looks like: ([1, 2, 3], {search_args})
733 @arg = ( { $ident_field => $arg[0] }, @arg[1 .. $#arg] );
735 # $arg looks like: [1, 2, 3]
736 @arg = ( { $ident_field => $arg } );
739 } elsif( $action eq 'retrieve_all' ) {
741 $method =~ s/retrieve_all/search/o;
744 my $fmobj = "Fieldmapper::$tt";
745 @arg = ( { $fmobj->Identity => { '!=' => undef } } );
746 $method .= '.atomic';
749 $method =~ s/search/id_list/o if $options->{idlist};
751 $method =~ s/\.atomic$//o if $self->substream($$options{substream} || 0);
752 $self->timeout($$options{timeout});
753 $self->discard($$options{discard});
755 # remove any stale events
758 if( $action eq 'update' or $action eq 'delete' or $action eq 'create' ) {
759 if(!($self->{xact} or $always_xact)) {
760 $logger->error("Attempt to update DB while not in a transaction : $method");
761 throw OpenSRF::EX::ERROR ("Attempt to update DB while not in a transaction : $method");
763 $self->log_activity($type, $action, $arg);
766 if($$options{checkperm}) {
767 my $a = ($action eq 'search') ? 'retrieve' : $action;
768 my $e = $self->_checkperm($type, $a, $$options{permorg});
779 $obj = $self->request($method, @arg);
780 } catch Error with { $err = shift; };
784 $self->log(I, "request returned no data : $method");
786 if( $action eq 'retrieve' ) {
787 $self->event(_mk_not_found($type, $arg));
789 } elsif( $action eq 'update' or
790 $action eq 'delete' or $action eq 'create' ) {
791 my $evt = OpenILS::Event->new(
792 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
798 OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
799 payload => $arg, debug => "$err" ));
806 if( $action eq 'create' and $obj == 0 ) {
807 my $evt = OpenILS::Event->new(
808 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
813 # If we havn't dealt with the error in a nice way, go ahead and throw it
816 OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
817 payload => $arg, debug => "$err" ));
821 if( $action eq 'search' ) {
822 $self->log(I, "$type.$action : returned ".scalar(@$obj). " result(s)");
823 $self->event(_mk_not_found($type, $arg)) unless @$obj;
826 if( $action eq 'create' ) {
827 my $idf = $obj->Identity;
828 $self->log(I, "created a new $type object with Identity " . $obj->$idf);
829 $arg->$idf($obj->$idf);
832 $self->data($obj); # cache the data for convenience
834 return ($obj) ? $obj : 1;
839 my( $type, $arg ) = @_;
840 (my $t = $type) =~ s/\./_/og;
842 return OpenILS::Event->new("${t}_NOT_FOUND", payload => $arg);
847 # utility method for loading
851 $str =~ s/Fieldmapper:://o;
857 # -------------------------------------------------------------
858 # Load up the methods from the FM classes
859 # -------------------------------------------------------------
862 no warnings; # Here we potentially redefine subs via eval
863 my $map = $Fieldmapper::fieldmap;
864 for my $object (keys %$map) {
865 my $obj = __fm2meth($object, '_');
866 my $type = __fm2meth($object, '.');
867 foreach my $command (qw/ update retrieve search create delete batch_retrieve retrieve_all /) {
868 eval "sub ${command}_$obj {return shift()->runmethod('$command', '$type', \@_);}\n";
870 # TODO: performance test against concatenating a big string of all the subs and eval'ing only ONCE.
874 init(); # Add very many subs to this namespace
877 my( $self, $arg, $options ) = @_;
879 my @arg = ( ref($arg) eq 'ARRAY' ) ? @$arg : ($arg);
880 my $method = $self->app.'.json_query.atomic';
881 $method =~ s/\.atomic$//o if $self->substream($$options{substream} || 0);
883 $self->timeout($$options{timeout});
884 $self->discard($$options{discard});
890 $obj = $self->request($method, @arg);
891 } catch Error with { $err = shift; };
895 OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
896 payload => $arg, debug => "$err" ));
900 $self->log(I, "json_query : returned ".scalar(@$obj). " result(s)") if (ref($obj));