X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FDB%2FRequirementSpec.pm;h=72651a3c32e2cf8a4b0098407738e2f3d784d09a;hb=fb692c5f36d073818aec001f281a70a77dfb6977;hp=f21e5137e5950e1a5e580dc7d6a00debf8b6dd46;hpb=405a41ef0836ac972a3ebb9e99f05de7d13d4275;p=kivitendo-erp.git diff --git a/SL/DB/RequirementSpec.pm b/SL/DB/RequirementSpec.pm index f21e5137e..72651a3c3 100644 --- a/SL/DB/RequirementSpec.pm +++ b/SL/DB/RequirementSpec.pm @@ -3,22 +3,34 @@ package SL::DB::RequirementSpec; use strict; use Carp; +use List::Util qw(max reduce); use Rose::DB::Object::Helpers; use SL::DB::MetaSetup::RequirementSpec; use SL::DB::Manager::RequirementSpec; use SL::Locale::String; +use SL::Util qw(_hashify); __PACKAGE__->meta->add_relationship( - items => { - type => 'one to many', - class => 'SL::DB::RequirementSpecItem', - column_map => { id => 'requirement_spec_id' }, + items => { + type => 'one to many', + class => 'SL::DB::RequirementSpecItem', + column_map => { id => 'requirement_spec_id' }, }, - text_blocks => { - type => 'one to many', - class => 'SL::DB::RequirementSpecTextBlock', - column_map => { id => 'requirement_spec_id' }, + text_blocks => { + type => 'one to many', + class => 'SL::DB::RequirementSpecTextBlock', + column_map => { id => 'requirement_spec_id' }, + }, + versioned_copies => { + type => 'one to many', + class => 'SL::DB::RequirementSpec', + column_map => { id => 'working_copy_id' }, + }, + orders => { + type => 'one to many', + class => 'SL::DB::RequirementSpecOrder', + column_map => { id => 'requirement_spec_id' }, }, ); @@ -44,13 +56,17 @@ sub _before_save_initialize_not_null_columns { return 1; } -sub text_blocks_for_position { - my ($self, $output_position) = @_; +sub text_blocks_sorted { + my ($self, %params) = _hashify(1, @_); + + my @text_blocks = @{ $self->text_blocks }; + @text_blocks = grep { $_->output_position == $params{output_position} } @text_blocks if exists $params{output_position}; + @text_blocks = sort { $a->position <=> $b->position } @text_blocks; - return [ sort { $a->position <=> $b->position } grep { $_->output_position == $output_position } @{ $self->text_blocks } ]; + return \@text_blocks; } -sub sections { +sub sections_sorted { my ($self, @rest) = @_; croak "This sub is not a writer" if @rest; @@ -58,12 +74,31 @@ sub sections { return [ sort { $a->position <=> $b->position } grep { !$_->parent_id } @{ $self->items } ]; } +sub sections { §ions_sorted; } + +sub orders_sorted { + my ($self, %params) = _hashify(1, @_); + my $by = $params{by} || 'itime'; + + return [ sort { $a->$by cmp $b->$by } @{ $self->orders } ]; +} + sub displayable_name { my ($self) = @_; return sprintf('%s: "%s"', $self->type->description, $self->title); } +sub versioned_copies_sorted { + my ($self, %params) = _hashify(1, @_); + + my @copies = @{ $self->versioned_copies }; + @copies = grep { $_->version->version_number <= $params{max_version_number} } @copies if $params{max_version_number}; + @copies = sort { $a->version->version_number <=> $b->version->version_number } @copies; + + return \@copies; +} + sub create_copy { my ($self, %params) = @_; @@ -82,13 +117,44 @@ sub _create_copy { my ($self, %params) = @_; my $copy = Rose::DB::Object::Helpers::clone_and_reset($self); - $copy->assign_attributes(%params); + $copy->copy_from($self, %params); + + return $copy; +} + +sub _copy_from { + my ($self, $params, %attributes) = @_; + + my $source = $params->{source}; + + croak "Missing parameter 'source'" unless $source; + + # Copy attributes. + if (!$params->{paste_template}) { + $self->assign_attributes(map({ ($_ => $source->$_) } qw(type_id status_id customer_id project_id title hourly_rate time_estimation previous_section_number previous_fb_number is_template)), + %attributes); + } + + my %paste_template_result; # Clone text blocks. - $copy->text_blocks(map { Rose::DB::Object::Helpers::clone_and_reset($_) } @{ $self->text_blocks }); + my $clone_text_block = sub { + my ($text_block) = @_; + my $cloned = Rose::DB::Object::Helpers::clone_and_reset($text_block); + $cloned->position(undef); + return $cloned; + }; + + $paste_template_result{text_blocks} = [ map { $clone_text_block->($_) } @{ $source->text_blocks_sorted } ]; + + if (!$params->{paste_template}) { + $self->text_blocks($paste_template_result{text_blocks}); + } else { + $self->add_text_blocks($paste_template_result{text_blocks}); + } # Save new object -- we need its ID for the items. - $copy->save; + $self->save; my %id_to_clone; @@ -97,7 +163,8 @@ sub _create_copy { $clone_item = sub { my ($item) = @_; my $cloned = Rose::DB::Object::Helpers::clone_and_reset($item); - $cloned->requirement_spec_id($copy->id); + $cloned->requirement_spec_id($self->id); + $cloned->position(undef); $cloned->children(map { $clone_item->($_) } @{ $item->children }); $id_to_clone{ $item->id } = $cloned; @@ -105,18 +172,314 @@ sub _create_copy { return $cloned; }; - $copy->items(map { $clone_item->($_) } @{ $self->sections }); + $paste_template_result{sections} = [ map { $clone_item->($_) } @{ $source->sections_sorted } ]; + + if (!$params->{paste_template}) { + $self->items($paste_template_result{sections}); + } else { + $self->add_items($paste_template_result{sections}); + } # Save the items -- need to do that before setting dependencies. - $copy->save; + $self->save; # Set dependencies. - foreach my $item (@{ $self->items }) { + foreach my $item (@{ $source->items }) { next unless @{ $item->dependencies }; $id_to_clone{ $item->id }->update_attributes(dependencies => [ map { $id_to_clone{$_->id} } @{ $item->dependencies } ]); } - return $copy; + $self->update_attributes(%attributes) unless $params->{paste_template}; + + return %paste_template_result; +} + +sub copy_from { + my ($self, $source, %attributes) = @_; + + $self->db->with_transaction(sub { $self->_copy_from({ source => $source, paste_template => 0 }, %attributes); }); +} + +sub paste_template { + my ($self, $template) = @_; + + $self->db->with_transaction(sub { $self->_copy_from({ source => $template, paste_template => 1 }); }); +} + +sub highest_version { + my ($self) = @_; + + return reduce { $a->version->version_number > $b->version->version_number ? $a : $b } @{ $self->versioned_copies }; +} + +sub is_working_copy { + my ($self) = @_; + + return !$self->working_copy_id; +} + +sub next_version_number { + my ($self) = @_; + + return max(0, map { $_->version->version_number } @{ $self->versioned_copies }) + 1; +} + +sub create_version { + my ($self, %attributes) = @_; + + croak "Cannot work on a versioned copy" if $self->working_copy_id; + + my ($copy, $version); + my $ok = $self->db->with_transaction(sub { + delete $attributes{version_number}; + + $version = SL::DB::RequirementSpecVersion->new(%attributes, version_number => $self->next_version_number)->save; + $copy = $self->create_copy; + $copy->update_attributes(version_id => $version->id, working_copy_id => $self->id); + $self->update_attributes(version_id => $version->id); + + 1; + }); + + return $ok ? ($copy, $version) : (); +} + +sub invalidate_version { + my ($self, %params) = @_; + + croak "Cannot work on a versioned copy" if $self->working_copy_id; + + return if !$self->id || !$self->version_id; + $self->update_attributes(version_id => undef); } 1; +__END__ + +=pod + +=encoding utf8 + +=head1 NAME + +SL::DB::RequirementSpec - RDBO model for requirement specs + +=head1 OVERVIEW + +The database structure behind requirement specs is a bit involved. The +important thing is how working copy/versions are handled. + +The table contains three important columns: C (which is also the +primary key), C and C. C +is a self-referencing column: it can be C, but if it isn't then +it contains another requirement spec C. C on the other +hand references the table C. + +The design is as follows: + +=over 2 + +=item * The user is always working on a working copy. The working copy +is identified in the database by having C set to +C. + +=item * All other entries in this table are referred to as I. A versioned copy is a copy of a working frozen at the moment +in time it was created. Each versioned copy refers back to the working +copy it belongs to: each has its C set. + +=item * Each versioned copy must reference an entry in the table +C. Meaning: for each versioned copy +C must not be C. + +=item * Directly after creating a versioned copy even the working copy +itself points to a certain version via its C column: to +the same version that the versioned copy just created points +to. However, any modification that will be visible to the customer +(text, positioning etc but not internal things like time/cost +estimation changes) will cause the working copy to be set to 'no +version' again. This is achieved via before save hooks in Perl. + +=back + +=head1 DATABASE TRIGGERS AND CHECKS + +Several database triggers and consistency checks exist that manage +requirement specs, their items and their dependencies. These are +described here instead of in the individual files for the other RDBO +models. + +=head2 DELETION + +When you delete a requirement spec all of its dependencies (items, +text blocks, versions etc.) are deleted by triggers. + +When you delete an item (either a section or a (sub-)function block) +all of its children will be deleted as well. This will trigger the +same trigger resulting in a recursive deletion with the bottom-most +items being deleted first. Their item dependencies are deleted as +well. + +=head2 UPDATING + +Whenever you update a requirement spec item a trigger will fire that +will update the parent's C column. This also happens +when an item is deleted or updated. + +=head2 CONSISTENCY CHECKS + +Several consistency checks are applied to requirement spec items: + +=over 2 + +=item * Column C can only contain one of +the values C
, C or C. + +=item * Column C must be C if +C is set to C
and C otherwise. + +=back + +=head1 FUNCTIONS + +=over 4 + +=item C + +Copies everything (basic attributes like type/title/customer, items, +text blocks, time/cost estimation) save for the versions from the +other requirement spec object C<$source> into C<$self> and saves +it. This is done within a transaction. + +C<%attributes> are attributes that are assigned to C<$self> after all +the basic attributes from C<$source> have been assigned. + +This function can be used for resetting a working copy to a specific +version. Example: + + my $requirement_spec = SL::DB::RequirementSpec->new(id => $::form->{id})->load; + my $versioned_copy = SL::DB::RequirementSpec->new(id => $::form->{versioned_copy_id})->load; + + $requirement_spec->copy_from( + $versioned_copy, + version_id => $versioned_copy->version_id, + ); + +=item C + +Creates and returns a copy of C<$self>. The copy is already +saved. Creating the copy happens within a transaction. + +=item C + +Prerequisites: C<$self> must be a working copy (see the overview), +not a versioned copy. + +This function creates a new version for C<$self>. This involves +several steps: + +=over 2 + +=item 1. The next version number is calculated using +L. + +=item 2. An instance of L is +created. Its attributes are copied from C<%attributes> save for the +version number which is taken from step 1. + +=item 3. A copy of C<$self> is created with L. + +=item 4. The version instance created in step is assigned to the copy +from step 3. + +=item 5. The C in C<$self> is set to the copy's ID from +step 3. + +=back + +All this is done within a transaction. + +In case of success a two-element list is returned consisting of the +copy & version objects created in steps 3 and 2 respectively. In case +of a failure an empty list will be returned. + +=item C + +Returns a human-readable name for this instance consisting of the type +and the title. + +=item C + +Given a working copy C<$self> this function returns the versioned copy +of C<$self> with the highest version number. If such a version exist +its instance is returned. Otherwise C is returned. + +This can be used for calculating the difference between the working +copy and the last version created for it. + +=item C + +Prerequisites: C<$self> must be a working copy (see the overview), +not a versioned copy. + +Sets the C field to C and saves C<$self>. + +=item C + +Returns trueish if C<$self> is a working copy and not a versioned +copy. The condition for this is that C is C. + +=item C + +Calculates and returns the next version number for this requirement +spec. Version numbers start at 1 and are incremented by one for each +version created for it, no matter whether or not it has been reverted +to a previous version since. It boils down to this pseudo-code: + + if (has_never_had_a_version) + return 1 + else + return max(version_number for all versions for this requirement spec) + 1 + +=item C + +An alias for L. + +=item C + +Returns an array reference of requirement spec items that do not have +a parent -- meaning that are sections. + +This is not a writer. Use the C relationship for that. + +=item C + +Returns an array reference of text blocks sorted by their positional +column in ascending order. If the C parameter is +given then only the text blocks belonging to that C +are returned. + +=item C + +Validate values before saving. Returns list or human-readable error +messages (if any). + +=item C + +Returns an array reference of versioned copies sorted by their version +number in ascending order. If the C parameter is +given then only the versioned copies whose version number is less than +or equal to C are returned. + +=back + +=head1 BUGS + +Nothing here yet. + +=head1 AUTHOR + +Moritz Bunkus Em.bunkus@linet-services.deE + +=cut