Skip to content

Commit

Permalink
Merge pull request #1220 from RotherOSS/issue-#883-OTOBOItsmTablesMig…
Browse files Browse the repository at this point in the history
…rate

Issue #883 otobo itsm tables migrate
  • Loading branch information
bschmalhofer committed Sep 10, 2021
2 parents 02e3c2e + 0cef21e commit 74c2e9e
Show file tree
Hide file tree
Showing 4 changed files with 233 additions and 222 deletions.
38 changes: 20 additions & 18 deletions Kernel/Modules/MigrateFromOTRS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ sub Run {
Task => 'OTOBOOTRSConnectionCheck',
UserID => 1,
OTRSData => \%GetParam,
)->{OTOBOOTRSConnectionCheck};
);
}
elsif ( $Self->{Subaction} eq 'OTRSDBSettings' && $AJAXTask eq 'CheckSettings' ) {
my %GetParam;
Expand Down Expand Up @@ -180,7 +180,7 @@ sub Run {
Task => 'OTOBOOTRSDBCheck',
UserID => 1,
DBData => \%GetParam,
)->{OTOBOOTRSDBCheck};
);
}
}
elsif ( $Self->{Subaction} eq 'PreChecks' || $Self->{Subaction} eq 'Copy' ) {
Expand All @@ -201,6 +201,7 @@ sub Run {
OTOBOMigrateConfigFromOTRS
OTOBONotificationMigrate
OTOBOStatsMigrate
OTOBOItsmTablesMigrate
OTOBOAutoResponseTemplatesMigrate
OTOBOResponseTemplatesMigrate
OTOBOSalutationsMigrate
Expand Down Expand Up @@ -248,10 +249,10 @@ sub Run {
PackageResolve => $Resolve // {},
);
};
if ( !$Result || !defined $Result->{$PerlTask}->{Successful} ) {
$Result->{$PerlTask}->{Successful} = 0;
$Result->{$PerlTask}->{Message} = $AJAXTask;
$Result->{$PerlTask}->{Comment} = 'A fatal error occured.';
if ( !$Result || !defined $Result->{Successful} ) {
$Result->{Successful} = 0;
$Result->{Message} = $AJAXTask;
$Result->{Comment} = 'A fatal error occured.';

$Kernel::OM->Get('Kernel::System::Log')->Log(
Priority => 'error',
Expand All @@ -260,15 +261,15 @@ sub Run {
}

# check if progress update has to be stopped
if ( $Self->{Subaction} eq 'Copy' && ( !$NextTask{$AJAXTask} || !$Result->{$PerlTask}->{Successful} ) ) {
if ( $Self->{Subaction} eq 'Copy' && ( !$NextTask{$AJAXTask} || !$Result->{Successful} ) ) {
my $Status = $CacheObject->Delete(
Type => 'OTRSMigration',
Key => 'MigrationState',
);
}

# store next task in cache in case of a restart
if ( $Self->{Subaction} eq 'Copy' && $Result->{$PerlTask}->{Successful} ) {
if ( $Self->{Subaction} eq 'Copy' && $Result->{Successful} ) {
$CacheObject->Set(
Type => 'OTRSMigration',
Key => 'Copy',
Expand All @@ -280,7 +281,7 @@ sub Run {
}

$Return = {
%{ $Result->{$PerlTask} },
$Result->%*,
NextTask => $NextTask{$AJAXTask},
};
}
Expand Down Expand Up @@ -500,9 +501,6 @@ sub Run {
}

# generate HTML for the current step
my $Output = $LayoutObject->Header(
Title => "$Title - $CurrentSubtitle",
);
$LayoutObject->Block(
Name => $Self->{Subaction},
Data => {
Expand All @@ -519,13 +517,16 @@ sub Run {
},
);
}
$Output .= $LayoutObject->Output(
TemplateFile => 'MigrateFromOTRS',
Data => {},
);
$Output .= $LayoutObject->Footer();

return $Output;
return join '',
$LayoutObject->Header(
Title => "$Title - $CurrentSubtitle",
),
$LayoutObject->Output(
TemplateFile => 'MigrateFromOTRS',
Data => {},
),
$LayoutObject->Footer();
}

sub _Finish {
Expand Down Expand Up @@ -649,6 +650,7 @@ sub _CheckConfig {

my $Home = $Kernel::OM->Get('Kernel::Config')->Get('Home');

# TODO: is this still needed? ConfigurationXML2DB is already called on OTOBOMigrateConfigFromOTRS
return $SysConfigObject->ConfigurationXML2DB(
UserID => 1,
Directory => "$Home/Kernel/Config/Files/XML",
Expand Down
130 changes: 24 additions & 106 deletions Kernel/System/MigrateFromOTRS.pm
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use utf8;

our @ObjectDependencies = (
'Kernel::System::Main',
'Kernel::System::MigrateFromOTRS::Base',
'Kernel::System::Log',
);

Expand Down Expand Up @@ -69,6 +68,8 @@ run migration task
sub Run {
my ( $Self, %Param ) = @_;

my %ResultFail = ( Successful => 0 );

# check needed stuff
for my $Needed (qw(Task UserID)) {
if ( !$Param{$Needed} ) {
Expand All @@ -77,131 +78,48 @@ sub Run {
Message => "Need $Needed!",
);

return;
}
}

# don't attempt to run when the pre check failed
return unless $Self->_ExecutePreCheck(%Param);

return $Self->_ExecuteRun(%Param);
}

sub _ExecutePreCheck {
my ( $Self, %Param ) = @_;

# determine the relevant tasks
my @Tasks;
{
my @AllTasks = grep { $_ && $_->{Module} } $Self->_TasksGet();

if ( $Param{Task} eq 'All' ) {
@Tasks = @AllTasks;
}
else {
@Tasks = grep { $_->{Module} eq $Param{Task} } @AllTasks;
return \%ResultFail;
}
}

if ( !@Tasks ) {
print STDERR "No valid Module $Param{Task} found. ",
q{Perhaps you need to add the new check to $Self->_TasksGet().};
my $Task = $Param{Task};
my $ModuleName = "Kernel::System::MigrateFromOTRS::$Task";

return 0;
}
return \%ResultFail unless $Kernel::OM->Get('Kernel::System::Main')->Require($ModuleName);

my $IsOK = 0;
my $TaskObject = $Kernel::OM->Create($ModuleName);

TASK:
for my $Task (@Tasks) {
return \%ResultFail unless $TaskObject;

my $ModuleName = "Kernel::System::MigrateFromOTRS::$Task->{Module}";
# don't attempt to run when the pre check failed
return \%ResultFail unless $Self->_ExecutePreCheck( %Param, TaskObject => $TaskObject );

# NOTE: This looks strange. When one check succeeded and next check can't be loaded that the
# check succeeds altogether.
last TASK unless $Kernel::OM->Get('Kernel::System::Main')->Require($ModuleName);
return $Self->_ExecuteRun( %Param, TaskObject => $TaskObject );
}

# NOTE: see the note above
$Self->{TaskObjects}->{$ModuleName} //= $Kernel::OM->Create($ModuleName);
sub _ExecutePreCheck {
my ( $Self, %Param ) = @_;

last TASK unless $Self->{TaskObjects}->{$ModuleName};
my $TaskObject = delete $Param{TaskObject};

# Execute previous check, printing a different message
# NOTE: when last check succeeds the the check succeeds altogether
if ( $Self->{TaskObjects}->{$ModuleName}->can('CheckPreviousRequirement') ) {
$IsOK = $Self->{TaskObjects}->{$ModuleName}->CheckPreviousRequirement(%Param);
}
}
# successful per default, CheckPreviousRequirement() is not a required method
return 1 unless $TaskObject->can('CheckPreviousRequirement');

return $IsOK;
return $TaskObject->CheckPreviousRequirement(%Param);
}

sub _ExecuteRun {
my ( $Self, %Param ) = @_;

# determine the relevant tasks
my @Tasks;
{
my @AllTasks = grep { $_ && $_->{Module} } $Self->_TasksGet();

if ( $Param{Task} eq 'All' ) {
@Tasks = @AllTasks;
}
else {
@Tasks = grep { $_->{Module} eq $Param{Task} } @AllTasks;
}
}

if ( !@Tasks ) {

return {
Message => "invalid task $Param{Task}",
Comment => "No valid Module $Param{Task} found. "
. qq{Perhaps you need to add the new check to $Self->_TasksGet().},
Successful => 0,
};
}

my $CurrentStep = 1;
my %Result;

TASK:
for my $Task (@Tasks) {

my $ModuleName = "Kernel::System::MigrateFromOTRS::$Task->{Module}";

last TASK unless $Kernel::OM->Get('Kernel::System::Main')->Require($ModuleName);

$Self->{TaskObjects}->{$ModuleName} //= $Kernel::OM->Create($ModuleName);

last TASK unless $Self->{TaskObjects}->{$ModuleName};
my $TaskObject = delete $Param{TaskObject};

# Execute Run-Component
if ( $Self->{TaskObjects}->{$ModuleName}->can('Run') ) {

$Result{ $Task->{Module} } = $Self->{TaskObjects}->{$ModuleName}->Run(%Param);

# Add counter to $Result.
$Result{ $Task->{Module} }->{CurrentStep} = $CurrentStep;
}

# Do not handle Run if task has no appropriate method.
else {
next TASK;
}

$CurrentStep++;
}

return \%Result;
}

sub _TasksGet {
my ( $Self, %Param ) = @_;
my %ResultFail = ( Successful => 0 );

my $MigrationBaseObject = $Kernel::OM->Get('Kernel::System::MigrateFromOTRS::Base');
# a migration step must have a Run-method
return \%ResultFail unless $TaskObject->can('Run');

return $MigrationBaseObject->TaskSecurityCheck();
# Execute Run-Component
return $TaskObject->Run(%Param);
}

1;
Loading

0 comments on commit 74c2e9e

Please sign in to comment.