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 = "${SES}::Scrtip";
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 # --------------------------------------------------------------------
57 $DB->DBFile($config{db});
58 OpenSRF::System->bootstrap_client(config_file => $config{bootstrap} );
62 # --------------------------------------------------------------------
63 # Finds the requestor and other info specific to this request
64 # --------------------------------------------------------------------
67 # fetch the requestor object
68 ($requestor, $evt) = $U->checkses($authtoken);
69 ol_handle_result($evt) if $evt;
72 # try the param, the workstation, and finally the user's ws org
74 $wsobj = ol_fetch_workstation($wsname);
75 $org = $wsobj->ws_ou if $wsobj;
76 $org = $requestor->ws_ou unless $org;
77 ol_handle_result(OpenILS::Event->new('OFFLINE_NO_ORG')) unless $org;
82 # --------------------------------------------------------------------
83 # Runs the requested action
84 # --------------------------------------------------------------------
87 if( $action eq 'create' ) {
89 $evt = $U->check_perms($requestor, $org, 'OFFLINE_UPLOAD');
90 ol_handle_result($evt) if $evt;
93 } elsif( $action eq 'load' ) {
95 $evt = $U->check_perms($requestor, $org, 'OFFLINE_UPLOAD');
96 ol_handle_result($evt) if $evt;
99 } elsif( $action eq 'execute' ) {
101 $evt = $U->check_perms($requestor, $org, 'OFFLINE_EXECUTE');
102 ol_handle_result($evt) if $evt;
105 } elsif( $action eq 'status' ) {
107 $evt = $U->check_perms($requestor, $org, 'OFFLINE_VIEW');
108 ol_handle_result($evt) if $evt;
114 # --------------------------------------------------------------------
115 # Creates a new session
116 # --------------------------------------------------------------------
117 sub ol_create_session {
119 my $desc = $cgi->param('desc') || "";
120 $seskey ||= time . "_${$}_" . rand();
122 $logger->offline("offline: user ".$requestor->id.
123 " creating new session with key $seskey and description $desc");
129 description => $desc,
130 creator => $requestor->id,
131 create_time => CORE::time(),
135 ol_handle_event('SUCCESS', payload => $seskey );
139 # --------------------------------------------------------------------
140 # Holds the meta-info for a script file
141 # --------------------------------------------------------------------
142 sub ol_create_script {
143 my $ws = shift || $wsname;
144 my $sk = shift || $seskey;
146 my $session = ol_find_sesion($sk);
147 my $delta = $cgi->param('delta') || 0;
149 my $script = $session->add_to_scripts(
151 requestor => $requestor->id,
152 timestamp => CORE::time,
154 logfile => "$basedir/pending/$sk/$ws.log",
155 time_delta => $delta,
159 # --------------------------------------------------------------------
160 # Finds the current session in the db
161 # --------------------------------------------------------------------
162 sub ol_find_session {
163 my $sk = shift || $seskey;
164 my $ses = $SES->retrieve($seskey);
165 ol_handle_event('OFFLINE_INVALID_SESSION', $seskey) unless $ses;
169 # --------------------------------------------------------------------
170 # Finds a script object in the DB based on workstation and seskey
171 # --------------------------------------------------------------------
173 my $ws = shift || $wsname;
174 my $sk = shift || $seskey;
175 my ($script) = $SCRIPT->search( session => $seskey, workstation => $ws );
180 # --------------------------------------------------------------------
181 # Creates a new script in the database and loads the new script file
182 # --------------------------------------------------------------------
184 my $session = ol_find_session;
185 my $handle = $cgi->upload('file');
186 my $outfile = "$basedir/pending/$seskey/$wsname.log";
188 ol_handl_event('OFFLINE_SESSION_FILE_EXISTS') if ol_find_script();
189 ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
191 open(FILE, ">>$outfile") or ol_handle_event('OFFLINE_FILE_ERROR');
192 while( <$handle> ) { print FILE; }
199 sub ol_handle_result {
201 my $json = JSON->perl2JSON($obj);
203 if( $cgi->param('html')) {
204 my $html = "<html><body onload='xulG.handle_event($json)></body></html>";
205 print "content-type: text/html\n\n";
210 print "content-type: text/plain\n\n";
217 sub ol_handle_event {
218 my( $evt, @args ) = @_;
219 ol_handle_result(OpenILS::Event->new($evt, @args));
223 my $session = ol_find_session();
224 my $scripts = $session->retrieve_all;
228 map { $data{$_} = $session->$_ } $session->columns;
231 for my $script ($scripts->columns) {
233 map { $sdata{$_} = $script->$_ } $script->columns;
234 push( @{$data{scripts}}, \%sdata );
237 my $type = $cgi->param('status_type');
239 ol_handle_result(\%data) if( ! $type || $type eq 'scripts' )
243 sub ol_fetch_workstation {
245 $logger->debug("offline: Fetching workstation $name");
246 my $ws = $U->storagereq(
247 'open-ils.storage.direct.actor.workstation.search.name', $name);
248 ol_handle_result(OpenILS::Event->new('WORKSTATION_NOT_FOUND')) unless $ws;