my ($self) = @_;
my $c = SL::Controller::CsvImport->new;
+ my $job = $self->{db_obj};
$c->profile($self->profile);
- $c->type($self->{db_obj}->data_as_hash->{type});
+ $c->type($job->data_as_hash->{type});
+
+ my $test = $job->data_as_hash->{test};
+
+ $self->track_progress(
+ progress => 0,
+ plan => {
+ 'parsing csv' => 1,
+ 'building data' => 2,
+ ( 'saving data' => 3, )x!!$test,
+ 'building report' => ($test ? 3 : 4),
+ },
+ num_phases => ($test ? 3 : 4),
+ );
$c->add_progress_tracker($self);
- $c->test_and_import(test => 1, session_id => $self->{db_obj}->data_as_hash->{session_id});
- my $report_id = $c->save_report;
- $self->{db_obj}->set_data(report_id => $report_id);
- $self->{db_obj}->save;
+ $c->test_and_import(test => 1, session_id => $job->data_as_hash->{session_id});
+
+ if ($c->errors) {
+ $job->set_data(
+ errors => $c->errors,
+ progress => -1,
+ )->save;
+ } else {
- $c->track_progress(100);
+ my $report_id = $c->save_report;
+ $job->set_data(report_id => $report_id)->save;
+
+ $c->track_progress(finished => 1);
+ }
}
sub track_progress {
- my ($self, $progress) = @_;
+ my ($self, %params) = @_;
+
+ my $data = $self->{db_obj}->data_as_hash;
+ my $progress = $data->{progress} || {};
+ $progress->{$_} = $params{$_} for keys %params;
$self->{db_obj}->set_data(progress => $progress);
$self->{db_obj}->save;
}
$self->profile($profile);
- if ($data->{progress} < 100) {
+ if ($data->{errors} and my $first_error = $data->{errors}->[0]) {
+ flash('error', $::locale->text('There was an error parsing the csv file: #1 in line #2.', $first_error->[2], $first_error->[0]));
+ }
+
+ if (!$data->{progress}{finished}) {
$self->render('csv_import/_deferred_results', { no_layout => 1 });
} else {
$self->action_report(report_id => $data->{report_id}, no_layout => 1);
file => $self->csv_file_name,
profile => $self->profile,
type => $self->profile->type,
+ test => $params{test},
)->save;
SL::System::TaskServer->start_if_not_running;
$worker->run;
+ return if $self->errors;
+
$self->num_imported(0);
$worker->save_objects if !$params{test};
sub save_report {
my ($self, $report_id) = @_;
+ $self->track_progress(phase => 'building report', progress => 0);
+
my $clone_profile = $self->profile->clone_and_reset_deep;
$clone_profile->save; # weird bug. if this isn't saved before adding it to the report, it will default back to the last profile.
my $o2 = $o1 + @methods;
for my $row (0 .. $#{ $self->data }) {
+ $self->track_progress(progress => $row / @{ $self->data } * 100) if $row % 100 == 0;
my $data_row = $self->{data}[$row];
$sth->execute($report->id, $_, $row + 1, $data_row->{info_data}{ $info_methods[$_] }) for 0 .. $#info_methods;
}
sub track_progress {
- my ($self, $progress) = @_;
+ my ($self, %params) = @_;
for my $tracker ($self->progress_tracker) {
- $tracker->track_progress($progress);
+ $tracker->track_progress(%params);
}
}
sub run {
my ($self) = @_;
- $self->controller->track_progress(0);
+ $self->controller->track_progress(phase => 'parsing csv', progress => 0);
my $profile = $self->profile;
$self->csv(SL::Helper::Csv->new(file => $self->file->file_name,
map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
));
- $self->controller->track_progress(1);
+ $self->controller->track_progress(progress => 10);
my $old_numberformat = $::myconfig{numberformat};
$::myconfig{numberformat} = $self->controller->profile->get('numberformat');
$self->csv->parse;
- $self->controller->track_progress(3);
+ $self->controller->track_progress(progress => 50);
$self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
$self->controller->raw_data_headers({ used => { }, headers => [ ] });
$self->controller->info_headers({ used => { }, headers => [ ] });
- $::lxdebug->dump(0, "self", $self->controller->info_headers);
- $::lxdebug->dump(0, "self", $self->controller->headers);
- $::lxdebug->dump(0, "self", $self->controller->raw_data_headers);
-
my @objects = $self->csv->get_objects;
- $self->controller->track_progress(4);
+ $self->controller->track_progress(progress => 70);
my @raw_data = @{ $self->csv->get_data };
- $self->controller->track_progress(4.5);
+ $self->controller->track_progress(progress => 80);
$self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @objects, @raw_data ]);
- $self->controller->track_progress(5);
+ $self->controller->track_progress(progress => 90);
$self->check_objects;
if ( $self->controller->profile->get('duplicates', 'no_check') ne 'no_check' ) {
}
$self->fix_field_lengths;
- $self->controller->track_progress(99);
+ $self->controller->track_progress(progress => 100);
$::myconfig{numberformat} = $old_numberformat;
}
return unless $data->[0];
return unless $data->[0]{object};
+ $self->controller->track_progress(phase => 'saving objects', progress => 0); # scale from 45..95%;
+
my $dbh = $data->[0]{object}->db;
$dbh->begin_work;
} continue {
if ($entry_index % 100 == 0) {
$dbh->commit;
- $self->controller->track_progress(45 + $entry_index/scalar(@$data) * 50); # scale from 45..95%;
+ $self->controller->track_progress(progress => $entry_index/scalar(@$data) * 100); # scale from 45..95%;
$dbh->begin_work;
}
}
return unless @{ $self->controller->data };
+ $self->controller->track_progress(phase => 'building data', progress => 0);
+
$self->makemodel_columns({});
my $i;
my $num_data = scalar @{ $self->controller->data };
foreach my $entry (@{ $self->controller->data }) {
- $self->controller->track_progress(8 + ($i/$num_data * 40)) if $i % 100 == 0; # scale from 5..45%
+ $self->controller->track_progress(progress => $i/$num_data * 100) if $i % 100 == 0;
$self->check_buchungsgruppe($entry);
$self->check_type($entry);
my $data = YAML::Load($self->data);
$data->{$_} = $data{$_} for keys %data;
$self->data(YAML::Dump($data));
+
+ $self;
}
sub validate {
+[%- USE L %]
+[%- USE T8 %]
+[%- USE HTML %]
+
+<h2>[% 'Import Status' | $T8 %]</h2>
+
+[% PROCESS 'common/flash.html' %]
+<div id='progress_description'></div>
<div id='progressbar'></div>
-[% IF progress < 100 %]
<script type='text/javascript'>
function reload_results () {
$.ajax({
error: function(e) { alert(e) },
});
}
+[%- UNLESS SELF.background_job.data_as_hash.progress < 0 %]
$(document).ready(function(){
- $('#progressbar').progressbar({ value: [% SELF.background_job.data_as_hash.progress * 1 %] });
+ $('#progress_description').html('[% SELF.background_job.data_as_hash.progress.plan.${SELF.background_job.data_as_hash.progress.phase} %] / [% SELF.background_job.data_as_hash.progress.num_phases %] [% SELF.background_job.data_as_hash.progress.phase | $T8 | html %]');
+ $('#progressbar').progressbar({ value: [% SELF.background_job.data_as_hash.progress.progress * 1 %] });
window.setTimeout(reload_results, 100);
})
+[%- END %]
</script>
-[% END %]