From aed4b69715f34188a9c906d72345632d41283415 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 31 Oct 2011 14:56:48 -0400 Subject: [PATCH] Vandelay; API for creating buckets from queues * Adds a new biblio record entry bucket type of "vandelay_queue" * Adds a new API call that allows callers to create new buckets (or add to existing buckets), copying imported records from a vandelay queue into the bucket. open-ils.vandelay.bib_queue.to_bucket Signed-off-by: Bill Erickson Signed-off-by: Mike Rylander --- .../perlmods/lib/OpenILS/Application/Vandelay.pm | 98 +++++++++++++++++++++- Open-ILS/src/sql/Pg/950.data.seed-values.sql | 7 ++ .../XXXX.data.vandelay-queue-bib-bucket-type.sql | 13 +++ 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/XXXX.data.vandelay-queue-bib-bucket-type.sql diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm index 62dca2e073..b30d6527c9 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm @@ -12,7 +12,6 @@ use OpenILS::Utils::CStoreEditor qw/:funcs/; use MARC::Batch; use MARC::Record; use MARC::File::XML ( BinaryEncoding => 'UTF-8' ); -use OpenILS::Utils::Fieldmapper; use Time::HiRes qw(time); use OpenSRF::Utils::Logger qw/$logger/; use MIME::Base64; @@ -1770,4 +1769,101 @@ sub match_set_update_tree { $e->commit or return $e->die_event; } +__PACKAGE__->register_method( + api_name => 'open-ils.vandelay.bib_queue.to_bucket', + method => 'bib_queue_to_bucket', + api_level => 1, + argc => 2, + signature => { + desc => q/Add to or create a new bib container (bucket) with the successfully + imported contents of a vandelay queue. Any user that has Vandelay + queue create permissions can append or create buckets from his-her own queues./, + params => [ + {desc => 'Authtoken', type => 'string'}, + {desc => 'Queue ID', type => 'number'}, + {desc => 'Bucket Name', type => 'string'} + ], + return => {desc => q/ + {bucket => $bucket, addcount => number-of-items-added-to-bucket, item_count => total-bucket-item-count} on success, + {add_count => 0} if there is nothing to do, and Event on error/} + } +); + +sub bib_queue_to_bucket { + my ($self, $conn, $auth, $q_id, $bucket_name) = @_; + + my $e = new_editor(xact => 1, authtoken => $auth); + return $e->die_event unless $e->checkauth; + + my $queue = $e->retrieve_vandelay_bib_queue($q_id) + or return $e->die_event; + + return OpenILS::Event->new('BAD_PARAMS', + note => q/Bucket creator must be queue owner/) + unless $queue->owner == $e->requestor->id; + + # find the bib IDs that will go into the bucket + my $bib_ids = $e->json_query({ + select => {vqbr => ['imported_as']}, + from => 'vqbr', + where => {queue => $q_id, imported_as => {'!=' => undef}} + }); + + if (!@$bib_ids) { # no records to add + $e->rollback; + return {add_count => 0}; + } + + # allow user to add to an existing bucket by name + my $bucket = $e->search_container_biblio_record_entry_bucket({ + owner => $e->requestor->id, + name => $bucket_name, + btype => 'vandelay_queue' + })->[0]; + + # if the bucket does not exist, create a new one + if (!$bucket) { + $bucket = Fieldmapper::container::biblio_record_entry_bucket->new; + $bucket->name($bucket_name); + $bucket->owner($e->requestor->id); + $bucket->btype('vandelay_queue'); + + $e->create_container_biblio_record_entry_bucket($bucket) + or return $e->die_event; + } + + # create the new bucket items + for my $bib_id ( map {$_->{imported_as}} @$bib_ids ) { + my $item = Fieldmapper::container::biblio_record_entry_bucket_item->new; + $item->target_biblio_record_entry($bib_id); + $item->bucket($bucket->id); + $e->create_container_biblio_record_entry_bucket_item($item) + or return $e->die_event; + } + + # re-fetch the bucket to pick up the correct create_time + $bucket = $e->retrieve_container_biblio_record_entry_bucket($bucket->id) + or return $e->die_event; + + # get the total count of items in this bucket + my $count = $e->json_query({ + select => {cbrebi => [{ + aggregate => 1, + transform => 'count', + alias => 'count', + column => 'id' + }]}, + from => 'cbrebi', + where => {bucket => $bucket->id} + })->[0]; + + $e->commit; + + return { + bucket => $bucket, + add_count => scalar(@$bib_ids), # items added to the bucket + item_count => $count->{count} # total items in buckets + }; +} + 1; diff --git a/Open-ILS/src/sql/Pg/950.data.seed-values.sql b/Open-ILS/src/sql/Pg/950.data.seed-values.sql index e317202c66..2422cfecdb 100644 --- a/Open-ILS/src/sql/Pg/950.data.seed-values.sql +++ b/Open-ILS/src/sql/Pg/950.data.seed-values.sql @@ -10396,3 +10396,10 @@ INSERT INTO action_trigger.validator ( module, description ) VALUES ( 'ReservationIsAvailable', 'Checked that a reserved resource is available for checkout' ); + +INSERT INTO container.biblio_record_entry_bucket_type (code, label) VALUES ( + 'vandelay_queue', + oils_i18n_gettext('vandelay_queue', 'Vandelay Queue', 'cbrebt', 'label') +); + + diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.data.vandelay-queue-bib-bucket-type.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.data.vandelay-queue-bib-bucket-type.sql new file mode 100644 index 0000000000..ef2ffd8266 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.data.vandelay-queue-bib-bucket-type.sql @@ -0,0 +1,13 @@ +-- Evergreen DB patch XXXX.data.vandelay-queue-bib-bucket-type.sql +-- +BEGIN; + +-- check whether patch can be applied +SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version); + +INSERT INTO container.biblio_record_entry_bucket_type (code, label) VALUES ( + 'vandelay_queue', + oils_i18n_gettext('vandelay_queue', 'Vandelay Queue', 'cbrebt', 'label') +); + +COMMIT; -- 2.11.0