bookbag CSV gets bib attrs; A/T unapi method collab/berick/bib-container-csv-flesh-record-attrs
authorBill Erickson <berick@esilibrary.com>
Tue, 8 Nov 2011 15:56:14 +0000 (10:56 -0500)
committerBill Erickson <berick@esilibrary.com>
Tue, 8 Nov 2011 20:43:15 +0000 (15:43 -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>
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/upgrade/XXXX.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 5f07972..34c1e82 100644 (file)
@@ -36,7 +36,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 {
@@ -249,8 +250,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;
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.data.bib-container-csv-unapi-template.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.data.bib-container-csv-unapi-template.sql
new file mode 100644 (file)
index 0000000..d949aef
--- /dev/null
@@ -0,0 +1,24 @@
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('XXXX', :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;