From 6d44b89e862b947afb2421d1708bae220c21bc19 Mon Sep 17 00:00:00 2001 From: Lebbeous Fogle-Weekley Date: Fri, 13 Apr 2012 17:18:58 -0400 Subject: [PATCH] first go Signed-off-by: Lebbeous Fogle-Weekley --- Open-ILS/examples/fm_IDL.xml | 68 +++++++++++++----- .../perlmods/lib/OpenILS/Application/Acq/Search.pm | 80 +++++++++++++++------- 2 files changed, 105 insertions(+), 43 deletions(-) diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml index 375f93fa5d..9d9b30f830 100644 --- a/Open-ILS/examples/fm_IDL.xml +++ b/Open-ILS/examples/fm_IDL.xml @@ -9657,35 +9657,67 @@ SELECT usr, - + - + + + - - - + + + diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Search.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Search.pm index 109284ccf1..16844d94d2 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Search.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Acq/Search.pm @@ -262,12 +262,12 @@ sub prepare_terms { } sub add_au_joins { - my ($from) = shift; + my ($graft_map) = shift; my $n = 0; foreach my $join (@_) { my ($hint, $attr, $num) = @$join; - my $start = $from->{"acqus"}{$hint}; + my $start = $graft_map->{$hint}; my $clause = { "class" => "au", "type" => "left", @@ -289,6 +289,50 @@ sub add_au_joins { $n; } +sub build_from_clause_and_joins { + my ($query, $core, $and_terms, $or_terms) = @_; + + my %graft_map = (); + + $query->{from}{$core} = {}; + + my $join_type = keys(%$or_terms) ? "left" : "inner"; + + my @classes = grep { $core ne $_ } (keys(%$and_terms), keys(%$or_terms)); + my %classes_uniq = map { $_ => 1 } @classes; + @classes = keys(%classes_uniq); + + my $acqlia_join = sub { + return {"type" => "left", "field" => "lineitem", "fkey" => "id"}; + }; + + foreach my $class (@classes) { + if ($class eq 'acqlia') { + if ($core eq 'acqinv') { + $graft_map{acqlia} = + $query->{from}{$core}{acqmapinv}{join}{jub}{join}{acqlia} = + $acqlia_join->(); + } elsif ($core eq 'jub') { + $graft_map{acqlia} = + $query->{from}{$core}{acqlia} = + $acqlia_join->(); + } else { + $graft_map{acqlia} = + $query->{from}{$core}{jub}{join}{acqlia} = + $acqlia_join->(); + } + } elsif ($class eq 'acqinv' or $core eq 'acqinv') { + $graft_map{$class} = $query->{from}{$core}{acqmapinv}{join}{$class}; + $graft_map{$class}{type} = $join_type; + } else { + $graft_map{$class} = $query->{from}{$core}{$class}; + $graft_map{$class}{type} = $join_type; + } + } + + return \%graft_map; +} + __PACKAGE__->register_method( method => "unified_search", api_name => "open-ils.acq.lineitem.unified_search", @@ -383,36 +427,22 @@ q/order_by clause must be of the long form, like: } my $query = { - "select" => $select_clause, - from => { - acqus => { - jub => {type => "full"}, - acqpo => {type => "full"}, - acqpl => {type => "full"}, - acqinv => {type => "full"} - } - }, - "order_by" => ($options->{"order_by"} || {$hint => {"id" => {}}}), - "offset" => ($options->{"offset"} || 0) + select => $select_clause, + order_by => ($options->{order_by} || {$hint => {id => {}}}), + offset => ($options->{offset} || 0) }; $query->{"limit"} = $options->{"limit"} if $options->{"limit"}; - # XXX for the future? but it doesn't quite work as is. -# # Remove anything in temporary picklists from search results. -# $and_terms ||= {}; -# $and_terms->{"acqpl"} ||= []; -# push @{$and_terms->{"acqpl"}}, {"name" => "", "__not" => 1}; + my $graft_map = build_from_clause_and_joins( + $query, $hint, $and_terms, $or_terms + ); $and_terms = prepare_terms($and_terms, 1); - $or_terms = prepare_terms($or_terms, 0) and do { - $query->{"from"}{"acqus"}{"jub"}{"join"}{"acqlia"} = { - "type" => "left", "field" => "lineitem", "fkey" => "id", - }; - }; + $or_terms = prepare_terms($or_terms, 0); - my $offset = add_au_joins($query->{"from"}, prepare_au_terms($and_terms)); - add_au_joins($query->{"from"}, prepare_au_terms($or_terms, $offset)); + my $offset = add_au_joins($graft_map, prepare_au_terms($and_terms)); + add_au_joins($graft_map, prepare_au_terms($or_terms, $offset)); if ($and_terms and $or_terms) { $query->{"where"} = { -- 2.11.0