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