1 use strict; use warnings;
2 package OpenILS::Utils::Editor;
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";
14 # -----------------------------------------------------------------------------
15 # Export some useful functions
16 # -----------------------------------------------------------------------------
17 use vars qw(@EXPORT_OK %EXPORT_TAGS);
19 use base qw/Exporter/;
20 push @EXPORT_OK, 'new_editor';
21 %EXPORT_TAGS = ( funcs => [ qw/ new_editor / ] );
23 sub new_editor { return OpenILS::Utils::Editor->new(@_); }
26 # -----------------------------------------------------------------------------
27 # These need to be auto-generated
28 # -----------------------------------------------------------------------------
30 'biblio.record_entry' => { update => 'UPDATE_MARC' },
31 'asset.copy' => { update => 'UPDATE_COPY' },
32 'asset.call_number' => { update => 'UPDATE_VOLUME' },
33 'action.circulation' => { retrieve => 'VIEW_CIRCULATIONS' },
36 use constant E => 'error';
37 use constant W => 'warn';
38 use constant I => 'info';
39 use constant D => 'debug';
40 use constant A => 'activity';
44 # -----------------------------------------------------------------------------
46 # xact=><true> : creates a storage transaction
47 # authtoken=>$token : the login session key
48 # -----------------------------------------------------------------------------
50 my( $class, %params ) = @_;
51 $class = ref($class) || $class;
52 my $self = bless( \%params, $class );
53 $self->{checked_perms} = {};
57 # -----------------------------------------------------------------------------
58 # Log the editor metadata along with the log string
59 # -----------------------------------------------------------------------------
61 my( $self, $lev, $str ) = @_;
63 $s .= "0|" unless $self->{xact};
64 $s .= "1|" if $self->{xact};
65 $s .= "0" unless $self->requestor;
66 $s .= $self->requestor->id if $self->requestor;
68 $logger->$lev("$s $str");
71 # -----------------------------------------------------------------------------
72 # Verifies the auth token and fetches the requestor object
73 # -----------------------------------------------------------------------------
76 $self->log(D, "checking auth token ".$self->authtoken);
77 my ($reqr, $evt) = $U->checkses($self->authtoken);
78 $self->event($evt) if $evt;
79 return $self->{requestor} = $reqr;
83 # -----------------------------------------------------------------------------
84 # Returns the last generated event
85 # -----------------------------------------------------------------------------
87 my( $self, $evt ) = @_;
88 $self->{event} = $evt if $evt;
89 return $self->{event};
92 # -----------------------------------------------------------------------------
93 # Clears the last caught event
94 # -----------------------------------------------------------------------------
97 $self->{event} = undef;
101 my( $self, $auth ) = @_;
102 $self->{authtoken} = $auth if $auth;
103 return $self->{authtoken};
106 # -----------------------------------------------------------------------------
107 # fetches the session, creating if necessary. If 'xact' is true on this
108 # object, a db session is created
109 # -----------------------------------------------------------------------------
111 my( $self, $session ) = @_;
112 $self->{session} = $session if $session;
114 if(!$self->{session}) {
115 $self->{session} = OpenSRF::AppSession->create('open-ils.storage');
117 if( ! $self->{session} ) {
118 my $str = "Error creating storage session with OpenSRF::AppSession->create()!";
120 throw OpenSRF::EX::ERROR ($str);
123 $self->{session}->connect if $self->{xact} or $self->{connect};
124 $self->xact_start if $self->{xact};
126 return $self->{session};
130 # -----------------------------------------------------------------------------
131 # Starts a storage transaction
132 # -----------------------------------------------------------------------------
135 $self->log(D, "starting new db session");
136 my $stat = $self->request('open-ils.storage.transaction.begin');
137 $self->log(E, "error starting database transaction") unless $stat;
138 if($self->authtoken) {
139 if(!$self->requestor) {
144 if($self->requestor) {
145 $user_id = $self->requestor->id;
146 $ws_id = $self->requestor->wsid;
148 $self->request('open-ils.storage.set_audit_info', $self->authtoken, $user_id, $ws_id);
153 # -----------------------------------------------------------------------------
154 # Commits a storage transaction
155 # -----------------------------------------------------------------------------
158 $self->log(D, "comitting db session");
159 my $stat = $self->request('open-ils.storage.transaction.commit');
160 $self->log(E, "error comitting database transaction") unless $stat;
164 # -----------------------------------------------------------------------------
165 # Rolls back a storage stransaction
166 # -----------------------------------------------------------------------------
169 $self->log(I, "rolling back db session");
170 return $self->request("open-ils.storage.transaction.rollback");
174 # -----------------------------------------------------------------------------
175 # commits the db session and destroys the session
176 # -----------------------------------------------------------------------------
179 return unless $self->{xact};
181 $self->session->disconnect;
182 $self->{session} = undef;
185 # -----------------------------------------------------------------------------
186 # clears all object data. Does not commit the db transaction.
187 # -----------------------------------------------------------------------------
190 $self->session->disconnect if $self->{session};
191 $$self{$_} = undef for (keys %$self);
195 # -----------------------------------------------------------------------------
197 # -----------------------------------------------------------------------------
206 # -----------------------------------------------------------------------------
207 # Does a simple storage request
208 # -----------------------------------------------------------------------------
210 my( $self, $method, @params ) = @_;
214 my $argstr = __arg_to_string( (scalar(@params)) == 1 ? $params[0] : \@params);
216 $self->log(I, "request $method : $argstr");
219 $val = $self->session->request($method, @params)->gather(1);
223 $self->log(E, "request error $method : $argstr : $err");
231 # -----------------------------------------------------------------------------
232 # Sets / Returns the requstor object. This is set when checkauth succeeds.
233 # -----------------------------------------------------------------------------
235 my($self, $requestor) = @_;
236 $self->{requestor} = $requestor if $requestor;
237 return $self->{requestor};
242 # -----------------------------------------------------------------------------
243 # Holds the last data received from a storage call
244 # -----------------------------------------------------------------------------
246 my( $self, $data ) = @_;
247 $self->{data} = $data if defined $data;
248 return $self->{data};
252 # -----------------------------------------------------------------------------
253 # True if this perm has already been checked at this org
254 # -----------------------------------------------------------------------------
256 my( $self, $perm, $org ) = @_;
257 $self->{checked_perms}->{$org} = {}
258 unless $self->{checked_perms}->{$org};
259 my $checked = $self->{checked_perms}->{$org}->{$perm};
261 $self->{checked_perms}->{$org}->{$perm} = 1;
269 # -----------------------------------------------------------------------------
270 # Returns true if the requested perm is allowed. If the perm check fails,
271 # $e->event is set and undef is returned
272 # The perm user is $e->requestor->id and perm org defaults to the requestor's
274 # If this perm at the given org has already been verified, true is returned
275 # and the perm is not re-checked
276 # -----------------------------------------------------------------------------
278 my( $self, $perm, $org ) = @_;
279 my $uid = $self->requestor->id;
280 $org ||= $self->requestor->ws_ou;
281 $self->log(I, "checking perms user=$uid, org=$org, perm=$perm");
282 return 1 if $self->perm_checked($perm, $org);
283 return $self->checkperm($uid, $org, $perm);
287 my($self, $userid, $org, $perm) = @_;
288 my $s = $self->request(
289 "open-ils.storage.permission.user_has_perm", $userid, $perm, $org );
292 my $e = OpenILS::Event->new('PERM_FAILURE', ilsperm => $perm, ilspermloc => $org);
302 # -----------------------------------------------------------------------------
303 # checks the appropriate perm for the operation
304 # -----------------------------------------------------------------------------
306 my( $self, $ptype, $action, $org ) = @_;
307 $org ||= $self->requestor->ws_ou;
308 my $perm = $PERMS{$ptype}{$action};
310 return undef if $self->perm_checked($perm, $org);
311 return $self->event unless $self->allowed($perm, $org);
313 $self->log(E, "no perm provided for $ptype.$action");
320 # -----------------------------------------------------------------------------
321 # Logs update actions to the activity log
322 # -----------------------------------------------------------------------------
324 my( $self, $type, $action, $arg ) = @_;
325 my $str = "$type.$action";
326 $str .= _prop_string($arg);
334 my @props = $obj->properties;
337 my $prop = $obj->$_() || "";
338 $prop = substr($prop, 0, 128) . "..." if length $prop > 131;
345 sub __arg_to_string {
347 return "" unless defined $arg;
348 return $arg->id if UNIVERSAL::isa($arg, "Fieldmapper");
349 return OpenSRF::Utils::JSON->perl2JSON($arg);
353 # -----------------------------------------------------------------------------
354 # This does the actual storage query.
356 # 'search' calls become search_where calls and $arg can be a search hash or
357 # an array-ref of storage search options.
359 # 'retrieve' expects an id
360 # 'update' expects an object
361 # 'create' expects an object
362 # 'delete' expects an object
364 # All methods return true on success and undef on failure. On failure,
365 # $e->event is set to the generated event.
366 # Note: this method assumes that updating a non-changed object and
367 # thereby receiving a 0 from storage, is a successful update.
369 # The method will therefore return true so the caller can just do
370 # $e->update_blah($x) or return $e->event;
371 # The true value returned from storage for all methods will be stored in
372 # $e->data, until the next method is called.
374 # not-found events are generated on retrieve and serach methods.
375 # action=search methods will return [] (==true) if no data is found. If the
376 # caller is interested in the not found event, they can do:
377 # return $e->event unless @$results;
378 # -----------------------------------------------------------------------------
380 my( $self, $action, $type, $arg, $options ) = @_;
383 my $method = "open-ils.storage.direct.$type.$action";
385 if( $action eq 'search' ) {
386 $method =~ s/search/search_where/o;
387 $method =~ s/direct/id_list/o if $options->{idlist};
388 $method = "$method.atomic";
389 @arg = @$arg if ref($arg) eq 'ARRAY';
391 } elsif( $action eq 'batch_retrieve' ) {
392 $method =~ s/batch_retrieve/batch.retrieve/o;
393 $method = "$method.atomic";
394 @arg = @$arg if ref($arg) eq 'ARRAY';
396 } elsif( $action eq 'retrieve_all' ) {
397 $method =~ s/retrieve_all/retrieve.all.atomic/o;
400 # remove any stale events
403 if( $action eq 'update' or $action eq 'delete' or $action eq 'create' ) {
404 $self->log_activity($type, $action, $arg);
407 if($$options{checkperm}) {
408 my $a = ($action eq 'search' or
409 $action eq 'batch_retrieve' or $action eq 'retrieve_all') ? 'retrieve' : $action;
410 my $e = $self->_checkperm($type, $a, $$options{permorg});
421 $obj = $self->request($method, @arg);
422 } catch Error with { $err = shift; };
426 $self->log(I, "request returned no data");
428 if( $action eq 'retrieve' ) {
429 $self->event(_mk_not_found($type, $arg));
431 } elsif( $action eq 'update' or
432 $action eq 'delete' or $action eq 'create' ) {
433 my $evt = OpenILS::Event->new(
434 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
440 OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
441 payload => $arg, debug => "$err" ));
448 if( $action eq 'create' and $obj == 0 ) {
449 my $evt = OpenILS::Event->new(
450 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
455 # If we havn't dealt with the error in a nice way, go ahead and throw it
458 OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
459 payload => $arg, debug => "$err" ));
463 if( $action eq 'search' or $action eq 'batch_retrieve' or $action eq 'retrieve_all') {
464 $self->log(I, "$type.$action : returned ".scalar(@$obj). " result(s)");
465 $self->event(_mk_not_found($type, $arg)) unless @$obj;
468 $arg->id($obj) if $action eq 'create'; # grabs the id on create
469 $self->data($obj); # cache the data for convenience
471 return ($obj) ? $obj : 1;
476 my( $type, $arg ) = @_;
477 (my $t = $type) =~ s/\./_/og;
479 return OpenILS::Event->new("${t}_NOT_FOUND", payload => $arg);
484 # utility method for loading
488 $str =~ s/Fieldmapper:://o;
494 # -------------------------------------------------------------
495 # Load up the methods from the FM classes
496 # -------------------------------------------------------------
497 my $map = $Fieldmapper::fieldmap;
498 for my $object (keys %$map) {
499 my $obj = __fm2meth($object,'_');
500 my $type = __fm2meth($object, '.');
502 my $update = "update_$obj";
504 "sub $update {return shift()->runmethod('update', '$type', \@_);}";
507 my $retrieve = "retrieve_$obj";
509 "sub $retrieve {return shift()->runmethod('retrieve', '$type', \@_);}";
512 my $search = "search_$obj";
514 "sub $search {return shift()->runmethod('search', '$type', \@_);}";
517 my $create = "create_$obj";
519 "sub $create {return shift()->runmethod('create', '$type', \@_);}";
522 my $delete = "delete_$obj";
524 "sub $delete {return shift()->runmethod('delete', '$type', \@_);}";
527 my $bretrieve = "batch_retrieve_$obj";
529 "sub $bretrieve {return shift()->runmethod('batch_retrieve', '$type', \@_);}";
532 my $retrieveall = "retrieve_all_$obj";
534 "sub $retrieveall {return shift()->runmethod('retrieve_all', '$type', \@_);}";