Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / lib / RT / Shredder / Plugin / Summary.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 package RT::Shredder::Plugin::Summary;
50
51 use strict;
52 use warnings FATAL => 'all';
53
54 use base qw(RT::Shredder::Plugin::SQLDump);
55
56 sub AppliesToStates { return 'before any action' }
57
58 sub TestArgs
59 {
60     my $self = shift;
61     my %args = (file_name => '', @_);
62     unless( $args{'file_name'} ) {
63         require POSIX;
64         $args{'file_name'} = POSIX::strftime( "summary-%Y%m%dT%H%M%S.XXXX.txt", gmtime );
65     }
66     return $self->SUPER::TestArgs( %args );
67 }
68
69 sub Run
70 {
71     my $self = shift;
72     my %args = ( Object => undef, @_ );
73     my $class = ref $args{'Object'};
74     $class =~ s/^RT:://;
75     $class =~ s/:://g;
76     my $method = 'WriteDown'. $class;
77     $method = 'WriteDownDefault' unless $self->can($method);
78     return $self->$method( %args );
79 }
80
81 my %skip_refs_to = ();
82
83 sub WriteDownDefault {
84     my $self = shift;
85     my %args = ( Object => undef, @_ );
86     return $self->_WriteDownHash(
87         $args{'Object'},
88         $self->_MakeHash( $args{'Object'} ),
89     );
90 }
91
92 # TODO: cover other objects
93 # ACE.pm
94 # Attachment.pm
95 # CustomField.pm
96 # CustomFieldValue.pm
97 # GroupMember.pm
98 # Group.pm
99 # Link.pm
100 # ObjectCustomFieldValue.pm
101 # Principal.pm
102 # Queue.pm
103 # Ticket.pm
104 # User.pm
105
106 # ScripAction.pm - works fine with defaults
107 # ScripCondition.pm - works fine with defaults
108 # Template.pm - works fine with defaults
109
110 sub WriteDownCachedGroupMember { return 1 }
111 sub WriteDownPrincipal { return 1 }
112
113 sub WriteDownGroup {
114     my $self = shift;
115     my %args = ( Object => undef, @_ );
116     if ( $args{'Object'}->RoleClass ) {
117         return $skip_refs_to{ $args{'Object'}->UID } = 1;
118     }
119     return $self->WriteDownDefault( %args );
120 }
121
122 sub WriteDownTransaction {
123     my $self = shift;
124     my %args = ( Object => undef, @_ );
125
126     my $props = $self->_MakeHash( $args{'Object'} );
127     $props->{'Object'} = delete $props->{'ObjectType'};
128     $props->{'Object'} .= '-'. delete $props->{'ObjectId'}
129         if $props->{'ObjectId'};
130     return 1 if $skip_refs_to{ $props->{'Object'} };
131
132     delete $props->{$_} foreach grep
133         !defined $props->{$_} || $props->{$_} eq '', keys %$props;
134
135     return $self->_WriteDownHash( $args{'Object'}, $props );
136 }
137
138 sub WriteDownScrip {
139     my $self = shift;
140     my %args = ( Object => undef, @_ );
141     my $props = $self->_MakeHash( $args{'Object'} );
142     $props->{'Action'} = $args{'Object'}->ActionObj->Name;
143     $props->{'Condition'} = $args{'Object'}->ConditionObj->Name;
144     $props->{'Template'} = $args{'Object'}->Template;
145     $props->{'Queue'} = $args{'Object'}->QueueObj->Name || 'global';
146
147     return $self->_WriteDownHash( $args{'Object'}, $props );
148 }
149
150 sub _MakeHash {
151     my ($self, $obj) = @_;
152     my $hash = $self->__MakeHash( $obj );
153     foreach (grep exists $hash->{$_}, qw(Creator LastUpdatedBy)) {
154         my $method = $_ .'Obj';
155         my $u = $obj->$method();
156         $hash->{ $_ } = $u->EmailAddress || $u->Name || $u->UID;
157     }
158     return $hash;
159 }
160
161 sub __MakeHash {
162     my ($self, $obj) = @_;
163     my %hash;
164     $hash{ $_ } = $obj->$_()
165         foreach sort keys %{ $obj->_ClassAccessible };
166     return \%hash;
167 }
168
169 sub _WriteDownHash {
170     my ($self, $obj, $hash) = @_;
171     return (0, 'no handle') unless my $fh = $self->{'opt'}{'file_handle'};
172
173     print $fh "=== ". $obj->UID ." ===\n"
174         or return (0, "Couldn't write to filehandle");
175
176     foreach my $key( sort keys %$hash ) {
177         my $val = $hash->{ $key };
178         next unless defined $val;
179         $val =~ s/\n/\n /g;
180         print $fh $key .': '. $val ."\n"
181             or return (0, "Couldn't write to filehandle");
182     }
183     print $fh "\n" or return (0, "Couldn't write to filehandle");
184     return 1;
185 }
186
187 1;