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::Logger qw($logger);
10 my $U = "OpenILS::Application::AppUtils";
13 # -----------------------------------------------------------------------------
14 # Export some useful functions
15 # -----------------------------------------------------------------------------
16 use vars qw(@EXPORT_OK %EXPORT_TAGS);
18 use base qw/Exporter/;
19 push @EXPORT_OK, 'new_editor';
20 %EXPORT_TAGS = ( funcs => [ qw/ new_editor / ] );
22 sub new_editor { return OpenILS::Utils::Editor->new(@_); }
25 # -----------------------------------------------------------------------------
26 # These need to be auto-generated
27 # -----------------------------------------------------------------------------
29 'biblio.record_entry' => { update => 'UPDATE_MARC' },
30 'asset.copy' => { update => 'UPDATE_COPY'},
31 'asset.call_number' => { update => 'UPDATE_VOLUME'},
32 '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';
46 # -----------------------------------------------------------------------------
48 # xact=><true> : creates a storage transaction
49 # authtoken=>$token : the login session key
50 # -----------------------------------------------------------------------------
52 my( $class, %params ) = @_;
53 $class = ref($class) || $class;
54 my $self = bless( \%params, $class );
55 $self->{checked_perms} = {};
56 $logstr = "editor [0";
57 $logstr = "editor [1" if $self->{xact};
61 # -----------------------------------------------------------------------------
62 # Log the editor metadata along with the log string
63 # -----------------------------------------------------------------------------
65 my( $self, $lev, $str ) = @_;
67 $s .= "0|" unless $self->{xact};
68 $s .= "1|" if $self->{xact};
69 $s .= "0" unless $self->requestor;
70 $s .= $self->requestor->id if $self->requestor;
72 $logger->$lev("$s $str");
75 # -----------------------------------------------------------------------------
76 # Verifies the auth token and fetches the requestor object
77 # -----------------------------------------------------------------------------
80 $self->log(D, "checking auth token ".$self->authtoken);
81 my ($reqr, $evt) = $U->checkses($self->authtoken);
82 $self->event($evt) if $evt;
83 return $self->{requestor} = $reqr;
87 # -----------------------------------------------------------------------------
88 # Returns the last generated event
89 # -----------------------------------------------------------------------------
91 my( $self, $evt ) = @_;
92 $self->{event} = $evt if $evt;
93 return $self->{event};
96 # -----------------------------------------------------------------------------
97 # Clears the last caught event
98 # -----------------------------------------------------------------------------
101 $self->{event} = undef;
105 my( $self, $auth ) = @_;
106 $self->{authtoken} = $auth if $auth;
107 return $self->{authtoken};
110 # -----------------------------------------------------------------------------
111 # fetches the session, creating if necessary. If 'xact' is true on this
112 # object, a db session is created
113 # -----------------------------------------------------------------------------
115 my( $self, $session ) = @_;
116 $self->{session} = $session if $session;
118 if(!$self->{session}) {
119 $self->{session} = OpenSRF::AppSession->create('open-ils.storage');
121 if( ! $self->{session} ) {
122 my $str = "Error creating storage session with OpenSRF::AppSession->create()!";
124 throw OpenSRF::EX::ERROR ($str);
127 $self->{session}->connect if $self->{xact} or $self->{connect};
128 $self->xact_start if $self->{xact};
130 return $self->{session};
134 # -----------------------------------------------------------------------------
135 # Starts a storage transaction
136 # -----------------------------------------------------------------------------
139 $self->log(D, "starting new db session");
140 my $stat = $self->request('open-ils.storage.transaction.begin');
141 $self->log(E, "error starting database transaction") unless $stat;
145 # -----------------------------------------------------------------------------
146 # Commits a storage transaction
147 # -----------------------------------------------------------------------------
150 $self->log(D, "comitting db session");
151 my $stat = $self->request('open-ils.storage.transaction.commit');
152 $self->log(E, "error comitting database transaction") unless $stat;
156 # -----------------------------------------------------------------------------
157 # Rolls back a storage stransaction
158 # -----------------------------------------------------------------------------
161 $self->log(I, "rolling back db session");
162 return $self->request("open-ils.storage.transaction.rollback");
166 # -----------------------------------------------------------------------------
167 # commits the db session and destroys the session
168 # -----------------------------------------------------------------------------
171 return unless $self->{xact};
173 $self->session->disconnect;
174 $self->{session} = undef;
177 # -----------------------------------------------------------------------------
178 # clears all object data. Does not commit the db transaction.
179 # -----------------------------------------------------------------------------
182 $self->session->disconnect if $self->{session};
183 $$self{$_} = undef for (keys %$self);
187 # -----------------------------------------------------------------------------
189 # -----------------------------------------------------------------------------
198 # -----------------------------------------------------------------------------
199 # Does a simple storage request
200 # -----------------------------------------------------------------------------
202 my( $self, $method, @params ) = @_;
208 $val = $self->session->request($method, @params)->gather(1);
212 $self->log(E, "request error $method=@params : $err");
220 # -----------------------------------------------------------------------------
221 # Sets / Returns the requstor object. This is set when checkauth succeeds.
222 # -----------------------------------------------------------------------------
224 my($self, $requestor) = @_;
225 $self->{requestor} = $requestor if $requestor;
226 return $self->{requestor};
231 # -----------------------------------------------------------------------------
232 # Holds the last data received from a storage call
233 # -----------------------------------------------------------------------------
235 my( $self, $data ) = @_;
236 $self->{data} = $data if defined $data;
237 return $self->{data};
241 # -----------------------------------------------------------------------------
242 # True if this perm has already been checked at this org
243 # -----------------------------------------------------------------------------
245 my( $self, $perm, $org ) = @_;
246 $self->{checked_perms}->{$org} = {}
247 unless $self->{checked_perms}->{$org};
248 my $checked = $self->{checked_perms}->{$org}->{$perm};
250 $self->{checked_perms}->{$org}->{$perm} = 1;
258 # -----------------------------------------------------------------------------
259 # Returns true if the requested perm is allowed. If the perm check fails,
260 # $e->event is set and undef is returned
261 # The perm user is $e->requestor->id and perm org defaults to the requestor's
263 # If this perm at the given org has already been verified, true is returned
264 # and the perm is not re-checked
265 # -----------------------------------------------------------------------------
267 my( $self, $perm, $org ) = @_;
268 my $uid = $self->requestor->id;
269 $org ||= $self->requestor->ws_ou;
270 $self->log(I, "checking perms user=$uid, org=$org, perm=$perm");
271 return 1 if $self->perm_checked($perm, $org);
273 my $s = $self->request(
274 "open-ils.storage.permission.user_has_perm", $uid, $perm, $org );
277 my $e = OpenILS::Event->new('PERM_FAILURE', ilsperm => $perm, ilspermloc => $org);
287 # -----------------------------------------------------------------------------
288 # checks the appropriate perm for the operation
289 # -----------------------------------------------------------------------------
291 my( $self, $ptype, $action, $org ) = @_;
292 $org ||= $self->requestor->ws_ou;
293 my $perm = $PERMS{$ptype}{$action};
295 return undef if $self->perm_checked($perm, $org);
296 return $self->event unless $self->allowed($perm, $org);
298 $self->log(E, "no perm provided for $ptype.$action");
305 # -----------------------------------------------------------------------------
306 # Logs update actions to the activity log
307 # -----------------------------------------------------------------------------
309 my( $self, $type, $action, $arg ) = @_;
310 my $str = "$type.$action";
311 $str .= _prop_string($arg);
319 my @props = $obj->properties;
322 my $prop = $obj->$_() || "";
323 $prop = substr($prop, 0, 128) . "..." if length $prop > 131;
329 sub _hash_to_string {
332 $str .= " $_=".$h->{$_} for keys %$h;
337 my( $action, $arg ) = @_;
338 return "" unless $arg;
339 return $arg->id if UNIVERSAL::isa($arg, "Fieldmapper");
340 return _hash_to_string($arg) if ref $arg eq 'HASH'; # search
341 return "[@$arg]" if ref $arg eq 'ARRAY';
342 return $arg; # retrieve
347 # -----------------------------------------------------------------------------
348 # This does the actual storage query.
350 # 'search' calls become search_where calls and $arg can be a search hash or
351 # an array-ref of storage search options.
353 # 'retrieve' expects an id
354 # 'update' expects an object
355 # 'create' expects an object
356 # 'delete' expects an object
358 # All methods return true on success and undef on failure. On failure,
359 # $e->event is set to the generated event.
360 # Note: this method assumes that updating a non-changed object and
361 # thereby receiving a 0 from storage, is a successful update.
363 # The method will therefore return true so the caller can just do
364 # $e->update_blah($x) or return $e->event;
365 # The true value returned from storage for all methods will be stored in
366 # $e->data, until the next method is called.
368 # not-found events are generated on retrieve and serach methods.
369 # action=search methods will return [] (==true) if no data is found. If the
370 # caller is interested in the not found event, they can do:
371 # return $e->event unless @$results;
372 # -----------------------------------------------------------------------------
374 my( $self, $action, $type, $arg, $options ) = @_;
377 my $method = "open-ils.storage.direct.$type.$action";
378 my $argstr = _arg_to_string($action, $arg);
380 if( $action eq 'search' ) {
381 $method =~ s/search/search_where/o;
382 $method =~ s/direct/id_list/o if $options->{idlist};
383 $method = "$method.atomic";
384 @arg = @$arg if ref($arg) eq 'ARRAY';
386 } elsif( $action eq 'batch_retrieve' ) {
387 $method =~ s/batch_retrieve/batch.retrieve/o;
388 $method = "$method.atomic";
389 @arg = @$arg if ref($arg) eq 'ARRAY';
391 } elsif( $action eq 'retrieve_all' ) {
392 $method =~ s/retrieve_all/retrieve.all.atomic/o;
395 # remove any stale events
398 $self->log(I, "$type.$action : $argstr");
399 if( $action eq 'update' or $action eq 'delete' or $action eq 'create' ) {
400 $self->log_activity($type, $action, $arg);
403 if($$options{checkperm}) {
404 my $a = ($action eq 'search' or
405 $action eq 'batch_retrieve' or $action eq 'retrieve_all') ? 'retrieve' : $action;
406 my $e = $self->checkperm($type, $a, $$options{permorg});
417 $obj = $self->request($method, @arg);
418 } catch Error with { $err = shift; };
422 $self->log(I, "request returned no data");
424 if( $action eq 'retrieve' ) {
425 $self->event(_mk_not_found($type, $arg));
427 } elsif( $action eq 'update' or
428 $action eq 'delete' or $action eq 'create' ) {
429 my $evt = OpenILS::Event->new(
430 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
437 if( $action eq 'create' and $obj == 0 ) {
438 my $evt = OpenILS::Event->new(
439 'DATABASE_UPDATE_FAILED', payload => $arg, debug => "$err" );
444 if( $action eq 'search' or $action eq 'batch_retrieve' or $action eq 'retrieve_all') {
445 $self->log(I, "$type.$action : returned ".scalar(@$obj). " result(s)");
446 $self->event(_mk_not_found($type, $arg)) unless @$obj;
449 $arg->id($obj) if $action eq 'create'; # grabs the id on create
450 $self->data($obj); # cache the data for convenience
452 return ($obj) ? $obj : 1;
457 my( $type, $arg ) = @_;
458 (my $t = $type) =~ s/\./_/og;
460 return OpenILS::Event->new("${t}_NOT_FOUND", payload => $arg);
466 # utility method for loading
470 $str =~ s/Fieldmapper:://o;
476 # -------------------------------------------------------------
477 # Load up the methods from the FM classes
478 # -------------------------------------------------------------
479 my $map = $Fieldmapper::fieldmap;
480 for my $object (keys %$map) {
481 my $obj = __fm2meth($object,'_');
482 my $type = __fm2meth($object, '.');
484 my $update = "update_$obj";
486 "sub $update {return shift()->runmethod('update', '$type', \@_);}";
489 my $retrieve = "retrieve_$obj";
491 "sub $retrieve {return shift()->runmethod('retrieve', '$type', \@_);}";
494 my $search = "search_$obj";
496 "sub $search {return shift()->runmethod('search', '$type', \@_);}";
499 my $create = "create_$obj";
501 "sub $create {return shift()->runmethod('create', '$type', \@_);}";
504 my $delete = "delete_$obj";
506 "sub $delete {return shift()->runmethod('delete', '$type', \@_);}";
509 my $bretrieve = "batch_retrieve_$obj";
511 "sub $bretrieve {return shift()->runmethod('batch_retrieve', '$type', \@_);}";
514 my $retrieveall = "retrieve_all_$obj";
516 "sub $retrieveall {return shift()->runmethod('retrieve_all', '$type', \@_);}";