2 use strict; use warnings;
6 use OpenSRF::Utils::Logger qw/$logger/;
7 use OpenILS::Application::AppUtils;
9 use OpenSRF::EX qw/:try/;
11 use OpenILS::Utils::Fieldmapper;
12 use Digest::MD5 qw/md5_hex/;
13 use OpenSRF::Utils qw/:daemon/;
14 use OpenILS::Utils::OfflineStore;
15 use OpenSRF::Utils::SettingsClient;
20 my $U = "OpenILS::Application::AppUtils";
21 my $DB = "OpenILS::Utils::OfflineStore";
22 my $SES = "${DB}::Session";
23 my $SCRIPT = "OpenILS::Utils::OfflineStore::Script";
25 # --------------------------------------------------------------------
27 # --------------------------------------------------------------------
29 do '##CONFIG##/offline-config.pl';
33 my $basedir = $config{base_dir} || die "Offline config error: no base_dir defined\n";
34 my $bootstrap = $config{bootstrap} || die "Offline config error: no bootstrap defined\n";
35 my $wsname = $cgi->param('ws');
36 my $org = $cgi->param('org');
37 my $authtoken = $cgi->param('ses') || "";
38 my $seskey = $cgi->param('seskey');
39 my $action = $cgi->param('action'); # - create, load, execute, status
51 # --------------------------------------------------------------------
53 # This function should behave as a child_init might behave in case
54 # this is moved to mod_perl
55 # --------------------------------------------------------------------
57 $DB->DBFile($config{dsn}, $config{usr}, $config{pw});
62 OpenSRF::System->bootstrap_client(config_file => $bootstrap );
63 Fieldmapper->import(IDL =>
64 OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
69 sub _ol_debug_params {
71 my @params = $cgi->param;
72 @params = sort { $a cmp $b } @params;
73 $s .= "$_=" . $cgi->param($_) . "\n" for @params;
75 warn '-'x60 ."\n$s\n";
79 # --------------------------------------------------------------------
80 # Finds the requestor and other info specific to this request
81 # --------------------------------------------------------------------
84 # fetch the requestor object
85 ($requestor, $evt) = $U->checkses($authtoken);
86 ol_handle_result($evt) if $evt;
88 # try the param, the workstation, and finally the user's ws org
90 $wsobj = ol_fetch_workstation($wsname);
91 $org = $wsobj->owning_lib if $wsobj;
92 $org = $requestor->ws_ou unless $org;
93 ol_handle_result(OpenILS::Event->new('OFFLINE_NO_ORG')) unless $org;
98 # --------------------------------------------------------------------
99 # Runs the requested action
100 # --------------------------------------------------------------------
105 if( $action eq 'create' ) {
107 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
108 ol_handle_result($evt) if $evt;
109 $payload = ol_create_session();
111 } elsif( $action eq 'load' ) {
113 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
114 ol_handle_result($evt) if $evt;
115 $payload = ol_load();
117 } elsif( $action eq 'execute' ) {
119 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_EXECUTE');
120 ol_handle_result($evt) if $evt;
121 $payload = ol_execute();
123 } elsif( $action eq 'status' ) {
125 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_VIEW');
126 ol_handle_result($evt) if $evt;
127 $payload = ol_status();
130 ol_handle_event('SUCCESS', payload => $payload );
134 # --------------------------------------------------------------------
135 # Creates a new session
136 # --------------------------------------------------------------------
137 sub ol_create_session {
139 my $desc = $cgi->param('desc') || "";
140 $seskey = time . "_${$}_" . int(rand() * 1000);
142 $logger->debug("offline: user ".$requestor->id.
143 " creating new session with key $seskey and description $desc");
149 description => $desc,
150 creator => $requestor->id,
151 create_time => CORE::time(),
160 # --------------------------------------------------------------------
161 # Holds the meta-info for a script file
162 # --------------------------------------------------------------------
163 sub ol_create_script {
166 my $session = ol_find_session($seskey);
167 my $delta = $cgi->param('delta') || 0;
169 my $script = $session->add_to_scripts(
171 requestor => $requestor->id,
172 create_time => CORE::time,
173 workstation => $wsname,
174 logfile => "$basedir/pending/$org/$seskey/$wsname.log",
175 time_delta => $delta,
181 # --------------------------------------------------------------------
182 # Finds the current session in the db
183 # --------------------------------------------------------------------
184 sub ol_find_session {
185 my $ses = $SES->retrieve($seskey);
186 ol_handle_event('OFFLINE_INVALID_SESSION', payload => $seskey) unless $ses;
190 # --------------------------------------------------------------------
191 # Finds a script object in the DB based on workstation and seskey
192 # --------------------------------------------------------------------
194 my $ws = shift || $wsname;
195 my $sk = shift || $seskey;
196 my ($script) = $SCRIPT->search( session => $seskey, workstation => $ws );
200 # --------------------------------------------------------------------
201 # Creates a new script in the database and loads the new script file
202 # --------------------------------------------------------------------
205 my $session = ol_find_session;
206 my $handle = $cgi->upload('file');
207 my $outdir = "$basedir/pending/$org/$seskey";
208 my $outfile = "$outdir/$wsname.log";
210 ol_handle_event('OFFLINE_SESSION_FILE_EXISTS') if ol_find_script();
211 ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
212 ol_handle_event('OFFLINE_SESSION_COMPLETE') if $session->end_time;
214 qx/mkdir -p $outdir/;
216 open(FILE, ">>$outfile") or ol_handle_event('OFFLINE_FILE_ERROR');
217 while( <$handle> ) { print FILE; $x++;}
220 ol_create_script($x);
226 # --------------------------------------------------------------------
227 sub ol_handle_result {
229 my $json = JSON->perl2JSON($obj);
231 # Clear this so it's not remembered
234 if( $cgi->param('html')) {
235 my $html = "<html><body onload='xulG.handle_event($json)'></body></html>";
236 print "content-type: text/html\n\n";
241 print "content-type: text/plain\n\n";
248 # --------------------------------------------------------------------
249 sub ol_handle_event {
250 my( $evt, @args ) = @_;
251 ol_handle_result(OpenILS::Event->new($evt, @args));
255 # --------------------------------------------------------------------
256 sub ol_flesh_session {
260 map { $data{$_} = $session->$_ } $session->columns;
263 for my $script ($session->scripts) {
265 map { $sdata{$_} = $script->$_ } $script->columns;
267 # the client doesn't need this info
268 delete $sdata{session};
270 delete $sdata{logfile};
272 push( @{$data{scripts}}, \%sdata );
279 # --------------------------------------------------------------------
280 # Returns various information on the sessions and scripts
281 # --------------------------------------------------------------------
284 my $type = $cgi->param('status_type') || "scripts";
286 # --------------------------------------------------------------------
287 # Returns info on every script upload attached to the current session
288 # --------------------------------------------------------------------
289 if( $type eq 'scripts' ) {
290 my $session = ol_find_session();
291 ol_handle_result(ol_flesh_session($session));
294 # --------------------------------------------------------------------
295 # Returns all scripts and sessions for the given org
296 # --------------------------------------------------------------------
297 } elsif( $type eq 'sessions' ) {
298 my @sessions = $SES->search( org => $org );
300 # can I do this in the DB without raw SQL?
301 @sessions = sort { $a->create_time <=> $b->create_time } @sessions;
303 push( @data, ol_flesh_session($_) ) for @sessions;
304 ol_handle_result(\@data);
307 # --------------------------------------------------------------------
308 # Returns total commands and completed commands counts
309 # --------------------------------------------------------------------
310 } elsif( $type eq 'summary' ) {
311 my $session = ol_find_session();
313 $logger->debug("offline: retrieving summary info ".
314 "for session ".$session->key." with completed=".$session->num_complete);
317 $count += $_->count for ($session->scripts);
319 { total => $count, num_complete => $session->num_complete });
323 # --------------------------------------------------------------------
324 # Returns the list of non-SUCCESS events that have occurred so far for
325 # this set of commands
326 # --------------------------------------------------------------------
327 } elsif( $type eq 'exceptions' ) {
329 my $session = ol_find_session();
330 my $resfile = "$basedir/pending/$org/$seskey/results";
331 if( $session->end_time ) {
332 $resfile = "$basedir/archive/$org/$seskey/results";
334 my $data = ol_file_to_perl($resfile);
335 $data = [ grep { $_->{event}->{ilsevent} ne '0' } @$data ];
336 ol_handle_result($data);
341 sub ol_fetch_workstation {
343 $logger->debug("offline: Fetching workstation $name");
344 my $ws = $U->storagereq(
345 'open-ils.storage.direct.actor.workstation.search.name', $name);
346 ol_handle_result(OpenILS::Event->new('ACTOR_WORKSTATION_NOT_FOUND')) unless $ws;
353 # --------------------------------------------------------------------
354 # Sorts the script commands then forks a child to executes them.
355 # --------------------------------------------------------------------
358 my $session = ol_find_session();
359 ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
360 ol_handle_event('OFFLINE_SESSION_COMPLETE') if $session->end_time;
362 my $commands = ol_collect_commands();
364 # --------------------------------------------------------------------
365 # Note that we must disconnect from opensrf before forking or the
366 # connection will be borked...
367 # --------------------------------------------------------------------
368 OpenSRF::Transport::PeerHandle->retrieve->disconnect;
374 # --------------------------------------------------------------------
375 # Tell the client all is well
376 # --------------------------------------------------------------------
377 ol_handle_event('SUCCESS'); # - this exits
382 # --------------------------------------------------------------------
383 # close stdout/stderr or apache will wait on the child to finish
384 # --------------------------------------------------------------------
388 $logger->debug("offline: child $$ processing data...");
390 # --------------------------------------------------------------------
391 # The child re-connects to the opensrf network and processes
392 # the script requests
393 # --------------------------------------------------------------------
394 OpenSRF::System->bootstrap_client(config_file => $bootstrap);
399 #Class::DBI->autoupdate(1);
403 my $sesion = ol_find_session();
404 $session->in_process(1);
405 ol_process_commands($session, $commands);
406 ol_archive_files($session);
410 $logger->error("offline: child process error $e");
415 sub ol_file_to_perl {
417 open(F, "$fname") or ol_handle_event('OFFLINE_FILE_ERROR');
420 push(@p, JSON->JSON2perl($_)) for @d;
425 # collects the commands and sorts them on timestamp+delta
426 sub ol_collect_commands {
427 my $ses = ol_find_session();
430 # cycle through every script loaded to this session
431 for my $script ($ses->scripts) {
432 my $coms = ol_file_to_perl($script->logfile);
434 # cycle through all of the commands for this script
435 for my $com (@$coms) {
436 $$com{_workstation} = $script->workstation;
437 $$com{_realtime} = $script->time_delta + $com->{timestamp};
438 push( @commands, $com );
442 # make sure thera are no blank commands
443 @commands = grep { ($_ and $_->{type}) } @commands;
446 @commands = sort { $a->{_realtime} <=> $b->{_realtime} } @commands;
448 # push user registrations to the front
449 my @regs = grep { $_->{type} eq 'register' } @commands;
450 my @others = grep { $_->{type} ne 'register' } @commands;
452 return [ @regs, @others ];
456 my $time = shift || CORE::time;
457 my (undef,undef, undef, $mday,$mon,$year) = localtime($time);
458 $mon++; $year += 1900;
459 $mday = "0$mday" unless $mday =~ /\d{2}/o;
460 $mon = "0$mon" unless $mon =~ /\d{2}/o;
461 return ($year, $mon, $mday);
465 # --------------------------------------------------------------------
466 # Moves all files from the pending directory to the archive directory
467 # and removes the pending directory
468 # --------------------------------------------------------------------
469 sub ol_archive_files {
471 my ($y, $m, $d) = ol_date();
473 my $dir = "$basedir/pending/$org/$seskey";
474 my $archdir = "$basedir/archive/$org/$seskey";
475 $logger->debug("offline: archiving files to $archdir");
477 # Tell the db the files are moving
478 $_->logfile($archdir.'/'.$_->workstation.'.log') for ($session->scripts);
480 qx/mkdir -p $archdir/;
481 qx/mv $_ $archdir/ for <$dir/*>;
486 # --------------------------------------------------------------------
487 # Appends results to the results file.
488 # --------------------------------------------------------------------
490 sub ol_append_result {
495 $obj = JSON->perl2JSON($obj);
498 open($rhandle, ">>$basedir/pending/$org/$seskey/results")
499 or ol_handle_event('OFFLINE_FILE_ERROR');
502 print $rhandle "$obj\n";
503 close($rhandle) if $last;
508 # --------------------------------------------------------------------
509 # Runs the commands and returns the list of errors
510 # --------------------------------------------------------------------
511 sub ol_process_commands {
514 my $commands = shift;
517 $session->start_time(CORE::time);
519 for my $d ( @$commands ) {
522 my $last = ($x++ == scalar(@$commands) - 1) ? 1 : 0;
523 my $res = { command => $d };
529 $logger->debug("offline: top of execute loop : $t");
532 $res->{event} = ol_handle_checkin($d) if $t eq 'checkin';
533 $res->{event} = ol_handle_inhouse($d) if $t eq 'in_house_use';
534 $res->{event} = ol_handle_checkout($d) if $t eq 'checkout';
535 $res->{event} = ol_handle_renew($d) if $t eq 'renew';
536 $res->{event} = ol_handle_register($d) if $t eq 'register';
538 } catch Error with { $err = shift; };
542 if( ref($err) eq 'OpenSRF::EX::JabberDisconnected' ) {
543 $logger->error("offline: we lost jabber .. trying to reconnect");
547 $res->{event} = OpenILS::Event->new('INTERNAL_SERVER_ERROR', debug => "$err");
556 ol_append_result($res, $last);
557 $session->num_complete( $session->num_complete + 1 );
559 $logger->debug("offline: set session [".$session->key."] num_complete to ".$session->num_complete);
562 $session->end_time(CORE::time);
563 $session->in_process(0);
567 # --------------------------------------------------------------------
568 # Runs an in_house_use action
569 # --------------------------------------------------------------------
570 sub ol_handle_inhouse {
573 my $realtime = $command->{_realtime};
574 my $ws = $command->{_workstation};
575 my $barcode = $command->{barcode};
576 my $count = $command->{count} || 1;
577 my $use_time = $command->{use_time} || "";
579 $logger->activity("offline: in_house_use : requestor=". $requestor->id.", realtime=$realtime, ".
580 "workstation=$ws, barcode=$barcode, count=$count, use_time=$use_time");
583 return OpenILS::Event->new(
584 'INTERNAL_SERVER_ERROR', payload => 'TOO MANY IN HOUSE USE');
587 my $ids = $U->simplereq(
589 'open-ils.circ.in_house_use.create', $authtoken,
590 { barcode => $barcode, count => $count, location => $org, use_time => $use_time } );
592 return OpenILS::Event->new('SUCCESS', payload => $ids) if( ref($ids) eq 'ARRAY' );
598 # --------------------------------------------------------------------
599 # Pulls the relevant circ args from the command, fetches data where
601 # --------------------------------------------------------------------
602 sub ol_circ_args_from_command {
605 my $type = $command->{type};
606 my $realtime = $command->{_realtime};
607 my $ws = $command->{_workstation};
608 my $barcode = $command->{barcode} || "";
609 my $cotime = $command->{checkout_time} || "";
610 my $pbc = $command->{patron_barcode};
611 my $due_date = $command->{due_date} || "";
612 my $noncat = ($command->{noncat}) ? "yes" : "no"; # for logging
614 $logger->activity("offline: $type : requestor=". $requestor->id.
615 ", realtime=$realtime, workstation=$ws, checkout_time=$cotime, ".
616 "patron=$pbc, due_date=$due_date, noncat=$noncat");
619 permit_override => 1,
621 checkout_time => $cotime,
622 patron_barcode => $pbc,
623 due_date => $due_date };
625 if( $command->{noncat} ) {
627 $args->{noncat_type} = $command->{noncat_type};
628 $args->{noncat_count} = $command->{noncat_count};
636 # --------------------------------------------------------------------
637 # Performs a checkout action
638 # --------------------------------------------------------------------
639 sub ol_handle_checkout {
641 my $args = ol_circ_args_from_command($command);
643 if( $args->{noncat} and $args->{noncat_count} > 99 ) {
644 return OpenILS::Event->new(
645 'INTERNAL_SERVER_ERROR', payload => 'TOO MANY NON CATS');
648 return $U->simplereq(
649 'open-ils.circ', 'open-ils.circ.checkout', $authtoken, $args );
653 # --------------------------------------------------------------------
654 # Performs the renewal action
655 # --------------------------------------------------------------------
656 sub ol_handle_renew {
658 my $args = ol_circ_args_from_command($command);
660 return $U->simplereq(
661 'open-ils.circ', 'open-ils.circ.renew', $authtoken, $args );
665 # --------------------------------------------------------------------
666 # Runs a checkin action
667 # --------------------------------------------------------------------
668 sub ol_handle_checkin {
671 my $realtime = $command->{_realtime};
672 my $ws = $command->{_workstation};
673 my $barcode = $command->{barcode};
674 my $backdate = $command->{backdate} || "";
676 $logger->activity("offline: checkin : requestor=". $requestor->id.
677 ", realtime=$realtime, ". "workstation=$ws, barcode=$barcode, backdate=$backdate");
679 return $U->simplereq(
681 'open-ils.circ.checkin', $authtoken,
682 { barcode => $barcode, backdate => $backdate } );
687 # --------------------------------------------------------------------
688 # Registers a new patron
689 # --------------------------------------------------------------------
690 sub ol_handle_register {
693 my $barcode = $command->{user}->{card}->{barcode};
694 delete $command->{user}->{card};
696 $logger->info("offline: creating new user with barcode $barcode");
698 # now, create the user
699 my $actor = Fieldmapper::actor::user->new;
700 my $card = Fieldmapper::actor::card->new;
703 # username defaults to the barcode
704 $actor->usrname( ($actor->usrname) ? $actor->usrname : $barcode );
706 # Set up all of the virtual IDs, isnew, etc.
710 $actor->cards([$card]);
715 $card->barcode($barcode);
721 for my $resp (@{$command->{user}->{survey_responses}}) {
722 my $sr = Fieldmapper::action::survey_response->new;
723 $sr->$_( $resp->{$_} ) for keys %$resp;
727 $logger->debug("offline: created new survey response for survey ".$sr->survey);
729 delete $command->{user}->{survey_responses};
730 $actor->survey_responses(\@sresp) if @sresp;
732 # extract the billing address
733 if( my $addr = $command->{user}->{billing_address} ) {
734 $billing_address = Fieldmapper::actor::user_address->new;
735 $billing_address->$_($addr->{$_}) for keys %$addr;
736 $billing_address->isnew(1);
737 $billing_address->id(-1);
738 $billing_address->usr(-1);
739 delete $command->{user}->{billing_address};
740 $logger->debug("offline: read billing address ".$billing_address->street1);
743 # extract the mailing address
744 if( my $addr = $command->{user}->{mailing_address} ) {
745 $mailing_address = Fieldmapper::actor::user_address->new;
746 $mailing_address->$_($addr->{$_}) for keys %$addr;
747 $mailing_address->isnew(1);
748 $mailing_address->id(-2);
749 $mailing_address->usr(-1);
750 delete $command->{user}->{mailing_address};
751 $logger->debug("offline: read mailing address ".$mailing_address->street1);
754 # make sure we have values for both
755 $billing_address ||= $mailing_address;
756 $mailing_address ||= $billing_address;
758 $actor->billing_address($billing_address->id);
759 $actor->mailing_address($mailing_address->id);
760 $actor->addresses([$mailing_address]);
762 push( @{$actor->addresses}, $billing_address )
763 unless $billing_address->id eq $mailing_address->id;
765 # pull all of the rest of the data from the command blob
766 $actor->$_( $command->{user}->{$_} ) for keys %{$command->{user}};
768 $logger->debug("offline: creating user object...");
769 $actor = $U->simplereq(
771 'open-ils.actor.patron.update', $authtoken, $actor);
773 return $actor if(ref($actor) eq 'HASH'); # an event occurred
775 return OpenILS::Event->new('SUCCESS', payload => $actor);