birthing MARCXMLSampler
[migration-tools.git] / Equinox-Migration / lib / Equinox / Migration / MapDrivenMARCXMLProc.pm
index 9297105..904ac25 100644 (file)
@@ -4,7 +4,17 @@ use warnings;
 use strict;
 
 use XML::Twig;
-use Equinox::Migration::SubfieldMapper 1.002;
+use Equinox::Migration::SubfieldMapper 1.003;
+
+# FIXME
+#
+# sample functionality should be extracted into a new module which
+# uses E::M::SM to drive sampling of individual datafields, and
+# reports ALL datafields which occur
+#
+# --sample should give the list of all datafields
+# --samplefile should take a SM map as teh argument and introspect the mapped datafields
+
 
 =head1 NAME
 
@@ -38,31 +48,18 @@ and C<marcfile> (the MARC data to be processed).
     my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( mapfile  => FILE,
                                                            marcfile => FILE );
 
-There is an optional third, argument, C<sample>, which specifies a
-arrayref of datafields to "sample" by reporting on subfields which are
-found in the data but not in the map.
-
-    my $m = Equinox::Migration::MapDrivenMARCXMLProc->new( mapfile  => FILE,
-                                                           marcfile => FILE,
-                                                           sample   => \@TAGS
-                                                         );
-
-See L</UNMAPPED TAGS> for more info.
-
 =cut
 
 sub new {
     my ($class, %args) = @_;
 
     my $self = bless { mods => { multi    => {},
-                                 once     => {},
+                                 bib      => {},
                                  required => {},
                                },
                        data => { recs => undef, # X::T record objects
                                  rptr => 0,     # next record pointer
                                  crec => undef, # parsed record storage
-                                 stag => undef, # list of tags to sample
-                                 umap => undef, # unmapped data samples
                                },
                      }, $class;
 
@@ -84,13 +81,6 @@ sub new {
         die "Can't open marc file: $!\n";
     }
 
-    # if we have a sample arg, set up the sample set and umap hash
-    if (defined $args{sample}) {
-        for my $s ( @{$args{sample}})
-          { $self->{data}{stag}{$s} = 1 }
-        $self->{data}{umap} = {};
-    }
-
     return $self;
 }
 
@@ -123,6 +113,9 @@ sub parse_record {
     $record->purge;
     $self->{data}{rptr}++;
 
+    # check for required fields
+    $self->check_required;
+
     return $self->{data}{crec};
 }
 
@@ -165,35 +158,81 @@ sub process_subs {
     my $code = $sub->{'att'}->{'code'};
 
     # handle unmapped tag/subs
-    unless ($map->has($tag, $code)) {
-        my $u = $self->{data}{umap};
-        my $s = $self->{data}{stag};
-        return unless (defined $s->{$tag});
-
-        # set a value, total-seen count and records-seen-in count
-        $u->{$tag}{$code}{value} = $sub->text unless defined $u->{$tag}{$code};
-        $u->{$tag}{$code}{count}++;
-        $u->{$tag}{$code}{rcnt}++ unless ( defined $u->{$tag}{$code}{last} and
-                                           $u->{$tag}{$code}{last} == $self->{data}{rptr} );
-        $u->{$tag}{$code}{last} = $self->{data}{rptr};
-        return;
-    }
+    return unless ($map->has($tag, $code));
 
     # fetch our datafield struct and fieldname
     my $dataf = $self->{data}{crec}{tags}[-1];
     my $field = $map->field($tag, $code);
 
-    # handle modifiers, or slug data in normally
+    # handle modifiers
     if (my $mods = $map->mods($field)) {
         if ($mods->{multi}) {
             my $name = $tag . $code;
             push @{$dataf->{multi}{$name}}, $sub->text;
+            return;
+        }
+    }
+
+    die "Multiple occurances of a non-multi field: $tag$code at rec ",
+      ($self->{data}{rptr} + 1),"\n" if (defined $dataf->{uni}{$code});
+    $dataf->{uni}{$code} = $sub->text;
+}
+
+
+sub check_required {
+    my ($self) = @_;
+    my $mods = $self->{map}->mods;
+    my $crec = $self->{data}{crec};
+
+    for my $tag_id (keys %{$mods->{required}}) {
+        for my $code (@{$mods->{required}{$tag_id}}) {
+            my $found = 0;
+
+            $found = 1 if ($crec->{bib}{($tag_id . $code)});
+            for my $tag (@{$crec->{tags}}) {
+                $found = 1 if ($tag->{multi}{($tag_id . $code)});
+                $found = 1 if ($tag->{uni}{$code});
+            }
+
+            die "Required mapping $tag_id$code not found in rec ",$self->{data}{rptr},"\n"
+              unless ($found);
         }
-    } else {
-        $dataf->{uni}{$code} = $sub->text;
     }
+
 }
 
+=head1 MODIFIERS
+
+MapDrivenMARCXMLProc implements the following modifiers, and passes
+them to L<Equinox::Migration::SubfieldMapper>, meaning that specifying
+any other modifiers in a MDMP map file will cause a fatal error when
+it is processed.
+
+=head2 multi
+
+If a mapping is declared to be C<multi>, then MDMP expects to see more
+than one instance of that subfield per datafield, and the data is
+handled accordingly (see L</PARSED RECORDS> below).
+
+Occurring zero or one time is legal for a C<multi> mapping.
+
+A mapping which is not flagged as C<multi>, but which occurs more than
+once per datafield will cause a fatal error.
+
+=head2 bib
+
+The C<bib> modifier declares that a mapping is "bib-level", and should
+be encountered once per B<record> instead of once per B<datafield> --
+which is another way of saying that it occurs in a non-repeating
+datafield or in a controlfield.
+
+=head2 required
+
+By default, if a mapping does not occur in a datafield (or record, in
+the case of C<bib> mappings), processing continues normally. if a
+mapping has the C<required> modifier, however, it must appear, or a
+fatal error will occur.
+
 =head1 PARSED RECORDS
 
 Given:
@@ -261,31 +300,6 @@ datafield will be given a value of '' (the null string) in the current
 record struct. Oppose subfields which are not mapped, which will be
 C<undef>.
 
-=head1 UNMAPPED TAGS
-
-If the C<sample> argument is passed to L</new>, there will also be a
-structure which holds data about unmapped subfields encountered in
-mapped tags which are also in the declared sample set. This
-information is collected over the life of the object and is not reset
-for every record processed (as the current record data neccessarily
-is).
-
-    { tag_id => {
-                  sub_code  => { value => VALUE,
-                                 count => COUNT,
-                                 rcnt => RCOUNT
-                               },
-                  ...
-                },
-      ...
-    }
-
-For each mapped tag, for each unmapped subfield, there is a hash of
-data about that subfield containing
-
-    * value - A sample of the subfield text
-    * count - Total number of times the subfield was seen
-    * rcnt  - The number of records the subfield was seen in
 
 =head1 AUTHOR