use XBase;
use Data::Dump qw/dump/;
use Encode qw/encode_utf8/;
-use YAML;
-use File::Slurp;
+use YAML qw/LoadFile DumpFile/;
=head1 NAME
my $ll_db = new WebPAC::Input::DBF(
path => '/path/to/database.dbf'
+ mapping_path => '/path/to/input/dbf/mapping.yml',
filter => sub {
my ($l,$field_nr) = @_;
# do something with $l which is line of input file
path to DBF file
+=item mapping_path
+
+path to mapping YAML which will be created on first run
+
=back
=cut
my $log = $self->_get_logger();
+ $log->logconfess("this module requires input_config") unless ( $arg->{input_config} );
+
my $db = XBase->new( $arg->{path} ) || $log->logdie("can't open ", $arg->{path}, ": $!");
my $size = $db->last_record;
$log->info("opening DBF database '$arg->{path}' with $size records");
- my $mapping = $arg->{input_config}->{mapping};
+ my $mapping_path = $arg->{input_config}->{mapping_path};
+ my $mapping;
- if ( ! $mapping ) {
- $log->debug("didn't found any mapping file in configuration", sub { dump( $arg->{input_config} ) });
+ if ( ! $mapping_path ) {
+ $log->debug("didn't found any mapping_path in configuration", sub { dump( $arg->{input_config} ) });
foreach my $field ( $db->field_names ) {
push @$mapping, { $field => { '900' => 'x' } };
$log->logdie("mapping file $mapping_path allready exists, aborting.") if ( -e $mapping_path );
- write_file( $mapping_path, Dump( { mapping => $mapping } ) ) ||
+ DumpFile( $mapping_path, Dump( { mapping => $mapping } ) ) ||
$log->logdie("can't write template file for mapping_path $mapping_path: $!");
$log->logdie("template file for mapping_path created as $mapping_path");
} else {
- $log->debug("using mapping ", sub { dump($mapping) });
+ $mapping = LoadFile( $mapping_path ) || $log->logdie("can't open $mapping_path: $!");
+ $log->logdie("missing top-level mapping key in $mapping_path") unless ( $mapping->{mapping} );
+ $mapping = $mapping->{mapping};
+ $log->debug("using mapping from $mapping_path = ", sub { dump($mapping) });
}
foreach my $mfn ( 1 .. $size ) {
'001' => [ $mfn ],
};
+ # fixme -- this *will* break given wrong structure!
+ foreach my $m ( @$mapping ) {
+ my $db_field = (keys %$m)[0];
+ my ( $f, $sf ) = %{ $m->{$db_field} };
+ push @{ $record->{$f} }, '^' . $sf . $row->{$db_field} if ( defined( $row->{$db_field} ) && $row->{$db_field} ne '' );
+ }
+
$self->{_rows}->{ $mfn } = $record;
$log->debug("created row $mfn ", dump( $record ));
}
Return hash from row. Taken from L<Biblio::Isis>
my $rec = $ll_db->_to_hash(
- mfn => $mfn;
- $row
+ mfn => $mfn,
+ row => $row,
);
=cut