X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCsvImport%2FBase.pm;h=e9049899b0a468f1821fcbc3ebcfd6d7976e50db;hb=6b92d724ba32af43410d9563e77de6bb1013a5f1;hp=69060262ba098dac51fe4be9db35f3efbe7cd5d7;hpb=a0d9f09a2e63f0be9eda333ab7fa78679ea55759;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index 69060262b..e9049899b 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -3,10 +3,12 @@ package SL::Controller::CsvImport::Base; use strict; use English qw(-no_match_vars); +use List::Util qw(min); use List::MoreUtils qw(pairwise any); use SL::Helper::Csv; +use SL::DB; use SL::DB::BankAccount; use SL::DB::Customer; use SL::DB::Language; @@ -54,7 +56,7 @@ sub run { return if ( !$self->csv->header || $self->csv->errors ); - my $headers = { headers => [ map {; $_->{key} } @{ $self->csv->specs->[0] } ] }; + my $headers = { headers => [ grep { $self->csv->dispatcher->is_known($_, 0) } @{ $self->csv->header } ] }; $headers->{methods} = [ map { $_->{path} } @{ $self->csv->specs->[0] } ]; $headers->{used} = { map { ($_ => 1) } @{ $headers->{headers} } }; $self->controller->headers($headers); @@ -337,6 +339,10 @@ sub check_objects { sub check_duplicates { } +sub check_auth { + $::auth->assert('config'); +} + sub check_std_duplicates { my $self = shift; @@ -459,32 +465,34 @@ sub save_objects { $self->controller->track_progress(phase => 'saving data', progress => 0); # scale from 45..95%; - my $dbh = $data->[0]{object}->db; - - $dbh->begin_work; - foreach my $entry_index (0 .. $#$data) { - my $entry = $data->[$entry_index]; - next if @{ $entry->{errors} }; - - my $object = $entry->{object_to_save} || $entry->{object}; - - my $ret; - if (!eval { $ret = $object->save(cascade => !!$self->save_with_cascade()); 1 }) { - push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $EVAL_ERROR); - } elsif ( !$ret ) { - push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $object->db->error); - } else { - $self->_save_history($object); - $self->controller->num_imported($self->controller->num_imported + 1); - } - } continue { - if ($entry_index % 100 == 0) { - $dbh->commit; - $self->controller->track_progress(progress => $entry_index/scalar(@$data) * 100); # scale from 45..95%; - $dbh->begin_work; - } + my $dbh = $data->[0]{object}->db->dbh; + + my $last_index = $#$data; + my $chunk_size = 100; # one transaction and progress update every 100 objects + + for my $chunk (0 .. $last_index / $chunk_size) { + $self->controller->track_progress(progress => ($chunk_size * $chunk)/scalar(@$data) * 100); # scale from 45..95%; + SL::DB->client->with_transaction(sub { + foreach my $entry_index ($chunk_size * $chunk .. min( $last_index, $chunk_size * ($chunk + 1) - 1 )) { + my $entry = $data->[$entry_index]; + next if @{ $entry->{errors} }; + + my $object = $entry->{object_to_save} || $entry->{object}; + + my $ret; + if (!eval { $ret = $object->save(cascade => !!$self->save_with_cascade()); 1 }) { + push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $EVAL_ERROR); + } elsif ( !$ret ) { + push @{ $entry->{errors} }, $::locale->text('Error when saving: #1', $object->db->error); + } else { + $self->_save_history($object); + $self->controller->num_imported($self->controller->num_imported + 1); + } + } + 1; + }) or do { die SL::DB->client->error }; } - $dbh->commit; + $self->controller->track_progress(progress => 100); } sub field_lengths {