]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/offline/offline.pl
getting closer
[working/Evergreen.git] / Open-ILS / src / offline / offline.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3 use CGI;
4 use OpenSRF::System;
5 use OpenSRF::Utils::Logger qw/$logger/;
6 use OpenILS::Application::AppUtils;
7 use OpenILS::Event;
8 use OpenSRF::EX qw/:try/;
9 use JSON;
10 use Data::Dumper;
11 use OpenILS::Utils::Fieldmapper;
12 use Digest::MD5 qw/md5_hex/;
13 use OpenSRF::Utils qw/:daemon/;
14 use OpenILS::Utils::OfflineStore;
15
16 use DBI;
17 $DBI::trace = 1;
18
19 #use Class::DBI;
20 #Class::DBI->autoupdate(1);
21 #local $OpenILS::Utils::OfflineStore::NO_TRIGGERS = 1;
22
23 my $U = "OpenILS::Application::AppUtils";
24 my $DB = "OpenILS::Utils::OfflineStore";
25 my $SES = "${DB}::Session";
26 my $SCRIPT = "OpenILS::Utils::OfflineStore::Script";
27 #$DB->autoupdate(1);
28
29 our %config;
30
31 # --------------------------------------------------------------------
32 # Load the config
33 # --------------------------------------------------------------------
34 #do '##CONFIG##/upload-server.pl';
35 do 'offline-config.pl';
36
37
38 my $cgi                 = new CGI;
39 my $basedir             = $config{base_dir};
40 my $wsname              = $cgi->param('ws');
41 my $org                 = $cgi->param('org');
42 my $authtoken   = $cgi->param('ses') || "";
43 my $seskey              = $cgi->param('seskey');
44 my $action              = $cgi->param('action'); # - create, load, execute, status
45 my $requestor; 
46 my $wsobj;
47 my $orgobj;
48 my $evt;
49
50
51 &ol_init;
52 &ol_runtime_init;
53 &ol_do_action;
54
55
56 # --------------------------------------------------------------------
57 # Set it all up
58 # This function should behave as a child_init might behave in case 
59 # this is moved to mod_perl
60 # --------------------------------------------------------------------
61 sub ol_init {
62         _ol_debug_params();
63         $DB->DBFile($config{db});
64         OpenSRF::System->bootstrap_client(config_file => $config{bootstrap} ); 
65 }
66
67
68 sub _ol_debug_params {
69         my $s = "";
70         my @params = $cgi->param;
71         @params = sort { $a cmp $b } @params;
72         $s .= "$_=" . $cgi->param($_) . "\n" for @params;
73         $s =~ s/\n$//o;
74         warn '-'x60 ."\n$s\n";
75 }
76
77
78 # --------------------------------------------------------------------
79 # Finds the requestor and other info specific to this request
80 # --------------------------------------------------------------------
81 sub ol_runtime_init {
82
83         # fetch the requestor object
84         ($requestor, $evt) = $U->checkses($authtoken);
85         ol_handle_result($evt) if $evt;
86
87
88         # try the param, the workstation, and finally the user's ws org
89         if(!$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;
94         }
95 }
96
97
98 # --------------------------------------------------------------------
99 # Runs the requested action
100 # --------------------------------------------------------------------
101 sub ol_do_action {
102
103         my $payload;
104
105         if( $action eq 'create' ) {
106                 
107                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
108                 ol_handle_result($evt) if $evt;
109                 $payload = ol_create_session();
110
111         } elsif( $action eq 'load' ) {
112
113                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
114                 ol_handle_result($evt) if $evt;
115                 $payload = ol_load();
116
117         } elsif( $action eq 'execute' ) {
118
119                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_EXECUTE');
120                 ol_handle_result($evt) if $evt;
121                 $payload = ol_execute();
122
123         } elsif( $action eq 'status' ) {
124
125                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_VIEW');
126                 ol_handle_result($evt) if $evt;
127                 $payload = ol_status();
128         }
129
130         ol_handle_event('SUCCESS', payload => $payload );
131 }
132
133
134 # --------------------------------------------------------------------
135 # Creates a new session
136 # --------------------------------------------------------------------
137 sub ol_create_session {
138
139         my $desc = $cgi->param('desc') || "";
140         $seskey = time . "_${$}_" . int(rand() * 1000);
141
142         $logger->debug("offline: user ".$requestor->id.
143                 " creating new session with key $seskey and description $desc");
144
145         $SES->create(
146                 {       
147                         key                             => $seskey,
148                         org                             => $org,
149                         description             => $desc,
150                         creator                 => $requestor->id,
151                         create_time             => CORE::time(), 
152                         num_complete    => 0,
153                 } 
154         );
155
156         return $seskey;
157 }
158
159
160 # --------------------------------------------------------------------
161 # Holds the meta-info for a script file
162 # --------------------------------------------------------------------
163 sub ol_create_script {
164         my $count = shift;
165
166         my $session = ol_find_session($seskey);
167         my $delta = $cgi->param('delta') || 0;
168
169         my $script = $session->add_to_scripts( 
170                 {
171                         requestor       => $requestor->id,
172                         create_time     => CORE::time,
173                         workstation     => $wsname,
174                         logfile         => "$basedir/pending/$org/$seskey/$wsname.log",
175                         time_delta      => $delta,
176                         count                   => $count,
177                 }
178         );
179 }
180
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;
187         return $ses;
188 }
189
190 # --------------------------------------------------------------------
191 # Finds a script object in the DB based on workstation and seskey
192 # --------------------------------------------------------------------
193 sub ol_find_script {
194         my $ws = shift || $wsname;
195         my $sk = shift || $seskey;
196         my ($script) = $SCRIPT->search( session => $seskey, workstation => $ws );
197         return $script;
198 }
199
200 # --------------------------------------------------------------------
201 # Creates a new script in the database and loads the new script file
202 # --------------------------------------------------------------------
203 sub ol_load {
204         my $session = ol_find_session;
205         my $handle      = $cgi->upload('file');
206         my $outdir = "$basedir/pending/$org/$seskey";
207         my $outfile = "$outdir/$wsname.log";
208
209         ol_handl_event('OFFLINE_SESSION_FILE_EXISTS') if ol_find_script();
210         ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
211         ol_handle_event('OFFLINE_SESSION_COMPLETE') if $session->end_time;
212
213         qx/mkdir -p $outdir/;
214         my $x = 0;
215         open(FILE, ">>$outfile") or ol_handle_event('OFFLINE_FILE_ERROR');
216         while( <$handle> ) { print FILE; $x++;}
217         close(FILE);
218
219         ol_create_script($x);
220
221         return undef;
222 }
223
224
225 # --------------------------------------------------------------------
226 sub ol_handle_result {
227         my $obj = shift;
228         my $json = JSON->perl2JSON($obj);
229
230         if( $cgi->param('html')) {
231                 my $html = "<html><body onload='xulG.handle_event($json)></body></html>";
232                 print "content-type: text/html\n\n";
233                 print "$html\n";
234
235         } else {
236
237                 print "content-type: text/plain\n\n";
238                 print "$json\n";
239         }
240
241         exit(0);
242 }
243
244 # --------------------------------------------------------------------
245 sub ol_handle_event {
246         my( $evt, @args ) = @_;
247         ol_handle_result(OpenILS::Event->new($evt, @args));
248 }
249
250
251 # --------------------------------------------------------------------
252 sub ol_flesh_session {
253         my $session = shift;
254         my %data;
255
256         map { $data{$_} = $session->$_ } $session->columns;
257         $data{scripts} = [];
258
259         for my $script ($session->scripts) {
260                 my %sdata;
261                 map { $sdata{$_} = $script->$_ } $script->columns;
262
263                 # the client doesn't need this info
264                 delete $sdata{session};
265                 delete $sdata{id};
266                 delete $sdata{logfile};
267
268                 push( @{$data{scripts}}, \%sdata );
269         }
270
271         return \%data;
272 }
273
274
275 # --------------------------------------------------------------------
276 # Returns various information on the sessions and scripts
277 # --------------------------------------------------------------------
278 sub ol_status {
279
280         my $type = $cgi->param('status_type') || "scripts";
281
282         # --------------------------------------------------------------------
283         # Returns info on every script upload attached to the current session
284         # --------------------------------------------------------------------
285         if( $type eq 'scripts' ) {
286                 my $session = ol_find_session();
287                 ol_handle_result(ol_flesh_session($session));
288
289         # --------------------------------------------------------------------
290         # Returns all scripts and sessions for the given org
291         # --------------------------------------------------------------------
292         } elsif( $type eq 'sessions' ) {
293                 my @sessions = $SES->search( org => $org );
294
295                 # can I do this in the DB without raw SQL?
296                 @sessions = sort { $a->create_time <=> $b->create_time } @sessions; 
297                 my @data;
298                 push( @data, ol_flesh_session($_) ) for @sessions;
299                 ol_handle_result(\@data);
300
301         # --------------------------------------------------------------------
302         # Returns total commands and completed commands counts
303         # --------------------------------------------------------------------
304         } elsif( $type eq 'summary' ) {
305                 my $session = ol_find_session();
306
307                 $logger->debug("offline: retrieving summary info ".
308                         "for session ".$session->key." with completed=".$session->num_complete);
309
310                 my $count = 0;
311                 $count += $_->count for ($session->scripts);
312                 ol_handle_result(
313                         { total => $count, num_complete => $session->num_complete });
314         }
315 }
316
317
318 sub ol_fetch_workstation {
319         my $name = shift;
320         $logger->debug("offline: Fetching workstation $name");
321         my $ws = $U->storagereq(
322                 'open-ils.storage.direct.actor.workstation.search.name', $name);
323         ol_handle_result(OpenILS::Event->new('WORKSTATION_NOT_FOUND')) unless $ws;
324         return $ws;
325 }
326
327
328
329
330 # --------------------------------------------------------------------
331 # Sorts the script commands then forks a child to executes them.
332 # --------------------------------------------------------------------
333 sub ol_execute {
334
335         my $session = ol_find_session();
336         ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
337         ol_handle_event('OFFLINE_SESSION_COMPLETE') if $session->end_time;
338
339         my $commands = ol_collect_commands();
340
341         # --------------------------------------------------------------------
342         # Note that we must disconnect from opensrf before forking or the 
343         # connection will be borked...
344         # --------------------------------------------------------------------
345         OpenSRF::Transport::PeerHandle->retrieve->disconnect;
346         #$session->in_process("1");
347         #$session->description("test ".CORE::time());
348         #$DB->disconnect;
349
350
351         if( safe_fork() ) {
352
353                 # --------------------------------------------------------------------
354                 # Tell the client all is well
355                 # --------------------------------------------------------------------
356                 ol_handle_event('SUCCESS'); # - this exits
357
358         } else {
359
360                 # --------------------------------------------------------------------
361                 # close stdout/stderr or apache will wait on the child to finish
362                 # --------------------------------------------------------------------
363                 close(STDOUT);
364                 close(STDERR);
365
366                 $logger->debug("offline: child $$ processing data...");
367
368                 # --------------------------------------------------------------------
369                 # The child re-connects to the opensrf network and processes
370                 # the script requests 
371                 # --------------------------------------------------------------------
372                 OpenSRF::System->bootstrap_client(config_file => $config{bootstrap});
373         
374                 try {
375
376                         #use Class::DBI
377                         #Class::DBI->autoupdate(1);
378                         my $sesion = ol_find_session();
379                         $session->in_process(1);
380                         ol_process_commands($session, $commands);
381                         ol_archive_files();
382
383                 } catch Error with {
384                         my $e = shift;
385                         $logger->error("offline: child process error $e");
386                 };
387         }
388 }
389
390 sub ol_file_to_perl {
391         my $fname = shift;
392         open(F, "$fname") or ol_handle_event('OFFLINE_FILE_ERROR');
393         my @d = <F>;
394         my @p;
395         push(@p, JSON->JSON2perl($_)) for @d;
396         close(F);
397         return \@p;
398 }
399
400 # collects the commands and sorts them on timestamp+delta
401 sub ol_collect_commands {
402         my $ses = ol_find_session();
403         my @commands;
404
405         # cycle through every script loaded to this session
406         for my $script ($ses->scripts) {
407                 my $coms = ol_file_to_perl($script->logfile);
408
409                 # cycle through all of the commands for this script
410                 for my $com (@$coms) {
411                         $$com{_worksation} = $script->workstation;
412                         $$com{_realtime} = $script->time_delta + $com->{timestamp};
413                         push( @commands, $com );
414                 }
415         }
416
417         # make sure thera are no blank commands
418         @commands = grep { ($_ and $_->{type}) } @commands;
419
420         # sort on realtime
421         @commands = sort { $a->{_realtime} <=> $b->{_realtime} } @commands;
422
423         # push user registrations to the front
424         my @regs                = grep { $_->{type} eq 'register' } @commands;
425         my @others      = grep { $_->{type} ne 'register' } @commands;
426
427         return [ @regs, @others ];
428 }
429
430 sub ol_date {
431         my (undef,undef, undef, $mday,$mon,$year) = localtime(time);
432         $mon++; $year   += 1900;
433         $mday   = "0$mday" unless $mday =~ /\d{2}/o;
434         $mon    = "0$mon" unless $mon   =~ /\d{2}/o;
435         return ($year, $mon, $mday);
436 }
437
438
439 # --------------------------------------------------------------------
440 # Moves all files from the pending directory to the archive directory
441 # and removes the pending directory
442 # --------------------------------------------------------------------
443 sub ol_archive_files {
444         my ($y, $m, $d) = ol_date();
445
446         my $dir = "$basedir/pending/$org/$seskey";
447         my $archdir = "$basedir/archive/$org/${y}_${m}_${d}/$seskey";
448         $logger->debug("offline: archiving files to $archdir");
449
450         qx/mkdir -p $archdir/;
451         qx/mv $_ $archdir/ for <$dir/*>;
452         qx/rmdir $dir/;
453 }
454
455
456 # --------------------------------------------------------------------
457 # Appends results to the results file.
458 # --------------------------------------------------------------------
459 my $rhandle;
460 sub ol_append_result {
461
462         my $obj = shift;
463         my $last = shift;
464
465         $obj = JSON->perl2JSON($obj);
466
467         if(!$rhandle) {
468                 open($rhandle, ">>$basedir/pending/$org/$seskey/results") 
469                         or ol_handle_event('OFFLINE_FILE_ERROR');
470         }
471
472         print $rhandle "$obj\n";
473         close($rhandle) if $last;
474 }
475
476
477
478 # --------------------------------------------------------------------
479 # Runs the commands and returns the list of errors
480 # --------------------------------------------------------------------
481 sub ol_process_commands {
482
483         my $session      = shift;
484         my $commands = shift;
485         my $x        = 0;
486
487         $session->start_time(CORE::time);
488
489         for my $d ( @$commands ) {
490
491                 my $t           = $d->{type};
492                 my $last = ($x++ == scalar(@$commands) - 1) ? 1 : 0;
493                 my $res = { command => $d };
494
495                 $res->{event} = ol_handle_checkin($d)   if $t eq 'checkin';
496                 $res->{event} = ol_handle_inhouse($d)   if $t eq 'in_house_use';
497                 $res->{event} = ol_handle_checkout($d) if $t eq 'checkout';
498                 $res->{event} = ol_handle_renew($d)             if $t eq 'renew';
499                 $res->{event} = ol_handle_register($d) if $t eq 'register';
500
501
502                 ol_append_result($res, $last);
503                 $session->num_complete( $session->num_complete + 1 );
504
505                 $logger->debug("offline: set session [".$session->key."] num_complete to ".$session->num_complete);
506         }
507
508         $session->end_time(CORE::time);
509         $session->in_process(0);
510 }
511
512
513 # --------------------------------------------------------------------
514 # Runs an in_house_use action
515 # --------------------------------------------------------------------
516 sub ol_handle_inhouse {
517
518         my $command             = shift;
519         my $realtime    = $command->{_realtime};
520         my $ws                  = $command->{_workstation};
521         my $barcode             = $command->{barcode};
522         my $count               = $command->{count} || 1;
523         my $use_time    = $command->{use_time} || "";
524
525         $logger->activity("offline: in_house_use : requestor=". $requestor->id.", realtime=$realtime, ".  
526                 "workstation=$ws, barcode=$barcode, count=$count, use_time=$use_time");
527
528         my $ids = $U->simplereq(
529                 'open-ils.circ', 
530                 'open-ils.circ.in_house_use.create', $authtoken, 
531                 { barcode => $barcode, count => $count, location => &offline_org, use_time => $use_time } );
532         
533         return OpenILS::Event->new('SUCCESS', payload => $ids) if( ref($ids) eq 'ARRAY' );
534         return $ids;
535 }
536
537
538
539 # --------------------------------------------------------------------
540 # Pulls the relevant circ args from the command, fetches data where 
541 # necessary
542 # --------------------------------------------------------------------
543 sub ol_circ_args_from_command {
544         my $command = shift;
545
546         my $type                        = $command->{type};
547         my $realtime    = $command->{_realtime};
548         my $ws                  = $command->{_workstation};
549         my $barcode             = $command->{barcode} || "";
550         my $cotime              = $command->{checkout_time} || "";
551         my $pbc                 = $command->{patron_barcode};
552         my $due_date    = $command->{due_date} || "";
553         my $noncat              = ($command->{noncat}) ? "yes" : "no"; # for logging
554
555         $logger->activity("offline: $type : requestor=". $requestor->id.
556                 ", realtime=$realtime, workstation=$ws, checkout_time=$cotime, ".
557                 "patron=$pbc, due_date=$due_date, noncat=$noncat");
558
559         my $args = { 
560                 permit_override => 1, 
561                 barcode                         => $barcode,            
562                 checkout_time           => $cotime, 
563                 patron_barcode          => $pbc,
564                 due_date                                => $due_date };
565
566         if( $command->{noncat} ) {
567                 $args->{noncat} = 1;
568                 $args->{noncat_type} = $command->{noncat_type};
569                 $args->{noncat_count} = $command->{noncat_count};
570         }
571
572         return $args;
573 }
574
575
576
577 # --------------------------------------------------------------------
578 # Performs a checkout action
579 # --------------------------------------------------------------------
580 sub ol_handle_checkout {
581         my $command     = shift;
582         my $args = ol_circ_args_from_command($command);
583         return $U->simplereq(
584                 'open-ils.circ', 'open-ils.circ.checkout', $authtoken, $args );
585 }
586
587
588 # --------------------------------------------------------------------
589 # Performs the renewal action
590 # --------------------------------------------------------------------
591 sub ol_handle_renew {
592         my $command = shift;
593         my $args = ol_circ_args_from_command($command);
594         my $t = time;
595         return $U->simplereq(
596                 'open-ils.circ', 'open-ils.circ.renew', $authtoken, $args );
597 }
598
599
600 # --------------------------------------------------------------------
601 # Runs a checkin action
602 # --------------------------------------------------------------------
603 sub ol_handle_checkin {
604
605         my $command             = shift;
606         my $realtime    = $command->{_realtime};
607         my $ws                  = $command->{_workstation};
608         my $barcode             = $command->{barcode};
609         my $backdate    = $command->{backdate} || "";
610
611         $logger->activity("offline: checkin : requestor=". $requestor->id.
612                 ", realtime=$realtime, ".  "workstation=$ws, barcode=$barcode, backdate=$backdate");
613
614         return $U->simplereq(
615                 'open-ils.circ', 
616                 'open-ils.circ.checkin', $authtoken,
617                 { barcode => $barcode, backdate => $backdate } );
618 }
619
620
621
622 # --------------------------------------------------------------------
623 # Registers a new patron
624 # --------------------------------------------------------------------
625 sub ol_handle_register {
626         my $command = shift;
627
628         my $barcode = $command->{user}->{card}->{barcode};
629         delete $command->{user}->{card}; 
630
631         $logger->info("offline: creating new user with barcode $barcode");
632
633         # now, create the user
634         my $actor       = Fieldmapper::actor::user->new;
635         my $card                = Fieldmapper::actor::card->new;
636
637
638         # username defaults to the barcode
639         $actor->usrname( ($actor->usrname) ? $actor->usrname : $barcode );
640
641         # Set up all of the virtual IDs, isnew, etc.
642         $actor->isnew(1);
643         $actor->id(-1);
644         $actor->card(-1);
645         $actor->cards([$card]);
646
647         $card->isnew(1);
648         $card->id(-1);
649         $card->usr(-1);
650         $card->barcode($barcode);
651
652         my $billing_address;
653         my $mailing_address;
654
655         my @sresp;
656         for my $resp (@{$command->{user}->{survey_responses}}) {
657                 my $sr = Fieldmapper::action::survey_response->new;
658                 $sr->$_( $resp->{$_} ) for keys %$resp;
659                 $sr->isnew(1);
660                 $sr->usr(-1);
661                 push(@sresp, $sr);
662                 $logger->debug("offline: created new survey response for survey ".$sr->survey);
663         }
664         delete $command->{user}->{survey_responses};
665         $actor->survey_responses(\@sresp) if @sresp;
666
667         # extract the billing address
668         if( my $addr = $command->{user}->{billing_address} ) {
669                 $billing_address = Fieldmapper::actor::user_address->new;
670                 $billing_address->$_($addr->{$_}) for keys %$addr;
671                 $billing_address->isnew(1);
672                 $billing_address->id(-1);
673                 $billing_address->usr(-1);
674                 delete $command->{user}->{billing_address};
675                 $logger->debug("offline: read billing address ".$billing_address->street1);
676         }
677
678         # extract the mailing address
679         if( my $addr = $command->{user}->{mailing_address} ) {
680                 $mailing_address = Fieldmapper::actor::user_address->new;
681                 $mailing_address->$_($addr->{$_}) for keys %$addr;
682                 $mailing_address->isnew(1);
683                 $mailing_address->id(-2);
684                 $mailing_address->usr(-1);
685                 delete $command->{user}->{mailing_address};
686                 $logger->debug("offline: read mailing address ".$mailing_address->street1);
687         }
688
689         # make sure we have values for both
690         $billing_address ||= $mailing_address;
691         $mailing_address ||= $billing_address;
692
693         $actor->billing_address($billing_address->id);
694         $actor->mailing_address($mailing_address->id);
695         $actor->addresses([$mailing_address]);
696
697         push( @{$actor->addresses}, $billing_address ) 
698                 unless $billing_address->id eq $mailing_address->id;
699         
700         # pull all of the rest of the data from the command blob
701         $actor->$_( $command->{user}->{$_} ) for keys %{$command->{user}};
702
703         $logger->debug("offline: creating user object...");
704         $actor = $U->simplereq(
705                 'open-ils.actor', 
706                 'open-ils.actor.patron.update', $authtoken, $actor);
707
708         return $actor if(ref($actor) eq 'HASH'); # an event occurred
709
710         return OpenILS::Event->new('SUCCESS', payload => $actor);
711 }
712
713
714
715
716
717
718