Asynchroner Import
[kivitendo-erp.git] / SL / BackgroundJob / CsvImport.pm
1 package SL::BackgroundJob::CsvImport;
2
3 use strict;
4
5 use parent qw(SL::BackgroundJob::Base);
6
7 use YAML ();
8 use SL::DB::CsvImportProfile;
9 use SL::SessionFile::Random;
10
11 sub create_job {
12   my ($self_or_class, %params) = @_;
13
14   my $package       = ref($self_or_class) || $self_or_class;
15   $package          =~ s/SL::BackgroundJob:://;
16
17   my $profile = delete $params{profile} || SL::DB::CsvImportProfile->new;
18   my $result  = delete $params{result}  || SL::SessionFile::Random->new;
19
20   my %data = (
21     profile => { $profile->flatten },
22     result  => $result->file_name,
23     %params,
24   );
25
26   my $job = SL::DB::BackgroundJob->new(
27     type         => 'once',
28     active       => 1,
29     package_name => $package,
30     data         => YAML::Dump(\%data),
31   );
32
33   return $job;
34 }
35
36 sub profile {
37   my ($self, $db_obj) = @_;
38
39   if (!$self->{profile}) {
40     $self->{profile} = SL::DB::CsvImportProfile->new;
41     my $data = YAML::Load($db_obj->data);
42     for (keys %$data) {
43       $self->{profile}->set($_ => $data->{$_});
44     }
45   }
46
47   return $self->{profile};
48 }
49
50 sub run {
51   my $self        = shift;
52   $self->{db_obj} = shift;
53
54   $self->do_import;
55
56   $self->cleanup;
57 }
58
59 sub do_import {
60   my ($self) = @_;
61
62   my $c = SL::Controller::CsvImport->new;
63   $c->profile($self->{profile});
64   $c->test_and_import(test => $self->);
65
66
67 }
68
69 sub cleanup {
70
71 }
72
73 1;
74
75 __END__
76
77 =encoding utf-8
78
79 =head1 NAME
80
81 SL::Background::CsvImport - backend for automatic imports of csv data
82
83 =head1 SYNOPSIS
84
85
86 use SL::BackgroundJob::CsvImport;
87
88
89 From a controller or external source:
90
91   my $job = SL::BackgroundJob::CsvImport->create_job(
92     file => $file,
93     %import_options
94   );
95
96 =head1 DESCRIPTION
97
98 =head1 FUNCTIONS
99
100 =head1 BUGS
101
102 =head1 AUTHOR
103
104 Sven Schoeling E<lt>s.schoeling@linet-services.deE<gt>
105
106 =cut