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/;
12 my $osrf_config = '/openils/conf/opensrf_core.xml';
13 my $remote_account = '';
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
21 my $remote_conn = undef;
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,
32 'verbose' => \$verbose
38 Collect CSV notification results file and update affected events. The assumed
39 format is "event-id","event-status".
41 # Fetch notification response file from a remote site and delete the file when
45 --osrf-config /openils/conf/opensrf_core.xml \
47 --remote-file some-file.csv \
49 --local-file csv-result-file.csv \
55 --osrf-config [/openils/conf/opensrf_core.xml]
56 Full path to opensrf_core.xml configuration file
59 Identifier of the config.remote_account entry from which files should
63 Name of file on the remote site to retrieve and process
66 Name given to local copy of retrieved files
69 Directory to store retrieved files
72 Name of local file which contains results.
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.
82 Display debug information during execution
90 die "--response-file OR --remote-account and --remote-file required\n"
91 unless $response_file or ($remote_account and $remote_file);
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;
99 if (!$response_file and $remote_account) {
101 # fetch data remotely and push the data into $response_file
103 my $racct = $editor->retrieve_config_remote_account($remote_account);
104 die "No such remote account $remote_account" unless $racct;
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');
113 # user specified local file name
114 $response_file = "$local_dir/$local_file";
116 $response_file = File::Temp->new()->filename;
119 print "Connecting to host $host\n" if $verbose;
121 $remote_conn = OpenILS::Utils::RemoteAccount->new(
123 remote_host => $host,
124 account_object => $racct,
125 local_file => $response_file,
126 remote_file => $remote_file
129 my $res = $remote_conn->get;
131 die "Unable to fetch from remote server [$remote_account] : " .
132 $remote_conn->error . "\n" unless $res;
134 print "Fetched file $remote_file => $response_file\n" if $verbose;
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";
142 binmode(FILE, ":utf8");
147 my ($id, $stat) = /"(.+)","(.+)"/g;
148 next unless $id and $stat;
150 $logger->info("csv: processing event $id; stat $stat");
152 my $event = $editor->retrieve_action_trigger_event($id);
155 $logger->warn("csv: unable to find event $id");
159 if ($event->async_output) {
160 $logger->info("csv: skipping event $id; async_output already set");
166 # store the response output
167 my $output = Fieldmapper::action_trigger::event_output->new;
168 $output->data($stat);
170 unless ($editor->create_action_trigger_event_output($output)) {
171 $logger->warn("csv: error creating event ".
172 "output for event $id: ". $editor->die_event);
176 # link the async response output to the original event
177 $event->async_output($output->id);
179 unless ($editor->update_action_trigger_event($event)) {
180 $logger->warn("csv: error updating event $id: ". $editor->die_event);
184 $editor->xact_commit;
190 # after we have successfully processed the file,
191 # delete it from the remote server.
193 $remote_conn->delete($remote_file);
195 # delete the local file unless the user
196 # specified a location to save the file
197 unlink($response_file) unless $local_file;