]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/offline/offline-lib.pl
batch of scripts for handling the processing of offline content
[Evergreen.git] / Open-ILS / src / offline / offline-lib.pl
1 #!/usr/bin/perl
2 use strict; use warnings;
3 use CGI;
4 use OpenSRF::System;
5 use OpenSRF::Utils::Logger qw/$logger/;
6 use OpenILS::Application::AppUtils;
7 use JSON;
8
9
10 our $U = "OpenILS::Application::AppUtils";
11 our %config;
12 #do '##CONFIG##/upload-server.pl';
13 do 'offline-config.pl';
14 our $cgi = new CGI;
15 our $base_dir = $config{base_dir};
16 my $bsconfig = $config{bootstrap};
17
18
19 # --------------------------------------------------------------------
20 # Connect to OpenSRF
21 # --------------------------------------------------------------------
22 OpenSRF::System->bootstrap_client(config_file => $bsconfig);
23
24
25
26
27 # --------------------------------------------------------------------
28 # Prints out an error message to the client
29 # --------------------------------------------------------------------
30 sub handle_error {
31         my $err = shift;
32         $logger->error("offline: $err");
33         print "content-type: text/html\n\n";
34         print <<"       HTML";
35         <html>
36                 <head>
37                         <title>Offline Upload Failed</title>
38                 </head>
39                 <body>
40                         <div style='margin-top: 50px; text-align: center;'>
41                                 <b style='color:red;'>Offline Upload Failed</b><br/>
42                                 <span> $err </span>
43                         </div>
44                 </body>
45         </html>
46         HTML
47         exit(1);
48 }
49
50
51 # --------------------------------------------------------------------
52 # Prints out a success message to the client
53 # --------------------------------------------------------------------
54 sub handle_success {
55         my $msg = shift;
56         $logger->info("offline: returned success message: $msg");
57         print "content-type: text/html\n\n";
58         print <<"       HTML";
59         <html>
60                 <head>
61                         <title>Success</title>
62                 </head>
63                 <body>
64                         <div style='margin-top: 50px; text-align: center;'>
65                                 <b style='color:blue;'> $msg </b><br/>
66                         </div>
67                 </body>
68         </html>
69         HTML
70 }
71
72
73
74 # --------------------------------------------------------------------
75 # Fetches and creates if necessary the pending directory
76 # --------------------------------------------------------------------
77 sub get_pending_dir {
78         my $org = shift;
79         my $dir = "$base_dir/pending/$org/";
80         system( ('mkdir', '-p', "$dir") ) == 0 
81                 or handle_error("Unable to create directory $dir");
82         $logger->debug("offline: created/fetched pending directory $dir");      
83         return $dir;
84 }
85
86 # --------------------------------------------------------------------
87 # Fetches and creates if necessary the archive directory
88 # --------------------------------------------------------------------
89 sub create_archive_dir {
90         my $org = shift;
91         my (undef,$min,$hour,$mday,$mon,$year) = localtime(time);
92
93         $mon++;
94         $year           += 1900;
95         $min            = "0$min"       unless $min             =~ /\d{2}/o;
96         $hour           = "0$hour"      unless $hour    =~ /\d{2}/o;
97         $mday           = "0$mday"      unless $mday    =~ /\d{2}/o;
98         $mon            = "0$mon"       unless $mon             =~ /\d{2}/o;
99
100         my $dir = "$base_dir/archive/$org/$year$mon$mday$hour$min/";
101         system( ('mkdir', '-p', "$dir") ) == 0 
102                 or handle_error("Unable to create archive directory $dir");
103         $logger->debug("offline: Created archive directory $dir");
104         return $dir;
105 }
106
107
108
109 # --------------------------------------------------------------------
110 # Fetches the workstation object by name
111 # --------------------------------------------------------------------
112 sub fetch_workstation {
113         my $name = shift;
114         $logger->debug("offline: Fetching workstation $name");
115         my $ws = $U->storagereq(
116                 'open-ils.storage.direct.actor.workstation.search.name', $name);
117         handle_error("Workstation $name does not exists") unless $ws;
118         return $ws;
119 }
120
121