Pflichtenhefte: nach Änderung an Textblöcken Version invalidieren
[kivitendo-erp.git] / SL / DB / RequirementSpec.pm
1 package SL::DB::RequirementSpec;
2
3 use strict;
4
5 use Carp;
6 use Rose::DB::Object::Helpers;
7
8 use SL::DB::MetaSetup::RequirementSpec;
9 use SL::DB::Manager::RequirementSpec;
10 use SL::Locale::String;
11
12 __PACKAGE__->meta->add_relationship(
13   items            => {
14     type           => 'one to many',
15     class          => 'SL::DB::RequirementSpecItem',
16     column_map     => { id => 'requirement_spec_id' },
17   },
18   text_blocks      => {
19     type           => 'one to many',
20     class          => 'SL::DB::RequirementSpecTextBlock',
21     column_map     => { id => 'requirement_spec_id' },
22   },
23   versioned_copies => {
24     type           => 'one to many',
25     class          => 'SL::DB::RequirementSpec',
26     column_map     => { id => 'working_copy_id' },
27   },
28 );
29
30 __PACKAGE__->meta->initialize;
31
32 __PACKAGE__->before_save('_before_save_initialize_not_null_columns');
33
34 sub validate {
35   my ($self) = @_;
36
37   my @errors;
38   push @errors, t8('The title is missing.') if !$self->title;
39
40   return @errors;
41 }
42
43 sub _before_save_initialize_not_null_columns {
44   my ($self) = @_;
45
46   $self->previous_section_number(0) if !defined $self->previous_section_number;
47   $self->previous_fb_number(0)      if !defined $self->previous_fb_number;
48
49   return 1;
50 }
51
52 sub text_blocks_for_position {
53   my ($self, $output_position) = @_;
54
55   return [ sort { $a->position <=> $b->position } grep { $_->output_position == $output_position } @{ $self->text_blocks } ];
56 }
57
58 sub sections {
59   my ($self, @rest) = @_;
60
61   croak "This sub is not a writer" if @rest;
62
63   return [ sort { $a->position <=> $b->position } grep { !$_->parent_id } @{ $self->items } ];
64 }
65
66 sub displayable_name {
67   my ($self) = @_;
68
69   return sprintf('%s: "%s"', $self->type->description, $self->title);
70 }
71
72 sub create_copy {
73   my ($self, %params) = @_;
74
75   return $self->_create_copy(%params) if $self->db->in_transaction;
76
77   my $copy;
78   if (!$self->db->do_transaction(sub { $copy = $self->_create_copy(%params) })) {
79     $::lxdebug->message(LXDebug->WARN(), "create_copy failed: " . join("\n", (split(/\n/, $self->db->error))[0..2]));
80     return undef;
81   }
82
83   return $copy;
84 }
85
86 sub _create_copy {
87   my ($self, %params) = @_;
88
89   my $copy = Rose::DB::Object::Helpers::clone_and_reset($self);
90   $copy->assign_attributes(%params);
91
92   # Clone text blocks.
93   $copy->text_blocks(map { Rose::DB::Object::Helpers::clone_and_reset($_) } @{ $self->text_blocks });
94
95   # Save new object -- we need its ID for the items.
96   $copy->save;
97
98   my %id_to_clone;
99
100   # Clone items.
101   my $clone_item;
102   $clone_item = sub {
103     my ($item) = @_;
104     my $cloned = Rose::DB::Object::Helpers::clone_and_reset($item);
105     $cloned->requirement_spec_id($copy->id);
106     $cloned->children(map { $clone_item->($_) } @{ $item->children });
107
108     $id_to_clone{ $item->id } = $cloned;
109
110     return $cloned;
111   };
112
113   $copy->items(map { $clone_item->($_) } @{ $self->sections });
114
115   # Save the items -- need to do that before setting dependencies.
116   $copy->save;
117
118   # Set dependencies.
119   foreach my $item (@{ $self->items }) {
120     next unless @{ $item->dependencies };
121     $id_to_clone{ $item->id }->update_attributes(dependencies => [ map { $id_to_clone{$_->id} } @{ $item->dependencies } ]);
122   }
123
124   return $copy;
125 }
126
127 sub previous_version {
128   my ($self) = @_;
129
130   my $and    = $self->version_id ? " AND (version_id <> ?)" : "";
131   my $id     = $self->db->dbh->selectrow_array(<<SQL, undef, $self->id, ($self->version_id) x !!$self->version_id);
132    SELECT MAX(id)
133    FROM requirement_specs
134    WHERE (working_copy_id = ?) $and
135 SQL
136
137   return $id ? SL::DB::RequirementSpec->new(id => $id)->load : undef;
138 }
139
140 sub is_working_copy {
141   my ($self) = @_;
142
143   return !$self->working_copy_id;
144 }
145
146 sub next_version_number {
147   my ($self) = @_;
148   my $max_number = $self->db->dbh->selectrow_array(<<SQL, undef, $self->id);
149     SELECT COALESCE(MAX(ver.version_number), 0)
150     FROM requirement_spec_versions ver
151     JOIN requirement_specs rs ON (rs.version_id = ver.id)
152     WHERE rs.working_copy_id = ?
153 SQL
154
155   return $max_number + 1;
156 }
157
158 sub create_version {
159   my ($self, %attributes) = @_;
160
161   my ($copy, $version);
162   my $ok = $self->db->do_transaction(sub {
163     delete $attributes{version_number};
164
165     $version = SL::DB::RequirementSpecVersion->new(%attributes, version_number => $self->next_version_number)->save;
166     $copy    = $self->create_copy;
167     $copy->update_attributes(version_id => $version->id, working_copy_id => $self->id);
168     $self->update_attributes(version_id => $version->id);
169
170     1;
171   });
172
173   return $ok ? ($copy, $version) : ();
174 }
175
176 sub invalidate_version {
177   my ($self, %params) = @_;
178
179   $::lxdebug->message(0, "Invalidate version called for id " . $self->id . " version " . $self->version_id);
180   $::lxdebug->show_backtrace(1);
181
182   return if !$self->id || !$self->version_id;
183   $self->update_attributes(version_id => undef);
184 }
185
186 1;