bugfixes. working pretty nicely now. even does the right thing with the ...
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Wed, 21 Mar 2012 19:20:23 +0000 (15:20 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Wed, 21 Mar 2012 19:20:23 +0000 (15:20 -0400)
... example from the google doc, if you first make all those class's
pcrud-controlled.

Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Flattener.pm

index 2cd2c19..09e20b1 100644 (file)
@@ -33,7 +33,6 @@ sub _flattened_search_single_flesh_wad {
     my $piece;
 
     while ($piece = shift @$path) {
-        $logger->debug("_flattened_search_single_flesh_wad() dealing with $piece");
         my $link = _fm_link_from_class($class, $piece);
         if ($link) {
             $flesh_fields->{$hint} ||= [];
@@ -70,8 +69,6 @@ sub _flattened_search_single_join_clause {
     my $alias;  # yes, we need it out at this scope.
 
     while ($piece = shift @$path) {
-        $logger->debug("_flattened_search_single_join_clause() dealing with $piece");
-
         my $link = _fm_link_from_class($class, $piece);
         if ($link) {
             $hint = $link->{class};
@@ -168,7 +165,7 @@ sub _flattened_search_merge_flesh_wad {
             # $new->{flesh_fields}{$key}, treating the arrays as sets.
 
             my %hash = map { $_ => 1 } (
-                @{ $old->{flesh_fields}{$key} }, 
+                @{ $old->{flesh_fields}{$key} },
                 @{ $new->{flesh_fields}{$key} }
             );
             $old->{flesh_fields}{$key} = [ keys(%hash) ];
@@ -200,8 +197,10 @@ sub _flattened_search_expand_filter_column {
 
             return $o->{$table}{$column};
         } else {    # field must be on core class
-            $o->{$column} = $o->{$key};
-            delete $o->{$key};
+            if ($column ne $key) {
+                $o->{$column} = $o->{$key};
+                delete $o->{$key};
+            }
             return $o->{$column};
         }
     } else {
@@ -220,17 +219,14 @@ sub _flattened_search_recursively_apply_map_to_filter {
             # intended to avoid trying to map things like "between" in
             # constructs like:
             #   {"somecolumn": {"between": [1,10]}}
-            # and to that extend, it succeeds.
+            # and to that extent, it works.
 
             if (not $state->{in_expr} and $key =~ /^[a-z]/) {
                 $state->{in_expr} = 1;
 
-                my $deeper = _flattened_search_expand_filter_column(
-                    $o, $key, $map
-                );
-
                 _flattened_search_recursively_apply_map_to_filter(
-                    $deeper, $map, $state
+                    _flattened_search_expand_filter_column($o, $key, $map),
+                    $map, $state
                 );
 
                 $state->{in_expr} = 0;
@@ -310,20 +306,27 @@ sub finish_jffolo {
     return $jffolo unless $slo->{sort};
 
     # The slo has a special format for 'sort' that gives callers what they
-    # need, but isn't as flexible as json_query's order_by.
+    # need, but isn't as flexible as json_query's 'order_by'.
     #
     # "sort": [{"column1": "asc"}, {"column2": "desc"}]
-    #   or optionally,
+    #   or
+    # "sort": ["column1", {"column2": "desc"}]
+    #   or
     # "sort": {"onlycolumn": "asc"}
+    #   or
+    # "sort": "onlycolumn"
 
     $jffolo->{order_by} = [];
 
     # coerce from optional simpler format (see comment blob above)
-    $slo->{sort} = [ $slo->{sort} ] if ref $slo->{sort} eq "HASH";
+    $slo->{sort} = [ $slo->{sort} ] unless ref $slo->{sort} eq "ARRAY";
 
     foreach my $exp (@{ $slo->{sort} }) {
-        # XXX By assuming that each sort expression is a single key/value pair,
-        # we preclude the ability to use transforms and the like for now.
+        $exp = { $exp => "asc" } unless ref $exp;
+
+        # XXX By assuming that each sort expression is (at most) a single
+        # key/value pair, we preclude the ability to use transforms and the
+        # like for now.
 
         my ($key) = keys(%$exp);
 
@@ -343,9 +346,17 @@ sub finish_jffolo {
     return $jffolo;
 }
 
+# Given a map and a fieldmapper object, return a flat representation as
+# specified by the map's display fields
 sub process_result {
     my ($map, $fmobj) = @_;
 
+    if (not ref $fmobj) {
+        throw OpenSRF::EX::ERROR(
+            "process_result() was passed an inappropriate second argument"
+        );
+    }
+
     my $flatrow = {};
 
     while (my ($key, $mapping) = each %$map) {
@@ -357,9 +368,10 @@ sub process_result {
         my $obj = $fmobj;
         while (my $step = shift @path) {
             $obj = $obj->$step;
+            last unless ref $obj;
         }
 
-        $flatrow->{$key} = $obj->$field;
+        $flatrow->{$key} = ref $obj ? $obj->$field : undef;
     }
 
     return $flatrow;