Converter/RT1.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Converter/RT3/Config.pm ↑ Summary Converter/Config.pm »
1 package RT::Extension::Converter::RT1;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base qw(Class::Accessor::Fast);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 __PACKAGE__->mk_accessors(qw(config _handle ));
7  
8 3 ▶use RT::Extension::Converter::RT1::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=2.6
10  
11 =head1 NAME
12  
13 RT::Extension::Converter::RT1 - Handle the RT1 side of a conversion
14  
15  
16 =head1 SYNOPSIS
17  
18 use RT::Extension::Converter::RT1;
19 my $converter = RT::Extension::Converter::RT1->new;
20  
21 =head1 DESCRIPTION
22  
23 Object that should be used by converter scripts to
24  
25 =head1 METHODS
26  
27 =head2 new
28  
29 Returns a converter object after setting up things such as the config
30  
31 =cut
32  
33 sub new {
34 my $class = shift;
35  
36 my $self = $class->SUPER::new(@_);
37 $self->config(RT::Extension::Converter::RT1::Config->new);
38 return $self;
39 }
40  
41 =head2 config
42  
43 Returns a config object
44  
45 =head2 _handle
46  
47 private method for the db handle of the RT1 database
48  
49 =head2 _connect
50  
51 conect to the RT1 database
52  
53 =cut
54  
55 # this probably really wants to be using DBIx::SearchBuilder or
56 # some other ORM, but we're really just doing a few simple SQL calls
57 # so we'll avoid having to map the old tables for now
58  
59 sub _connect {
60 my $self = shift;
61 my $config = $self->config;
62
63 my $dsn = sprintf("DBI:mysql:database=%s;host=%s;",
64 $config->database, $config->dbhost,
65 { RaiseError => 1 });
66 print "connecting to $dsn" if $config->debug;
67 my $dbh = DBI->connect($dsn, $config->dbuser, $config->dbpassword)
68 or die "Can't connect to RT1 database: ".$DBI::errstr;
69  
70 return $self->_handle($dbh);
71 }
72  
73 =head2 _run_query
74  
75 Takes a sql string and a list of placeholder values
76  
77 _run_query( sql => $sql, placeholders => \@placeholders )
78  
79 Returns a statement handle
80  
81 =cut
82  
83 sub _run_query {
84 my $self = shift;
85 my %args = @_;
86  
87 my $handle= $self->_handle|| $self->_connect;
88  
89 my @placeholders = @{$args{placeholders}||[]};
90
91 my $sth = $handle->prepare($args{sql});
92 $sth->execute(@placeholders) or
93 die("Can't run query: $args{sql} - " .
94 join(" ",@placeholders) .
95 "\nReason:" . $DBI::errstr . "\n");
96
97 return $sth;
98 }
99  
100 =head3 _fetch_data
101  
102 wrapper around _run_query to hide the boring
103 bits of iterating over the data set and
104 cleaning up when we get to the end of the data.
105  
106 =cut
107  
108 sub _fetch_data {
109 my $self = shift;
110 my %args = @_;
111 my $name = delete $args{name};
112  
113 my $sth = $self->_sth($name);
114  
115 unless ($sth) {
116 $sth = $self->_run_query( %args );
117 $self->_sth( $name => $sth );
118 }
119  
120 my $data = $sth->fetchrow_hashref;
121  
122 $self->_clean_sth($name) unless $data;
123  
124 return $data;
125 }
126  
127 =head2 _sth
128  
129 Stores several named sth's for this object (since multiple queries
130 can be happening simultaneously).
131  
132 Takes
133 Name => sth for set
134 Name for get
135  
136 =cut
137  
138 sub _sth {
139 my $self = shift;
140  
141 if (@_ > 1) {
142 my ($name,$sth) = @_;
143 $self->{sths}{$name} = $sth;
144 } elsif (@_) {
145 my $name = shift;
146 $self->{sths}{$name};
147 } else {
148 die "You must pass at least a name to _sth";
149 }
150 }
151  
152 =head3 _clean_sth
153  
154 finishes the sth and gets rid of it
155 takes the name of the sth
156  
157 =cut
158  
159 sub _clean_sth {
160 my $self = shift;
161 my $name = shift;
162  
163 $self->_sth($name)->finish;
164 $self->_sth($name,undef);
165 return;
166 }
167  
168 =head2 get_user
169  
170 Intended to be called in a loop.
171 Wraps over the DBH iterator. When called for the first time,
172 will fetch the users and returns one as a hashref.
173 Will keep returning one until we run out.
174  
175 =cut
176  
177 sub get_user {
178 my $self = shift;
179  
180 my $sql = <<ESQL;
181 select user_id as Name,
182 real_name as RealName,
183 password as Password,
184 email as EmailAddress,
185 phone as WorkPhone,
186 comments as Comments,
187 admin_rt as SuperUser
188 from users
189 ESQL
190  
191 my $user_data = $self->_fetch_data( name => 'User', sql => $sql );
192  
193 if ($user_data && !$user_data->{EmailAddress}) {
194 $user_data->{EmailAddress} = $user_data->{Name}.'@'.$self->config->email_domain;
195 }
196  
197 return $user_data;
198 }
199  
200 =head3 get_queue
201  
202 Intended to be called in a loop.
203 Wraps over the DBH iterator. When called for the first time,
204 will fetch the queues and returns one as a hashref.
205 Will keep returning one until we run out.
206  
207 =cut
208  
209 sub get_queue {
210 my $self = shift;
211  
212 my $sql = <<ESQL;
213 select queue_id as Name,
214 mail_alias as CorrespondAddress,
215 comment_alias as CommentAddress,
216 default_prio as InitialPriority,
217 default_final_prio as FinalPriority,
218 default_due_in as DefaultDueIn
219 from queues
220 ESQL
221  
222 my $queue_data = $self->_fetch_data( name => 'Queue', sql => $sql );
223  
224 if ($queue_data) {
225 $queue_data->{Description} = "Imported from RT 1.0";
226 }
227  
228 return $queue_data;
229  
230 }
231  
232 =head3 get_area
233  
234 Intended to be called in a loop.
235 Wraps over the DBH iterator. When called for the first time,
236 will fetch the areas for the queue and returns one as a hashref.
237 Will keep returning one until we run out.
238  
239 Takes one argument, Name => Queue's Name
240  
241 =cut
242  
243 sub get_area {
244 my $self = shift;
245 my %args = @_;
246  
247 my $sql = 'select area from queue_areas where queue_id = ?';
248  
249 my $area_data = $self->_fetch_data( name => 'Area',
250 sql => $sql,
251 placeholders => [$args{Name}] );
252  
253 return $area_data;
254 }
255  
256 =head3 get_queue_acl
257  
258 Intended to be called in a loop.
259 Wraps over the DBH iterator. When called for the first time,
260 will fetch the acls for the queue and returns one as a hashref.
261 Will keep returning one until we run out.
262  
263 Takes one argument, Name => Queue's Name
264  
265 =cut
266  
267 sub get_queue_acl {
268 my $self = shift;
269 my %args = @_;
270  
271 my $sql = 'select user_id, display, manipulate, admin from queue_acl where queue_id = ?';
272  
273 my $acl_data = $self->_fetch_data( name => 'ACL',
274 sql => $sql,
275 placeholders => [$args{Name}] );
276  
277 return $acl_data;
278 }
279  
280 =head3 get_ticket
281  
282 Intended to be called in a loop.
283 Wraps over the DBH iterator. When called for the first time,
284 will fetch all tickets and return one as a hashref.
285 Will keep returning one until we run out.
286  
287 =cut
288  
289 sub get_ticket {
290 my $self = shift;
291 my %args = @_;
292  
293 my $sql = <<SQL;
294 select serial_num as id,
295 effective_sn as EffectiveId,
296 status as Status,
297 requestors as Requestors,
298 owner as Owner,
299 subject as Subject,
300 priority as Priority,
301 final_priority as FinalPriority,
302 initial_priority as InitialPriority,
303 date_due as Due,
304 date_told as Told,
305 date_created as Created,
306 date_acted as Updated,
307 queue_id as Queue,
308 area as Area
309 from each_req
310 SQL
311 my $ticket_data = $self->_fetch_data( name => 'Ticket', sql => $sql );
312  
313 return $ticket_data;
314 }
315  
316 =head2 get_transactions
317  
318 Takes the ticketid passed in and returns an arrayref
319 of transaction data.
320  
321 =cut
322  
323 sub get_transactions {
324 my $self = shift;
325 my $ticket_id = shift;
326 my $transactions;
327  
328 my $sql = 'select * from transactions where serial_num = ? order by trans_date asc';
329 while (my $transaction = $self->_fetch_data( name => 'Transaction',
330 sql => $sql,
331 placeholders => [$ticket_id]) ) {
332 if ($transaction->{actor} && $transaction->{actor} !~ /\@/) {
333 $transaction->{actor} .= '@'.$self->config->email_domain;
334 }
335 push @$transactions,$transaction;
336 }
337 return $transactions;
338 }
339  
340 =head1 AUTHOR
341  
342 Kevin Falcone C<< <falcone@bestpractical.com> >>
343  
344  
345 =head1 LICENCE AND COPYRIGHT
346  
347 Copyright (c) 2007, Best Practical Solutions, LLC. All rights reserved.
348  
349 This module is free software; you can redistribute it and/or
350 modify it under the same terms as Perl itself. See L<perlartistic>.
351  
352  
353 =head1 DISCLAIMER OF WARRANTY
354  
355 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
356 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
357 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
358 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
359 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
360 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
361 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
362 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
363 NECESSARY SERVICING, REPAIR, OR CORRECTION.
364  
365 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
366 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
367 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
368 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
369 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
370 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
371 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
372 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
373 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
374 SUCH DAMAGES.
375  
376 =cut
377  
378 1;
« Converter/RT3/Config.pm ↑ Summary Converter/Config.pm »