rt 4.0.23
[freeside.git] / rt / lib / RT / Shredder / Record.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 use RT::Record ();
50 package RT::Record;
51
52 use strict;
53 use warnings;
54 use warnings FATAL => 'redefine';
55
56 use RT::Shredder::Constants;
57 use RT::Shredder::Exceptions;
58
59 =head2 _AsString
60
61 Returns string in format ClassName-ObjectId.
62
63 =cut
64
65 sub _AsString { return ref($_[0]) ."-". $_[0]->id }
66
67 =head2 _AsInsertQuery
68
69 Returns INSERT query string that duplicates current record and
70 can be used to insert record back into DB after delete.
71
72 =cut
73
74 sub _AsInsertQuery
75 {
76     my $self = shift;
77
78     my $dbh = $RT::Handle->dbh;
79
80     my $res = "INSERT INTO ". $dbh->quote_identifier( $self->Table );
81     my $values = $self->{'values'};
82     $res .= "(". join( ",", map { $dbh->quote_identifier( $_ ) } sort keys %$values ) .")";
83     $res .= " VALUES";
84     $res .= "(". join( ",", map { $dbh->quote( $values->{$_} ) } sort keys %$values ) .")";
85     $res .= ";";
86
87     return $res;
88 }
89
90 sub BeforeWipeout { return 1 }
91
92 =head2 Dependencies
93
94 Returns L<RT::Shredder::Dependencies> object.
95
96 =cut
97
98 sub Dependencies
99 {
100     my $self = shift;
101     my %args = (
102             Shredder => undef,
103             Flags => DEPENDS_ON,
104             @_,
105            );
106
107     unless( $self->id ) {
108         RT::Shredder::Exception->throw('Object is not loaded');
109     }
110
111     my $deps = RT::Shredder::Dependencies->new();
112     if( $args{'Flags'} & DEPENDS_ON ) {
113         $self->__DependsOn( %args, Dependencies => $deps );
114     }
115     if( $args{'Flags'} & RELATES ) {
116         $self->__Relates( %args, Dependencies => $deps );
117     }
118     return $deps;
119 }
120
121 sub __DependsOn
122 {
123     my $self = shift;
124     my %args = (
125             Shredder => undef,
126             Dependencies => undef,
127             @_,
128            );
129     my $deps = $args{'Dependencies'};
130     my $list = [];
131
132 # Object custom field values
133     my $objs = $self->CustomFieldValues;
134     $objs->{'find_expired_rows'} = 1;
135     push( @$list, $objs );
136
137 # Object attributes
138     $objs = $self->Attributes;
139     push( @$list, $objs );
140
141 # Transactions
142     $objs = RT::Transactions->new( $self->CurrentUser );
143     $objs->Limit( FIELD => 'ObjectType', VALUE => ref $self );
144     $objs->Limit( FIELD => 'ObjectId', VALUE => $self->id );
145     push( @$list, $objs );
146
147 # Links
148     if ( $self->can('_Links') ) {
149         # XXX: We don't use Links->Next as it's dies when object
150         #      is linked to object that doesn't exist
151         #      also, ->Next skip links to deleted tickets :(
152         foreach ( qw(Base Target) ) {
153             my $objs = $self->_Links( $_ );
154             $objs->_DoSearch;
155             push @$list, $objs->ItemsArrayRef;
156         }
157     }
158
159 # ACE records
160     $objs = RT::ACL->new( $self->CurrentUser );
161     $objs->LimitToObject( $self );
162     push( @$list, $objs );
163
164     $deps->_PushDependencies(
165             BaseObject => $self,
166             Flags => DEPENDS_ON,
167             TargetObjects => $list,
168             Shredder => $args{'Shredder'}
169         );
170     return;
171 }
172
173 sub __Relates
174 {
175     my $self = shift;
176     my %args = (
177             Shredder => undef,
178             Dependencies => undef,
179             @_,
180            );
181     my $deps = $args{'Dependencies'};
182     my $list = [];
183
184     if( $self->_Accessible( 'Creator', 'read' ) ) {
185         my $obj = RT::Principal->new( $self->CurrentUser );
186         $obj->Load( $self->Creator );
187
188         if( $obj && defined $obj->id ) {
189             push( @$list, $obj );
190         } else {
191             my $rec = $args{'Shredder'}->GetRecord( Object => $self );
192             $self = $rec->{'Object'};
193             $rec->{'State'} |= INVALID;
194             push @{ $rec->{'Description'} },
195                 "Have no related User(Creator) #". $self->Creator ." object";
196         }
197     }
198
199     if( $self->_Accessible( 'LastUpdatedBy', 'read' ) ) {
200         my $obj = RT::Principal->new( $self->CurrentUser );
201         $obj->Load( $self->LastUpdatedBy );
202
203         if( $obj && defined $obj->id ) {
204             push( @$list, $obj );
205         } else {
206             my $rec = $args{'Shredder'}->GetRecord( Object => $self );
207             $self = $rec->{'Object'};
208             $rec->{'State'} |= INVALID;
209             push @{ $rec->{'Description'} },
210                 "Have no related User(LastUpdatedBy) #". $self->LastUpdatedBy ." object";
211         }
212     }
213
214     $deps->_PushDependencies(
215             BaseObject => $self,
216             Flags => RELATES,
217             TargetObjects => $list,
218             Shredder => $args{'Shredder'}
219         );
220
221     # cause of this $self->SUPER::__Relates should be called last
222     # in overridden subs
223     my $rec = $args{'Shredder'}->GetRecord( Object => $self );
224     $rec->{'State'} |= VALID unless( $rec->{'State'} & INVALID );
225
226     return;
227 }
228
229 # implement proxy method because some RT classes
230 # override Delete method
231 sub __Wipeout
232 {
233     my $self = shift;
234     my $msg = $self->_AsString ." wiped out";
235     $self->SUPER::Delete;
236     $RT::Logger->info( $msg );
237     return;
238 }
239
240 sub ValidateRelations
241 {
242     my $self = shift;
243     my %args = (
244             Shredder => undef,
245             @_
246            );
247     unless( $args{'Shredder'} ) {
248         $args{'Shredder'} = RT::Shredder->new();
249     }
250
251     my $rec = $args{'Shredder'}->PutObject( Object => $self );
252     return if( $rec->{'State'} & VALID );
253     $self = $rec->{'Object'};
254
255     $self->_ValidateRelations( %args, Flags => RELATES );
256     $rec->{'State'} |= VALID unless( $rec->{'State'} & INVALID );
257
258     return;
259 }
260
261 sub _ValidateRelations
262 {
263     my $self = shift;
264     my %args = ( @_ );
265
266     my $deps = $self->Dependencies( %args );
267
268     $deps->ValidateRelations( %args );
269
270     return;
271 }
272
273 1;