2 use strict; use warnings;
6 use OpenSRF::Utils::Logger qw/$logger/;
7 use OpenILS::Application::AppUtils;
9 use OpenSRF::EX qw/:try/;
12 use OpenILS::Utils::Fieldmapper;
13 use Digest::MD5 qw/md5_hex/;
14 use OpenSRF::Utils qw/:daemon/;
15 use OpenILS::Utils::OfflineStore;
18 my $U = "OpenILS::Application::AppUtils";
19 my $DB = "OpenILS::Utils::OfflineStore";
20 my $SES = "${DB}::Session";
21 my $SCRIPT = "OpenILS::Utils::OfflineStore::Script";
25 # --------------------------------------------------------------------
27 # --------------------------------------------------------------------
28 #do '##CONFIG##/upload-server.pl';
29 do 'offline-config.pl';
33 my $basedir = $config{base_dir};
34 my $wsname = $cgi->param('ws');
35 my $org = $cgi->param('org');
36 my $authtoken = $cgi->param('ses') || "";
37 my $seskey = $cgi->param('seskey');
38 my $action = $cgi->param('action'); # - create, load, execute, status
50 # --------------------------------------------------------------------
52 # This function should behave as a child_init might behave in case
53 # this is moved to mod_perl
54 # --------------------------------------------------------------------
58 $s .= "$_=" . $cgi->param($_) . "\n" for $cgi->param;
59 warn '-'x60 ."\n$s\n" . '-'x60 ."\n";
61 $DB->DBFile($config{db});
62 OpenSRF::System->bootstrap_client(config_file => $config{bootstrap} );
66 # --------------------------------------------------------------------
67 # Finds the requestor and other info specific to this request
68 # --------------------------------------------------------------------
71 # fetch the requestor object
72 ($requestor, $evt) = $U->checkses($authtoken);
73 ol_handle_result($evt) if $evt;
76 # try the param, the workstation, and finally the user's ws org
78 $wsobj = ol_fetch_workstation($wsname);
79 $org = $wsobj->owning_lib if $wsobj;
80 $org = $requestor->ws_ou unless $org;
81 ol_handle_result(OpenILS::Event->new('OFFLINE_NO_ORG')) unless $org;
86 # --------------------------------------------------------------------
87 # Runs the requested action
88 # --------------------------------------------------------------------
93 if( $action eq 'create' ) {
95 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
96 ol_handle_result($evt) if $evt;
97 $payload = ol_create_session();
99 } elsif( $action eq 'load' ) {
101 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
102 ol_handle_result($evt) if $evt;
103 $payload = ol_load();
105 } elsif( $action eq 'execute' ) {
107 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_EXECUTE');
108 ol_handle_result($evt) if $evt;
109 $payload = ol_execute();
111 } elsif( $action eq 'status' ) {
113 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_VIEW');
114 ol_handle_result($evt) if $evt;
115 $payload = ol_status();
118 ol_handle_event('SUCCESS', payload => $payload );
122 # --------------------------------------------------------------------
123 # Creates a new session
124 # --------------------------------------------------------------------
125 sub ol_create_session {
127 my $desc = $cgi->param('desc') || "";
128 $seskey ||= time . "_${$}_" . int(rand() * 100);
130 $logger->debug("offline: user ".$requestor->id.
131 " creating new session with key $seskey and description $desc");
137 description => $desc,
138 creator => $requestor->id,
139 create_time => CORE::time(),
148 # --------------------------------------------------------------------
149 # Holds the meta-info for a script file
150 # --------------------------------------------------------------------
151 sub ol_create_script {
152 my $ws = shift || $wsname;
153 my $sk = shift || $seskey;
155 my $session = ol_find_session($sk);
156 my $delta = $cgi->param('delta') || 0;
158 my $script = $session->add_to_scripts( {
159 requestor => $requestor->id,
160 timestamp => CORE::time,
162 logfile => "$basedir/pending/$org/$sk/$ws.log",
163 time_delta => $delta,
167 # --------------------------------------------------------------------
168 # Finds the current session in the db
169 # --------------------------------------------------------------------
170 sub ol_find_session {
171 my $sk = shift || $seskey;
172 my $ses = $SES->retrieve($seskey);
173 ol_handle_event('OFFLINE_INVALID_SESSION', $seskey) unless $ses;
177 # --------------------------------------------------------------------
178 # Finds a script object in the DB based on workstation and seskey
179 # --------------------------------------------------------------------
181 my $ws = shift || $wsname;
182 my $sk = shift || $seskey;
183 my ($id) = $SCRIPT->search( session => $seskey, workstation => $ws );
184 my ($script) = $SCRIPT->retrieve("$id") if $id;
188 # --------------------------------------------------------------------
189 # Creates a new script in the database and loads the new script file
190 # --------------------------------------------------------------------
192 my $session = ol_find_session;
193 my $handle = $cgi->upload('file');
194 my $outdir = "$basedir/pending/$org/$seskey";
195 my $outfile = "$outdir/$wsname.log";
197 ol_handl_event('OFFLINE_SESSION_FILE_EXISTS') if ol_find_script();
198 ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
200 qx/mkdir -p $outdir/;
201 open(FILE, ">>$outfile") or ol_handle_event('OFFLINE_FILE_ERROR');
202 while( <$handle> ) { print FILE; }
211 sub ol_handle_result {
213 my $json = JSON->perl2JSON($obj);
215 if( $cgi->param('html')) {
216 my $html = "<html><body onload='xulG.handle_event($json)></body></html>";
217 print "content-type: text/html\n\n";
222 print "content-type: text/plain\n\n";
229 sub ol_handle_event {
230 my( $evt, @args ) = @_;
231 ol_handle_result(OpenILS::Event->new($evt, @args));
236 my $session = ol_find_session();
239 map { $data{$_} = $session->$_ } $session->columns;
242 for my $script ($session->scripts) {
244 map { $sdata{$_} = $script->$_ } $script->columns;
245 push( @{$data{scripts}}, \%sdata );
248 my $type = $cgi->param('status_type');
250 ol_handle_result(\%data) if( ! $type || $type eq 'scripts' )
254 sub ol_fetch_workstation {
256 $logger->debug("offline: Fetching workstation $name");
257 my $ws = $U->storagereq(
258 'open-ils.storage.direct.actor.workstation.search.name', $name);
259 ol_handle_result(OpenILS::Event->new('WORKSTATION_NOT_FOUND')) unless $ws;