Rough, untested cut. use rec id as they added content key; fetch isbn/upc from full_r...
authorBill Erickson <berick@esilibrary.com>
Wed, 27 Apr 2011 15:42:58 +0000 (11:42 -0400)
committerBen Shum <bshum@biblio.org>
Wed, 12 Dec 2012 15:50:36 +0000 (10:50 -0500)
Open-ILS/src/perlmods/lib/OpenILS/WWW/AddedContent.pm

index 0e85a54..5e9c2bf 100644 (file)
@@ -15,6 +15,7 @@ use OpenSRF::EX qw(:try);
 use OpenSRF::Utils::Cache;
 use OpenSRF::System;
 use OpenSRF::Utils::Logger qw/$logger/;
+use OpenILS::Utils::CStoreEditor;
 
 use LWP::UserAgent;
 use MIME::Base64;
@@ -81,45 +82,83 @@ sub handler {
     my @path_parts = split( /\//, $r->unparsed_uri );
     my $type = $path_parts[-3];
     my $format = $path_parts[-2];
-    my $key = $path_parts[-1];
+    my $id = $path_parts[-1];
     my $res;
 
     child_init() unless $handler;
 
-    return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $key;
+    return Apache2::Const::NOT_FOUND unless $handler and $type and $format and $id;
 
     my $err;
     my $data;
     my $method = "${type}_${format}";
 
     return Apache2::Const::NOT_FOUND unless $handler->can($method);
-    return $res if defined($res = $AC->serve_from_cache($type, $format, $key));
+    return $res if defined($res = $AC->serve_from_cache($type, $format, $id));
     return Apache2::Const::NOT_FOUND unless $AC->lookups_enabled;
 
-    try {
-        $data = $handler->$method($key);
-    } catch Error with { 
-        $err = shift; 
-        decr_error_countdown();
-        $logger->debug("added content handler failed: $method($key) => $err");
-    };
+    my $keys = get_rec_keys($id);
+    my @isbns = grep { $_->{tag} eq '020' } @$keys;
+    my @upcs = grep { $_->{tag} eq '024' } @$keys;
 
-    return Apache2::Const::NOT_FOUND if $err;
+    for my $key (@isbns, @upcs) {
+
+        my $key_type = $key->{tag};
+        my $key_value = $key->{value};
+        $logger->info("AC: trying key $key_value : $key_type");
+
+        try {
+            $data = $handler->$method($key_value, $key_type);
+        } catch Error with {
+            $err = shift;
+            decr_error_countdown();
+            $logger->debug("added content handler failed: $method($id) => $err");
+        };
+
+        return Apache2::Const::NOT_FOUND if $err;
+        next unless $data;
+    }
 
     if(!$data) {
         # if the AC lookup found no corresponding data, cache that information
-        $logger->debug("added content handler returned no results $method($key)") unless $data;
-        $AC->cache_result($type, $format, $key, {nocontent=>1});
+        $logger->debug("added content handler returned no results $method($id)") unless $data;
+        $AC->cache_result($type, $format, $id, {nocontent=>1});
         return Apache2::Const::NOT_FOUND;
     }
     
     $AC->print_content($data);
-    $AC->cache_result($type, $format, $key, $data);
+    $AC->cache_result($type, $format, $id, $data);
 
     reset_error_countdown();
     return Apache2::Const::OK;
 }
 
+# returns [{tag => $tag, value => $value}, {tag => $tag2, value => $value2}]
+sub get_rec_keys {
+    my $id = shift;
+    return OpenILS::Utils::CStoreEditor->new->json_query({
+        select => {mfr => ['tag', 'value']},
+        from => 'mfr',
+        where => {
+            record => $id,
+            '-or' => [
+                {
+                    '-and' => [
+                        {tag => '020'},
+                        {subfield => 'a'}
+                    ]
+                }, {
+                    '-and' => [
+                        {tag => '024'},
+                        {subfield => 'a'},
+                        {ind1 => 1}
+                    ]
+                }
+            ]
+        }
+    });
+}
+
 sub print_content {
     my($class, $data, $from_cache) = @_;
     return Apache2::Const::NOT_FOUND if $data->{nocontent};