kivitendo/SL/BackgroundJob/CsvImport.pm @ d7492165
82c3b7eb | Sven Schöling | package SL::BackgroundJob::CsvImport;
|
||
use strict;
|
||||
use parent qw(SL::BackgroundJob::Base);
|
||||
use YAML ();
|
||||
d7492165 | Sven Schöling | use SL::Controller::CsvImport;
|
||
82c3b7eb | Sven Schöling | 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;
|
||||
$self->cleanup;
|
||||
}
|
||||
sub do_import {
|
||||
my ($self) = @_;
|
||||
my $c = SL::Controller::CsvImport->new;
|
||||
d7492165 | Sven Schöling | $c->profile($self->profile);
|
||
$c->type($self->{db_obj}->data_as_hash->{type});
|
||||
$c->add_progress_tracker($self);
|
||||
$c->test_and_import(test => 1, session_id => $self->{db_obj}->data_as_hash->{session_id});
|
||||
my $report_id = $c->save_report;
|
||||
$self->{db_obj}->set_data(report_id => $report_id);
|
||||
$self->{db_obj}->save;
|
||||
$c->track_progress(100);
|
||||
}
|
||||
sub track_progress {
|
||||
my ($self, $progress) = @_;
|
||||
82c3b7eb | Sven Schöling | |||
d7492165 | Sven Schöling | $self->{db_obj}->set_data(progress => $progress);
|
||
$self->{db_obj}->save;
|
||||
82c3b7eb | Sven Schöling | }
|
||
sub cleanup {
|
||||
}
|
||||
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
|