31c5eba587b66fc585dcfb7a29d74a32b4c67c33
[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 ($cur_class->meta->relationship($step)) { #a
91         my $next_class = $cur_class->meta->relationship($step)->class;
92         push @{ $spec->{steps} }, [ $step, $next_class, $index ];
93         $cur_class = $next_class;
94       } else { # simple dispatch
95         push @{ $spec->{steps} }, [ $step ];
96         last;
97       }
98     } else {
99       $self->unknown_column($col, $path);
100     }
101   }
102
103   return $spec;
104 }
105
106 sub unknown_column {
107   my ($self, $col, $path) = @_;
108   return if $self->_csv->ignore_unknown_columns;
109
110   $self->_push_error([
111     $col,
112     undef,
113     "header field '$col' is not recognized",
114     undef,
115     0,
116   ]);
117 }
118
119 sub _csv {
120   $_[0]->{_csv};
121 }
122
123 sub errors {
124   @{ $_[0]->_errors }
125 }
126
127 sub _push_error {
128   my ($self, @errors) = @_;
129   my @new_errors = ($self->errors, @errors);
130   $self->_errors(\@new_errors);
131 }
132
133 1;