1 package SL::DB::RequirementSpecItem;
6 use List::MoreUtils qw(any);
7 use Rose::DB::Object::Helpers;
8 use Rose::DB::Object::Util;
10 use SL::DB::MetaSetup::RequirementSpecItem;
11 use SL::DB::Manager::RequirementSpecItem;
12 use SL::DB::Helper::ActsAsList;
13 use SL::DB::Helper::AttrDuration;
15 use SL::Locale::String;
16 use SL::PrefixedNumber;
18 __PACKAGE__->meta->add_relationship(
20 type => 'one to many',
21 class => 'SL::DB::RequirementSpecItem',
22 column_map => { id => 'parent_id' },
25 map_class => 'SL::DB::RequirementSpecDependency',
26 map_from => 'depending_item',
27 map_to => 'depended_item',
28 type => 'many to many',
31 map_class => 'SL::DB::RequirementSpecDependency',
32 map_from => 'depended_item',
33 map_to => 'depending_item',
34 type => 'many to many',
38 __PACKAGE__->meta->initialize;
40 __PACKAGE__->configure_acts_as_list(group_by => [qw(requirement_spec_id parent_id)]);
41 __PACKAGE__->attr_duration(qw(time_estimation));
43 __PACKAGE__->before_save(\&_before_save_create_fb_number);
44 __PACKAGE__->before_save(\&_before_save_invalidate_requirement_spec_version);
45 __PACKAGE__->before_delete(\&_before_delete_invalidate_requirement_spec_version);
47 sub _before_save_create_fb_number {
50 return 1 if $self->fb_number;
51 return 0 if !$self->requirement_spec_id;
53 my $method = 'previous_' . ($self->parent_id ? 'fb' : 'section') . '_number';
54 my $next_number = $self->requirement_spec->$method + 1;
56 $self->requirement_spec->update_attributes($method => $next_number) || return 0;
58 $method = 'requirement_spec_' . ($self->parent_id ? 'function_block' : 'section') . '_number_format';
59 my $format = SL::DB::Default->get->$method;
61 $self->fb_number(SL::PrefixedNumber->new(number => $format || 0)->set_to($next_number));
66 sub _before_save_invalidate_requirement_spec_version {
67 my ($self, %params) = @_;
69 return 1 if !$self->requirement_spec_id;
71 my %changed_columns = map { $_ => 1 } (Rose::DB::Object::Helpers::dirty_columns($self));
72 my $has_changed = !Rose::DB::Object::Util::is_in_db($self);
73 $has_changed ||= any { $changed_columns{$_} } qw(requirement_spec_id parent_id position fb_number title description);
75 if (!$has_changed && $self->id) {
76 my $old_item = SL::DB::RequirementSpecItem->new(id => $self->id)->load;
77 $has_changed = join(':', sort map { $_->id } @{ $self->dependencies }) ne join(':', sort map { $_->id } @{ $old_item->dependencies });
80 $self->requirement_spec->invalidate_version if $has_changed;
85 sub _before_delete_invalidate_requirement_spec_version {
86 my ($self, %params) = @_;
88 $self->requirement_spec->invalidate_version if $self->requirement_spec_id;
97 push @errors, t8('The title is missing.') if !$self->parent_id && !$self->title;
102 sub children_sorted {
103 my ($self, @args) = @_;
105 croak "Not a writer" if @args;
107 return [ sort { $a->position <=> $b->position } $self->children ];
111 my ($self, @args) = @_;
113 croak "Not a writer" if @args;
114 $self = $self->parent while $self->parent_id;
120 my ($self, @args) = @_;
122 croak "Not a writer" if @args;
124 return $self->item_type eq 'section' ? 'function-block' : 'sub-function-block';
136 SL::DB::RequirementSpecItem - Items for requirement specs
140 Please see L<SL::DB::RequirementSpec> for the architectual overview.
148 Returns the C<item_type> for children of C<$self>.
150 =item C<children_sorted>
152 Returns an array reference of direct children (not of grandchildren)
153 for C<$self> ordered by their positional column in ascending order.
157 Returns the section this item belongs to. It can be C<$self> if
158 C<$self> is already a section, its parent or grandparent.
162 Validates before saving and returns an array of human-readable error
163 messages in case of an error.
173 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>