From 46642d7ab8c8d5c0cb3caf26dbb67bc399f318fd Mon Sep 17 00:00:00 2001
From: Dan Scott <dscott@laurentian.ca>
Date: Sun, 18 Sep 2011 21:12:23 -0400
Subject: [PATCH] TPAC: Give MFHD summaries OU / depth awareness

Rather than returning all results and filtering on the client-side, as
we're currently doing in the JSPAC, teach the
open-ils.search.serial.record.bib.retrieve method to accept optional OU
& OU depth arguments and do the filtering in the query; cuts down on
network traffic and should be generally more efficient.

Note that in the absence of an explicit "depth" CGI param,
EGCatLoader/Record currently defaults to a depth of "0"; we could
teach it to look up & cache the depth of the "loc" param for the sake of
convenience & arguably more accuracy.

Also note that the sub/dist serial approach probably needs to be taught
similar method-side filtering, but I hope more knowledgeable brains will
work out what should happen with ssub.holding_lib / sdist.owning_lib.

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Signed-off-by: Bill Erickson <berick@esilibrary.com>
---
 .../lib/OpenILS/Application/Search/Serial.pm       | 60 ++++++++++++++++++++--
 .../perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm |  4 +-
 2 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Serial.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Serial.pm
index 0617f5ec7a..27c92066f1 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Serial.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Serial.pm
@@ -107,14 +107,48 @@ Given a bib record ID, returns a hash of holdings statements
 #);
 
 sub bib_to_svr {
-	my ($self, $client, $bib) = @_;
+	my ($self, $client, $bib, $ou, $ou_depth) = @_;
 	
 	my $svrs = [];
 
 	my $e = OpenILS::Utils::CStoreEditor->new();
+
+    if (!$ou) {
+        # Get the root of the org_tree
+        my $aous = $e->search_actor_org_unit([{
+            "parent_ou" => undef
+        }]); 
+
+        $ou = $aous->[0]->id();
+    }
+
+    # ou_depth can be undef in get_org_descendants
+    my @orgs = $U->get_org_descendants($ou, $ou_depth);
+
     # TODO: 'deleted' ssub support
-    my $sdists = $e->search_serial_distribution([{ "+ssub" => {"record_entry" => $bib} }, { "flesh" => 1, "flesh_fields" => {'sdist' => [ "record_entry", "holding_lib", "basic_summary", "supplement_summary", "index_summary" ]}, "join" => {"ssub" => {}} }]);
-	my $sres = $e->search_serial_record_entry([{ record => $bib, deleted => 'f', "+sdist" => {"id" => undef} }, { "join" => {"sdist" => { 'type' => 'left' }} }]);
+    my $sdists = $e->search_serial_distribution([
+        {
+            "+ssub" => {"record_entry" => $bib}
+        },
+        {
+            "flesh" => 1,
+            "flesh_fields" => {
+                'sdist' => [ "record_entry", "holding_lib", "basic_summary", "supplement_summary", "index_summary" ]
+            },
+            "join" => {"ssub" => {}}
+        }
+    ]);
+	my $sres = $e->search_serial_record_entry([
+        {
+            record => $bib,
+            deleted => 'f',
+            "owning_lib" => { "in" => @orgs },
+            "+sdist" => {"id" => undef}
+        },
+        {
+            "join" => { "sdist" => { 'type' => 'left' } } 
+        }
+    ]);
 	if (!ref $sres and !ref $sdists) {
 		return undef;
 	}
@@ -189,7 +223,25 @@ __PACKAGE__->register_method(
 	method	=> "bib_to_svr",
 	api_name	=> "open-ils.search.serial.record.bib.retrieve",
 	argc		=> 1, 
-	note		=> "Given a bibliographic record ID, return holdings in svr form"
+    signature => {
+        desc   => 'Given a bibliographic record ID, return holdings in svr form',
+        params => [
+            {   name => 'bibid',
+                desc => 'ID of the bibliographic record to which serial holdings are attached',
+                type => 'number'
+            },
+            {
+                name => 'ou',
+                desc => 'ID of the org_unit on which the search is based',
+                type => 'number'
+            },
+            {
+                name => 'ou_depth',
+                desc => 'Depth of the org tree at which the search should occur', 
+                type => 'number'
+            }
+        ]
+    }
 );
 
 1;
diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
index 2a42644c8c..009372c575 100644
--- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
+++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Record.pm
@@ -219,12 +219,10 @@ sub get_holding_summaries {
 sub get_mfhd_summaries {
     my ($self, $rec_id, $org, $depth) = @_;
 
-    # XXX TODO Filter results on OU / depth before returning
-    # Perhaps in a modified form of the osrf method, rather than here
     my $serial = create OpenSRF::AppSession("open-ils.search");
     my $result = $serial->request(
         "open-ils.search.serial.record.bib.retrieve",
-        $rec_id
+        $rec_id, $org, $depth
     )->gather(1);
 
     $serial->kill_me;
-- 
2.11.0