1 package SL::Helper::Csv::Dispatcher;
7 use Scalar::Util qw(weaken);
8 use Rose::Object::MakeMethods::Generic scalar => [ qw(
12 use SL::Helper::Csv::Error;
15 my ($class, $parent) = @_;
16 my $self = bless { }, $class;
18 weaken($self->{_csv} = $parent);
25 my ($self, $line) = @_;
27 my $class = $self->_class_by_line($line);
28 croak 'no class given' unless $class;
30 eval "require " . $class;
31 my $obj = $class->new;
33 my $specs = $self->_specs_by_line($line);
34 for my $spec (@{ $specs }) {
35 $self->apply($obj, $spec, $line->{$spec->{key}});
41 # return class for given line
42 # if only one profile is given, return this profiles class
43 # if more than one profile is given, identify class by first
46 my ($self, $line) = @_;
49 if ($self->_csv->is_multiplexed) {
50 foreach my $p (@{ $self->_csv->profile }) {
51 my $row_ident = $p->{row_ident};
52 if ($line->{datatype} eq $row_ident) {
58 $class = $self->_csv->profile->[0]->{class};
65 my ($self, $line) = @_;
69 if ($self->_csv->is_multiplexed) {
70 foreach my $p (@{ $self->_csv->profile }) {
71 my $row_ident = $p->{row_ident};
72 if ($line->{datatype} eq $row_ident) {
73 $spec = $self->_specs->[$i];
79 $spec = $self->_specs->[0];
87 my ($self, $obj, $spec, $value) = @_;
90 for my $step (@{ $spec->{steps} }) {
91 my ($acc, $class, $index) = @$step;
96 if (! $obj->$acc || !$obj->$acc->[$index]) {
97 my @objects = $obj->$acc;
98 $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
100 $obj = $obj->$acc->[$index];
103 $obj->$acc($class->new);
115 my ($self, $col) = @_;
116 return grep { $col eq $_->{key} } $self->_specs;
120 my ($self, %params) = @_;
128 foreach my $h (@{ $self->_csv->header }) {
130 if ($self->_csv->profile) {
131 $profile = $self->_csv->profile->[$i]->{profile};
132 $class = $self->_csv->profile->[$i]->{class};
135 my $spec = $self->_parse_profile(profile => $profile,
142 $self->_specs(\@specs);
144 return ! $self->errors;
148 my ($self, %params) = @_;
150 my $profile = $params{profile};
151 my $class = $params{class};
152 my $header = $params{header};
156 for my $col (@$header) {
158 if ($self->_csv->strict_profile) {
159 if (exists $profile->{$col}) {
160 push @specs, $self->make_spec($col, $profile->{$col}, $class);
162 $self->unknown_column($col, undef);
165 if (exists $profile->{$col}) {
166 push @specs, $self->make_spec($col, $profile->{$col}, $class);
168 push @specs, $self->make_spec($col, $col, $class);
173 $self->_csv->_push_error($self->errors);
179 my ($self, $col, $path, $cur_class) = @_;
181 my $spec = { key => $col, steps => [] };
185 return unless $cur_class;
187 for my $step_index ( split /\.(?!\d)/, $path ) {
188 my ($step, $index) = split /\./, $step_index;
189 if ($cur_class->can($step)) {
190 if (my $rel = $cur_class->meta->relationship($step)) { #a
191 if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
195 "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
201 my $next_class = $cur_class->meta->relationship($step)->class;
202 push @{ $spec->{steps} }, [ $step, $next_class, $index ];
203 $cur_class = $next_class;
204 eval "require $cur_class; 1" or die "could not load class '$cur_class'";
206 } else { # simple dispatch
207 push @{ $spec->{steps} }, [ $step ];
211 $self->unknown_column($col, $path);
219 my ($self, $col, $path) = @_;
220 return if $self->_csv->ignore_unknown_columns;
225 "header field '$col' is not recognized",
240 my ($self, @errors) = @_;
241 my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
242 $self->_errors(\@new_errors);