From: miker Date: Sat, 9 Oct 2010 02:32:49 +0000 (+0000) Subject: mod_perl handler to allow batch update bib records from an ephemeral template X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=0cb161e99c9972386a6db0bdd7d2ac1a3e1f02ed;p=evergreen%2Fpines.git mod_perl handler to allow batch update bib records from an ephemeral template git-svn-id: svn://svn.open-ils.org/ILS/trunk@18255 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- diff --git a/Open-ILS/examples/apache/eg_vhost.conf b/Open-ILS/examples/apache/eg_vhost.conf index da947e0b45..4a2266fc0d 100644 --- a/Open-ILS/examples/apache/eg_vhost.conf +++ b/Open-ILS/examples/apache/eg_vhost.conf @@ -395,6 +395,17 @@ RewriteRule - - [E=locale:en-US] [L] allow from all + + SetHandler perl-script + PerlSetVar OILSProxyTitle "Batch Update Login" + PerlSetVar OILSProxyDescription "Please log in to update records in batch" + PerlSetVar OILSProxyPermissions "STAFF_LOGIN" + PerlHandler OpenILS::WWW::Proxy OpenILS::WWW::TemplateBatchBibUpdate + PerlSendHeader On + Options +ExecCGI + allow from all + + SetHandler perl-script PerlSetVar OILSProxyTitle "Circ Extras Login" diff --git a/Open-ILS/src/perlmods/OpenILS/WWW/TemplateBatchBibUpdate.pm b/Open-ILS/src/perlmods/OpenILS/WWW/TemplateBatchBibUpdate.pm new file mode 100644 index 0000000000..5e88fea14d --- /dev/null +++ b/Open-ILS/src/perlmods/OpenILS/WWW/TemplateBatchBibUpdate.pm @@ -0,0 +1,596 @@ +package OpenILS::WWW::TemplateBatchBibUpdate; +use strict; +use warnings; +use bytes; + +use Apache2::Log; +use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log); +use APR::Const -compile => qw(:error SUCCESS); +use APR::Table; + +use Apache2::RequestRec (); +use Apache2::RequestIO (); +use Apache2::RequestUtil; +use CGI; +use Data::Dumper; +use Text::CSV; + +use OpenSRF::EX qw(:try); +use OpenSRF::Utils qw/:datetime/; +use OpenSRF::Utils::Cache; +use OpenSRF::System; +use OpenSRF::AppSession; +use XML::LibXML; +use XML::LibXSLT; + +use Encode; +use Unicode::Normalize; +use OpenILS::Utils::Fieldmapper; +use OpenSRF::Utils::Logger qw/$logger/; + +use MARC::Record; +use MARC::File::XML; + +use UNIVERSAL::require; + +our @formats = qw/USMARC UNIMARC XML BRE/; + +# set the bootstrap config and template include directory when +# this module is loaded +my $bootstrap; + +sub import { + my $self = shift; + $bootstrap = shift; +} + + +sub child_init { + OpenSRF::System->bootstrap_client( config_file => $bootstrap ); + Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL")); +} + +sub handler { + my $r = shift; + my $cgi = new CGI; + + my $authid = $cgi->cookie('ses') || $cgi->param('ses'); + my $usr = verify_login($authid); + return show_template($r) unless ($usr); + + + my $template = $cgi->param('template'); + return show_template($r) unless ($template); + + # find some IDs ... + my @records; + + @records = map { $_ ? ($_) : () } $cgi->param('recid'); + + if (!@records) { # try for a file + my $file = $cgi->param('idfile'); + if ($file) { + my $col = $cgi->param('idcolumn') || 0; + my $csv = new Text::CSV; + + while (<$file>) { + $csv->parse($_); + my @data = $csv->fields; + my $id = $data[$col]; + $id =~ s/\D+//o; + next unless ($id); + push @records, $id; + } + } + } + + my $e = OpenSRF::AppSession->connect('open-ils.cstore'); + $e->request('open-ils.cstore.transaction.begin')->gather(1); + + # still no records ... + my $container = $cgi->param('containerid'); + if ($container) { + my $bucket = $e->request( + 'open-ils.cstore.direct.container.biblio_record_entry_bucket.retrieve', + $container + )->gather(1); + unless($bucket) { + $e->request('open-ils.cstore.transaction.rollback')->gather(1); + $e->disconnect; + $r->log->error("No such bucket $container"); + $logger->error("No such bucket $container"); + return Apache2::Const::NOT_FOUND; + } + my $recs = $e->request( + 'open-ils.cstore.direct.container.biblio_record_entry_bucket_item.search.atomic', + { bucket => $container } + )->gather(1); + @records = map { ($_->target_biblio_record_entry) } @$recs; + } + + unless (@records) { + $e->request('open-ils.cstore.transaction.rollback')->gather(1); + $e->disconnect; + return show_template($r); + } + + # we have a template and some record ids, so... + + # insert the template record + my $min_id = $e->request( + 'open-ils.cstore.json_query', + { select => { bre => [{ column => 'id', transform => 'min', aggregate => 1}] }, from => 'bre' } + )->gather(1)->{id} - 1; + + warn "new template bib id = $min_id\n"; + + my $tmpl_rec = Fieldmapper::biblio::record_entry->new; + $tmpl_rec->id($min_id); + $tmpl_rec->deleted('t'); + $tmpl_rec->active('f'); + $tmpl_rec->marc($template); + $tmpl_rec->creator($usr->id); + $tmpl_rec->editor($usr->id); + + warn "about to create bib $min_id\n"; + $e->request('open-ils.cstore.direct.biblio.record_entry.create', $tmpl_rec )->gather(1); + + # create the new container for the records and the template + my $bucket = Fieldmapper::container::biblio_record_entry_bucket->new; + $bucket->owner($usr->id); + $bucket->name('Temporary Merge Bucket'); + + $bucket = $e->request('open-ils.cstore.direct.container.biblio_record_entry_bucket.create', $bucket )->gather(1); + + # create items in the bucket + my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new; + $item->bucket($bucket->id); + $item->target_biblio_record_entry($min_id); + + $e->request('open-ils.cstore.direct.container.biblio_record_entry_bucket_item.create', $item )->gather(1); + + for my $r (@records) { + $item->target_biblio_record_entry($r); + $e->request('open-ils.cstore.direct.container.biblio_record_entry_bucket_item.create', $item )->gather(1); + } + + $e->request('open-ils.cstore.transaction.commit')->gather(1); + $e->disconnect; + + # fire the background bucket processor + my $cache_key = OpenSRF::AppSession + ->create('open-ils.actor') + ->request('open-ils.cat.container.template_overlay.background', $bucket->id) + ->gather(1); + + return show_processing_template($r, $bucket->id, \@records, $cache_key); +} + +sub verify_login { + my $auth_token = shift; + return undef unless $auth_token; + + my $user = OpenSRF::AppSession + ->create("open-ils.auth") + ->request( "open-ils.auth.session.retrieve", $auth_token ) + ->gather(1); + + if (ref($user) eq 'HASH' && $user->{ilsevent} == 1001) { + return undef; + } + + return $user if ref($user); + return undef; +} + +sub show_processing_template { + my $r = shift; + my $bid = shift; + my $recs = shift; + my $cache_key = shift; + + my $rec_string = @$recs; + + $r->content_type('text/html'); + $r->print(< + + + Merging records... + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ StatusRecord Count
Success
Failure
+ +
+ + + +HTML + + return Apache2::Const::OK; +} + + +sub show_template { + my $r = shift; + + $r->content_type('text/html'); + $r->print(<<'HTML'); + + + + Merge Template Builder + + + + + + + + + + + + +
+ + + + +
+ +
+ +
+
+ +
or
+ +
+ + +
+ +
or
+ + + +
+
+ + + +
+ +
+ + + + + +
Update Template Preview:
+ + + +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Rule Type + +
MARC Tag +
Optional Subfields
MARC Data
Optionally Restrict By
Subfield +
Regular Expression
+ +
+
+
+
+ + + +HTML + + return Apache2::Const::OK; +} + +1; + +