2730502b385d3cdaebc28d5b7aeb73675deb49fb
[kivitendo-erp.git] / SL / Helper / Csv.pm
1 package SL::Helper::Csv;
2
3 use strict;
4 use warnings;
5
6 use version 0.77;
7 use Carp;
8 use IO::File;
9 use Params::Validate qw(:all);
10 use List::MoreUtils qw(all pairwise);
11 use Text::CSV_XS;
12 use Rose::Object::MakeMethods::Generic scalar => [ qw(
13   file encoding sep_char quote_char escape_char header profile
14   numberformat dateformat ignore_unknown_columns strict_profile is_multiplexed
15   _row_header _io _csv _objects _parsed _data _errors all_cvar_configs case_insensitive_header
16 ) ];
17
18 use SL::Helper::Csv::Dispatcher;
19 use SL::Helper::Csv::Error;
20
21 # public interface
22
23 sub new {
24   my $class  = shift;
25   my %params = validate(@_, {
26     sep_char               => { default => ';' },
27     quote_char             => { default => '"' },
28     escape_char            => { default => '"' },
29     header                 => { type    => ARRAYREF, optional => 1 },
30     profile                => { type    => ARRAYREF, optional => 1 },
31     file                   => 1,
32     encoding               => 0,
33     numberformat           => 0,
34     dateformat             => 0,
35     ignore_unknown_columns => 0,
36     strict_profile         => 0,
37     case_insensitive_header => 0,
38   });
39   my $self = bless {}, $class;
40
41   $self->$_($params{$_}) for keys %params;
42
43   $self->_io(IO::File->new);
44   $self->_csv(Text::CSV_XS->new({
45     binary => 1,
46     sep_char    => $self->sep_char,
47     quote_char  => $self->quote_char,
48     escape_char => $self->escape_char,
49
50   }));
51   $self->_errors([]);
52
53   return $self;
54 }
55
56 sub parse {
57   my ($self, %params) = @_;
58
59   $self->_open_file;
60   return if ! $self->_check_multiplexed;
61   return if ! $self->_check_header;
62   return if ! $self->dispatcher->parse_profile;
63   return if ! $self->_parse_data;
64
65   $self->_parsed(1);
66   return $self;
67 }
68
69 sub get_data {
70   $_[0]->_data;
71 }
72
73 sub get_objects {
74   my ($self, %params) = @_;
75   croak 'must parse first' unless $self->_parsed;
76
77   $self->_make_objects unless $self->_objects;
78   return wantarray ? @{ $self->_objects } : $self->_objects;
79 }
80
81 sub errors {
82   @{ $_[0]->_errors }
83 }
84
85 sub check_header {
86   $_[0]->_check_header;
87 }
88
89 # private stuff
90
91 sub _open_file {
92   my ($self, %params) = @_;
93
94   $self->encoding($self->_guess_encoding) if !$self->encoding;
95
96   $self->_io->open($self->file, '<' . $self->_encode_layer)
97     or die "could not open file " . $self->file;
98
99   return $self->_io;
100 }
101
102 # check, if data is multiplexed and if all nessesary infos are given
103 sub _check_multiplexed {
104   my ($self, %params) = @_;
105
106   $self->is_multiplexed(0);
107
108   # If more than one profile is given, it is multiplexed.
109   if ($self->profile) {
110     my @profile = @{ $self->profile };
111     if (scalar @profile > 1) {
112       # Each profile needs a class and a row_ident
113       my $info_ok = all { defined $_->{class} && defined $_->{row_ident} } @profile;
114       $self->_push_error([
115         0,
116         "missing class or row_ident in one of the profiles for multiplexed data",
117         0,
118         0]) unless $info_ok;
119
120       # If header is given, there need to be a header for each profile
121       # and no empty headers.
122       if ($info_ok && $self->header) {
123         my @header = @{ $self->header };
124         my $t_ok = scalar @profile == scalar @header;
125         $self->_push_error([
126           0,
127           "number of headers and number of profiles must be the same for multiplexed data",
128           0,
129           0]) unless $t_ok;
130         $info_ok = $info_ok && $t_ok;
131
132         $t_ok = all { scalar @$_ > 0} @header;
133         $self->_push_error([
134           0,
135           "no empty headers are allowed for multiplexed data",
136           0,
137           0]) unless $t_ok;
138         $info_ok = $info_ok && $t_ok;
139       }
140       $self->is_multiplexed($info_ok);
141       return $info_ok;
142     }
143   }
144
145   # ok, if not multiplexed
146   return 1;
147 }
148
149 sub _check_header {
150   my ($self, %params) = @_;
151   my $header;
152
153   $header = $self->header;
154   if (!$header) {
155     my $n_header = ($self->is_multiplexed)? scalar @{ $self->profile } : 1;
156     foreach my $p_num (0..$n_header - 1) {
157       my $h = $self->_csv->getline($self->_io);
158
159       $self->_push_error([
160         $self->_csv->error_input,
161         $self->_csv->error_diag,
162         0,
163       ]) unless $h;
164
165       if ($self->is_multiplexed) {
166         push @{ $header }, $h;
167       } else {
168         $header = $h;
169       }
170     }
171   }
172
173   # Special case: utf8 BOM.
174   # certain software (namely MS Office and notepad.exe insist on prefixing
175   # data with a discouraged but valid byte order mark
176   # if not removed, the first header field will not be recognized
177   if ($header) {
178     my $h = ($self->is_multiplexed)? $header->[0] : $header;
179
180     if ($h && $h->[0] && $self->encoding =~ /utf-?8/i) {
181       $h->[0] =~ s/^\x{FEFF}//;
182     }
183   }
184
185   # check, if all header fields are parsed well
186   if ($self->is_multiplexed) {
187     return unless $header && all { $_ } @$header;
188   } else {
189     return unless $header;
190   }
191
192   # Special case: human stupidity
193   # people insist that case sensitivity doesn't exist and try to enter all
194   # sorts of stuff. at this point we've got a profile (with keys that represent
195   # valid methods), and a header full of strings. if two of them match, the user
196   # mopst likely meant that field, so rewrite the header
197   if ($self->case_insensitive_header) {
198     die 'case_insensitive_header is only possible with profile' unless $self->profile;
199     if ($header) {
200       my $h_aref = ($self->is_multiplexed)? $header : [ $header ];
201       my $p_num  = 0;
202       foreach my $h (@{ $h_aref }) {
203         my @names = (
204           keys %{ $self->profile->[$p_num]->{profile} || {} },
205         );
206         for my $name (@names) {
207           for my $i (0..$#$h) {
208             $h->[$i] = $name if lc $h->[$i] eq lc $name;
209           }
210         }
211         $p_num++;
212       }
213     }
214   }
215
216   return $self->header($header);
217 }
218
219 sub _parse_data {
220   my ($self, %params) = @_;
221   my (@data, @errors);
222
223   while (1) {
224     my $row = $self->_csv->getline($self->_io);
225     if ($row) {
226       my $header = $self->_header_by_row($row);
227       my %hr;
228       @hr{@{ $header }} = @$row;
229       push @data, \%hr;
230     } else {
231       last if $self->_csv->eof;
232       # Text::CSV_XS 0.89 added record number to error_diag
233       if (qv(Text::CSV_XS->VERSION) >= qv('0.89')) {
234         push @errors, [
235           $self->_csv->error_input,
236           $self->_csv->error_diag,
237         ];
238       } else {
239         push @errors, [
240           $self->_csv->error_input,
241           $self->_csv->error_diag,
242           $self->_io->input_line_number,
243         ];
244       }
245     }
246     last if $self->_csv->eof;
247   }
248
249   $self->_data(\@data);
250   $self->_push_error(@errors);
251
252   return ! @errors;
253 }
254
255 sub _header_by_row {
256   my ($self, $row) = @_;
257
258   # initialize lookup hash if not already done
259   if ($self->is_multiplexed && ! defined $self->_row_header ) {
260     $self->_row_header({ pairwise { no warnings 'once'; $a->{row_ident} => $b } @{ $self->profile }, @{ $self->header } });
261   }
262
263   if ($self->is_multiplexed) {
264     return $self->_row_header->{$row->[0]}
265   } else {
266     return $self->header;
267   }
268 }
269
270 sub _encode_layer {
271   ':encoding(' . $_[0]->encoding . ')';
272 }
273
274 sub _make_objects {
275   my ($self, %params) = @_;
276   my @objs;
277
278   local $::myconfig{numberformat} = $self->numberformat if $self->numberformat;
279   local $::myconfig{dateformat}   = $self->dateformat   if $self->dateformat;
280
281   for my $line (@{ $self->_data }) {
282     my $tmp_obj = $self->dispatcher->dispatch($line);
283     push @objs, $tmp_obj;
284   }
285
286   $self->_objects(\@objs);
287 }
288
289 sub dispatcher {
290   my ($self, %params) = @_;
291
292   $self->{_dispatcher} ||= $self->_make_dispatcher;
293 }
294
295 sub _make_dispatcher {
296   my ($self, %params) = @_;
297
298   die 'need a header to make a dispatcher' unless $self->header;
299
300   return SL::Helper::Csv::Dispatcher->new($self);
301 }
302
303 sub _guess_encoding {
304   # won't fix
305   'utf-8';
306 }
307
308 sub _push_error {
309   my ($self, @errors) = @_;
310   my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
311   $self->_errors(\@new_errors);
312 }
313
314
315 1;
316
317 __END__
318
319 =encoding utf-8
320
321 =head1 NAME
322
323 SL::Helper::Csv - take care of csv file uploads
324
325 =head1 SYNOPSIS
326
327   use SL::Helper::Csv;
328
329   my $csv = SL::Helper::Csv->new(
330     file        => \$::form->{upload_file},
331     encoding    => 'utf-8', # undef means utf8
332     sep_char    => ',',     # default ';'
333     quote_char  => '\'',    # default '"'
334     escape_char => '"',     # default '"'
335     header      => [ qw(id text sellprice word) ], # see later
336     profile     => [ { profile => { sellprice => 'sellprice_as_number'},
337                        class   => 'SL::DB::Part' } ],
338   );
339
340   my $status  = $csv->parse;
341   my $hrefs   = $csv->get_data;
342   my @objects = $csv->get_objects;
343
344   my @errors  = $csv->errors;
345
346 =head1 DESCRIPTION
347
348 See Synopsis.
349
350 Text::CSV offeres already good functions to get lines out of a csv file, but in
351 most cases you will want those line to be parsed into hashes or even objects,
352 so this model just skips ahead and gives you objects.
353
354 Its basic assumptions are:
355
356 =over 4
357
358 =item You do know what you expect to be in that csv file.
359
360 This means first and foremost you have knowledge about encoding, number and
361 date format, csv parameters such as quoting and separation characters. You also
362 know what content will be in that csv and what L<Rose::DB> is responsible for
363 it. You provide valid header columns and their mapping to the objects.
364
365 =item You do NOT know if the csv provider yields to your expectations.
366
367 Stuff that does not work with what you expect should not crash anything, but
368 give you a hint what went wrong. As a result, if you remember to check for
369 errors after each step, you should be fine.
370
371 =item Data does not make sense. It's just data.
372
373 Almost all data imports have some type of constraints. Some data needs to be
374 unique, other data needs to be connected to existing data sets. This will not
375 happen here. You will receive a plain mapping of the data into the class tree,
376 nothing more.
377
378 =item Multiplex data
379
380 This module can handle multiplexed data of different class types. In that case
381 multiple profiles with classes and row identifiers must be given. Multiple
382 headers may also be given or read from csv data. Data must contain the row
383 identifier in the first column and it's field name must be 'datatype'.
384
385 =back
386
387 =head1 METHODS
388
389 =over 4
390
391 =item C<new> PARAMS
392
393 Standard constructor. You can use this to set most of the data.
394
395 =item C<parse>
396
397 Do the actual work. Will return true ($self actually) if success, undef if not.
398
399 =item C<get_objects>
400
401 Parse the data into objects and return those.
402
403 This method will return list or arrayref depending on context.
404
405 =item C<get_data>
406
407 Returns an arrayref of the raw lines as hashrefs.
408
409 =item C<errors>
410
411 Return all errors that came up during parsing. See error handling for detailed
412 information.
413
414 =back
415
416 =head1 PARAMS
417
418 =over 4
419
420 =item C<file>
421
422 The file which contents are to be read. Can be a name of a physical file or a
423 scalar ref for memory data.
424
425 =item C<encoding>
426
427 Encoding of the CSV file. Note that this module does not do any encoding
428 guessing. Know what your data is. Defaults to utf-8.
429
430 =item C<sep_char>
431
432 =item C<quote_char>
433
434 =item C<escape_char>
435
436 Same as in L<Text::CSV>
437
438 =item C<header> \@HEADERS
439
440 If given, it contains an ARRAY of the header fields for not multiplexed data.
441 Or an ARRAYREF for each different class type for multiplexed data. These
442 ARRAYREFS are the header fields which are an array of columns. In this case
443 the first lines are not used as a header. Empty header fields will be ignored
444 in objects.
445
446 If not given, headers are taken from the first n lines of data, where n is the
447 number of different class types.
448
449 In case of multiplexed data the first column must be named 'datatype'. This
450 name must be given in the header.
451
452 Examples:
453
454   classic data of one type:
455   [ 'name', 'street', 'zipcode', 'city' ]
456
457   multiplexed data with two different types
458   [ [ 'datatype', 'ordernumber', 'customer', 'transdate' ],
459     [ 'datatype', 'partnumber', 'qty', 'sellprice' ] ]
460
461 =item C<profile> [{profile => \%ACCESSORS, class => class, row_ident => ri},]
462
463 This is an ARRAYREF to HASHREFs which may contain the keys C<profile>, C<class>
464 and C<row_ident>.
465
466 The C<profile> is a HASHREF which may be used to map header fields to custom
467 accessors. Example:
468
469   [ {profile => { listprice => listprice_as_number }} ]
470
471 In this case C<listprice_as_number> will be used to read in values from the
472 C<listprice> column.
473
474 In case of a One-To-One relationsship these can also be set over
475 relationsships by sparating the steps with a dot (C<.>). This will work:
476
477   [ {profile => { customer => 'customer.name' }} ]
478
479 And will result in something like this:
480
481   $obj->customer($obj->meta->relationship('customer')->class->new);
482   $obj->customer->name($csv_line->{customer})
483
484 But beware, this will not try to look up anything in the database. You will
485 simply receive objects that represent what the profile defined. If some of
486 these information are unique, and should be connected to preexisting data, you
487 will have to do that for yourself. Since you provided the profile, it is
488 assumed you know what to do in this case.
489
490 If no profile is given, any header field found will be taken as is.
491
492 If the path in a profile entry is empty, the field will be subjected to
493 C<strict_profile> and C<case_insensitive_header> checking, will be parsed into
494 C<get_data>, but will not be attempted to be dispatched into objects.
495
496 If C<class> is present, the line will be handed to the new sub of this class,
497 and the return value used instead of the line itself.
498
499 C<row_ident> is a string to recognize the right profile and class for each data
500 line in multiplexed data. It must match the value in the column 'dataype' for
501 each class.
502
503 In case of multiplexed data, C<class> and C<row_ident> must be given.
504 Example:
505   [ {
506       class     => 'SL::DB::Order',
507       row_ident => 'O'
508     },
509     {
510       class     => 'SL::DB::OrderItem',
511       row_ident => 'I',
512       profile   => {sellprice => sellprice_as_number}
513     } ]
514
515 =item C<ignore_unknown_columns>
516
517 If set, the import will ignore unkown header columns. Useful for lazy imports,
518 but deactivated by default.
519
520 =item C<case_insensitive_header>
521
522 If set, header columns will be matched against profile entries case
523 insensitive, and on match the profile name will be taken.
524
525 Only works if a profile is given, will die otherwise.
526
527 If both C<case_insensitive_header> and C<strict_profile> is set, matched header
528 columns will be accepted.
529
530 =item C<strict_profile>
531
532 If set, all columns to be parsed must be specified in C<profile>. Every header
533 field not listed there will be treated like an unknown column.
534
535 If both C<case_insensitive_header> and C<strict_profile> is set, matched header
536 columns will be accepted.
537
538 =back
539
540 =head1 ERROR HANDLING
541
542 After parsing a file all errors will be accumulated into C<errors>.
543 Each entry is an object with the following attributes:
544
545  raw_input:  offending raw input,
546  code:   Text::CSV error code if Text:CSV signalled an error, 0 else,
547  diag:   error diagnostics,
548  line:   position in line,
549  col:    estimated line in file,
550
551 Note that the last entry can be off, but will give an estimate.
552
553 =head1 CAVEATS
554
555 =over 4
556
557 =item *
558
559 sep_char, quote_char, and escape_char are passed to Text::CSV on creation.
560 Changing them later has no effect currently.
561
562 =item *
563
564 Encoding errors are not dealt with properly.
565
566 =back
567
568 =head1 TODO
569
570 Dispatch to child objects, like this:
571
572  $csv = SL::Helper::Csv->new(
573    file    => ...
574    profile => [ {
575      profile => [
576        makemodel => {
577          make_1  => make,
578          model_1 => model,
579        },
580        makemodel => {
581          make_2  => make,
582          model_2 => model,
583        },
584      ],
585      class   => SL::DB::Part,
586    } ]
587  );
588
589 =head1 AUTHOR
590
591 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>
592
593 =cut