]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/SIP.pm
added logic which makes the server re-login on auth session timeout
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / SIP.pm
1 #
2 # ILS.pm: Test ILS interface module
3 #
4
5 package OpenILS::SIP;
6 use warnings; use strict;
7
8 use Sys::Syslog qw(syslog);
9
10 use OpenILS::SIP::Item;
11 use OpenILS::SIP::Patron;
12 use OpenILS::SIP::Transaction;
13 use OpenILS::SIP::Transaction::Checkout;
14 use OpenILS::SIP::Transaction::Checkin;
15 use OpenILS::SIP::Transaction::Renew;
16
17 use OpenSRF::System;
18 use OpenILS::Utils::Fieldmapper;
19 use OpenSRF::Utils::SettingsClient;
20 use OpenILS::Application::AppUtils;
21 use OpenSRF::Utils qw/:datetime/;
22 use DateTime::Format::ISO8601;
23 my $U = 'OpenILS::Application::AppUtils';
24
25 my $editor;
26 my $config;
27
28 use Digest::MD5 qw(md5_hex);
29
30 sub new {
31         my ($class, $institution, $login) = @_;
32         my $type = ref($class) || $class;
33         my $self = {};
34
35         $self->{login} = $login;
36
37         $config = $institution;
38         syslog("LOG_DEBUG", "OILS: new ILS '%s'", $institution->{id});
39         $self->{institution} = $institution;
40
41         my $bsconfig = $institution->{implementation_config}->{bootstrap};
42
43         syslog('LOG_DEBUG', "OILS: loading bootstrap config: $bsconfig");
44         
45         local $/ = "\n";
46         OpenSRF::System->bootstrap_client(config_file => $bsconfig);
47         syslog('LOG_DEBUG', "OILS: bootstrap loaded..");
48
49         $self->{osrf_config} = OpenSRF::Utils::SettingsClient->new;
50
51         Fieldmapper->import($self->{osrf_config}->config_value('IDL'));
52
53         bless( $self, $type );
54
55         return undef unless 
56                 $self->login( $login->{id}, $login->{password} );
57
58         return $self;
59 }
60
61 sub verify_session {
62         my $self = shift;
63         my $ses = $U->simplereq( 
64                 'open-ils.auth',
65                 'open-ils.auth.session.retrieve',  $self->{authtoken} );
66         return 1 unless $U->event_code($ses);
67         syslog('LOG_INFO', "OILS: Logging back after session timeout as user ".$self->{login}->{id});
68         return $self->login( $self->{login}->{id}, $self->{login}->{password} );
69 }
70
71 sub to_bool {
72         my $val = shift;
73         return ($val and $val =~ /true/io);
74 }
75
76 sub editor {
77         $editor = make_editor() unless $editor;
78         return $editor;
79 }
80 sub reset_editor {
81         $editor = undef;
82         return editor();
83 }
84
85 sub config {
86         return $config;
87 }
88
89
90 # Creates the global editor object
91 sub make_editor {
92         require OpenILS::Utils::CStoreEditor;
93         my $e = OpenILS::Utils::CStoreEditor->new(xact => 1);
94         # gnarly cstore hack to re-gen autogen methods after IDL is loaded
95         if(!UNIVERSAL::can($e, 'search_actor_card')) {
96                 syslog("LOG_WARNING", "OILS: Reloading CStoreEditor...");
97                 delete $INC{'OpenILS/Utils/CStoreEditor.pm'};
98                 require OpenILS::Utils::CStoreEditor;
99                 $e = OpenILS::Utils::CStoreEditor->new(xact =>1);
100         }
101         return $e;
102 }
103
104 sub format_date {
105         my $class = shift;
106         my $date = shift;
107         return "" unless $date;
108
109         $date = DateTime::Format::ISO8601->new->
110                 parse_datetime(OpenSRF::Utils::clense_ISO8601($date));
111         my @time = localtime($date->epoch);
112
113         my $year = $time[5]+1900;
114         my $mon = $time[4]+1;
115         my $day = $time[3];
116
117         $mon =~ s/^(\d)$/0$1/;
118         $day =~ s/^(\d)$/0$1/;
119         $date = "$year$mon$day";
120         syslog('LOG_DEBUG', "OILS: formatted date: $date");
121         return $date;
122 }
123
124
125
126 sub login {
127         my( $self, $username, $password ) = @_;
128         syslog('LOG_DEBUG', "OILS: Logging in with username $username");
129
130         my $seed = $U->simplereq( 
131                 'open-ils.auth',
132                 'open-ils.auth.authenticate.init', $username );
133
134         my $response = $U->simplereq(
135                 'open-ils.auth', 
136                 'open-ils.auth.authenticate.complete', 
137                 {       
138                         username => $username, 
139                         password => md5_hex($seed . md5_hex($password)), 
140                         type            => 'opac',
141                 }
142         );
143
144         if( my $code = $U->event_code($response) ) {
145                 my $txt = $response->{textcode};
146                 syslog('LOG_WARNING', "OILS: Login failed for $username.  $txt:$code");
147                 return undef;
148         }
149
150         my $key = $response->{payload}->{authtoken};
151         syslog('LOG_INFO', "OILS: Login succeeded for $username : authkey = $key");
152         return $self->{authtoken} = $key;
153 }
154
155
156 sub find_patron {
157         my $self = shift;
158         return OpenILS::SIP::Patron->new(@_);
159 }
160
161
162 sub find_item {
163         my $self = shift;
164         return OpenILS::SIP::Item->new(@_);
165 }
166
167
168 sub institution {
169     my $self = shift;
170     return $self->{institution}->{id};
171 }
172
173 sub supports {
174         my ($self, $op) = @_;
175         my ($i) = grep { $_->{name} eq $op }  
176                 @{$config->{implementation_config}->{supports}->{item}};
177         return to_bool($i->{value});
178 }
179
180 sub check_inst_id {
181         my ($self, $id, $whence) = @_;
182         if ($id ne $self->{institution}->{id}) {
183                 syslog("LOG_WARNING", 
184                         "OILS: %s: received institution '%s', expected '%s'",
185                         $whence, $id, $self->{institution}->{id});
186         }
187 }
188
189 sub checkout_ok {
190         return to_bool($config->{policy}->{checkout});
191 }
192
193 sub checkin_ok {
194         return to_bool($config->{policy}->{checkin});
195     return 0;
196 }
197
198 sub renew_ok {
199         return to_bool($config->{policy}->{renew});
200 }
201
202 sub status_update_ok {
203         return to_bool($config->{policy}->{status_update});
204 }
205
206 sub offline_ok {
207         return to_bool($config->{policy}->{offline});
208 }
209
210
211
212 ##
213 ## Checkout(patron_id, item_id, sc_renew):
214 ##    patron_id & item_id are the identifiers send by the terminal
215 ##    sc_renew is the renewal policy configured on the terminal
216 ## returns a status opject that can be queried for the various bits
217 ## of information that the protocol (SIP or NCIP) needs to generate
218 ## the response.
219 ##
220
221 sub checkout {
222         my ($self, $patron_id, $item_id, $sc_renew) = @_;
223
224         $self->verify_session;
225         
226         syslog('LOG_DEBUG', "OILS: OpenILS::Checkout attempt: patron=$patron_id, item=$item_id");
227         
228         my $xact                = OpenILS::SIP::Transaction::Checkout->new( authtoken => $self->{authtoken} );
229         my $patron      = $self->find_patron($patron_id);
230         my $item                = $self->find_item($item_id);
231         
232         $xact->patron($patron);
233         $xact->item($item);
234
235         if (!$patron) {
236                 $xact->screen_msg("Invalid Patron");
237                 return $xact;
238         }
239
240         if (!$patron->charge_ok) {
241                 $xact->screen_msg("Patron Blocked");
242                 return $xact;
243         }
244
245         if( !$item ) {
246                 $xact->screen_msg("Invalid Item");
247                 return $xact;
248         }
249
250         syslog('LOG_DEBUG', "OILS: OpenILS::Checkout data loaded OK, checking out...");
251         $xact->do_checkout();
252
253         if ($item->{patron} && ($item->{patron} ne $patron_id)) {
254                 # I can't deal with this right now
255                 # XXX check in then check out?
256                 $xact->screen_msg("Item checked out to another patron");
257                 $xact->ok(0);
258         } 
259
260         $xact->desensitize(!$item->magnetic);
261
262         if( $xact->ok ) {
263
264                 #editor()->commit;
265                 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
266                         "patron %s checkout %s succeeded", $patron_id, $item_id);
267
268         } else {
269
270                 #editor()->xact_rollback;
271                 syslog("LOG_DEBUG", "OILS: OpenILS::Checkout: " .
272                         "patron %s checkout %s FAILED, rolling back xact...", $patron_id, $item_id);
273         }
274
275         return $xact;
276 }
277
278
279 sub checkin {
280         my ($self, $item_id, $trans_date, $return_date,
281         $current_loc, $item_props, $cancel) = @_;
282
283         $self->verify_session;
284
285         syslog('LOG_DEBUG', "OILS: OpenILS::Checkin on item=$item_id");
286         
287         my $patron;
288         my $xact                = OpenILS::SIP::Transaction::Checkin->new(authtoken => $self->{authtoken});
289         my $item                = $self->find_item($item_id);
290
291         $xact->item($item);
292
293         if(!$xact->item) {
294                 $xact->screen_msg("Invalid item barcode: $item_id");
295                 $xact->ok(0);
296                 return $xact;
297         }
298
299         $xact->do_checkin( $trans_date, $return_date, $current_loc, $item_props );
300         
301         if ($xact->ok) {
302
303                 $xact->patron($patron = $self->find_patron($item->{patron}));
304                 delete $item->{patron};
305                 delete $item->{due_date};
306                 syslog('LOG_INFO', "OILS: Checkin succeeded");
307                 #editor()->commit;
308
309         } else {
310
311                 #editor()->xact_rollback;
312                 syslog('LOG_WARNING', "OILS: Checkin failed");
313         }
314         # END TRANSACTION
315
316         return $xact;
317 }
318
319 ## If the ILS caches patron information, this lets it free it up
320 sub end_patron_session {
321     my ($self, $patron_id) = @_;
322     return (1, 'Thank you for using OpenILS!', '');
323 }
324
325
326 #sub pay_fee {
327 #    my ($self, $patron_id, $patron_pwd, $fee_amt, $fee_type,
328 #       $pay_type, $fee_id, $trans_id, $currency) = @_;
329 #    my $trans;
330 #    my $patron;
331 #
332 #    $trans = new ILS::Transaction::FeePayment;
333 #
334 #    $patron = new ILS::Patron $patron_id;
335 #
336 #    $trans->transaction_id($trans_id);
337 #    $trans->patron($patron);
338 #    $trans->ok(1);
339 #
340 #    return $trans;
341 #}
342 #
343 #sub add_hold {
344 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
345 #       $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
346 #    my ($patron, $item);
347 #    my $hold;
348 #    my $trans;
349 #
350 #
351 #    $trans = new ILS::Transaction::Hold;
352 #
353 #    # BEGIN TRANSACTION
354 #    $patron = new ILS::Patron $patron_id;
355 #    if (!$patron
356 #       || (defined($patron_pwd) && !$patron->check_password($patron_pwd))) {
357 #       $trans->screen_msg("Invalid Patron.");
358 #
359 #       return $trans;
360 #    }
361 #
362 #    $item = new ILS::Item ($item_id || $title_id);
363 #    if (!$item) {
364 #       $trans->screen_msg("No such item.");
365 #
366 #       # END TRANSACTION (conditionally)
367 #       return $trans;
368 #    } elsif ($item->fee && ($fee_ack ne 'Y')) {
369 #       $trans->screen_msg = "Fee required to place hold.";
370 #
371 #       # END TRANSACTION (conditionally)
372 #       return $trans;
373 #    }
374 #
375 #    $hold = {
376 #       item_id         => $item->id,
377 #       patron_id       => $patron->id,
378 #       expiration_date => $expiry_date,
379 #       pickup_location => $pickup_location,
380 #       hold_type       => $hold_type,
381 #    };
382 #
383 #    $trans->ok(1);
384 #    $trans->patron($patron);
385 #    $trans->item($item);
386 #    $trans->pickup_location($pickup_location);
387 #
388 #    push(@{$item->hold_queue}, $hold);
389 #    push(@{$patron->{hold_items}}, $hold);
390 #
391 #
392 #    # END TRANSACTION
393 #    return $trans;
394 #}
395 #
396 #sub cancel_hold {
397 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id) = @_;
398 #    my ($patron, $item, $hold);
399 #    my $trans;
400 #
401 #    $trans = new ILS::Transaction::Hold;
402 #
403 #    # BEGIN TRANSACTION
404 #    $patron = new ILS::Patron $patron_id;
405 #    if (!$patron) {
406 #       $trans->screen_msg("Invalid patron barcode.");
407 #
408 #       return $trans;
409 #    } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
410 #       $trans->screen_msg('Invalid patron password.');
411 #
412 #       return $trans;
413 #    }
414 #
415 #    $item = new ILS::Item ($item_id || $title_id);
416 #    if (!$item) {
417 #       $trans->screen_msg("No such item.");
418 #
419 #       # END TRANSACTION (conditionally)
420 #       return $trans;
421 #    }
422 #
423 #    # Remove the hold from the patron's record first
424 #    $trans->ok($patron->drop_hold($item_id));
425 #
426 #    if (!$trans->ok) {
427 #       # We didn't find it on the patron record
428 #       $trans->screen_msg("No such hold on patron record.");
429 #
430 #       # END TRANSACTION (conditionally)
431 #       return $trans;
432 #    }
433 #
434 #    # Now, remove it from the item record.  If it was on the patron
435 #    # record but not on the item record, we'll treat that as success.
436 #    foreach my $i (0 .. scalar @{$item->hold_queue}) {
437 #       $hold = $item->hold_queue->[$i];
438 #
439 #       if ($hold->{patron_id} eq $patron->id) {
440 #           # found it: delete it.
441 #           splice @{$item->hold_queue}, $i, 1;
442 #           last;
443 #       }
444 #    }
445 #
446 #    $trans->screen_msg("Hold Cancelled.");
447 #    $trans->patron($patron);
448 #    $trans->item($item);
449 #
450 #    return $trans;
451 #}
452 #
453 #
454 ## The patron and item id's can't be altered, but the
455 ## date, location, and type can.
456 #sub alter_hold {
457 #    my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
458 #       $expiry_date, $pickup_location, $hold_type, $fee_ack) = @_;
459 #    my ($patron, $item);
460 #    my $hold;
461 #    my $trans;
462 #
463 #    $trans = new ILS::Transaction::Hold;
464 #
465 #    # BEGIN TRANSACTION
466 #    $patron = new ILS::Patron $patron_id;
467 #    if (!$patron) {
468 #       $trans->screen_msg("Invalid patron barcode.");
469 #
470 #       return $trans;
471 #    }
472 #
473 #    foreach my $i (0 .. scalar @{$patron->{hold_items}}) {
474 #       $hold = $patron->{hold_items}[$i];
475 #
476 #       if ($hold->{item_id} eq $item_id) {
477 #           # Found it.  So fix it.
478 #           $hold->{expiration_date} = $expiry_date if $expiry_date;
479 #           $hold->{pickup_location} = $pickup_location if $pickup_location;
480 #           $hold->{hold_type} = $hold_type if $hold_type;
481 #
482 #           $trans->ok(1);
483 #           $trans->screen_msg("Hold updated.");
484 #           $trans->patron($patron);
485 #           $trans->item(new ILS::Item $hold->{item_id});
486 #           last;
487 #       }
488 #    }
489 #
490 #    # The same hold structure is linked into both the patron's
491 #    # list of hold items and into the queue of outstanding holds
492 #    # for the item, so we don't need to search the hold queue for
493 #    # the item, since it's already been updated by the patron code.
494 #
495 #    if (!$trans->ok) {
496 #       $trans->screen_msg("No such outstanding hold.");
497 #    }
498 #
499 #    return $trans;
500 #}
501
502
503 sub renew {
504         my ($self, $patron_id, $patron_pwd, $item_id, $title_id,
505                 $no_block, $nb_due_date, $third_party, $item_props, $fee_ack) = @_;
506
507         $self->verify_session;
508
509         my $trans = OpenILS::SIP::Transaction::Renew->new( authtoken => $self->{authtoken} );
510         $trans->patron($self->find_patron($patron_id));
511         $trans->item($self->find_item($item_id));
512
513         if(!$trans->patron) {
514                 $trans->screen_msg("Invalid patron barcode.");
515                 $trans->ok(0);
516                 return $trans;
517         }
518
519         if(!$trans->patron->renew_ok) {
520                 $trans->screen_msg("Renewals not allowed.");
521                 $trans->ok(0);
522                 return $trans;
523         }
524
525         if(!$trans->item) {
526                 if( $title_id ) {
527                         $trans->screen_msg("Item Id renewal not supported.");
528                 } else {
529                         $trans->screen_msg("Invalid item barcode.");
530                 }
531                 $trans->ok(0);
532                 return $trans;
533         }
534
535         if(!$trans->item->{patron} or 
536                         $trans->item->{patron} ne $patron_id) {
537                 $trans->screen_msg("Item not checked out to " . $trans->patron->name);
538                 $trans->ok(0);
539                 return $trans;
540         }
541
542         # Perform the renewal
543         $trans->do_renew();
544
545         $trans->desensitize(0); # It's already checked out
546         $trans->item->{due_date} = $nb_due_date if $no_block eq 'Y';
547         $trans->item->{sip_item_properties} = $item_props if $item_props;
548
549         return $trans;
550 }
551
552
553
554
555
556 #
557 #sub renew_all {
558 #    my ($self, $patron_id, $patron_pwd, $fee_ack) = @_;
559 #    my ($patron, $item_id);
560 #    my $trans;
561 #
562 #    $trans = new ILS::Transaction::RenewAll;
563 #
564 #    $trans->patron($patron = new ILS::Patron $patron_id);
565 #    if (defined $patron) {
566 #       syslog("LOG_DEBUG", "ILS::renew_all: patron '%s': renew_ok: %s",
567 #              $patron->name, $patron->renew_ok);
568 #    } else {
569 #       syslog("LOG_DEBUG", "ILS::renew_all: Invalid patron id: '%s'",
570 #              $patron_id);
571 #    }
572 #
573 #    if (!defined($patron)) {
574 #       $trans->screen_msg("Invalid patron barcode.");
575 #       return $trans;
576 #    } elsif (!$patron->renew_ok) {
577 #       $trans->screen_msg("Renewals not allowed.");
578 #       return $trans;
579 #    } elsif (defined($patron_pwd) && !$patron->check_password($patron_pwd)) {
580 #       $trans->screen_msg("Invalid patron password.");
581 #       return $trans;
582 #    }
583 #
584 #    foreach $item_id (@{$patron->{items}}) {
585 #       my $item = new ILS::Item $item_id;
586 #
587 #       if (!defined($item)) {
588 #           syslog("LOG_WARNING",
589 #                  "renew_all: Invalid item id associated with patron '%s'",
590 #                  $patron->id);
591 #           next;
592 #       }
593 #
594 #       if (@{$item->hold_queue}) {
595 #           # Can't renew if there are outstanding holds
596 #           push @{$trans->unrenewed}, $item_id;
597 #       } else {
598 #           $item->{due_date} = time + (14*24*60*60); # two weeks hence
599 #           push @{$trans->renewed}, $item_id;
600 #       }
601 #    }
602 #
603 #    $trans->ok(1);
604 #
605 #    return $trans;
606 #}
607
608 1;