1 package SL::BackgroundJob::CsvImport;
5 use parent qw(SL::BackgroundJob::Base);
9 use SL::DB::CsvImportProfile;
12 my ($self_or_class, %params) = @_;
14 my $package = ref($self_or_class) || $self_or_class;
15 $package =~ s/SL::BackgroundJob:://;
19 session_id => $::auth->get_session_id,
22 my $job = SL::DB::BackgroundJob->new(
25 package_name => $package,
26 data => SL::YAML::Dump(\%data),
35 if (!$self->{profile}) {
36 my $data = SL::YAML::Load($self->{db_obj}->data);
37 $self->{profile} = SL::DB::Manager::CsvImportProfile->find_by(id => $data->{profile_id});
40 return $self->{profile};
45 $self->{db_obj} = shift;
53 require SL::Controller::CsvImport;
54 my $c = SL::Controller::CsvImport->new;
55 my $job = $self->{db_obj};
57 $c->profile($self->profile);
58 $c->mappings(SL::JSON::from_json($self->profile->get('json_mappings'))) if $self->profile->get('json_mappings');
59 $c->type($job->data_as_hash->{type});
60 $c->{employee_id} = $job->data_as_hash->{employee_id};
62 my $test = $job->data_as_hash->{test};
65 # $::locale->text('parsing csv')
66 # $::locale->text('building data')
67 # $::locale->text('saving data')
68 # $::locale->text('building report')
69 $self->track_progress(
74 ( 'saving data' => 3, )x!$test,
75 'building report' => ($test ? 3 : 4),
77 num_phases => ($test ? 3 : 4),
79 $c->add_progress_tracker($self);
81 my $session_id = $job->data_as_hash->{session_id};
83 $c->test_and_import(test => $test, session_id => $session_id);
89 $result = $::locale->text('Import finished with errors.');
92 my $report_id = $c->save_report(session_id => $session_id, test => $test);
93 $job->set_data(report_id => $report_id)->save;
95 $c->track_progress(finished => 1);
96 $result = $::locale->text('Import finished without errors.');
103 my ($self, %params) = @_;
105 my $data = $self->{db_obj}->data_as_hash;
106 my $progress = $data->{progress} || {};
108 $progress->{$_} = $params{$_} for keys %params;
109 $self->{db_obj}->set_data(progress => $progress);
110 $self->{db_obj}->save;
121 SL::Background::CsvImport - backend for automatic imports of csv data
126 use SL::BackgroundJob::CsvImport;
129 From a controller or external source:
131 my $job = SL::BackgroundJob::CsvImport->create_job(
144 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>