]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/offline/offline.pl
beginnings of sqlite version
[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 = "${SES}::Scrtip";
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         $DB->DBFile($config{db});
58         OpenSRF::System->bootstrap_client(config_file => $config{bootstrap} ); 
59 }
60
61
62 # --------------------------------------------------------------------
63 # Finds the requestor and other info specific to this request
64 # --------------------------------------------------------------------
65 sub ol_runtime_init {
66
67         # fetch the requestor object
68         ($requestor, $evt) = $U->checkses($authtoken);
69         ol_handle_result($evt) if $evt;
70
71
72         # try the param, the workstation, and finally the user's ws org
73         if(!$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;
78         }
79 }
80
81
82 # --------------------------------------------------------------------
83 # Runs the requested action
84 # --------------------------------------------------------------------
85 sub ol_do_action {
86
87         if( $action eq 'create' ) {
88                 
89                 $evt = $U->check_perms($requestor, $org, 'OFFLINE_UPLOAD');
90                 ol_handle_result($evt) if $evt;
91                 ol_create_session();
92
93         } elsif( $action eq 'load' ) {
94
95                 $evt = $U->check_perms($requestor, $org, 'OFFLINE_UPLOAD');
96                 ol_handle_result($evt) if $evt;
97                 ol_load();
98
99         } elsif( $action eq 'execute' ) {
100
101                 $evt = $U->check_perms($requestor, $org, 'OFFLINE_EXECUTE');
102                 ol_handle_result($evt) if $evt;
103                 ol_execute();
104
105         } elsif( $action eq 'status' ) {
106
107                 $evt = $U->check_perms($requestor, $org, 'OFFLINE_VIEW');
108                 ol_handle_result($evt) if $evt;
109                 ol_status();
110         }
111 }
112
113
114 # --------------------------------------------------------------------
115 # Creates a new session
116 # --------------------------------------------------------------------
117 sub ol_create_session {
118
119         my $desc = $cgi->param('desc') || "";
120         $seskey ||=  time . "_${$}_" . rand();
121
122         $logger->offline("offline: user ".$requestor->id.
123                 " creating new session with key $seskey and description $desc");
124
125         $SES->create(
126                 {       
127                         key                     => $seskey,
128                         org                     => $org,
129                         description => $desc,
130                         creator         => $requestor->id,
131                         create_time => CORE::time(), 
132                         complete                => 0,
133                 } 
134         );
135         ol_handle_event('SUCCESS', payload => $seskey );
136 }
137
138
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;
145
146         my $session = ol_find_sesion($sk);
147         my $delta = $cgi->param('delta') || 0;
148
149         my $script = $session->add_to_scripts(
150                 session         => $sk,
151                 requestor       => $requestor->id,
152                 timestamp       => CORE::time,
153                 workstation     => $ws,
154                 logfile         => "$basedir/pending/$sk/$ws.log",
155                 time_delta      => $delta,
156         );
157 }
158
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;
166         return $ses;
167 }
168
169 # --------------------------------------------------------------------
170 # Finds a script object in the DB based on workstation and seskey
171 # --------------------------------------------------------------------
172 sub ol_find_script {
173         my $ws = shift || $wsname;
174         my $sk = shift || $seskey;
175         my ($script) = $SCRIPT->search( session => $seskey, workstation => $ws );
176         return $script;
177 }
178
179
180 # --------------------------------------------------------------------
181 # Creates a new script in the database and loads the new script file
182 # --------------------------------------------------------------------
183 sub ol_load {
184         my $session = ol_find_session;
185         my $handle      = $cgi->upload('file');
186         my $outfile = "$basedir/pending/$seskey/$wsname.log";
187
188         ol_handl_event('OFFLINE_SESSION_FILE_EXISTS') if ol_find_script();
189         ol_handle_event('OFFLINE_SESSION_ACTIVE') if $session->in_process;
190
191         open(FILE, ">>$outfile") or ol_handle_event('OFFLINE_FILE_ERROR');
192         while( <$handle> ) { print FILE; }
193         close(FILE);
194
195         ol_create_script();
196 }
197
198
199 sub ol_handle_result {
200         my $obj = shift;
201         my $json = JSON->perl2JSON($obj);
202
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";
206                 print "$html\n";
207
208         } else {
209
210                 print "content-type: text/plain\n\n";
211                 print "$json\n";
212         }
213
214         exit(0);
215 }
216
217 sub ol_handle_event {
218         my( $evt, @args ) = @_;
219         ol_handle_result(OpenILS::Event->new($evt, @args));
220 }
221
222 sub ol_status {
223         my $session = ol_find_session();
224         my $scripts = $session->retrieve_all;
225
226         my %data;
227
228         map { $data{$_} = $session->$_ } $session->columns;
229         $data{scripts} = [];
230
231         for my $script ($scripts->columns) {
232                 my %sdata;
233                 map { $sdata{$_} = $script->$_ } $script->columns;
234                 push( @{$data{scripts}}, \%sdata );
235         }
236
237         my $type = $cgi->param('status_type');
238
239         ol_handle_result(\%data) if( ! $type || $type eq 'scripts' ) 
240 }
241
242
243 sub ol_fetch_workstation {
244         my $name = shift;
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;
249         return $ws;
250 }
251
252