1 package SL::BackgroundJob::CsvImport;
5 use parent qw(SL::BackgroundJob::Base);
9 use SL::DB::CsvImportProfile;
10 use SL::SessionFile::Random;
13 my ($self_or_class, %params) = @_;
15 my $package = ref($self_or_class) || $self_or_class;
16 $package =~ s/SL::BackgroundJob:://;
18 my $profile = delete $params{profile} || SL::DB::CsvImportProfile->new;
19 my $new_profile = $profile->clone_and_reset_deep;
24 profile_id => $new_profile->id,
25 session_id => $::auth->get_session_id,
28 my $job = SL::DB::BackgroundJob->new(
31 package_name => $package,
32 data => YAML::Dump(\%data),
41 if (!$self->{profile}) {
42 my $data = YAML::Load($self->{db_obj}->data);
43 $self->{profile} = SL::DB::Manager::CsvImportProfile->find_by(id => $data->{profile_id});
46 return $self->{profile};
51 $self->{db_obj} = shift;
59 require SL::Controller::CsvImport;
60 my $c = SL::Controller::CsvImport->new;
61 my $job = $self->{db_obj};
63 $c->profile($self->profile);
64 $c->mappings(SL::JSON::from_json($self->profile->get('json_mappings'))) if $self->profile->get('json_mappings');
65 $c->type($job->data_as_hash->{type});
66 $c->{employee_id} = $job->data_as_hash->{employee_id};
68 my $test = $job->data_as_hash->{test};
71 # $::locale->text('parsing csv')
72 # $::locale->text('building data')
73 # $::locale->text('saving data')
74 # $::locale->text('building report')
75 $self->track_progress(
80 ( 'saving data' => 3, )x!$test,
81 'building report' => ($test ? 3 : 4),
83 num_phases => ($test ? 3 : 4),
85 $c->add_progress_tracker($self);
87 my $session_id = $job->data_as_hash->{session_id};
89 $c->test_and_import(test => $test, session_id => $session_id);
95 $result = $::locale->text('Import finished with errors.');
98 my $report_id = $c->save_report(session_id => $session_id, test => $test);
99 $job->set_data(report_id => $report_id)->save;
101 $c->track_progress(finished => 1);
102 $result = $::locale->text('Import finished without errors.');
109 my ($self, %params) = @_;
111 my $data = $self->{db_obj}->data_as_hash;
112 my $progress = $data->{progress} || {};
114 $progress->{$_} = $params{$_} for keys %params;
115 $self->{db_obj}->set_data(progress => $progress);
116 $self->{db_obj}->save;
127 SL::Background::CsvImport - backend for automatic imports of csv data
132 use SL::BackgroundJob::CsvImport;
135 From a controller or external source:
137 my $job = SL::BackgroundJob::CsvImport->create_job(
150 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>