1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2013 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
49 package RT::Migrate::Importer;
60 my $self = bless {}, $class;
76 # Should we attempt to preserve record IDs as they are created?
77 $self->{OriginalId} = $args{OriginalId};
79 $self->{Progress} = $args{Progress};
81 $self->{HandleError} = sub { 0 };
82 $self->{HandleError} = $args{HandleError}
83 if $args{HandleError} and ref $args{HandleError} eq 'CODE';
85 if ($args{DumpObjects}) {
87 $self->{DumpObjects} = { map { $_ => 1 } @{$args{DumpObjects}} };
90 # Objects we've created
93 # Columns we need to update when an object is later created
94 $self->{Pending} = {};
96 # Objects missing from the source database before serialization
97 $self->{Invalid} = [];
100 $self->{ObjectCount} = {};
102 # To know what global CFs need to be unglobal'd and applied to what
103 $self->{NewQueues} = [];
104 $self->{NewCFs} = [];
109 return $self->{Metadata};
116 return if $self->{Metadata};
117 $self->{Metadata} = $data;
119 die "Incompatible format version: ".$data->{Format}
120 if $data->{Format} ne "0.8";
122 $self->{Organization} = $data->{Organization};
123 $self->{Clone} = $data->{Clone};
124 $self->{Incremental} = $data->{Incremental};
125 $self->{Files} = $data->{Files} if $data->{Final};
131 die "Stream initialized after objects have been recieved!"
132 if keys %{ $self->{UIDs} };
134 die "Cloning does not support importing the Original Id separately\n"
135 if $self->{OriginalId} and $self->{Clone};
137 die "RT already contains data; overwriting will not work\n"
138 if ($self->{Clone} and not $self->{Incremental})
139 and RT->SystemUser->Id;
141 # Basic facts of life, as a safety net
142 $self->Resolve( RT->System->UID => ref RT->System, RT->System->Id );
143 $self->SkipTransactions( RT->System->UID );
145 if ($self->{OriginalId}) {
146 # Where to shove the original ticket ID
147 my $cf = RT::CustomField->new( RT->SystemUser );
148 $cf->LoadByName( Queue => 0, Name => $self->{OriginalId} );
150 warn "Failed to find global CF named $self->{OriginalId} -- creating one";
153 Name => $self->{OriginalId},
154 Type => 'FreeformSingle',
162 my ($uid, $class, $id) = @_;
163 $self->{UIDs}{$uid} = [ $class, $id ];
164 return unless $self->{Pending}{$uid};
166 for my $ref (@{$self->{Pending}{$uid}}) {
167 my ($pclass, $pid) = @{ $self->Lookup( $ref->{uid} ) };
168 my $obj = $pclass->new( RT->SystemUser );
169 $obj->LoadByCols( Id => $pid );
171 Field => $ref->{column},
173 ) if defined $ref->{column};
175 Field => $ref->{classcolumn},
177 ) if defined $ref->{classcolumn};
179 Field => $ref->{uri},
180 Value => $self->LookupObj($uid)->URI,
181 ) if defined $ref->{uri};
183 delete $self->{Pending}{$uid};
189 unless (defined $uid) {
190 carp "Tried to lookup an undefined UID";
193 return $self->{UIDs}{$uid};
199 my $ref = $self->Lookup( $uid );
201 my ($class, $id) = @{ $ref };
203 my $obj = $class->new( RT->SystemUser );
214 classcolumn => undef,
218 my $uid = delete $args{for};
221 push @{$self->{Pending}{$uid}}, \%args;
223 push @{$self->{Invalid}}, \%args;
227 sub SkipTransactions {
230 return if $self->{Clone};
231 $self->{SkipTransactions}{$uid} = 1;
234 sub ShouldSkipTransaction {
237 return exists $self->{SkipTransactions}{$uid};
242 my ($obj, $data) = @_;
243 for my $col (keys %{$data}) {
244 next if defined $obj->__Value($col) and length $obj->__Value($col);
245 next unless defined $data->{$col} and length $data->{$col};
247 if (ref $data->{$col}) {
248 my $uid = ${ $data->{$col} };
249 my $ref = $self->Lookup( $uid );
251 $data->{$col} = $ref->[1];
261 $obj->__Set( Field => $col, Value => $data->{$col} );
267 my ($column, $class, $uid, $data) = @_;
269 my $obj = $class->new( RT->SystemUser );
270 $obj->Load( $data->{$column} );
271 return unless $obj->Id;
273 $self->SkipTransactions( $uid );
275 $self->Resolve( $uid => $class => $obj->Id );
281 my ($column, $class, $uid, $data) = @_;
283 my $obj = $self->SkipBy(@_);
285 $self->MergeValues( $obj, $data );
292 return $string if $self->{Clone};
293 return $string if not defined $self->{Organization};
294 return $self->{Organization}.": $string";
299 my ($class, $uid, $data) = @_;
301 # Use a simpler pre-inflation if we're cloning
302 if ($self->{Clone}) {
303 $class->RT::Record::PreInflate( $self, $uid, $data );
305 # Non-cloning always wants to make its own id
307 return unless $class->PreInflate( $self, $uid, $data );
310 my $obj = $class->new( RT->SystemUser );
311 my ($id, $msg) = eval {
312 # catch and rethrow on the outside so we can provide more info
314 $obj->DBIx::SearchBuilder::Record::Create(
319 $msg ||= ''; # avoid undef
320 my $err = "Failed to create $uid: $msg $@\n" . Data::Dumper::Dumper($data) . "\n";
321 if (not $self->{HandleError}->($self, $err)) {
328 $self->{ObjectCount}{$class}++;
329 $self->Resolve( $uid => $class, $id );
331 # Load it back to get real values into the columns
332 $obj = $class->new( RT->SystemUser );
334 $obj->PostInflate( $self );
343 no warnings 'redefine';
344 local *RT::Ticket::Load = sub {
347 $self->LoadById( $id );
351 my $loaded = Storable::fd_retrieve($fh);
353 # Metadata is stored at the start of the stream as a hashref
354 if (ref $loaded eq "HASH") {
355 $self->LoadMetadata( $loaded );
360 my ($class, $uid, $data) = @{$loaded};
362 if ($self->{Incremental}) {
363 my $obj = $class->new( RT->SystemUser );
364 $obj->Load( $data->{id} );
366 # undef $uid means "delete it"
368 $self->{ObjectCount}{$class}++;
369 } elsif ( $obj->Id ) {
370 # If it exists, update it
371 $class->RT::Record::PreInflate( $self, $uid, $data );
372 $obj->__Set( Field => $_, Value => $data->{$_} )
374 $self->{ObjectCount}{$class}++;
377 $obj = $self->Create( $class, $uid, $data );
379 $self->{Progress}->($obj) if $obj and $self->{Progress};
381 } elsif ($self->{Clone}) {
382 my $obj = $self->Create( $class, $uid, $data );
383 $self->{Progress}->($obj) if $obj and $self->{Progress};
387 # If it's a queue, store its ID away, as we'll need to know
388 # it to split global CFs into non-global across those
389 # fields. We do this before inflating, so that queues which
390 # got merged still get the CFs applied
391 push @{$self->{NewQueues}}, $uid
392 if $class eq "RT::Queue";
394 my $origid = $data->{id};
395 my $obj = $self->Create( $class, $uid, $data );
398 # If it's a ticket, we might need to create a
399 # TicketCustomField for the previous ID
400 if ($class eq "RT::Ticket" and $self->{OriginalId}) {
401 my ($id, $msg) = $obj->AddCustomFieldValue(
402 Field => $self->{OriginalId},
403 Value => $self->Organization . ":$origid",
404 RecordTransaction => 0,
406 warn "Failed to add custom field to $uid: $msg"
410 # If it's a CF, we don't know yet if it's global (the OCF
411 # hasn't been created yet) to store away the CF for later
413 push @{$self->{NewCFs}}, $uid
414 if $class eq "RT::CustomField"
415 and $obj->LookupType =~ /^RT::Queue/;
417 $self->{Progress}->($obj) if $self->{Progress};
423 $self->{Progress}->(undef, 'force') if $self->{Progress};
425 return if $self->{Clone};
427 # Take global CFs which we made and make them un-global
428 my @queues = grep {$_} map {$self->LookupObj( $_ )} @{$self->{NewQueues}};
429 for my $obj (map {$self->LookupObj( $_ )} @{$self->{NewCFs}}) {
430 my $ocf = $obj->IsApplied( 0 ) or next;
432 $obj->AddToObject( $_ ) for @queues;
434 $self->{NewQueues} = [];
435 $self->{NewCFs} = [];
441 return %{ $self->{ObjectCount} };
446 return wantarray ? sort keys %{ $self->{Pending} }
447 : keys %{ $self->{Pending} };
452 return wantarray ? sort { $a->{uid} cmp $b->{uid} } @{ $self->{Invalid} }
458 return $self->{Organization};
463 return defined $self->{Progress} unless @_;
464 return $self->{Progress} = $_[0];