1 package SL::Controller::CsvImport::BaseMulti;
5 use List::MoreUtils qw(pairwise);
9 use parent qw(SL::Controller::CsvImport::Base);
11 use Rose::Object::MakeMethods::Generic
13 'scalar --get_set_init' => [ qw(cvar_configs_by cvar_columns_by) ],
17 my ($self, %params) = @_;
19 $self->test_run($params{test});
21 $self->controller->track_progress(phase => 'parsing csv', progress => 0);
23 my $profile = $self->profile;
25 $self->csv(SL::Helper::Csv->new(file => $self->file->file_name,
26 encoding => $self->controller->profile->get('charset'),
28 ignore_unknown_columns => 1,
30 case_insensitive_header => 1,
31 map { ( $_ => $self->controller->profile->get($_) ) } qw(sep_char escape_char quote_char),
34 $self->controller->track_progress(progress => 10);
36 my $old_numberformat = $::myconfig{numberformat};
37 $::myconfig{numberformat} = $self->controller->profile->get('numberformat');
41 $self->controller->track_progress(progress => 50);
43 $self->controller->errors([ $self->csv->errors ]) if $self->csv->errors;
45 return if ( !$self->csv->header || $self->csv->errors );
49 foreach my $header (@{ $self->csv->header }) {
51 my $profile = $self->csv->profile->[$i]->{profile};
52 my $row_ident = $self->csv->profile->[$i]->{row_ident};
54 my $h = { headers => [ grep { $profile->{$_} } @{ $header } ] };
55 $h->{methods} = [ map { $profile->{$_} } @{ $h->{headers} } ];
56 $h->{used} = { map { ($_ => 1) } @{ $h->{headers} } };
58 $headers->{$row_ident} = $h;
62 $self->controller->headers($headers);
66 foreach my $p (@{ $self->csv->profile }) {
67 $raw_data_headers->{ $p->{row_ident} } = { used => { }, headers => [ ] };
68 $info_headers->{ $p->{row_ident} } = { used => { }, headers => [ ] };
70 $self->controller->raw_data_headers($raw_data_headers);
71 $self->controller->info_headers($info_headers);
73 my $objects = $self->csv->get_objects;
75 $self->controller->track_progress(progress => 70);
77 my @raw_data = @{ $self->csv->get_data };
79 $self->controller->track_progress(progress => 80);
81 $self->controller->data([ pairwise { { object => $a, raw_data => $b, errors => [], information => [], info_data => {} } } @$objects, @raw_data ]);
83 $self->controller->track_progress(progress => 90);
86 if ( $self->controller->profile->get('duplicates', 'no_check') ne 'no_check' ) {
87 $self->check_std_duplicates();
88 $self->check_duplicates();
90 $self->fix_field_lengths;
92 $self->controller->track_progress(progress => 100);
94 $::myconfig{numberformat} = $old_numberformat;
98 my ($self, $row_ident, @columns) = @_;
100 my $h = $self->controller->headers->{$row_ident};
102 foreach my $column (grep { !$h->{used}->{$_} } @columns) {
103 $h->{used}->{$column} = 1;
104 push @{ $h->{methods} }, $column;
105 push @{ $h->{headers} }, $column;
109 sub add_info_columns {
110 my ($self, $row_ident, @columns) = @_;
112 my $h = $self->controller->info_headers->{$row_ident};
114 foreach my $column (grep { !$h->{used}->{ $_->{method} } } map { ref $_ eq 'HASH' ? $_ : { method => $_, header => $_ } } @columns) {
115 $h->{used}->{ $column->{method} } = 1;
116 push @{ $h->{methods} }, $column->{method};
117 push @{ $h->{headers} }, $column->{header};
121 sub add_raw_data_columns {
122 my ($self, $row_ident, @columns) = @_;
124 my $h = $self->controller->raw_data_headers->{$row_ident};
126 foreach my $column (grep { !$h->{used}->{$_} } @columns) {
127 $h->{used}->{$column} = 1;
128 push @{ $h->{headers} }, $column;
132 sub add_cvar_raw_data_columns {
135 foreach my $data (@{ $self->controller->data }) {
136 my $ri = $data->{raw_data}->{datatype};
137 map { $self->add_raw_data_columns($ri, $_) if exists $data->{raw_data}->{$_} } @{ $self->cvar_columns_by->{row_ident}->{$ri} };
141 sub init_cvar_configs_by {
142 # Must be overridden by derived specialized importer classes.
146 sub init_cvar_columns_by {
150 foreach my $p (@{ $self->profile }) {
151 my $ri = $p->{row_ident};
152 $ccb->{row_ident}->{$ri} = [ map { "cvar_" . $_->name } (@{ $self->cvar_configs_by->{row_ident}->{$ri} }) ];
159 my ($self, $entry, %params) = @_;
161 my %type_to_column = ( text => 'text_value',
162 textfield => 'text_value',
163 select => 'text_value',
164 date => 'timestamp_value_as_date',
165 timestamp => 'timestamp_value_as_date',
166 number => 'number_value_as_number',
167 bool => 'bool_value' );
169 $params{sub_module} ||= '';
171 foreach my $config (@{ $self->cvar_configs_by->{row_ident}->{$entry->{raw_data}->{datatype}} }) {
172 next unless exists $entry->{raw_data}->{ "cvar_" . $config->name };
173 my $value = $entry->{raw_data}->{ "cvar_" . $config->name };
174 my $column = $type_to_column{ $config->type } || die "Program logic error: unknown custom variable storage type";
176 push @cvars, SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => $params{sub_module});
179 $entry->{object}->custom_variables(\@cvars) if @cvars;
186 foreach my $class (@{ $self->class }) {
187 eval "require " . $class;
189 my %unwanted = map { ( $_ => 1 ) } (qw(itime mtime), map { $_->name } @{ $class->meta->primary_key_columns });
191 $prof{datatype} = '';
192 for my $col ($class->meta->columns) {
193 next if $unwanted{$col};
195 my $name = $col->isa('Rose::DB::Object::Metadata::Column::Numeric') ? "$col\_as_number"
196 : $col->isa('Rose::DB::Object::Metadata::Column::Date') ? "$col\_as_date"
197 : $col->isa('Rose::DB::Object::Metadata::Column::Timestamp') ? "$col\_as_date"
203 $prof{ 'cvar_' . $_->name } = '' for @{ $self->cvar_configs_by->{class}->{$class} };
205 $class =~ m/^SL::DB::(.+)/;
206 push @profile, {'profile' => \%prof, 'class' => $class, 'row_ident' => $::locale->text($1)};
212 sub add_displayable_columns {
213 my ($self, $row_ident, @columns) = @_;
215 my $dis_cols = $self->controller->displayable_columns || {};
217 my @cols = @{ $dis_cols->{$row_ident} || [] };
218 my %ex_col_map = map { $_->{name} => $_ } @cols;
220 foreach my $column (@columns) {
221 if ($ex_col_map{ $column->{name} }) {
222 @{ $ex_col_map{ $column->{name} } }{ keys %{ $column } } = @{ $column }{ keys %{ $column } };
228 my $by_name_datatype_first = sub { 'datatype' eq $a->{name} ? -1 :
229 'datatype' eq $b->{name} ? 1 :
230 $a->{name} cmp $b->{name} };
231 $dis_cols->{$row_ident} = [ sort $by_name_datatype_first @cols ];
233 $self->controller->displayable_columns($dis_cols);
236 sub setup_displayable_columns {
239 foreach my $p (@{ $self->profile }) {
240 $self->add_displayable_columns($p->{row_ident}, map { { name => $_ } } keys %{ $p->{profile} });
244 sub add_cvar_columns_to_displayable_columns {
245 my ($self, $row_ident) = @_;
247 $self->add_displayable_columns($row_ident,
248 map { { name => 'cvar_' . $_->name,
249 description => $::locale->text('#1 (custom variable)', $_->description) } }
250 @{ $self->cvar_configs_by->{row_ident}->{$row_ident} });
256 my %field_lengths_by_ri = ();
258 foreach my $p (@{ $self->profile }) {
259 my %field_lengths = map { $_->name => $_->length } grep { $_->type eq 'varchar' } @{ $p->{class}->meta->columns };
260 $field_lengths_by_ri{ $p->{row_ident} } = \%field_lengths;
263 return %field_lengths_by_ri;
266 sub fix_field_lengths {
269 my %field_lengths_by_ri = $self->field_lengths;
270 foreach my $entry (@{ $self->controller->data }) {
271 next unless @{ $entry->{errors} };
272 my %field_lengths = %{ $field_lengths_by_ri{ $entry->{raw_data}->{datatype} } };
273 map { $entry->{object}->$_(substr($entry->{object}->$_, 0, $field_lengths{$_})) if $entry->{object}->$_ } keys %field_lengths;
279 sub is_multiplexed { 1 }