2 use strict; use warnings;
5 use OpenSRF::Utils::Logger qw/$logger/;
6 use OpenILS::Application::AppUtils;
8 use OpenSRF::EX qw/:try/;
11 our $U = "OpenILS::Application::AppUtils";
14 # --------------------------------------------------------------------
15 # Load the config options
16 # --------------------------------------------------------------------
17 our $META_FILE = "meta"; # name of the metadata file
18 our $LOCK_FILE = "lock"; # name of the lock file
20 our $ORG_UNIT; # org unit object
21 our $TIME_DELTA; # time offset for the log files
22 our %config; # config data
23 our $cgi; # our CGI object
24 our $PRINT_HTML; # true if access the CGIs via a web browser
25 our $AUTHTOKEN; # The login session key
26 our $REQUESTOR; # the requestor user object
27 our $base_dir; # the base directory for logs
30 #do '##CONFIG##/upload-server.pl';
31 do 'offline-config.pl';
39 # --------------------------------------------------------------------
40 # Loads the necessary CGI params, connects to OpenSRF, and verifies
42 # --------------------------------------------------------------------
45 $base_dir = $config{base_dir};
46 my $bsconfig = $config{bootstrap};
49 # --------------------------------------------------------------------
51 # --------------------------------------------------------------------
52 OpenSRF::System->bootstrap_client(config_file => $bsconfig);
55 # --------------------------------------------------------------------
56 # Load the required CGI params
57 # --------------------------------------------------------------------
59 $PRINT_HTML = $cgi->param('html') || "";
60 $AUTHTOKEN = $cgi->param('ses')
61 or handle_event(OpenILS::Event->new('NO_SESSION'));
63 $ORG = $cgi->param('org') || "";
65 my $ws = fetch_workstation($cgi->param('ws'));
66 $ORG = $ws->owning_lib if $ws;
70 ($ORG_UNIT, $evt) = $U->fetch_org_unit($ORG);
71 handle_event($evt) if $evt;
74 ($REQUESTOR, $evt) = $U->checkses($AUTHTOKEN);
75 handle_event($evt) if $evt;
77 $TIME_DELTA = $cgi->param('delta') || "0";
80 # --------------------------------------------------------------------
81 # Generic HTML template to provide basic functionality
82 # --------------------------------------------------------------------
83 my $on = ($ORG_UNIT) ? $ORG_UNIT->name : "";
87 <title>{TITLE}</title>
90 <div style='text-align: center; border-bottom: 2px solid #E0F0E0; padding: 10px; margin-bottom: 50px;'>
91 <div style='margin: 5px;'><b>$on</b></div>
92 <a style='margin: 6px;' href='offline-upload.pl?ses=$AUTHTOKEN&org=$ORG&html=1'>Upload More Files</a>
93 <a style='margin: 6px;' href='offline-status.pl?ses=$AUTHTOKEN&org=$ORG&html=1'>Status Page</a>
94 <a style='margin: 6px;' href='offline-execute.pl?ses=$AUTHTOKEN&org=$ORG&html=1'>Execute Batch</a>
96 <div style='text-align: center;'>
104 # --------------------------------------------------------------------
105 # Print out a full HTML page and exits
106 # --------------------------------------------------------------------
109 my $title = $args{title} || "";
110 my $body = $args{body} || "";
113 $HTML =~ s/{TITLE}/$title/;
114 $HTML =~ s/{BODY}/$body/;
116 } else { # it can happen..
120 print "content-type: text/html\n\n";
126 # --------------------------------------------------------------------
127 # Prints JSON to the client
128 # --------------------------------------------------------------------
130 my( $obj, $add_header ) = @_;
131 print "content-type: text/html\n\n" if $add_header;
132 print JSON->perl2JSON($obj);
135 # --------------------------------------------------------------------
136 # Prints the JSON form of the event out to the client
137 # --------------------------------------------------------------------
142 $logger->info("offline: returning event ".$evt->{textcode});
146 # maybe make this smarter
148 title => 'Offline Event Occurred',
149 body => JSON->perl2JSON($evt));
159 # --------------------------------------------------------------------
160 # Fetches and creates if necessary the pending directory
161 # --------------------------------------------------------------------
162 sub get_pending_dir {
163 my $dir = "$base_dir/pending/$ORG/";
164 system( ('mkdir', '-p', "$dir") ) == 0
165 or handle_error("Unable to create directory $dir");
169 # --------------------------------------------------------------------
170 # Fetches and creates if necessary the archive directory
171 # --------------------------------------------------------------------
172 sub create_archive_dir {
173 my (undef,$min,$hour,$mday,$mon,$year) = localtime(time);
177 $min = "0$min" unless $min =~ /\d{2}/o;
178 $hour = "0$hour" unless $hour =~ /\d{2}/o;
179 $mday = "0$mday" unless $mday =~ /\d{2}/o;
180 $mon = "0$mon" unless $mon =~ /\d{2}/o;
182 my $dir = "$base_dir/archive/$ORG/$year$mon$mday$hour$min/";
183 system( ('mkdir', '-p', "$dir") ) == 0
184 or handle_error("Unable to create archive directory $dir");
190 # --------------------------------------------------------------------
191 # Fetches the workstation object by name
192 # --------------------------------------------------------------------
193 sub fetch_workstation {
195 $logger->debug("offline: Fetching workstation $name");
196 my $ws = $U->storagereq(
197 'open-ils.storage.direct.actor.workstation.search.name', $name);
198 handle_error("Workstation $name does not exists") unless $ws;
204 $data = JSON->perl2JSON($data);
205 my $mf = get_pending_dir($ORG) . "/$META_FILE";
206 $logger->debug("offline: Append metadata to file $mf: $data");
207 open(F, ">>$mf") or handle_event(OpenILS::Event->new('OFFLINE_FILE_ERROR', payload => $@));
213 my $mf = get_pending_dir($ORG) . "/$META_FILE";
214 open(F, "$mf") or return [];
218 push(@resp, JSON->JSON2perl($_)) for @data;
219 @resp = grep { $_ and $_->{'workstation'} } @resp;
226 $log =~ s#/.*/(\w+)#$1#og;