add new parallel field modifier to extract_holdings
[migration-tools.git] / Equinox-Migration / lib / Equinox / Migration / MapDrivenMARCXMLProc.pm
index b98a7ff..8208b8b 100644 (file)
@@ -1,5 +1,21 @@
 package Equinox::Migration::MapDrivenMARCXMLProc;
 
+# Copyright 2009-2012, Equinox Software, Inc.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+
 use warnings;
 use strict;
 
@@ -21,9 +37,10 @@ our $VERSION = '1.005';
 
 my $dstore;
 my $sfmap;
-my @modlist = qw( multi ignoremulti bib required first concatenate );
+my @modlist = qw( multi ignoremulti bib required first concatenate parallel );
 my %allmods = ();
 my $multis = {};
+my $parallel_fields = {};
 my $reccount;
 my $verbose = 0;
 
@@ -54,7 +71,9 @@ sub new {
 
     $verbose = 1 if $args{verbose};
 
-    my $self = bless { multis => \$multis,
+    my $self = bless {
+                        multis => \$multis,
+                        parallel_fields => \$parallel_fields,
                      }, $class;
 
     # initialize map and taglist
@@ -94,6 +113,28 @@ sub parse_record {
     for my $f (@fields)
       { process_field($f, $crec) }
 
+    # fill in blank values if needed
+    for my $mappedtag ( @{ $sfmap->tags }) {
+        unless (exists $crec->{tmap}{$mappedtag}) {
+            push @{ $crec->{tags} }, {};
+            for my $mappedsub ( @{ $sfmap->subfields($mappedtag) } ) {
+                my $fieldname = $sfmap->field($mappedtag, $mappedsub);
+                my $mods = $sfmap->mods($fieldname);
+                next if $mods->{multi};
+                if ($mods->{parallel}) {
+                    push @{ $crec->{tags}[-1]{parallel}{$mappedsub} }, '';
+                    $crec->{tags}[-1]{uni} = undef;
+                } else {
+                    $crec->{tags}[-1]{uni}{$mappedsub} = '';
+                    $crec->{tags}[-1]{parallel} = undef;
+                }
+                $crec->{tags}[-1]{multi} = undef;
+                $crec->{tags}[-1]{tag} = $mappedtag;
+            }
+            push @{ $crec->{tmap}{$mappedtag} }, $#{ $crec->{tags} };
+        }
+    }
+
     # cleanup memory and increment pointer
     $record->purge;
     $reccount++;
@@ -123,7 +164,7 @@ sub process_field {
         return;
     }
     if ($sfmap->has($tag)) {
-        push @{$crec->{tags}}, { tag => $tag, uni => undef, multi => undef };
+        push @{$crec->{tags}}, { tag => $tag, uni => undef, multi => undef, parallel => undef };
         push @{$crec->{tmap}{$tag}}, (@{$crec->{tags}} - 1);
         my @subs = $field->children('subfield');
         for my $sub (@subs)
@@ -134,12 +175,13 @@ sub process_field {
             my $fieldname = $sfmap->field($tag, $mappedsub);
             my $mods = $sfmap->mods($fieldname);
             next if $mods->{multi};
-            $crec->{tags}[-1]{uni}{$mappedsub} = ''
-              unless defined $crec->{tags}[-1]{uni}{$mappedsub};
-        }
-        for my $mappedtag ( @{ $sfmap->tags }) {
-            $crec->{tmap}{$mappedtag} = undef
-              unless defined $crec->{tmap}{$mappedtag};
+            if ($mods->{parallel}) {
+                push @{ $crec->{tags}[-1]{parallel}{$mappedsub} }, ''
+                    unless defined $crec->{tags}[-1]{parallel}{$mappedsub};
+            } else {
+                $crec->{tags}[-1]{uni}{$mappedsub} = ''
+                    unless defined $crec->{tags}[-1]{uni}{$mappedsub};
+            }
         }
     }
 }
@@ -163,6 +205,12 @@ sub process_subs {
         return if ($sub->text =~ /$filter/i);
     }
 
+    if ($mods->{parallel}) {
+        $parallel_fields->{$tag}{$code} = 1;
+        push @{$dataf->{parallel}{$code}}, $sub->text;
+        return;
+    }
+
     # handle multi modifier
     if ($mods->{multi}) {
         $multis->{$tag}{$code} = 1;
@@ -261,6 +309,17 @@ sub get_multis {
     return $multis;
 }
 
+=head2 get_parallel_fields
+
+Returns hashref of C<{tag}{code}> for all mapped parallel fields
+
+=cut
+
+sub get_parallel_fields {
+    my ($self) = @_;
+    return $parallel_fields;
+}
+
 =head1 MODIFIERS
 
 MapDrivenMARCXMLProc implements the following modifiers, and passes