1 package SL::DB::RequirementSpec;
6 use List::Util qw(max reduce);
7 use Rose::DB::Object::Helpers;
9 use SL::DB::MetaSetup::RequirementSpec;
10 use SL::DB::Manager::RequirementSpec;
11 use SL::Locale::String;
12 use SL::Util qw(_hashify);
14 __PACKAGE__->meta->add_relationship(
16 type => 'one to many',
17 class => 'SL::DB::RequirementSpecItem',
18 column_map => { id => 'requirement_spec_id' },
21 type => 'one to many',
22 class => 'SL::DB::RequirementSpecTextBlock',
23 column_map => { id => 'requirement_spec_id' },
26 type => 'one to many',
27 class => 'SL::DB::RequirementSpec',
28 column_map => { id => 'working_copy_id' },
32 __PACKAGE__->meta->initialize;
34 __PACKAGE__->before_save('_before_save_initialize_not_null_columns');
40 push @errors, t8('The title is missing.') if !$self->title;
45 sub _before_save_initialize_not_null_columns {
48 $self->previous_section_number(0) if !defined $self->previous_section_number;
49 $self->previous_fb_number(0) if !defined $self->previous_fb_number;
54 sub text_blocks_sorted {
55 my ($self, %params) = _hashify(1, @_);
57 my @text_blocks = @{ $self->text_blocks };
58 @text_blocks = grep { $_->output_position == $params{output_position} } @text_blocks if exists $params{output_position};
59 @text_blocks = sort { $a->position <=> $b->position } @text_blocks;
65 my ($self, @rest) = @_;
67 croak "This sub is not a writer" if @rest;
69 return [ sort { $a->position <=> $b->position } grep { !$_->parent_id } @{ $self->items } ];
72 sub sections { §ions_sorted; }
74 sub displayable_name {
77 return sprintf('%s: "%s"', $self->type->description, $self->title);
80 sub versioned_copies_sorted {
81 my ($self, %params) = _hashify(1, @_);
83 my @copies = @{ $self->versioned_copies };
84 @copies = grep { $_->version->version_number <= $params{max_version_number} } @copies if $params{max_version_number};
85 @copies = sort { $a->version->version_number <=> $b->version->version_number } @copies;
91 my ($self, %params) = @_;
93 return $self->_create_copy(%params) if $self->db->in_transaction;
96 if (!$self->db->do_transaction(sub { $copy = $self->_create_copy(%params) })) {
97 $::lxdebug->message(LXDebug->WARN(), "create_copy failed: " . join("\n", (split(/\n/, $self->db->error))[0..2]));
105 my ($self, %params) = @_;
107 my $copy = Rose::DB::Object::Helpers::clone_and_reset($self);
108 $copy->copy_from($self, %params);
114 my ($self, $source, %attributes) = @_;
116 croak "Missing parameter 'source'" unless $source;
119 $self->assign_attributes(map({ ($_ => $source->$_) } qw(type_id status_id customer_id project_id title hourly_rate net_sum previous_section_number previous_fb_number is_template)),
123 $self->text_blocks(map { Rose::DB::Object::Helpers::clone_and_reset($_) } @{ $source->text_blocks });
125 # Save new object -- we need its ID for the items.
134 my $cloned = Rose::DB::Object::Helpers::clone_and_reset($item);
135 $cloned->requirement_spec_id($self->id);
136 $cloned->children(map { $clone_item->($_) } @{ $item->children });
138 $id_to_clone{ $item->id } = $cloned;
143 $self->items(map { $clone_item->($_) } @{ $source->sections });
145 # Save the items -- need to do that before setting dependencies.
149 foreach my $item (@{ $source->items }) {
150 next unless @{ $item->dependencies };
151 $id_to_clone{ $item->id }->update_attributes(dependencies => [ map { $id_to_clone{$_->id} } @{ $item->dependencies } ]);
154 $self->update_attributes(%attributes);
160 my ($self, $source, %attributes) = @_;
162 $self->db->with_transaction(sub { $self->_copy_from($source, %attributes); });
165 sub highest_version {
168 return reduce { $a->version->version_number > $b->version->version_number ? $a : $b } @{ $self->versioned_copies };
171 sub is_working_copy {
174 return !$self->working_copy_id;
177 sub next_version_number {
180 return max(0, map { $_->version->version_number } @{ $self->versioned_copies }) + 1;
184 my ($self, %attributes) = @_;
186 croak "Cannot work on a versioned copy" if $self->working_copy_id;
188 my ($copy, $version);
189 my $ok = $self->db->with_transaction(sub {
190 delete $attributes{version_number};
192 $version = SL::DB::RequirementSpecVersion->new(%attributes, version_number => $self->next_version_number)->save;
193 $copy = $self->create_copy;
194 $copy->update_attributes(version_id => $version->id, working_copy_id => $self->id);
195 $self->update_attributes(version_id => $version->id);
200 return $ok ? ($copy, $version) : ();
203 sub invalidate_version {
204 my ($self, %params) = @_;
206 croak "Cannot work on a versioned copy" if $self->working_copy_id;
208 return if !$self->id || !$self->version_id;
209 $self->update_attributes(version_id => undef);
221 SL::DB::RequirementSpec - RDBO model for requirement specs
225 The database structure behind requirement specs is a bit involved. The
226 important thing is how working copy/versions are handled.
228 The table contains three important columns: C<id> (which is also the
229 primary key), C<working_copy_id> and C<version_id>. C<working_copy_id>
230 is a self-referencing column: it can be C<NULL>, but if it isn't then
231 it contains another requirement spec C<id>. C<version_id> on the other
232 hand references the table C<requirement_spec_versions>.
234 The design is as follows:
238 =item * The user is always working on a working copy. The working copy
239 is identified in the database by having C<working_copy_id> set to
242 =item * All other entries in this table are referred to as I<versioned
243 copies>. A versioned copy is a copy of a working frozen at the moment
244 in time it was created. Each versioned copy refers back to the working
245 copy it belongs to: each has its C<working_copy_id> set.
247 =item * Each versioned copy must reference an entry in the table
248 C<requirement_spec_versions>. Meaning: for each versioned copy
249 C<version_id> must not be C<NULL>.
251 =item * Directly after creating a versioned copy even the working copy
252 itself points to a certain version via its C<version_id> column: to
253 the same version that the versioned copy just created points
254 to. However, any modification that will be visible to the customer
255 (text, positioning etc but not internal things like time/cost
256 estimation changes) will cause the working copy to be set to 'no
257 version' again. This is achieved via before save hooks in Perl.
261 =head1 DATABASE TRIGGERS AND CHECKS
263 Several database triggers and consistency checks exist that manage
264 requirement specs, their items and their dependencies. These are
265 described here instead of in the individual files for the other RDBO
270 When you delete a requirement spec all of its dependencies (items,
271 text blocks, versions etc.) are deleted by triggers.
273 When you delete an item (either a section or a (sub-)function block)
274 all of its children will be deleted as well. This will trigger the
275 same trigger resulting in a recursive deletion with the bottom-most
276 items being deleted first. Their item dependencies are deleted as
281 Whenever you update a requirement spec item a trigger will fire that
282 will update the parent's C<time_estimation> column. This also happens
283 when an item is deleted or updated.
285 =head2 CONSISTENCY CHECKS
287 Several consistency checks are applied to requirement spec items:
291 =item * Column C<requirement_spec_item.item_type> can only contain one of
292 the values C<section>, C<function-block> or C<sub-function-block>.
294 =item * Column C<requirement_spec_item.parent_id> must be C<NULL> if
295 C<requirement_spec_item.item_type> is set to C<section> and C<NOT
304 =item C<copy_from $source, %attributes>
306 Copies everything (basic attributes like type/title/customer, items,
307 text blocks, time/cost estimation) save for the versions from the
308 other requirement spec object C<$source> into C<$self> and saves
309 it. This is done within a transaction.
311 C<%attributes> are attributes that are assigned to C<$self> after all
312 the basic attributes from C<$source> have been assigned.
314 This function can be used for resetting a working copy to a specific
317 my $requirement_spec = SL::DB::RequirementSpec->new(id => $::form->{id})->load;
318 my $versioned_copy = SL::DB::RequirementSpec->new(id => $::form->{versioned_copy_id})->load;
320 $requirement_spec->copy_from(
322 version_id => $versioned_copy->version_id,
327 Creates and returns a copy of C<$self>. The copy is already
328 saved. Creating the copy happens within a transaction.
330 =item C<create_version %attributes>
332 Prerequisites: C<$self> must be a working copy (see L</working_copy>),
333 not a versioned copy.
335 This function creates a new version for C<$self>. This involves
340 =item 1. The next version number is calculated using
341 L</next_version_number>.
343 =item 2. An instance of L<SL::DB::RequirementSpecVersion> is
344 created. Its attributes are copied from C<%attributes> save for the
345 version number which is taken from step 1.
347 =item 3. A copy of C<$self> is created with L</create_copy>.
349 =item 4. The version instance created in step is assigned to the copy
352 =item 5. The C<version_id> in C<$self> is set to the copy's ID from
357 All this is done within a transaction.
359 In case of success a two-element list is returned consisting of the
360 copy & version objects created in steps 3 and 2 respectively. In case
361 of a failure an empty list will be returned.
363 =item C<displayable_name>
365 Returns a human-readable name for this instance consisting of the type
368 =item C<highest_version>
370 Given a working copy C<$self> this function returns the versioned copy
371 of C<$self> with the highest version number. If such a version exist
372 its instance is returned. Otherwise C<undef> is returned.
374 This can be used for calculating the difference between the working
375 copy and the last version created for it.
377 =item C<invalidate_version>
379 Prerequisites: C<$self> must be a working copy (see L</working_copy>),
380 not a versioned copy.
382 Sets the C<version_id> field to C<undef> and saves C<$self>.
384 =item C<is_working_copy>
386 Returns trueish if C<$self> is a working copy and not a versioned
387 copy. The condition for this is that C<working_copy_id> is C<undef>.
389 =item C<next_version_number>
391 Calculates and returns the next version number for this requirement
392 spec. Version numbers start at 1 and are incremented by one for each
393 version created for it, no matter whether or not it has been reverted
394 to a previous version since. It boils down to this pseudo-code:
396 if (has_never_had_a_version)
399 return max(version_number for all versions for this requirement spec) + 1
403 An alias for L</sections_sorted>.
405 =item C<sections_sorted>
407 Returns an array reference of requirement spec items that do not have
408 a parent -- meaning that are sections.
410 This is not a writer. Use the C<items> relationship for that.
412 =item C<text_blocks_sorted %params>
414 Returns an array reference of text blocks sorted by their positional
415 column in ascending order. If the C<output_position> parameter is
416 given then only the text blocks belonging to that C<output_position>
421 Validate values before saving. Returns list or human-readable error
424 =item C<versioned_copies_sorted %params>
426 Returns an array reference of versioned copies sorted by their version
427 number in ascending order. If the C<max_version_number> parameter is
428 given then only the versioned copies whose version number is less than
429 or equal to C<max_version_number> are returned.
439 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>