1 package SL::DB::RequirementSpecItem;
6 use List::MoreUtils qw(any);
7 use Rose::DB::Object::Helpers;
8 use Rose::DB::Object::Util;
11 use SL::DB::MetaSetup::RequirementSpecItem;
12 use SL::DB::Manager::RequirementSpecItem;
13 use SL::DB::Helper::ActsAsList;
14 use SL::DB::Helper::AttrDuration;
15 use SL::DB::Helper::AttrHTML;
17 use SL::Locale::String;
18 use SL::PrefixedNumber;
20 __PACKAGE__->meta->add_relationship(
22 type => 'one to many',
23 class => 'SL::DB::RequirementSpecItem',
24 column_map => { id => 'parent_id' },
27 map_class => 'SL::DB::RequirementSpecDependency',
28 map_from => 'depending_item',
29 map_to => 'depended_item',
30 type => 'many to many',
33 map_class => 'SL::DB::RequirementSpecDependency',
34 map_from => 'depended_item',
35 map_to => 'depending_item',
36 type => 'many to many',
40 __PACKAGE__->meta->initialize;
42 __PACKAGE__->configure_acts_as_list(group_by => [qw(requirement_spec_id parent_id)]);
43 __PACKAGE__->attr_duration(qw(time_estimation));
44 __PACKAGE__->attr_html('description');
46 __PACKAGE__->before_save(\&_before_save_create_fb_number);
47 __PACKAGE__->before_save(\&_before_save_invalidate_requirement_spec_version);
48 __PACKAGE__->before_delete(\&_before_delete_invalidate_requirement_spec_version);
50 sub _before_save_create_fb_number {
53 return 1 if $self->fb_number;
54 return 0 if !$self->requirement_spec_id;
56 my $method = 'previous_' . ($self->parent_id ? 'fb' : 'section') . '_number';
57 my $next_number = $self->requirement_spec->$method + 1;
59 $self->requirement_spec->update_attributes($method => $next_number) || return 0;
61 $method = ($self->parent_id ? 'function_block' : 'section') . '_number_format';
62 my $format = $self->requirement_spec->type->$method;
64 $self->fb_number(SL::PrefixedNumber->new(number => $format || 0)->set_to($next_number));
69 sub _before_save_invalidate_requirement_spec_version {
70 my ($self, %params) = @_;
72 return 1 if !$self->requirement_spec_id || $self->requirement_spec->working_copy_id;
74 my %changed_columns = map { $_ => 1 } (Rose::DB::Object::Helpers::dirty_columns($self));
75 my $has_changed = !Rose::DB::Object::Util::is_in_db($self);
76 $has_changed ||= any { $changed_columns{$_} } qw(requirement_spec_id parent_id position fb_number title description);
78 if (!$has_changed && $self->id) {
79 my $old_item = SL::DB::RequirementSpecItem->new(id => $self->id)->load;
80 $has_changed = join(':', sort map { $_->id } @{ $self->dependencies }) ne join(':', sort map { $_->id } @{ $old_item->dependencies });
83 $self->requirement_spec->invalidate_version if $has_changed;
88 sub _before_delete_invalidate_requirement_spec_version {
89 my ($self, %params) = @_;
91 $self->requirement_spec->invalidate_version if $self->requirement_spec_id;
100 push @errors, t8('The title is missing.') if !$self->parent_id && !$self->title;
105 sub children_sorted {
106 my ($self, @args) = @_;
108 croak "Not a writer" if @args;
110 return [ sort { $a->position <=> $b->position } $self->children ];
114 my ($self, @args) = @_;
116 croak "Not a writer" if @args;
117 $self = $self->parent while $self->parent_id;
123 my ($self, @args) = @_;
125 croak "Not a writer" if @args;
127 return $self->item_type eq 'section' ? 'function-block' : 'sub-function-block';
130 sub content_excerpt {
133 return Common::truncate($self->description_as_stripped_html // '', at => 200);
146 SL::DB::RequirementSpecItem - Items for requirement specs
150 Please see L<SL::DB::RequirementSpec> for the architectual overview.
158 Returns the C<item_type> for children of C<$self>.
160 =item C<children_sorted>
162 Returns an array reference of direct children (not of grandchildren)
163 for C<$self> ordered by their positional column in ascending order.
167 Returns the section this item belongs to. It can be C<$self> if
168 C<$self> is already a section, its parent or grandparent.
172 Validates before saving and returns an array of human-readable error
173 messages in case of an error.
183 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>