]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/offline/offline.pl
now creating sessions and uploading script files
[Evergreen.git] / Open-ILS / src / offline / offline.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3 use DBI;
4 use CGI;
5 use OpenSRF::System;
6 use OpenSRF::Utils::Logger qw/$logger/;
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Event;
9 use OpenSRF::EX qw/:try/;
10 use JSON;
11 use Data::Dumper;
12 use OpenILS::Utils::Fieldmapper;
13 use Digest::MD5 qw/md5_hex/;
14 use OpenSRF::Utils qw/:daemon/;
15 use OpenILS::Utils::OfflineStore;
16 $DBI::trace = 1;
17
18 my $U = "OpenILS::Application::AppUtils";
19 my $DB = "OpenILS::Utils::OfflineStore";
20 my $SES = "${DB}::Session";
21 my $SCRIPT = "OpenILS::Utils::OfflineStore::Script";
22
23 our %config;
24
25 # --------------------------------------------------------------------
26 # Load the config
27 # --------------------------------------------------------------------
28 #do '##CONFIG##/upload-server.pl';
29 do 'offline-config.pl';
30
31
32 my $cgi                 = new CGI;
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
39 my $requestor; 
40 my $wsobj;
41 my $orgobj;
42 my $evt;
43
44
45 &ol_init;
46 &ol_runtime_init;
47 &ol_do_action;
48
49
50 # --------------------------------------------------------------------
51 # Set it all up
52 # This function should behave as a child_init might behave in case 
53 # this is moved to mod_perl
54 # --------------------------------------------------------------------
55 sub ol_init {
56
57         my $s = "";
58         $s .= "$_=" . $cgi->param($_) . "\n" for $cgi->param;
59         warn '-'x60 ."\n$s\n" . '-'x60 ."\n";
60
61         $DB->DBFile($config{db});
62         OpenSRF::System->bootstrap_client(config_file => $config{bootstrap} ); 
63 }
64
65
66 # --------------------------------------------------------------------
67 # Finds the requestor and other info specific to this request
68 # --------------------------------------------------------------------
69 sub ol_runtime_init {
70
71         # fetch the requestor object
72         ($requestor, $evt) = $U->checkses($authtoken);
73         ol_handle_result($evt) if $evt;
74
75
76         # try the param, the workstation, and finally the user's ws org
77         if(!$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;
82         }
83 }
84
85
86 # --------------------------------------------------------------------
87 # Runs the requested action
88 # --------------------------------------------------------------------
89 sub ol_do_action {
90
91         my $payload;
92
93         if( $action eq 'create' ) {
94                 
95                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
96                 ol_handle_result($evt) if $evt;
97                 $payload = ol_create_session();
98
99         } elsif( $action eq 'load' ) {
100
101                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_UPLOAD');
102                 ol_handle_result($evt) if $evt;
103                 $payload = ol_load();
104
105         } elsif( $action eq 'execute' ) {
106
107                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_EXECUTE');
108                 ol_handle_result($evt) if $evt;
109                 $payload = ol_execute();
110
111         } elsif( $action eq 'status' ) {
112
113                 $evt = $U->check_perms($requestor->id, $org, 'OFFLINE_VIEW');
114                 ol_handle_result($evt) if $evt;
115                 $payload = ol_status();
116         }
117
118         ol_handle_event('SUCCESS', payload => $payload );
119 }
120
121
122 # --------------------------------------------------------------------
123 # Creates a new session
124 # --------------------------------------------------------------------
125 sub ol_create_session {
126
127         my $desc = $cgi->param('desc') || "";
128         $seskey ||=  time . "_${$}_" . int(rand() * 100);
129
130         $logger->debug("offline: user ".$requestor->id.
131                 " creating new session with key $seskey and description $desc");
132
133         $SES->create(
134                 {       
135                         key                     => $seskey,
136                         org                     => $org,
137                         description => $desc,
138                         creator         => $requestor->id,
139                         create_time => CORE::time(), 
140                         complete                => 0,
141                 } 
142         );
143
144         return $seskey;
145 }
146
147
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;
154
155         my $session = ol_find_session($sk);
156         my $delta = $cgi->param('delta') || 0;
157
158         my $script = $session->add_to_scripts( {
159                 requestor       => $requestor->id,
160                 timestamp       => CORE::time,
161                 workstation     => $ws,
162                 logfile         => "$basedir/pending/$org/$sk/$ws.log",
163                 time_delta      => $delta,
164         });
165 }
166
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;
174         return $ses;
175 }
176
177 # --------------------------------------------------------------------
178 # Finds a script object in the DB based on workstation and seskey
179 # --------------------------------------------------------------------
180 sub ol_find_script {
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;
185         return $script;
186 }
187
188 # --------------------------------------------------------------------
189 # Creates a new script in the database and loads the new script file
190 # --------------------------------------------------------------------
191 sub ol_load {
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";
196
197         ol_handl_event('OFFLINE_SESSION_FILE_EXISTS') if ol_find_script();
198         ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
199
200         qx/mkdir -p $outdir/;
201         open(FILE, ">>$outfile") or ol_handle_event('OFFLINE_FILE_ERROR');
202         while( <$handle> ) { print FILE; }
203         close(FILE);
204
205         ol_create_script();
206
207         return undef;
208 }
209
210
211 sub ol_handle_result {
212         my $obj = shift;
213         my $json = JSON->perl2JSON($obj);
214
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";
218                 print "$html\n";
219
220         } else {
221
222                 print "content-type: text/plain\n\n";
223                 print "$json\n";
224         }
225
226         exit(0);
227 }
228
229 sub ol_handle_event {
230         my( $evt, @args ) = @_;
231         ol_handle_result(OpenILS::Event->new($evt, @args));
232 }
233
234 sub ol_status {
235
236         my $session = ol_find_session();
237         my %data;
238
239         map { $data{$_} = $session->$_ } $session->columns;
240         $data{scripts} = [];
241
242         for my $script ($session->scripts) {
243                 my %sdata;
244                 map { $sdata{$_} = $script->$_ } $script->columns;
245                 push( @{$data{scripts}}, \%sdata );
246         }
247
248         my $type = $cgi->param('status_type');
249
250         ol_handle_result(\%data) if( ! $type || $type eq 'scripts' ) 
251 }
252
253
254 sub ol_fetch_workstation {
255         my $name = shift;
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;
260         return $ws;
261 }
262
263