Revision 82c3b7eb
Von Sven Schöling vor fast 12 Jahren hinzugefügt
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 |
SL/Controller/CsvImport.pm | ||
---|---|---|
13 | 13 |
use SL::Controller::CsvImport::Part; |
14 | 14 |
use SL::Controller::CsvImport::Shipto; |
15 | 15 |
use SL::Controller::CsvImport::Project; |
16 |
use SL::BackgroundJob::CsvImport; |
|
17 |
use SL::System::TaskServer; |
|
16 | 18 |
|
17 | 19 |
use List::MoreUtils qw(none); |
18 | 20 |
|
... | ... | |
43 | 45 |
|
44 | 46 |
sub action_test { |
45 | 47 |
my ($self) = @_; |
46 |
$self->test_and_import(test => 1); |
|
48 |
$self->test_and_import_deferred(test => 1);
|
|
47 | 49 |
} |
48 | 50 |
|
49 | 51 |
sub action_import { |
50 | 52 |
my $self = shift; |
51 |
$self->test_and_import(test => 0); |
|
53 |
$self->test_and_import_deferred(test => 0);
|
|
52 | 54 |
} |
53 | 55 |
|
54 | 56 |
sub action_save { |
... | ... | |
71 | 73 |
$self->redirect_to(action => 'new', 'profile.type' => $self->type); |
72 | 74 |
} |
73 | 75 |
|
76 |
sub action_result { |
|
77 |
my $self = shift; |
|
78 |
|
|
79 |
# load resultobject |
|
80 |
$self->{background_job} = SL::DB::Manager::BackgroundJob->find_by(id => $::form->{job}); |
|
81 |
|
|
82 |
my $data = $self->{background_job}->data_as_hash; |
|
83 |
|
|
84 |
my $profile = SL::DB::CsvImportProfile->new(type => $data->{type}); |
|
85 |
my $profile_data = $data->{profile}; |
|
86 |
for (keys %$profile_data) { |
|
87 |
$profile->set($_ => $profile_data->{$_}); |
|
88 |
} |
|
89 |
|
|
90 |
$self->profile($profile); |
|
91 |
|
|
92 |
if ($data->{progress} < 100) { |
|
93 |
$self->render('csv_import/_deferred_results', { no_layout => 1 }); |
|
94 |
} else { |
|
95 |
die 'what? done? panic, no idea what to do'; |
|
96 |
} |
|
97 |
} |
|
98 |
|
|
74 | 99 |
sub action_download_sample { |
75 | 100 |
my $self = shift; |
76 | 101 |
|
... | ... | |
157 | 182 |
$self->render('csv_import/form', title => $title); |
158 | 183 |
} |
159 | 184 |
|
185 |
sub test_and_import_deferred { |
|
186 |
my ($self, %params) = @_; |
|
187 |
|
|
188 |
$self->profile_from_form; |
|
189 |
|
|
190 |
if ($::form->{file}) { |
|
191 |
my $file = SL::SessionFile->new($self->csv_file_name, mode => '>'); |
|
192 |
$file->fh->print($::form->{file}); |
|
193 |
$file->fh->close; |
|
194 |
} |
|
195 |
|
|
196 |
$self->{background_job} = SL::BackgroundJob::CsvImport->create_job( |
|
197 |
file => $self->csv_file_name, |
|
198 |
profile => $self->profile, |
|
199 |
type => $self->profile->type, |
|
200 |
)->save; |
|
201 |
|
|
202 |
SL::System::TaskServer->start_if_not_running; |
|
203 |
SL::System::TaskServer->wake_up; |
|
204 |
|
|
205 |
flash('info', $::locale->text('Your import is beig processed.')); |
|
206 |
|
|
207 |
$self->{deferred} = 1; |
|
208 |
|
|
209 |
$self->render_inputs; |
|
210 |
} |
|
211 |
|
|
160 | 212 |
sub test_and_import { |
161 | 213 |
my ($self, %params) = @_; |
162 | 214 |
|
... | ... | |
189 | 241 |
$self->import_status($params{test} ? 'tested' : 'imported'); |
190 | 242 |
|
191 | 243 |
flash('info', $::locale->text('Objects have been imported.')) if !$params{test}; |
192 |
|
|
193 |
$self->action_new; |
|
194 | 244 |
} |
195 | 245 |
|
196 | 246 |
sub load_default_profile { |
SL/DB/CsvImportProfile.pm | ||
---|---|---|
105 | 105 |
return $clone; |
106 | 106 |
} |
107 | 107 |
|
108 |
sub flatten { |
|
109 |
my ($self) = @_; |
|
110 |
|
|
111 |
return map { |
|
112 |
$_->key => $_->value |
|
113 |
} $self->settings; |
|
114 |
} |
|
115 |
|
|
108 | 116 |
# |
109 | 117 |
# hooks |
110 | 118 |
# |
SL/System/TaskServer.pm | ||
---|---|---|
43 | 43 |
return $self->_run_script_command('start'); |
44 | 44 |
} |
45 | 45 |
|
46 |
sub start_if_not_running { |
|
47 |
my ($self) = @_; |
|
48 |
|
|
49 |
$self->start unless $self->is_running; |
|
50 |
} |
|
51 |
|
|
46 | 52 |
sub stop { |
47 | 53 |
my ($self) = @_; |
48 | 54 |
|
templates/webpages/csv_import/_deferred_results.html | ||
---|---|---|
1 |
<div id='progressbar'></div> |
|
2 |
[% IF progress < 100 %] |
|
3 |
<script type='text/javascript'> |
|
4 |
function reload_results () { |
|
5 |
$.ajax({ |
|
6 |
url: 'controller.pl', |
|
7 |
data: { |
|
8 |
action: 'CsvImport/result', |
|
9 |
'profile.type': '[% SELF.profile.type %]', |
|
10 |
job: '[% SELF.background_job.id %]' |
|
11 |
}, |
|
12 |
success: function(data) { $('#results').html(data) }, |
|
13 |
error: function(e) { alert(e) }, |
|
14 |
}); |
|
15 |
} |
|
16 |
$(document).ready(function(){ |
|
17 |
$('#progressbar').progressbar({ value: [% progress * 1 %] }); |
|
18 |
window.setTimeout(reload_results, 2000); |
|
19 |
}) |
|
20 |
</script> |
|
21 |
[% END %] |
templates/webpages/csv_import/_results.html | ||
---|---|---|
1 |
[%- IF SELF.errors %] |
|
2 |
[%- PROCESS 'csv_import/_errors.html' %] |
|
3 |
[%- END %] |
|
4 |
|
|
5 |
[%- PROCESS 'csv_import/_result.html' %] |
|
6 |
[%- PROCESS 'csv_import/_preview.html' %] |
|
7 |
|
|
8 |
[% IF progress == 100 %] |
|
9 |
<script type='text/javascript'> |
|
10 |
$(function(){ $('#action_import').show() }) |
|
11 |
</script> |
|
12 |
[% END %] |
templates/webpages/csv_import/form.html | ||
---|---|---|
228 | 228 |
|
229 | 229 |
</form> |
230 | 230 |
|
231 |
[%- IF SELF.import_status %] |
|
232 |
[%- IF SELF.errors %] |
|
233 |
[%- PROCESS 'csv_import/_errors.html' %] |
|
234 |
[%- END %] |
|
235 |
|
|
236 |
[%- PROCESS 'csv_import/_result.html' %] |
|
237 |
[%- PROCESS 'csv_import/_preview.html' %] |
|
231 |
<div id='results'> |
|
232 |
[%- IF SELF.deferred %] |
|
233 |
[%- PROCESS 'csv_import/_deferred_results.html' %] |
|
234 |
[%- ELSIF SELF.import_status %] |
|
235 |
[%- PROCESS 'csv_import/_results.html' %] |
|
238 | 236 |
[%- END %] |
237 |
</div> |
|
238 |
|
|
239 | 239 |
|
240 | 240 |
<script type="text/javascript"> |
241 | 241 |
<!-- |
Auch abrufbar als: Unified diff
Asynchroner Import