1 package SL::DB::CsvImportProfile;
5 use List::Util qw(first);
7 require SL::DB::MetaSetup::CsvImportProfile;
8 use Rose::DB::Object::Helpers qw(clone_and_reset);
10 __PACKAGE__->meta->add_relationship(
12 type => 'one to many',
13 class => 'SL::DB::CsvImportProfileSetting',
14 column_map => { id => 'csv_import_profile_id' },
18 __PACKAGE__->meta->initialize;
20 __PACKAGE__->meta->make_manager_class;
22 __PACKAGE__->before_save('_before_save_unset_default_on_others');
28 sub new_with_default {
29 my ($class, $type) = @_;
31 return $class->new(type => $type)->set_defaults;
37 $self->_set_defaults(sep_char => ',',
41 numberformat => $::myconfig{numberformat},
42 duplicates => 'no_check',
49 my ($self, %params) = @_;
51 while (my ($key, $value) = each %params) {
52 my $setting = $self->_get_setting($key);
55 $setting = SL::DB::CsvImportProfileSetting->new(key => $key);
56 $self->settings(@{ $self->settings || [] }, $setting);
59 $setting->value($value);
66 my ($self, $key, $default) = @_;
68 my $setting = $self->_get_setting($key);
69 return $setting ? $setting->value : $default;
73 my ($self, %params) = @_;
75 while (my ($key, $value) = each %params) {
76 $self->settings(@{ $self->settings || [] }, { key => $key, value => $value }) if !$self->_get_setting($key);
82 sub clone_and_reset_deep {
85 my $clone = $self->clone_and_reset;
86 $clone->settings(map { $_->clone_and_reset } $self->settings);
104 sub _before_save_unset_default_on_others {
107 if ($self->is_default) {
108 SL::DB::Manager::CsvImportProfile->update_all(set => { is_default => 0 },
109 where => [ type => $self->type,
110 '!id' => $self->id ]);
121 my ($self, $key) = @_;
122 return first { $_->key eq $key } @{ $self->settings || [] };