Csv Errors sind nun Objekte mit entsprechendem Zugriff.
[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 Rose::Object::MakeMethods::Generic scalar => [ qw(
9   _specs _errors
10 ) ];
11
12 use SL::Helper::Csv::Error;
13
14 sub new {
15   my ($class, $parent) = @_;
16   my $self = bless { }, $class;
17
18   weaken($self->{_csv} = $parent);
19   $self->_errors([]);
20
21   return $self;
22 }
23
24 sub dispatch {
25   my ($self, $obj, $line) = @_;
26
27   for my $spec (@{ $self->_specs }) {
28     $self->apply($obj, $spec, $line->{$spec->{key}});
29   }
30 }
31
32 sub apply {
33   my ($self, $obj, $spec, $value) = @_;
34   return unless $value;
35
36   for my $step (@{ $spec->{steps} }) {
37     my ($acc, $class, $index) = @$step;
38     if ($class) {
39
40       # autovifify
41       if (defined $index) {
42         if (! $obj->$acc || !$obj->$acc->[$index]) {
43           my @objects = $obj->$acc;
44           $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
45         }
46         $obj = $obj->$acc->[$index];
47       } else {
48         if (! $obj->$acc) {
49           $obj->$acc($class->new);
50         }
51         $obj = $obj->$acc;
52       }
53
54     } else {
55       $obj->$acc($value);
56     }
57   }
58 }
59
60 sub is_known {
61   my ($self, $col) = @_;
62   return grep { $col eq $_->{key} } $self->_specs;
63 }
64
65 sub parse_profile {
66   my ($self, %params) = @_;
67
68   my $header  = $self->_csv->header;
69   my $profile = $self->_csv->profile;
70   my @specs;
71
72   for my $col (@$header) {
73     next unless $col;
74     push @specs, $self->make_spec($col, $profile->{$col} || $col);
75   }
76
77   $self->_specs(\@specs);
78   $self->_csv->_push_error($self->errors);
79   return ! $self->errors;
80 }
81
82 sub make_spec {
83   my ($self, $col, $path) = @_;
84
85   my $spec = { key => $col, steps => [] };
86   my $cur_class = $self->_csv->class;
87
88   for my $step_index ( split /\.(?!\d)/, $path ) {
89     my ($step, $index) = split /\./, $step_index;
90     if ($cur_class->can($step)) {
91       if (my $rel = $cur_class->meta->relationship($step)) { #a
92         if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
93           $self->_push_error([
94             $path,
95             undef,
96             "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
97             undef,
98             0,
99           ]);
100           return;
101         } else {
102           my $next_class = $cur_class->meta->relationship($step)->class;
103           push @{ $spec->{steps} }, [ $step, $next_class, $index ];
104           $cur_class = $next_class;
105           eval "require $cur_class; 1" or die "could not load class '$cur_class'";
106         }
107       } else { # simple dispatch
108         push @{ $spec->{steps} }, [ $step ];
109         last;
110       }
111     } else {
112       $self->unknown_column($col, $path);
113     }
114   }
115
116   return $spec;
117 }
118
119 sub unknown_column {
120   my ($self, $col, $path) = @_;
121   return if $self->_csv->ignore_unknown_columns;
122
123   $self->_push_error([
124     $col,
125     undef,
126     "header field '$col' is not recognized",
127     undef,
128     0,
129   ]);
130 }
131
132 sub _csv {
133   $_[0]->{_csv};
134 }
135
136 sub errors {
137   @{ $_[0]->_errors }
138 }
139
140 sub _push_error {
141   my ($self, @errors) = @_;
142   my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
143   $self->_errors(\@new_errors);
144 }
145
146 1;