bookbag CSV gets bib attrs; A/T unapi method
authorBill Erickson <berick@esilibrary.com>
Tue, 8 Nov 2011 15:56:14 +0000 (10:56 -0500)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Wed, 21 Dec 2011 15:56:57 +0000 (10:56 -0500)
* Added a bib record unapi retrieval utility method for action/trigger
templates.

* Updated the bookbag CSV template to include data for the "item_type"
record attribute both to have it and as an example of how the unapi
retrieval works.

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>

Open-ILS/src/perlmods/lib/OpenILS/Application/AppUtils.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/ContainerCSV.pm
Open-ILS/src/sql/Pg/002.schema.config.sql
Open-ILS/src/sql/Pg/950.data.seed-values.sql
Open-ILS/src/sql/Pg/upgrade/0660.data.bib-container-csv-unapi-template.sql [new file with mode: 0644]

index 0ecf415..d214274 100644 (file)
@@ -1876,8 +1876,9 @@ sub bib_record_list_via_search {
     return [ map { pop @$_ } @{$search_result->{ids}} ];
 }
 
+# 'no_flesh' avoids fleshing the target_biblio_record_entry
 sub bib_container_items_via_search {
-    my ($class, $container_id, $search_query, $search_args) = @_;
+    my ($class, $container_id, $search_query, $search_args, $no_flesh) = @_;
 
     # First, Use search API to get container items sorted in any way that crad
     # sorters support.
@@ -1903,13 +1904,16 @@ sub bib_container_items_via_search {
         return;
     }
 
+    my @flesh_fields = qw/notes/;
+    push(@flesh_fields, 'target_biblio_record_entry') unless $no_flesh;
+
     my $items = $e->search_container_biblio_record_entry_bucket_item([
         {
             "target_biblio_record_entry" => $id_list,
             "bucket" => $container_id
         }, {
             flesh => 1,
-            flesh_fields => {"cbrebi" => [qw/notes target_biblio_record_entry/]}
+            flesh_fields => {"cbrebi" => \@flesh_fields}
         }
     ]);
     unless ($items) {
@@ -1920,11 +1924,13 @@ sub bib_container_items_via_search {
         return;
     }
 
-    $e->disconnect;
-
     # ... and put them in the same order that the search API said they
     # should be in.
-    my %ordering_hash = map { $_->target_biblio_record_entry->id, $_ } @$items;
+    my %ordering_hash = map { 
+        ($no_flesh) ? $_->target_biblio_record_entry : $_->target_biblio_record_entry->id, 
+        $_ 
+    } @$items;
+
     return [map { $ordering_hash{$_} } @$id_list];
 }
 
index 14a69e7..1bd0e43 100644 (file)
@@ -37,7 +37,8 @@ sub get_li_attr {
 }
 
 # helper functions inserted into the TT environment
-my $_TT_helpers = {
+my $_TT_helpers; # define first so one helper can use another
+$_TT_helpers = {
 
     # turns a date into something TT can understand
     format_date => sub {
@@ -270,8 +271,27 @@ my $_TT_helpers = {
     xml_doc => sub {
         my ($str) = @_;
         return $str ? (new XML::LibXML)->parse_string($str) : undef;
-    }
+    },
 
+    unapi_bre => sub {
+        my ($bre_id, $unapi_args) = @_;
+        $unapi_args ||= {};
+        $unapi_args->{flesh} ||= '{}',
+
+        my $query = { 
+            from => [
+                'unapi.bre', $bre_id, 'marcxml','record', 
+                $unapi_args->{flesh}, 
+                $unapi_args->{site}, 
+                $unapi_args->{depth}, 
+                $unapi_args->{flesh_depth}, 
+            ]
+        };
+
+        my $unapi = new_editor()->json_query($query);
+        return undef unless @$unapi;
+        return $_TT_helpers->{xml_doc}->($unapi->[0]->{'unapi.bre'});
+    }
 };
 
 
index 28a3419..6e2306e 100644 (file)
@@ -28,9 +28,9 @@ sub handler {
 
     # get items for bookbags (bib containers of btype bookbag)
     if ($env->{user_data}{item_search}) {
-        # use the search api for bib container items
+        # use the search api for bib container items.  fetch record IDs only.
         my $items = $U->bib_container_items_via_search(
-            $env->{target}->id, $env->{user_data}{item_search}
+            $env->{target}->id, $env->{user_data}{item_search}, undef, 1 
         ) or return 0;  # TODO build error output for db?
 
         $env->{items} = $items;
index defe858..863045b 100644 (file)
@@ -86,7 +86,7 @@ CREATE TRIGGER no_overlapping_deps
     BEFORE INSERT OR UPDATE ON config.db_patch_dependencies
     FOR EACH ROW EXECUTE PROCEDURE evergreen.array_overlap_check ('deprecates');
 
-INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0659', :eg_version); -- tsbere/dbs
+INSERT INTO config.upgrade_log (version, applied_to) VALUES ('0660', :eg_version); -- berick/senator
 
 CREATE TABLE config.bib_source (
        id              SERIAL  PRIMARY KEY,
index eca99a4..e23ceac 100644 (file)
 # the environment because a special reactor will take care of filling it in.
 
 FOR item IN items;
-    bibxml = helpers.xml_doc(item.target_biblio_record_entry.marc);
+    bibxml = helpers.unapi_bre(item.target_biblio_record_entry, {flesh => '{mra}'});
     title = "";
     FOR part IN bibxml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b"]');
         title = title _ part.textContent;
     END;
     author = bibxml.findnodes('//*[@tag="100"]/*[@code="a"]').textContent;
+    item_type = bibxml.findnodes('//*[local-name()="attributes"]/*[local-name()="field"][@name="item_type"]').getAttribute('coded-value');
 
-    helpers.csv_datum(title) %],[% helpers.csv_datum(author) %],[% FOR note IN item.notes; helpers.csv_datum(note.note); ","; END; "\n";
+    helpers.csv_datum(title) %],[% helpers.csv_datum(author) %],[% helpers.csv_datum(item_type) %],[% FOR note IN item.notes; helpers.csv_datum(note.note); ","; END; "\n";
 END -%]
 $$
 );
diff --git a/Open-ILS/src/sql/Pg/upgrade/0660.data.bib-container-csv-unapi-template.sql b/Open-ILS/src/sql/Pg/upgrade/0660.data.bib-container-csv-unapi-template.sql
new file mode 100644 (file)
index 0000000..db81402
--- /dev/null
@@ -0,0 +1,24 @@
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('0660', :eg_version);
+
+UPDATE action_trigger.event_definition SET template = $$
+[%-
+# target is the bookbag itself. The 'items' variable does not need to be in
+# the environment because a special reactor will take care of filling it in.
+
+FOR item IN items;
+    bibxml = helpers.unapi_bre(item.target_biblio_record_entry, {flesh => '{mra}'});
+    title = "";
+    FOR part IN bibxml.findnodes('//*[@tag="245"]/*[@code="a" or @code="b"]');
+        title = title _ part.textContent;
+    END;
+    author = bibxml.findnodes('//*[@tag="100"]/*[@code="a"]').textContent;
+    item_type = bibxml.findnodes('//*[local-name()="attributes"]/*[local-name()="field"][@name="item_type"]').getAttribute('coded-value');
+
+    helpers.csv_datum(title) %],[% helpers.csv_datum(author) %],[% helpers.csv_datum(item_type) %],[% FOR note IN item.notes; helpers.csv_datum(note.note); ","; END; "\n";
+END -%]
+$$
+WHERE reactor = 'ContainerCSV';
+
+COMMIT;