Skip to content

Commit

Permalink
ConvertTransaction -> import_transaction
Browse files Browse the repository at this point in the history
Much fixups to make moar betterer
  • Loading branch information
afresh1 committed Aug 11, 2012
1 parent 20477e9 commit 6f4fc24
Showing 1 changed file with 53 additions and 44 deletions.
97 changes: 53 additions & 44 deletions rt-ticket-importer
Expand Up @@ -236,69 +236,78 @@ sub import_ticket {
return $newid;
}

sub ConvertTransaction {
sub import_transaction {
my ( $transaction, $ticket_id ) = @_;

my $id = shift;

return $TRANSACTIONS{$id} if ( $TRANSACTIONS{$id} );
my %t;
foreach my $key ( keys %$transaction ) {
my $value = $transaction->{$key};
next unless $value;

my $transaction = $transactions->{$id};
if ( ref $value ) {
next;
}
elsif ( $user_types{$key} ) {
$value = [ grep {$_} map { ConvertUser($_) } split /\s*,\s*/,
$value ];
next unless @{$value};
$value = $value->[0] if @{$value} == 1;
}

my $transactionObj = new RT::Transaction($RT::SystemUser);
$t{$key} = $value;
}

foreach my $key ( keys %$transaction ) {
$transaction->{$key} = undef if ( ref( $transaction->{$key} ) );
if ( $link_types{ $t{Type} } ) {
if ($ticket_id) { # If this is the first time through don't add
queue_link_txn($transaction);
return;
}
$t{OldValue} = $E{Tickets}{ $t{OldValue} };
$t{NewValue} = $E{Tickets}{ $t{NewValue} };
}

# We're cheating and only importing Ticket Transactions
$transaction->{'ObjectId'} = ConvertTicket( $transaction->{'ObjectId'} );
$transaction->{'Creator'} = ConvertUser( $transaction->{'Creator'} );
return undef unless ( defined( $transaction->{'ObjectId'} ) );
if ( $t{Type} eq 'Set' and $t{Field} eq 'Queue' ) {
$t{OldValue} = ConvertQueue( $t{OldValue} );
$t{NewValue} = ConvertQueue( $t{NewValue} );
}

if ( ( $transaction->{'Type'} eq 'Owner' )
|| ( $transaction->{'Type'} eq 'Requestor' )
|| ( $transaction->{'Type'} eq 'Cc' )
|| ( $transaction->{'Type'} eq 'AdminCc' ) )
if ( ( $t{Type} eq 'Set' and $user_types{ $t{Field} } )
or $user_types{ $t{Type} } )
{
$transaction->{'OldValue'}
= ConvertUser( $transaction->{'OldValue'} );
$transaction->{'NewValue'}
= ConvertUser( $transaction->{'NewValue'} );
$t{OldValue} = ConvertUser( $t{OldValue} );
$t{NewValue} = ConvertUser( $t{NewValue} );
}

if ( ( $transaction->{'Type'} eq 'DependsOn' )
|| ( $transaction->{'Type'} eq 'DependedOnBy' )
|| ( $transaction->{'Type'} eq 'RefersTo' )
|| ( $transaction->{'Type'} eq 'ReferredToBy' )
|| ( $transaction->{'Type'} eq 'MemberOf' )
|| ( $transaction->{'Type'} eq 'MergedInto' )
|| ( $transaction->{'Type'} eq 'HasMember' ) )
{
$transaction->{'OldValue'}
= ConvertTicket( $transaction->{'OldValue'} );
$transaction->{'NewValue'}
= ConvertTicket( $transaction->{'NewValue'} );
if ( ( $t{Type} eq 'EmailRecord' ) ) {
$t{OldValue} = $E{Tickets}{ $t{OldValue} } if $t{OldValue};
$t{NewValue} = $E{Tickets}{ $t{NewValue} } if $t{NewValue};
}

$transaction->{'ActivateScrips'} = 0;
$t{ActivateScrips} = 0;

my ( $newid, $msg ) = $transactionObj->Create(%$transaction);
my $transactionObj = new RT::Transaction($RT::SystemUser);
my ( $newid, $msg ) = $transactionObj->Create(%t);

unless ($newid) {
print STDERR "Failed to create Transaction $id ($msg)!!!\n";
return undef;
print $msg;
return;
}

foreach my $custom_field ( @{ $transaction->{CustomFields} } ) {
$transactionObj->AddCustomFieldValue(
Field => $custom_field->{Name},
Value => $custom_field->{Value},
);
}

print "Created Transaction # $id : as # $newid\n";
$| = 1;
$transactionObj->{'_AccessibleCache'}{Created}
= { 'read' => 1, 'write' => 1, 'auto' => 0 };
$transactionObj->{'_AccessibleCache'}{Creator}
= { 'read' => 1, 'write' => 1, 'auto' => 0 };
$transactionObj->SetCreated( $transaction->{'Created'} );
$transactionObj->SetCreator( $transaction->{'Creator'} );
$transactionObj->{_AccessibleCache}{Created}
= { read => 1, write => 1, auto => 0 };
$transactionObj->{_AccessibleCache}{Creator}
= { read => 1, write => 1, auto => 0 };
$transactionObj->SetCreated( $t{Created} );
$transactionObj->SetCreator( $t{Creator} );

$TRANSACTIONS{$id} = $newid;
return $newid;
}

Expand Down

0 comments on commit 6f4fc24

Please sign in to comment.