+sub add_cvar_raw_data_columns {
+ my ($self) = @_;
+
+ foreach my $data (@{ $self->controller->data }) {
+ my $ri = $data->{raw_data}->{datatype};
+ map { $self->add_raw_data_columns($ri, $_) if exists $data->{raw_data}->{$_} } @{ $self->cvar_columns_by->{row_ident}->{$ri} };
+ }
+}
+
+sub init_cvar_configs_by {
+ # Must be overridden by derived specialized importer classes.
+ return {};
+}
+
+sub init_cvar_columns_by {
+ my ($self) = @_;
+
+ my $ccb;
+ foreach my $p (@{ $self->profile }) {
+ my $ri = $p->{row_ident};
+ $ccb->{row_ident}->{$ri} = [ map { "cvar_" . $_->name } (@{ $self->cvar_configs_by->{row_ident}->{$ri} }) ];
+ }
+
+ return $ccb;
+}
+
+sub handle_cvars {
+ my ($self, $entry, %params) = @_;
+
+ return if @{ $entry->{errors} };
+ return unless $entry->{object}->can('cvars_by_config');
+
+ my %type_to_column = ( text => 'text_value',
+ textfield => 'text_value',
+ select => 'text_value',
+ date => 'timestamp_value_as_date',
+ timestamp => 'timestamp_value_as_date',
+ number => 'number_value_as_number',
+ bool => 'bool_value' );
+
+ $params{sub_module} ||= '';
+
+ # autovivify all cvars (cvars_by_config will do that for us)
+ my @cvars;
+ @cvars = @{ $entry->{object}->cvars_by_config };
+
+ foreach my $config (@{ $self->cvar_configs_by->{row_ident}->{$entry->{raw_data}->{datatype}} }) {
+ next unless exists $entry->{raw_data}->{ "cvar_" . $config->name };
+ my $value = $entry->{raw_data}->{ "cvar_" . $config->name };
+ my $column = $type_to_column{ $config->type } || die "Program logic error: unknown custom variable storage type";
+
+ my $cvar = SL::DB::CustomVariable->new(config_id => $config->id, $column => $value, sub_module => $params{sub_module});
+
+ # replace autovivified cvar by new one
+ my $idx = firstidx { $_->config_id == $config->id } @cvars;
+ $cvars[$idx] = $cvar if -1 != $idx;
+ }
+
+ $entry->{object}->custom_variables(\@cvars) if @cvars;
+}
+