X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FCsvImport%2FBase.pm;h=5f7e95e0e424c2e42978d7d9ae8859ef1477a0f3;hb=7b38e23d6509d1e4520caf77c9698bdb077c215f;hp=f84d54b0e01421f125d3ad9533cfb1b0280d6afc;hpb=280cbfe97461e08c8dbc604dd487b08b79ffd8ed;p=kivitendo-erp.git diff --git a/SL/Controller/CsvImport/Base.pm b/SL/Controller/CsvImport/Base.pm index f84d54b0e..5f7e95e0e 100644 --- a/SL/Controller/CsvImport/Base.pm +++ b/SL/Controller/CsvImport/Base.pm @@ -15,12 +15,16 @@ use parent qw(Rose::Object); use Rose::Object::MakeMethods::Generic ( - scalar => [ qw(controller file csv save_with_cascade) ], + scalar => [ qw(controller file csv test_run save_with_cascade) ], 'scalar --get_set_init' => [ qw(profile displayable_columns existing_objects class manager_class cvar_columns all_cvar_configs all_languages payment_terms_by all_vc vc_by) ], ); sub run { - my ($self) = @_; + my ($self, %params) = @_; + + $self->test_run($params{test_run}); + + $self->controller->track_progress(phase => 'parsing csv', progress => 0); my $profile = $self->profile; $self->csv(SL::Helper::Csv->new(file => $self->file->file_name, @@ -29,14 +33,19 @@ sub run { profile => $profile, ignore_unknown_columns => 1, strict_profile => 1, + case_insensitive_header => 1, map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char), )); + $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(progress => 50); + $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors; return if ( !$self->csv->header || $self->csv->errors ); @@ -49,9 +58,17 @@ sub run { $self->controller->info_headers({ used => { }, headers => [ ] }); my @objects = $self->csv->get_objects; + + $self->controller->track_progress(progress => 70); + my @raw_data = @{ $self->csv->get_data }; + + $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(progress => 90); + $self->check_objects; if ( $self->controller->profile->get('duplicates', 'no_check') ne 'no_check' ) { $self->check_std_duplicates(); @@ -59,6 +76,8 @@ sub run { } $self->fix_field_lengths; + $self->controller->track_progress(progress => 100); + $::myconfig{numberformat} = $old_numberformat; } @@ -103,6 +122,11 @@ sub add_cvar_raw_data_columns { map { $self->add_raw_data_columns($_) if exists $self->controller->data->[0]->{raw_data}->{$_} } @{ $self->cvar_columns }; } +sub init_all_cvar_configs { + # Must be overridden by derived specialized importer classes. + return []; +} + sub init_cvar_columns { my ($self) = @_; @@ -172,8 +196,6 @@ sub check_vc { sub handle_cvars { my ($self, $entry) = @_; - return unless $self->can('all_cvar_configs'); - my %type_to_column = ( text => 'text_value', textfield => 'text_value', select => 'text_value', @@ -212,7 +234,9 @@ sub init_profile { $profile{$col} = $name; } - $self->profile(\%profile); + $profile{ 'cvar_' . $_->name } = '' for @{ $self->all_cvar_configs }; + + \%profile; } sub add_displayable_columns { @@ -241,8 +265,6 @@ sub setup_displayable_columns { sub add_cvar_columns_to_displayable_columns { my ($self) = @_; - return unless $self->can('all_cvar_configs'); - $self->add_displayable_columns(map { { name => 'cvar_' . $_->name, description => $::locale->text('#1 (custom variable)', $_->description) } } @{ $self->all_cvar_configs }); @@ -357,7 +379,16 @@ sub save_objects { my $data = $params{data} || $self->controller->data; - foreach my $entry (@{ $data }) { + 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; + foreach my $entry_index (0 .. $#$data) { + my $entry = $data->[$entry_index]; next if @{ $entry->{errors} }; my $object = $entry->{object_to_save} || $entry->{object}; @@ -367,7 +398,14 @@ sub save_objects { } else { $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; + } } + $dbh->commit; } sub field_lengths {