Warnungen gefixt.
[kivitendo-erp.git] / SL / Helper / Csv / Dispatcher.pm
1 package SL::Helper::Csv::Dispatcher;
2
3 use strict;
4
5 use Data::Dumper;
6 use Carp;
7 use Scalar::Util qw(weaken);
8 use List::MoreUtils qw(all pairwise);
9 use Rose::Object::MakeMethods::Generic scalar => [ qw(
10   _specs _row_class _row_spec _errors
11 ) ];
12
13 use SL::Helper::Csv::Error;
14
15 sub new {
16   my ($class, $parent) = @_;
17   my $self = bless { }, $class;
18
19   weaken($self->{_csv} = $parent);
20   $self->_errors([]);
21
22   return $self;
23 }
24
25 sub dispatch {
26   my ($self, $line) = @_;
27
28   my $class = $self->_class_by_line($line);
29   croak 'no class given' unless $class;
30
31   eval "require " . $class;
32   my $obj = $class->new;
33
34   my $specs = $self->_specs_by_line($line);
35   for my $spec (@{ $specs }) {
36     $self->apply($obj, $spec, $line->{$spec->{key}});
37   }
38
39   return $obj;
40 }
41
42 sub _class_by_line {
43   my ($self, $line) = @_;
44
45   # initialize lookup hash if not already done
46   if ($self->_csv->is_multiplexed && ! defined $self->_row_class ) {
47     $self->_row_class({ map { $_->{row_ident} => $_->{class} } @{ $self->_csv->profile } });
48   }
49
50   if ($self->_csv->is_multiplexed) {
51     return $self->_row_class->{$line->{datatype}};
52   } else {
53     return $self->_csv->profile->[0]->{class};
54   }
55 }
56
57 sub _specs_by_line {
58   my ($self, $line) = @_;
59
60   # initialize lookup hash if not already done
61   if ($self->_csv->is_multiplexed && ! defined $self->_row_spec ) {
62     $self->_row_spec({ pairwise { no warnings 'once'; $a->{row_ident} => $b } @{ $self->_csv->profile }, @{ $self->_specs } });
63   }
64
65   if ($self->_csv->is_multiplexed) {
66     return $self->_row_spec->{$line->{datatype}};
67   } else {
68     return $self->_specs->[0];
69   }
70 }
71
72
73 sub apply {
74   my ($self, $obj, $spec, $value) = @_;
75   return unless $value;
76
77   for my $step (@{ $spec->{steps} }) {
78     my ($acc, $class, $index) = @$step;
79     if ($class) {
80
81       # autovifify
82       if (defined $index) {
83         if (! $obj->$acc || !$obj->$acc->[$index]) {
84           my @objects = $obj->$acc;
85           $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
86         }
87         $obj = $obj->$acc->[$index];
88       } else {
89         if (! $obj->$acc) {
90           $obj->$acc($class->new);
91         }
92         $obj = $obj->$acc;
93       }
94
95     } else {
96       $obj->$acc($value);
97     }
98   }
99 }
100
101 sub is_known {
102   my ($self, $col) = @_;
103   return grep { $col eq $_->{key} } $self->_specs;
104 }
105
106 sub parse_profile {
107   my ($self, %params) = @_;
108
109   my $profile;
110   my $class;
111   my $header;
112   my @specs;
113
114   my $i = 0;
115   foreach my $h (@{ $self->_csv->header }) {
116     $header = $h;
117     if ($self->_csv->profile) {
118       $profile = $self->_csv->profile->[$i]->{profile};
119       $class   = $self->_csv->profile->[$i]->{class};
120     }
121
122     my $spec = $self->_parse_profile(profile => $profile,
123                                      class   => $class,
124                                      header  => $header);
125     push @specs, $spec;
126     $i++;
127   }
128
129   $self->_specs(\@specs);
130
131   return ! $self->errors;
132 }
133
134 sub _parse_profile {
135   my ($self, %params) = @_;
136
137   my $profile = $params{profile};
138   my $class   = $params{class};
139   my $header  = $params{header};
140
141   my @specs;
142
143   for my $col (@$header) {
144     next unless $col;
145     if ($self->_csv->strict_profile) {
146       if (exists $profile->{$col}) {
147         push @specs, $self->make_spec($col, $profile->{$col}, $class);
148       } else {
149         $self->unknown_column($col, undef);
150       }
151     } else {
152       if (exists $profile->{$col}) {
153         push @specs, $self->make_spec($col, $profile->{$col}, $class);
154       } else {
155         push @specs, $self->make_spec($col, $col, $class);
156       }
157     }
158   }
159
160   $self->_csv->_push_error($self->errors);
161
162   return \@specs;
163 }
164
165 sub make_spec {
166   my ($self, $col, $path, $cur_class) = @_;
167
168   my $spec = { key => $col, steps => [] };
169
170   return unless $path;
171
172   return unless $cur_class;
173
174   for my $step_index ( split /\.(?!\d)/, $path ) {
175     my ($step, $index) = split /\./, $step_index;
176     if ($cur_class->can($step)) {
177       if (my $rel = $cur_class->meta->relationship($step)) { #a
178         if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
179           $self->_push_error([
180             $path,
181             undef,
182             "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
183             undef,
184             0,
185           ]);
186           return;
187         } else {
188           my $next_class = $cur_class->meta->relationship($step)->class;
189           push @{ $spec->{steps} }, [ $step, $next_class, $index ];
190           $cur_class = $next_class;
191           eval "require $cur_class; 1" or die "could not load class '$cur_class'";
192         }
193       } else { # simple dispatch
194         push @{ $spec->{steps} }, [ $step ];
195         last;
196       }
197     } else {
198       $self->unknown_column($col, $path);
199     }
200   }
201
202   return $spec;
203 }
204
205 sub unknown_column {
206   my ($self, $col, $path) = @_;
207   return if $self->_csv->ignore_unknown_columns;
208
209   $self->_push_error([
210     $col,
211     undef,
212     "header field '$col' is not recognized",
213     undef,
214     0,
215   ]);
216 }
217
218 sub _csv {
219   $_[0]->{_csv};
220 }
221
222 sub errors {
223   @{ $_[0]->_errors }
224 }
225
226 sub _push_error {
227   my ($self, @errors) = @_;
228   my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
229   $self->_errors(\@new_errors);
230 }
231
232 1;