LP#1549505: Add a "badges" filter, and logic to gather badge orgs from location_group...
authorMike Rylander <mrylander@gmail.com>
Mon, 25 Jan 2016 21:09:04 +0000 (16:09 -0500)
committerGalen Charlton <gmc@esilibrary.com>
Wed, 24 Feb 2016 22:38:23 +0000 (17:38 -0500)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Signed-off-by: Galen Charlton <gmc@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm

index 6ff8db8..c7b15dc 100644 (file)
@@ -643,6 +643,7 @@ __PACKAGE__->add_search_filter( 'locations' );
 __PACKAGE__->add_search_filter( 'location_groups' );
 __PACKAGE__->add_search_filter( 'bib_source' );
 __PACKAGE__->add_search_filter( 'badge_orgs' );
+__PACKAGE__->add_search_filter( 'badges' );
 __PACKAGE__->add_search_filter( 'site' );
 __PACKAGE__->add_search_filter( 'pref_ou' );
 __PACKAGE__->add_search_filter( 'lasso' );
@@ -814,6 +815,15 @@ sub toSQL {
 
     my $pubdate_join = "LEFT JOIN metabib.record_sorter pubdate_t ON m.source = pubdate_t.source AND attr = 'pubdate'";
 
+    my $badge_join = '';
+    my ($badge_filter) = $self->find_filter('badges');
+    if ($badge_filter && @{$badge_filter->args}) {
+        my $badges = join (',', grep /^\d+$/ @{$badge_filter->args});
+        if ($badges) {
+            $badge_join = "'INNER JOIN rating.record_badge_score badge ON m.source = badge.record AND badge.id = ANY ('{$badges}')";
+        }
+    }
+
     my $bre_join = '';
     if ($self->find_modifier('deleted')) {
         $bre_join = 'INNER JOIN biblio.record_entry bre ON m.source = bre.id AND bre.deleted';
@@ -909,12 +919,13 @@ SELECT  $key AS id,
         FIRST(pubdate_t.value) AS tie_break
   FROM  metabib.metarecord_source_map m
         $$flat_plan{from}
-        $pubdate_join
         $mra_join
         $mrv_join
         $bre_join
-        $lang_join
+        $badge_join
+        $pubdate_join
         $pop_join
+        $lang_join
   WHERE 1=1
         $flat_where
   GROUP BY 1
index 634fd7b..8aafd92 100644 (file)
@@ -3151,39 +3151,62 @@ sub query_parser_fts_wrapper {
     my $borgs = undef;
     my $site = undef;
 
-    my ($site_filter) = $base_plan->parse_tree->find_filter('site');
-    if ($site_filter && @{$site_filter->args}) {
-        $site = $top_org if ($site_filter->args->[0] eq '-');
-        $site = $top_org if ($site_filter->args->[0] eq $top_org->shortname);
-        $site = actor::org_unit->search( { shortname => $site_filter->args->[0] })->next unless ($site);
-
-    } elsif ($args{org_unit}) {
-        $site = $top_org if ($args{org_unit} eq '-');
-        $site = $top_org if ($args{org_unit} eq $top_org->shortname);
-        $site = actor::org_unit->search( { shortname => $args{org_unit} })->next unless ($site);
-    }
-
-    if ($site && $site->id <> $top_org->id) {
-        my ($depth_filter) = $base_plan->parse_tree->find_filter('depth');
-        my $depth = $depth_filter->args->[0] if ($depth_filter && $depth_filter->args->[0] =~ /^\d+$/);
-        $depth = $args{depth} if (!defined($depth) && defined($args{depth}) && $args{depth} =~ /^\d+$/);
-
-        if (defined $depth and $depth > 0) { # Not scoped to the top of the tree
-            $borgs = OpenSRF::AppSession->create( 'open-ils.cstore' )->request(
-                'open-ils.cstore.json_query.atomic',
-                { from => [ 'actor.org_unit_descendants', $site->id, $depth ] }
-            )->gather(1);
-        } else {
-            $borgs = OpenSRF::AppSession->create( 'open-ils.cstore' )->request(
-                'open-ils.cstore.json_query.atomic',
-                { from => [ 'actor.org_unit_descendants', $site->id ] }
-            )->gather(1);
+    my @lg_id_list = @{$args{location_groups}} if (ref $args{location_groups});
+
+    my ($lg_filter) = $base_plan->parse_tree->find_filter('location_groups');
+    @lg_id_list = @{$lg_filter->args} if ($lg_filter && @{$lg_filter->args});
+
+    if (@lg_id_list) {
+        my @borg_list;
+        for my $lg ( grep { /^\d+$/ } @lg_id_list} ) {
+            my $lg_obj = asset::copy_location_group->retrieve($lg);
+            next unless $lg_obj;
+
+            if (''.$lg_obj->owner == $top_org->id) {
+                $borgs = undef;
+                last;
+            }
+
+            push(@borg_list, ''.$lg_obj->owner);
         }
+        $borgs = join(',', @borg_list) if @borg_list;
+    }
 
-        if (ref $borgs && @$borgs) {
-            $borgs = join(',', map { $_->{'actor.org_unit_descendants'} } @$borgs);
-        } else {
-            $borgs = undef;
+    if (!$borgs) {
+        my ($site_filter) = $base_plan->parse_tree->find_filter('site');
+        if ($site_filter && @{$site_filter->args}) {
+            $site = $top_org if ($site_filter->args->[0] eq '-');
+            $site = $top_org if ($site_filter->args->[0] eq $top_org->shortname);
+            $site = actor::org_unit->search( { shortname => $site_filter->args->[0] })->next unless ($site);
+    
+        } elsif ($args{org_unit}) {
+            $site = $top_org if ($args{org_unit} eq '-');
+            $site = $top_org if ($args{org_unit} eq $top_org->shortname);
+            $site = actor::org_unit->search( { shortname => $args{org_unit} })->next unless ($site);
+        }
+    
+        if ($site && $site->id <> $top_org->id) {
+            my ($depth_filter) = $base_plan->parse_tree->find_filter('depth');
+            my $depth = $depth_filter->args->[0] if ($depth_filter && $depth_filter->args->[0] =~ /^\d+$/);
+            $depth = $args{depth} if (!defined($depth) && defined($args{depth}) && $args{depth} =~ /^\d+$/);
+    
+            if (defined $depth and $depth > 0) { # Not scoped to the top of the tree
+                $borgs = OpenSRF::AppSession->create( 'open-ils.cstore' )->request(
+                    'open-ils.cstore.json_query.atomic',
+                    { from => [ 'actor.org_unit_descendants', $site->id, $depth ] }
+                )->gather(1);
+            } else {
+                $borgs = OpenSRF::AppSession->create( 'open-ils.cstore' )->request(
+                    'open-ils.cstore.json_query.atomic',
+                    { from => [ 'actor.org_unit_descendants', $site->id ] }
+                )->gather(1);
+            }
+    
+            if (ref $borgs && @$borgs) {
+                $borgs = join(',', map { $_->{'actor.org_unit_descendants'} } @$borgs);
+            } else {
+                $borgs = undef;
+            }
         }
     }
 
@@ -3217,7 +3240,7 @@ sub query_parser_fts_wrapper {
         $args{item_form} = [ split '', $f ];
     }
 
-    for my $filter ( qw/locations location_groups statuses between audience language lit_form item_form item_type bib_level vr_format/ ) {
+    for my $filter ( qw/locations location_groups statuses between audience language lit_form item_form item_type bib_level vr_format badges/ ) {
         if (my $s = $args{$filter}) {
             $s = [$s] if (!ref($s));