kivitendo/SL/BackgroundJob/CsvImport.pm @ 79b7fc43
82c3b7eb | Sven Schöling | package SL::BackgroundJob::CsvImport;
|
||
use strict;
|
||||
use parent qw(SL::BackgroundJob::Base);
|
||||
use YAML ();
|
||||
use SL::DB::CsvImportProfile;
|
||||
use SL::SessionFile::Random;
|
||||
sub create_job {
|
||||
my ($self_or_class, %params) = @_;
|
||||
my $package = ref($self_or_class) || $self_or_class;
|
||||
$package =~ s/SL::BackgroundJob:://;
|
||||
my $profile = delete $params{profile} || SL::DB::CsvImportProfile->new;
|
||||
d7492165 | Sven Schöling | my $new_profile = $profile->clone_and_reset_deep;
|
||
$new_profile->save;
|
||||
82c3b7eb | Sven Schöling | |||
my %data = (
|
||||
%params,
|
||||
d7492165 | Sven Schöling | profile_id => $new_profile->id,
|
||
session_id => $::auth->get_session_id,
|
||||
82c3b7eb | Sven Schöling | );
|
||
my $job = SL::DB::BackgroundJob->new(
|
||||
type => 'once',
|
||||
active => 1,
|
||||
package_name => $package,
|
||||
data => YAML::Dump(\%data),
|
||||
);
|
||||
return $job;
|
||||
}
|
||||
sub profile {
|
||||
d7492165 | Sven Schöling | my ($self) = @_;
|
||
82c3b7eb | Sven Schöling | |||
if (!$self->{profile}) {
|
||||
d7492165 | Sven Schöling | my $data = YAML::Load($self->{db_obj}->data);
|
||
$self->{profile} = SL::DB::Manager::CsvImportProfile->find_by(id => $data->{profile_id});
|
||||
82c3b7eb | Sven Schöling | }
|
||
return $self->{profile};
|
||||
}
|
||||
sub run {
|
||||
my $self = shift;
|
||||
$self->{db_obj} = shift;
|
||||
$self->do_import;
|
||||
}
|
||||
sub do_import {
|
||||
my ($self) = @_;
|
||||
d344cfd7 | Sven Schöling | require SL::Controller::CsvImport;
|
||
82c3b7eb | Sven Schöling | my $c = SL::Controller::CsvImport->new;
|
||
98f37c10 | Sven Schöling | my $job = $self->{db_obj};
|
||
82c3b7eb | Sven Schöling | |||
d7492165 | Sven Schöling | $c->profile($self->profile);
|
||
98f37c10 | Sven Schöling | $c->type($job->data_as_hash->{type});
|
||
ec4aa5bb | Bernd Bleßmann | $c->{employee_id} = $job->data_as_hash->{employee_id};
|
||
98f37c10 | Sven Schöling | |||
my $test = $job->data_as_hash->{test};
|
||||
b2321d6a | Sven Schöling | |||
# $::locale->text('parsing csv')
|
||||
# $::locale->text('building data')
|
||||
# $::locale->text('saving data')
|
||||
# $::locale->text('building report')
|
||||
98f37c10 | Sven Schöling | $self->track_progress(
|
||
progress => 0,
|
||||
plan => {
|
||||
'parsing csv' => 1,
|
||||
'building data' => 2,
|
||||
6c9ab8b5 | Sven Schöling | ( 'saving data' => 3, )x!$test,
|
||
98f37c10 | Sven Schöling | 'building report' => ($test ? 3 : 4),
|
||
},
|
||||
num_phases => ($test ? 3 : 4),
|
||||
);
|
||||
d7492165 | Sven Schöling | $c->add_progress_tracker($self);
|
||
85a71bad | Bernd Bleßmann | my $session_id = $job->data_as_hash->{session_id};
|
||
d7492165 | Sven Schöling | |||
85a71bad | Bernd Bleßmann | $c->test_and_import(test => $test, session_id => $session_id);
|
||
98f37c10 | Sven Schöling | |||
if ($c->errors) {
|
||||
$job->set_data(
|
||||
errors => $c->errors,
|
||||
)->save;
|
||||
} else {
|
||||
d7492165 | Sven Schöling | |||
85a71bad | Bernd Bleßmann | my $report_id = $c->save_report(session_id => $session_id);
|
||
98f37c10 | Sven Schöling | $job->set_data(report_id => $report_id)->save;
|
||
$c->track_progress(finished => 1);
|
||||
}
|
||||
d7492165 | Sven Schöling | }
|
||
sub track_progress {
|
||||
98f37c10 | Sven Schöling | my ($self, %params) = @_;
|
||
my $data = $self->{db_obj}->data_as_hash;
|
||||
my $progress = $data->{progress} || {};
|
||||
82c3b7eb | Sven Schöling | |||
98f37c10 | Sven Schöling | $progress->{$_} = $params{$_} for keys %params;
|
||
d7492165 | Sven Schöling | $self->{db_obj}->set_data(progress => $progress);
|
||
$self->{db_obj}->save;
|
||||
82c3b7eb | Sven Schöling | }
|
||
1;
|
||||
__END__
|
||||
=encoding utf-8
|
||||
=head1 NAME
|
||||
SL::Background::CsvImport - backend for automatic imports of csv data
|
||||
=head1 SYNOPSIS
|
||||
use SL::BackgroundJob::CsvImport;
|
||||
From a controller or external source:
|
||||
my $job = SL::BackgroundJob::CsvImport->create_job(
|
||||
file => $file,
|
||||
%import_options
|
||||
);
|
||||
=head1 DESCRIPTION
|
||||
=head1 FUNCTIONS
|
||||
=head1 BUGS
|
||||
=head1 AUTHOR
|
||||
Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
|
||||
=cut
|