2 use strict; use warnings;
4 # --------------------------------------------------------------------
5 # Loads the offline script files for a given org, sorts and runs the
6 # scripts, and returns the exception list
7 # --------------------------------------------------------------------
9 our ($REQUESTOR, $META_FILE, $LOCK_FILE, $AUTHTOKEN, $U, %config, $cgi, $base_dir, $logger, $ORG);
11 require 'offline-lib.pl';
13 my $evt = $U->check_perms($REQUESTOR->id, $ORG, 'OFFLINE_EXECUTE');
14 handle_event($evt) if $evt;
16 my $resp = &process_data( &sort_data( &collect_data() ) );
18 handle_event(OpenILS::Event->new('SUCCESS', payload => $resp));
22 # --------------------------------------------------------------------
23 # Collects all of the script logs into an in-memory structure that
25 # Returns a blob like -> { $ws1 => [ commands... ], $ws2 => [ commands... ] }
26 # --------------------------------------------------------------------
29 handle_event(OpenILS::Event->new('OFFLINE_PARAM_ERROR')) unless $ORG;
30 my $dir = get_pending_dir();
31 handle_event(OpenILS::Event->new('OFFLINE_SESSION_ACTIVE')) if (-e "$dir/$LOCK_FILE");
33 # Lock the pending directory
34 system(("touch", "$dir/$LOCK_FILE")) == 0
35 or handle_event(OpenILS::Event->new('OFFLINE_FILE_ERROR'));
40 # Load the data from the list of files
41 while( ($file = <$dir/*.log>) ) {
42 $logger->debug("offline: Loading script file $file");
43 open(F, $file) or handle_event(
44 OpenILS::Event->new('OFFLINE_FILE_ERROR'));
45 my $ws = log_to_wsname($file);
47 push(@{$data{$ws}}, , <F>);
54 # --------------------------------------------------------------------
56 # --------------------------------------------------------------------
61 $logger->debug("offline: Sorting data");
62 my $meta = read_meta();
64 # cycle through the workstations
65 for my $ws (keys %$data) {
67 # find the meta line for this ws.
68 my ($m) = grep { $_->{workstation} eq $ws } @$meta;
70 $logger->debug("offline: Sorting workstations $ws with a time delta of ".$m->{delta});
72 my @scripts = @{$$data{$ws}};
74 # cycle through the scripts for the current workstation
75 for my $s (@scripts) {
76 my $command = JSON->JSON2perl($s);
77 $command->{_workstation} = $ws;
78 $command->{_realtime} = $command->{timestamp} + $m->{delta};
79 $logger->debug("offline: setting realtime to ".
80 $command->{_realtime} . " from timestamp " . $command->{timestamp});
81 push( @parsed, $command );
86 @parsed = sort { $a->{_realtime} <=> $b->{_realtime} } @parsed;
91 # --------------------------------------------------------------------
92 # Runs the commands and returns the list of errors
93 # --------------------------------------------------------------------
101 push( @resp, {command => $d, event => handle_checkin($d)}) if( $t eq 'checkin' );
102 push( @resp, {command => $d, event => handle_inhouse($d)}) if( $t eq 'in_house_use' );
103 push( @resp, {command => $d, event => handle_checkout($d)}) if( $t eq 'checkout' );
104 push( @resp, {command => $d, event => handle_renew($d)}) if( $t eq 'renew' );
105 push( @resp, {command => $d, event => handle_register($d)}) if( $t eq 'register' );
111 # --------------------------------------------------------------------
112 # Runs a checkin action
113 # --------------------------------------------------------------------
117 my $realtime = $command->{_realtime};
118 my $ws = $command->{_workstation};
119 my $barcode = $command->{barcode};
120 my $backdate = $command->{backdate} || "";
122 $logger->activity("offline: checkin : requestor=". $REQUESTOR->id.
123 ", realtime=$realtime, ". "workstation=$ws, barcode=$barcode, backdate=$backdate");
125 return $U->simplereq(
127 'open-ils.circ.checkin', $AUTHTOKEN,
128 { barcode => $barcode, backdate => $backdate } );
132 # --------------------------------------------------------------------
133 # Runs an in_house_use action
134 # --------------------------------------------------------------------
138 my $realtime = $command->{_realtime};
139 my $ws = $command->{_workstation};
140 my $barcode = $command->{barcode};
141 my $count = $command->{count} || 1;
143 $logger->activity("offline: in_house_use : requestor=". $REQUESTOR->id.
144 ", realtime=$realtime, ". "workstation=$ws, barcode=$barcode, count=$count");
146 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
149 my $ids = $U->simplereq(
151 'open-ils.circ.in_house_use.create', $AUTHTOKEN,
152 { copyid => $copy->id, count => $count, location => $ORG} );
154 return OpenILS::Event->new('SUCCESS', payload => $ids) if( ref($ids) eq 'ARRAY' );
160 sub handle_checkout {
162 return OpenILS::Event->new('SUCCESS', payload => $command);
168 return OpenILS::Event->new('SUCCESS', payload => $command);
172 sub handle_register {
174 return OpenILS::Event->new('SUCCESS', payload => $command);
179 # --------------------------------------------------------------------
180 # Removes the log file and Moves the script files from the pending
181 # directory to the archive dir
182 # --------------------------------------------------------------------
184 my $archivedir = create_archive_dir();
185 my $pendingdir = get_pending_dir();
186 my @files = <$pendingdir/*.log>;
187 push(@files, <$pendingdir/$META_FILE>);
189 $logger->debug("offline: Archiving files to $archivedir...");
190 system( ("rm", "$pendingdir/$LOCK_FILE") ) == 0
191 or handle_event(OpenILS::Event->new('OFFLINE_FILE_ERROR'));
193 return unless (<$pendingdir/*>);
196 system( ("mv", "$f", "$archivedir") ) == 0
197 or handle_event(OpenILS::Event->new('OFFLINE_FILE_ERROR'));
200 system( ("rmdir", "$pendingdir") ) == 0
201 or handle_event(OpenILS::Event->new('OFFLINE_FILE_ERROR'));