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;
141 # -----------------------------------------------------------------------------
142 # Commits a storage transaction
143 # -----------------------------------------------------------------------------
146 $self->log(D, "comitting db session");
147 my $stat = $self->request('open-ils.storage.transaction.commit');
148 $self->log(E, "error comitting database transaction") unless $stat;
152 # -----------------------------------------------------------------------------
153 # Rolls back a storage stransaction
154 # -----------------------------------------------------------------------------
157 $self->log(I, "rolling back db session");
158 return $self->request("open-ils.storage.transaction.rollback");
162 # -----------------------------------------------------------------------------
163 # commits the db session and destroys the session
164 # -----------------------------------------------------------------------------
167 return unless $self->{xact};
169 $self->session->disconnect;
170 $self->{session} = undef;
173 # -----------------------------------------------------------------------------
174 # clears all object data. Does not commit the db transaction.
175 # -----------------------------------------------------------------------------
178 $self->session->disconnect if $self->{session};
179 $$self{$_} = undef for (keys %$self);
183 # -----------------------------------------------------------------------------
185 # -----------------------------------------------------------------------------
194 # -----------------------------------------------------------------------------
195 # Does a simple storage request
196 # -----------------------------------------------------------------------------
198 my( $self, $method, @params ) = @_;
202 my $argstr = __arg_to_string( (scalar(@params)) == 1 ? $params[0] : \@params);
204 $self->log(I, "request $method : $argstr");
207 $val = $self->session->request($method, @params)->gather(1);
211 $self->log(E, "request error $method : $argstr : $err");
219 # -----------------------------------------------------------------------------
220 # Sets / Returns the requstor object. This is set when checkauth succeeds.
221 # -----------------------------------------------------------------------------
223 my($self, $requestor) = @_;
224 $self->{requestor} = $requestor if $requestor;
225 return $self->{requestor};
230 # -----------------------------------------------------------------------------
231 # Holds the last data received from a storage call
232 # -----------------------------------------------------------------------------
234 my( $self, $data ) = @_;
235 $self->{data} = $data if defined $data;
236 return $self->{data};
240 # -----------------------------------------------------------------------------
241 # True if this perm has already been checked at this org
242 # -----------------------------------------------------------------------------
244 my( $self, $perm, $org ) = @_;
245 $self->{checked_perms}->{$org} = {}
246 unless $self->{checked_perms}->{$org};
247 my $checked = $self->{checked_perms}->{$org}->{$perm};
249 $self->{checked_perms}->{$org}->{$perm} = 1;
257 # -----------------------------------------------------------------------------
258 # Returns true if the requested perm is allowed. If the perm check fails,
259 # $e->event is set and undef is returned
260 # The perm user is $e->requestor->id and perm org defaults to the requestor's
262 # If this perm at the given org has already been verified, true is returned
263 # and the perm is not re-checked
264 # -----------------------------------------------------------------------------
266 my( $self, $perm, $org ) = @_;
267 my $uid = $self->requestor->id;
268 $org ||= $self->requestor->ws_ou;
269 $self->log(I, "checking perms user=$uid, org=$org, perm=$perm");
270 return 1 if $self->perm_checked($perm, $org);
271 return $self->checkperm($uid, $org, $perm);
275 my($self, $userid, $org, $perm) = @_;
276 my $s = $self->request(
277 "open-ils.storage.permission.user_has_perm", $userid, $perm, $org );
280 my $e = OpenILS::Event->new('PERM_FAILURE', ilsperm => $perm, ilspermloc => $org);
290 # -----------------------------------------------------------------------------
291 # checks the appropriate perm for the operation
292 # -----------------------------------------------------------------------------
294 my( $self, $ptype, $action, $org ) = @_;
295 $org ||= $self->requestor->ws_ou;
296 my $perm = $PERMS{$ptype}{$action};
298 return undef if $self->perm_checked($perm, $org);
299 return $self->event unless $self->allowed($perm, $org);
301 $self->log(E, "no perm provided for $ptype.$action");
308 # -----------------------------------------------------------------------------
309 # Logs update actions to the activity log
310 # -----------------------------------------------------------------------------
312 my( $self, $type, $action, $arg ) = @_;
313 my $str = "$type.$action";
314 $str .= _prop_string($arg);
322 my @props = $obj->properties;
325 my $prop = $obj->$_() || "";
326 $prop = substr($prop, 0, 128) . "..." if length $prop > 131;
333 sub __arg_to_string {
335 return "" unless defined $arg;
336 return $arg->id if UNIVERSAL::isa($arg, "Fieldmapper");
337 return OpenSRF::Utils::JSON->perl2JSON($arg);
341 # -----------------------------------------------------------------------------
342 # This does the actual storage query.
344 # 'search' calls become search_where calls and $arg can be a search hash or
345 # an array-ref of storage search options.
347 # 'retrieve' expects an id
348 # 'update' expects an object
349 # 'create' expects an object
350 # 'delete' expects an object
352 # All methods return true on success and undef on failure. On failure,
353 # $e->event is set to the generated event.
354 # Note: this method assumes that updating a non-changed object and
355 # thereby receiving a 0 from storage, is a successful update.
357 # The method will therefore return true so the caller can just do
358 # $e->update_blah($x) or return $e->event;
359 # The true value returned from storage for all methods will be stored in
360 # $e->data, until the next method is called.
362 # not-found events are generated on retrieve and serach methods.
363 # action=search methods will return [] (==true) if no data is found. If the
364 # caller is interested in the not found event, they can do:
365 # return $e->event unless @$results;
366 # -----------------------------------------------------------------------------
368 my( $self, $action, $type, $arg, $options ) = @_;
371 my $method = "open-ils.storage.direct.$type.$action";
373 if( $action eq 'search' ) {
374 $method =~ s/search/search_where/o;
375 $method =~ s/direct/id_list/o if $options->{idlist};
376 $method = "$method.atomic";
377 @arg = @$arg if ref($arg) eq 'ARRAY';
379 } elsif( $action eq 'batch_retrieve' ) {
380 $method =~ s/batch_retrieve/batch.retrieve/o;
381 $method = "$method.atomic";
382 @arg = @$arg if ref($arg) eq 'ARRAY';
384 } elsif( $action eq 'retrieve_all' ) {
385 $method =~ s/retrieve_all/retrieve.all.atomic/o;
388 # remove any stale events
391 if( $action eq 'update' or $action eq 'delete' or $action eq 'create' ) {
392 $self->log_activity($type, $action, $arg);
395 if($$options{checkperm}) {
396 my $a = ($action eq 'search' or
397 $action eq 'batch_retrieve' or $action eq 'retrieve_all') ? 'retrieve' : $action;
398 my $e = $self->_checkperm($type, $a, $$options{permorg});
409 $obj = $self->request($method, @arg);
410 } catch Error with { $err = shift; };
414 $self->log(I, "request returned no data");
416 if( $action eq 'retrieve' ) {
417 $self->event(_mk_not_found($type, $arg));
419 } elsif( $action eq 'update' or
420 $action eq 'delete' or $action eq 'create' ) {
421 my $evt = OpenILS::Event->new(
422 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
428 OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
429 payload => $arg, debug => "$err" ));
436 if( $action eq 'create' and $obj == 0 ) {
437 my $evt = OpenILS::Event->new(
438 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
443 # If we havn't dealt with the error in a nice way, go ahead and throw it
446 OpenILS::Event->new( 'DATABASE_QUERY_FAILED',
447 payload => $arg, debug => "$err" ));
451 if( $action eq 'search' or $action eq 'batch_retrieve' or $action eq 'retrieve_all') {
452 $self->log(I, "$type.$action : returned ".scalar(@$obj). " result(s)");
453 $self->event(_mk_not_found($type, $arg)) unless @$obj;
456 $arg->id($obj) if $action eq 'create'; # grabs the id on create
457 $self->data($obj); # cache the data for convenience
459 return ($obj) ? $obj : 1;
464 my( $type, $arg ) = @_;
465 (my $t = $type) =~ s/\./_/og;
467 return OpenILS::Event->new("${t}_NOT_FOUND", payload => $arg);
472 # utility method for loading
476 $str =~ s/Fieldmapper:://o;
482 # -------------------------------------------------------------
483 # Load up the methods from the FM classes
484 # -------------------------------------------------------------
485 my $map = $Fieldmapper::fieldmap;
486 for my $object (keys %$map) {
487 my $obj = __fm2meth($object,'_');
488 my $type = __fm2meth($object, '.');
490 my $update = "update_$obj";
492 "sub $update {return shift()->runmethod('update', '$type', \@_);}";
495 my $retrieve = "retrieve_$obj";
497 "sub $retrieve {return shift()->runmethod('retrieve', '$type', \@_);}";
500 my $search = "search_$obj";
502 "sub $search {return shift()->runmethod('search', '$type', \@_);}";
505 my $create = "create_$obj";
507 "sub $create {return shift()->runmethod('create', '$type', \@_);}";
510 my $delete = "delete_$obj";
512 "sub $delete {return shift()->runmethod('delete', '$type', \@_);}";
515 my $bretrieve = "batch_retrieve_$obj";
517 "sub $bretrieve {return shift()->runmethod('batch_retrieve', '$type', \@_);}";
520 my $retrieveall = "retrieve_all_$obj";
522 "sub $retrieveall {return shift()->runmethod('retrieve_all', '$type', \@_);}";