Fieldmapper toXML repairs and additions
authorBill Erickson <berick@esilibrary.com>
Thu, 12 Apr 2012 15:07:10 +0000 (11:07 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Wed, 1 Aug 2012 18:52:33 +0000 (14:52 -0400)
1. Be sure the append the current element to the document in progress

2. Use the class hint instead of the fully qualified class_name (e.g.
Fielmapper::actor::user) to avoid XML errors:

namespace error : Failed to parse QName 'Fieldmapper:'

3. Added support for additional options including "no_virt" which tells
the routine to skip all virtual fields and "skip_fields" to support
leaving specific fields out of the output.  The main use case for skip
fields is au => ['passwd'], but other examples might include large
fields like bre => ['marc'].

Signed-off-by: Bill Erickson <berick@esilibrary.com>
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>

Open-ILS/src/perlmods/lib/OpenILS/Utils/Fieldmapper.pm

index c7e7a25..33050fd 100644 (file)
@@ -337,14 +337,23 @@ sub toXML {
     my $self = shift;
     return undef unless (ref $self);
 
+    my $opts = shift || {};
+    my $no_virt = $$opts{no_virt}; # skip virtual fields
+    my $skip_fields = $$opts{skip_fields} || {}; # eg. {au => ['passwd']}
+    my @to_skip = @{$$skip_fields{$self->json_hint}} 
+        if $$skip_fields{$self->json_hint};
+
     my $dom = XML::LibXML::Document->new;
-    my $root = $dom->createElement( $self->class_name );
+    my $root = $dom->createElement( $self->json_hint );
     $dom->setDocumentElement( $root );
 
-    for my $f ($self->properties) {
+    my @field_names = $no_virt ? $self->real_fields : $self->properties;
+
+    for my $f (@field_names) {
         next if ($f eq 'isnew');
         next if ($f eq 'ischanged');
         next if ($f eq 'isdeleted');
+        next if (grep {$_ eq $f} @to_skip);
 
         my $value = $self->$f();
         my $element = $dom->createElement( $f );
@@ -354,7 +363,7 @@ sub toXML {
         if (ref($value)) { # array
             for my $k (@$value) {
                 if (blessed($k)) {
-                    my $subdoc = $k->toXML;
+                    my $subdoc = $k->toXML($opts);
                     next unless $subdoc;
                     my $subnode = $subdoc->documentElement;
                     $dom->adoptNode($subnode);
@@ -368,6 +377,8 @@ sub toXML {
         } else {
             $element->appendText($value);
         }
+
+        $root->appendChild($element);
     }
 
     return $dom;