| 1 | package ETLp::Plugin::Iterative::CSVLoader; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ETLp::Plugin::Iterative::CSVLoader - Plugin to load delimited files | ||
| 8 | |||
| 9 | |||
| 10 | =head1 METHODS | ||
| 11 | |||
| 12 | =head2 type | ||
| 13 | |||
| 14 | Registers the plugin type. | ||
| 15 | |||
| 16 | =head2 run | ||
| 17 | |||
| 18 | Executes the pipeline | ||
| 19 | |||
| 20 | =head1 ITEM | ||
| 21 | |||
| 22 | The item attribute hashref contains specific configuration information | ||
| 23 | |||
| 24 | =head2 localize | ||
| 25 | |||
| 26 | Whether to localize eol markers for the file being loaded | ||
| 27 | |||
| 28 | =head1 CONFIGURATION SETTINGS | ||
| 29 | |||
| 30 | The "config" section within the configuration file must contain the | ||
| 31 | following settings | ||
| 32 | |||
| 33 | =head2 controlfile_dir | ||
| 34 | |||
| 35 | Where the contrfolfile is located | ||
| 36 | |||
| 37 | =head2 controlfile | ||
| 38 | |||
| 39 | The name of the controlfile | ||
| 40 | |||
| 41 | =cut | ||
| 42 | |||
| 43 | class ETLp::Plugin::Iterative::CSVLoader extends ETLp::Plugin { | ||
| 44 | use ETLp::File::Config; | ||
| 45 | use File::Copy; | ||
| 46 | use Try::Tiny; | ||
| 47 | use File::Basename; | ||
| 48 | use ETLp::Loader::CSV; | ||
| 49 | |||
| 50 | sub type { | ||
| 51 | return 'csv_loader'; | ||
| 52 | } | ||
| 53 | |||
| 54 | method run(Str $filename) { | ||
| 55 | my $app_config = $self->config->{config}; | ||
| 56 | my $item = $self->item; | ||
| 57 | |||
| 58 | my $rule_conf = ETLp::File::Config->new( | ||
| 59 | directory => $app_config->{controlfile_dir}, | ||
| 60 | definition => $app_config->{controlfile} | ||
| 61 | ); | ||
| 62 | |||
| 63 | # Audit the file | ||
| 64 | my $aud_file_process = $self->audit->item->file_process; | ||
| 65 | |||
| 66 | my $file_id = $aud_file_process->get_canonical_id; | ||
| 67 | |||
| 68 | unless (-s $filename == 0) { | ||
| 69 | # The number of rows to skip (allows for headers in | ||
| 70 | # the files) | ||
| 71 | my $skip = $item->{skip} || '0'; | ||
| 72 | |||
| 73 | $aud_file_process->update_message('loading the file'); | ||
| 74 | |||
| 75 | my $csv_options = $item->{csv_options}; | ||
| 76 | |||
| 77 | my $loader = ETLp::Loader::CSV->new( | ||
| 78 | table => $app_config->{table_name}, | ||
| 79 | columns => $rule_conf->fields, | ||
| 80 | rules => $rule_conf->rules, | ||
| 81 | logger => $self->logger, | ||
| 82 | file_id => $file_id, | ||
| 83 | skip => $skip, | ||
| 84 | ignore_field_count => $item->{ignore_field_count}, | ||
| 85 | csv_options => $csv_options || {}, | ||
| 86 | localize => $item->{localize} || $app_config->{localize} || 0, | ||
| 87 | ); | ||
| 88 | |||
| 89 | unless ($loader->load($filename)) { | ||
| 90 | $aud_file_process->update_status('failed'); | ||
| 91 | $aud_file_process->update_message($loader->error); | ||
| 92 | ETLpException->throw(error => $loader->error); | ||
| 93 | } | ||
| 94 | |||
| 95 | $aud_file_process->record_count($loader->rows_loaded); | ||
| 96 | } else { | ||
| 97 | $aud_file_process->record_count(0); | ||
| 98 | } | ||
| 99 | |||
| 100 | $aud_file_process->update_status('succeeded'); | ||
| 101 | |||
| 102 | move($filename, $app_config->{archive_dir}) || | ||
| 103 | ETLpException->throw( error => "Unable to move $filename to " | ||
| 104 | . $app_config->{archive_dir} . ": " | ||
| 105 | . $!); | ||
| 106 | |||
| 107 | return $app_config->{archive_dir} . '/' . basename($filename); | ||
| 108 | } | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | =head1 LICENSE AND COPYRIGHT | ||
| 113 | |||
| 114 | Copyright 2010 Redbone Systems Ltd | ||
| 115 | |||
| 116 | This program is free software; you can redistribute it and/or modify it | ||
| 117 | under the terms of either: the GNU General Public License as published | ||
| 118 | by the Free Software Foundation; or the Artistic License. | ||
| 119 | |||
| 120 | The terms are in the LICENSE file that accompanies this application |