SL::Helper::Csv: neues flag "strict_profile". Wenn gesetzt werden nur Daten aus dem...
[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     if ($self->_csv->strict_profile) {
75       if (exists $profile->{$col}) {
76         push @specs, $self->make_spec($col, $profile->{$col});
77       } else {
78         $self->unknown_column($col, undef);
79       }
80     } else {
81       push @specs, $self->make_spec($col, $profile->{$col} || $col);
82     }
83   }
84
85   $self->_specs(\@specs);
86   $self->_csv->_push_error($self->errors);
87   return ! $self->errors;
88 }
89
90 sub make_spec {
91   my ($self, $col, $path) = @_;
92
93   my $spec = { key => $col, steps => [] };
94   my $cur_class = $self->_csv->class;
95
96   for my $step_index ( split /\.(?!\d)/, $path ) {
97     my ($step, $index) = split /\./, $step_index;
98     if ($cur_class->can($step)) {
99       if (my $rel = $cur_class->meta->relationship($step)) { #a
100         if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
101           $self->_push_error([
102             $path,
103             undef,
104             "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
105             undef,
106             0,
107           ]);
108           return;
109         } else {
110           my $next_class = $cur_class->meta->relationship($step)->class;
111           push @{ $spec->{steps} }, [ $step, $next_class, $index ];
112           $cur_class = $next_class;
113           eval "require $cur_class; 1" or die "could not load class '$cur_class'";
114         }
115       } else { # simple dispatch
116         push @{ $spec->{steps} }, [ $step ];
117         last;
118       }
119     } else {
120       $self->unknown_column($col, $path);
121     }
122   }
123
124   return $spec;
125 }
126
127 sub unknown_column {
128   my ($self, $col, $path) = @_;
129   return if $self->_csv->ignore_unknown_columns;
130
131   $self->_push_error([
132     $col,
133     undef,
134     "header field '$col' is not recognized",
135     undef,
136     0,
137   ]);
138 }
139
140 sub _csv {
141   $_[0]->{_csv};
142 }
143
144 sub errors {
145   @{ $_[0]->_errors }
146 }
147
148 sub _push_error {
149   my ($self, @errors) = @_;
150   my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
151   $self->_errors(\@new_errors);
152 }
153
154 1;