$package =~ s/SL::BackgroundJob:://;
my $profile = delete $params{profile} || SL::DB::CsvImportProfile->new;
- my $result = delete $params{result} || SL::SessionFile::Random->new;
+ my $new_profile = $profile->clone_and_reset_deep;
+ $new_profile->save;
my %data = (
- profile => { $profile->flatten },
- result => $result->file_name,
%params,
+ profile_id => $new_profile->id,
+ session_id => $::auth->get_session_id,
);
my $job = SL::DB::BackgroundJob->new(
}
sub profile {
- my ($self, $db_obj) = @_;
+ my ($self) = @_;
if (!$self->{profile}) {
- $self->{profile} = SL::DB::CsvImportProfile->new;
- my $data = YAML::Load($db_obj->data);
- for (keys %$data) {
- $self->{profile}->set($_ => $data->{$_});
- }
+ my $data = YAML::Load($self->{db_obj}->data);
+ $self->{profile} = SL::DB::Manager::CsvImportProfile->find_by(id => $data->{profile_id});
}
return $self->{profile};
$self->{db_obj} = shift;
$self->do_import;
-
- $self->cleanup;
}
sub do_import {
my ($self) = @_;
+ require SL::Controller::CsvImport;
my $c = SL::Controller::CsvImport->new;
- $c->profile($self->{profile});
- $c->test_and_import(test => $self->);
+ my $job = $self->{db_obj};
+
+ $c->profile($self->profile);
+ $c->type($job->data_as_hash->{type});
+ $c->{employee_id} = $job->data_as_hash->{employee_id};
+
+ my $test = $job->data_as_hash->{test};
+
+
+ # $::locale->text('parsing csv')
+ # $::locale->text('building data')
+ # $::locale->text('saving data')
+ # $::locale->text('building report')
+ $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);
+
+ my $session_id = $job->data_as_hash->{session_id};
+ $c->test_and_import(test => $test, session_id => $session_id);
+ my $result;
+ if ($c->errors) {
+ $job->set_data(
+ errors => $c->errors,
+ )->save;
+ $result = $::locale->text('Import finished with errors.');
+ } else {
+ my $report_id = $c->save_report(session_id => $session_id);
+ $job->set_data(report_id => $report_id)->save;
+
+ $c->track_progress(finished => 1);
+ $result = $::locale->text('Import finished without errors.');
+ }
+
+ return $result;
}
-sub cleanup {
+sub track_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;
}
1;