Csv/Dispatcher: dynamisches $class require schon im Specbuilder.
[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       if (defined $index) {
40         if (! $obj->$acc || !$obj->$acc->[$index]) {
41           my @objects = $obj->$acc;
42           $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
43         }
44         $obj = $obj->$acc->[$index];
45       } else {
46         if (! $obj->$acc) {
47           $obj->$acc($class->new);
48         }
49         $obj = $obj->$acc;
50       }
51
52     } else {
53       $obj->$acc($value);
54     }
55   }
56 }
57
58 sub is_known {
59   my ($self, $col) = @_;
60   return grep { $col eq $_->{key} } $self->_specs;
61 }
62
63 sub parse_profile {
64   my ($self, %params) = @_;
65
66   my $header  = $self->_csv->header;
67   my $profile = $self->_csv->profile;
68   my @specs;
69
70   for my $col (@$header) {
71     next unless $col;
72     push @specs, $self->make_spec($col, $profile->{$col} || $col);
73   }
74
75   $self->_specs(\@specs);
76   $self->_csv->_push_error($self->errors);
77   return ! $self->errors;
78 }
79
80 sub make_spec {
81   my ($self, $col, $path) = @_;
82
83   my $spec = { key => $col, steps => [] };
84   my $cur_class = $self->_csv->class;
85
86   for my $step_index ( split /\.(?!\d)/, $path ) {
87     my ($step, $index) = split /\./, $step_index;
88     if ($cur_class->can($step)) {
89       if (my $rel = $cur_class->meta->relationship($step)) { #a
90         if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
91           $self->_push_error([
92             $path,
93             undef,
94             "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
95             undef,
96             0,
97           ]);
98           return;
99         } else {
100           my $next_class = $cur_class->meta->relationship($step)->class;
101           push @{ $spec->{steps} }, [ $step, $next_class, $index ];
102           $cur_class = $next_class;
103           eval "require $cur_class; 1" or die "could not load class '$cur_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;