| 1 | package ETLp::Plugin::Iterative::Validate; | ||
| 2 | |||
| 3 | use MooseX::Declare; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | ETLp::Plugin::Iterative::Validate - Plugin for validating data | ||
| 8 | |||
| 9 | =cut | ||
| 10 | |||
| 11 | class ETLp::Plugin::Iterative::Validate extends ETLp::Plugin { | ||
| 12 | use Try::Tiny; | ||
| 13 | use ETLp::File::Validate; | ||
| 14 | use File::Basename; | ||
| 15 | use Mail::Sendmail; | ||
| 16 | use Data::Dumper; | ||
| 17 | |||
| 18 | =head1 METHODS | ||
| 19 | |||
| 20 | =head2 type | ||
| 21 | |||
| 22 | Registers the plugin type. | ||
| 23 | |||
| 24 | =head2 run | ||
| 25 | |||
| 26 | Executes the pipeline | ||
| 27 | |||
| 28 | =cut | ||
| 29 | sub type { | ||
| 30 | return 'validate'; | ||
| 31 | } | ||
| 32 | |||
| 33 | method run (Str $filename) { | ||
| 34 | my $item = $self->item; | ||
| 35 | my $app_config = $self->config->{config}; | ||
| 36 | my $aud_file_process = $self->audit->item->file_process; | ||
| 37 | |||
| 38 | $self->logger->debug("file proc id: " . $aud_file_process->id); | ||
| 39 | |||
| 40 | my $skip = $item->{skip} || '0'; | ||
| 41 | my $on_error = $item->{on_error} || $app_config->{on_error} || 'die'; | ||
| 42 | |||
| 43 | $aud_file_process->update_message('validating file'); | ||
| 44 | |||
| 45 | my $validator; | ||
| 46 | |||
| 47 | unless (-s $filename == 0) { | ||
| 48 | my $localize = $item->{localize} || $app_config->{localize} || 0; | ||
| 49 | |||
| 50 | $self->logger->debug("CSV Options: " . Dumper($item->{csv_options})); | ||
| 51 | |||
| 52 | $validator = ETLp::File::Validate->new( | ||
| 53 | file_config_directory => $app_config->{controlfile_dir}, | ||
| 54 | file_definition => $app_config->{controlfile}, | ||
| 55 | type => $item->{file_type}, | ||
| 56 | skip => $skip, | ||
| 57 | ignore_field_count => $item->{ignore_field_count}, | ||
| 58 | localize => $localize, | ||
| 59 | csv_options => $item->{csv_options} || {} | ||
| 60 | ); | ||
| 61 | |||
| 62 | unless ($validator->validate($filename)) { | ||
| 63 | $self->logger->debug(Dumper($validator->get_errors)); | ||
| 64 | my @errors; | ||
| 65 | |||
| 66 | foreach my $error (@{$validator->get_errors}) { | ||
| 67 | push @errors, | ||
| 68 | "Line number: " . $error->{line_number} | ||
| 69 | . "\n\tfield name:" . $error->{field_name} | ||
| 70 | . "\n\tfield value:" . $error->{field_value} | ||
| 71 | . "\n\terror:" . $error->{message}; | ||
| 72 | } | ||
| 73 | |||
| 74 | if ($item->{email_alerts}) { | ||
| 75 | my %email = ( | ||
| 76 | To => $item->{email_alerts}, | ||
| 77 | From => $self->env_config->{email_sender}, | ||
| 78 | Subject => "Error Validating " . basename($filename), | ||
| 79 | Message => join("\n", @errors) | ||
| 80 | ); | ||
| 81 | |||
| 82 | sendmail(%email) || $self->logger->error( | ||
| 83 | "Problem sending validation error to " | ||
| 84 | . $item->{email_alerts} . "\n" | ||
| 85 | . $Mail::Sendmail::error | ||
| 86 | ); | ||
| 87 | |||
| 88 | $self->logger->debug("Validation email sent"); | ||
| 89 | } | ||
| 90 | |||
| 91 | my $error = basename($filename) . | ||
| 92 | " failed validation:\n" . join("\n", @errors); | ||
| 93 | |||
| 94 | $aud_file_process->update_status('failed'); | ||
| 95 | $aud_file_process->update_message($error); | ||
| 96 | |||
| 97 | ETLpException->throw(error => $error); | ||
| 98 | } | ||
| 99 | |||
| 100 | $aud_file_process->update_status('succeeded'); | ||
| 101 | return $filename; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | =head1 LICENSE AND COPYRIGHT | ||
| 107 | |||
| 108 | Copyright 2010 Redbone Systems Ltd | ||
| 109 | |||
| 110 | This program is free software; you can redistribute it and/or modify it | ||
| 111 | under the terms of either: the GNU General Public License as published | ||
| 112 | by the Free Software Foundation; or the Artistic License. | ||
| 113 | |||
| 114 | The terms are in the LICENSE file that accompanies this application |