]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/support-scripts/csv_notify_fetcher.pl
LP1054322 - libparent-perl not needed for Ubuntu Precise
[working/Evergreen.git] / Open-ILS / src / support-scripts / csv_notify_fetcher.pl
1 #!/usr/bin/perl
2 use strict; 
3 use warnings;
4 use DateTime;
5 use Getopt::Long;
6 use OpenSRF::AppSession;
7 use OpenILS::Utils::CStoreEditor;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Utils::RemoteAccount;
10 use OpenSRF::Utils::Logger qw/$logger/;
11
12 my $osrf_config     = '/openils/conf/opensrf_core.xml';
13 my $remote_account  = '';
14 my $remote_file     = '';
15 my $local_dir       = '/tmp'; # locally stored file directory
16 my $local_file      = ''; # locally stored file name
17 my $response_file   = ''; # local file with response data
18 my $delete_file     = 0;
19 my $verbose         = 0;
20 my $help            = 0;
21 my $remote_conn     = undef;
22
23 GetOptions(
24     'osrf-config=s'     => \$osrf_config,
25     'remote-account=s'  => \$remote_account,
26     'remote-file=s'     => \$remote_file,
27     'local-file=s'      => \$local_file,
28     'local-dir=s'       => \$local_dir,
29     'response-file=s'   => \$response_file,
30     'delete-file'       => \$delete_file,
31     'help'              => \$help,
32     'verbose'           => \$verbose
33 );
34
35 sub help {
36     print <<HELP;
37
38 Collect CSV notification results file and update affected events.  The assumed
39 format is "event-id","event-status".
40
41 # Fetch notification response file from a remote site and delete the file when
42 # done.
43
44 $0 \
45     --osrf-config /openils/conf/opensrf_core.xml \
46     --remote-account 1 \
47     --remote-file some-file.csv \
48     --local-dir /tmp \
49     --local-file csv-result-file.csv \
50     --delete-file \
51     --verbose
52
53 Options
54
55     --osrf-config [/openils/conf/opensrf_core.xml]
56         Full path to opensrf_core.xml configuration file
57
58     --remote-account
59         Identifier of the config.remote_account entry from which files should
60         be retrieved
61
62     --remote-file
63         Name of file on the remote site to retrieve and process
64
65     --local-file
66         Name given to local copy of retrieved files
67
68     --local-dir [/tmp]
69         Directory to store retrieved files
70
71     --response-file
72         Name of local file which contains results.  
73
74     --delete-file
75         Delete the remote file after processing.  If the user did not specify
76         a value for --local-file, the local file will be deleted as well.
77
78     --help
79         Print this help
80
81     --verbose
82         Display debug information during execution
83
84 HELP
85     exit;
86 }
87
88 help() if $help;
89
90 die "--response-file OR --remote-account and --remote-file required\n"
91     unless $response_file or ($remote_account and $remote_file);
92
93 OpenSRF::System->bootstrap_client(config_file => $osrf_config);
94 Fieldmapper->import(IDL => 
95     OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
96 OpenILS::Utils::CStoreEditor::init();
97 my $editor = OpenILS::Utils::CStoreEditor->new;
98
99 if (!$response_file and $remote_account) {
100
101     # fetch data remotely and push the data into $response_file
102
103     my $racct = $editor->retrieve_config_remote_account($remote_account);
104     die "No such remote account $remote_account" unless $racct;
105
106     my $type;
107     my $host = $racct->host;
108     ($host =~ s/^(S?FTP)://i and $type = uc($1)) or                   
109     ($host =~ s/^(SSH|SCP)://i and $type = 'SCP');
110     $host =~ s#//##;
111
112     if ($local_file) {
113         # user specified local file name
114         $response_file = "$local_dir/$local_file";
115     } else {
116         $response_file = File::Temp->new()->filename;
117     }
118
119     print "Connecting to host $host\n" if $verbose;
120
121     $remote_conn = OpenILS::Utils::RemoteAccount->new(
122         type            => $type,
123         remote_host     => $host,
124         account_object  => $racct,
125         local_file      => $response_file,
126         remote_file     => $remote_file
127     );
128
129     my $res = $remote_conn->get;
130
131     die "Unable to fetch from  remote server [$remote_account] : " . 
132         $remote_conn->error . "\n" unless $res;
133
134     print "Fetched file $remote_file => $response_file\n" if $verbose;
135 }
136
137 # at this point, $file contains CSV, because it was already 
138 # there or because we just fetched it from the remote account
139 open(FILE, $response_file) or 
140     die "Unable to open response file: '$response_file' : $!\n";
141
142 binmode(FILE, ":utf8");
143     
144 while (<FILE>) {
145     chomp;
146
147     my ($id, $stat) = /"(.+)","(.+)"/g;
148     next unless $id and $stat;
149
150     $logger->info("csv: processing event $id; stat $stat");
151
152     my $event = $editor->retrieve_action_trigger_event($id);
153
154     if (!$event) {
155         $logger->warn("csv: unable to find event $id");
156         next;
157     }
158
159     if ($event->async_output) {
160         $logger->info("csv: skipping event $id; async_output already set");
161         next;
162     }
163
164     $editor->xact_begin;
165
166     # store the response output
167     my $output = Fieldmapper::action_trigger::event_output->new;
168     $output->data($stat);
169
170     unless ($editor->create_action_trigger_event_output($output)) {
171         $logger->warn("csv: error creating event ".
172             "output for event $id: ". $editor->die_event);
173         next;
174     }
175
176     # link the async response output to the original event
177     $event->async_output($output->id);
178
179     unless ($editor->update_action_trigger_event($event)) {
180         $logger->warn("csv: error updating event $id: ". $editor->die_event);
181         next;
182     }
183
184     $editor->xact_commit;
185 }
186
187 $editor->disconnect;
188
189 if ($delete_file) {
190     # after we have successfully processed the file, 
191     # delete it from the remote server.
192
193     $remote_conn->delete($remote_file);
194
195     # delete the local file unless the user 
196     # specified a location to save the file
197     unlink($response_file) unless $local_file;
198 }