From 6c0a378d10a98862d731fad02a65cc93095c1f30 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Mon, 13 Jun 2022 16:15:36 +0200 Subject: [PATCH] Bug 27421: Commit and revert Signed-off-by: Nick Clemens --- Koha/BackgroundJob.pm | 2 + Koha/BackgroundJob/MARCImportCommitBatch.pm | 110 ++++++++++ Koha/BackgroundJob/MARCImportRevertBatch.pm | 110 ++++++++++ .../apache-shared-intranet-plack.conf | 1 - .../marc_import_commit_batch.inc | 42 ++++ .../marc_import_revert_batch.inc | 38 ++++ .../background_jobs/stage_marc_for_import.inc | 3 + .../prog/en/modules/admin/background_jobs.tt | 4 + .../en/modules/tools/manage-marc-import.tt | 123 ++++------- tools/manage-marc-import.pl | 201 ++++-------------- 10 files changed, 398 insertions(+), 236 deletions(-) create mode 100644 Koha/BackgroundJob/MARCImportCommitBatch.pm create mode 100644 Koha/BackgroundJob/MARCImportRevertBatch.pm create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_commit_batch.inc create mode 100644 koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_revert_batch.inc diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index b968eb23b0..d146c38aad 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -407,6 +407,8 @@ sub core_types_to_classes { update_elastic_index => 'Koha::BackgroundJob::UpdateElasticIndex', update_holds_queue_for_biblios => 'Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue', stage_marc_for_import => 'Koha::BackgroundJob::StageMARCForImport', + marc_import_commit_batch => 'Koha::BackgroundJob::MARCImportCommitBatch', + marc_import_revert_batch => 'Koha::BackgroundJob::MARCImportRevertBatch', }; } diff --git a/Koha/BackgroundJob/MARCImportCommitBatch.pm b/Koha/BackgroundJob/MARCImportCommitBatch.pm new file mode 100644 index 0000000000..3febc91b51 --- /dev/null +++ b/Koha/BackgroundJob/MARCImportCommitBatch.pm @@ -0,0 +1,110 @@ +package Koha::BackgroundJob::MARCImportCommitBatch; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Try::Tiny; + +use base 'Koha::BackgroundJob'; + +use C4::ImportBatch qw( + BatchCommitRecords +); + +=head1 NAME + +Koha::BackgroundJob::MARCImportCommitBatch - Commit records + +This is a subclass of Koha::BackgroundJob. + +=head1 API + +=head2 Class methods + +=head3 job_type + +Define the job type of this job: marc_import_commit_batch + +=cut + +sub job_type { + return 'marc_import_commit_batch'; +} + +=head3 process + +Commit the records + +=cut + +sub process { + my ( $self, $args ) = @_; + + $self->start; + + my $import_batch_id = $args->{import_batch_id}; + my $frameworkcode = $args->{frameworkcode}; + + my @messages; + my $job_progress = 0; + + my ( $num_added, $num_updated, $num_items_added, + $num_items_replaced, $num_items_errored, $num_ignored ); + try { + ( + $num_added, $num_updated, $num_items_added, + $num_items_replaced, $num_items_errored, $num_ignored + ) + = BatchCommitRecords( $import_batch_id, $frameworkcode, 50, + sub { my $job_progress = shift; $self->progress( $job_progress )->store } ); + } + catch { + warn $_; + die "Something terrible has happened!" + if ( $_ =~ /Rollback failed/ ); # Rollback failed + }; + + my $report = { + num_added => $num_added, + num_updated => $num_updated, + num_items_added => $num_items_added, + num_items_replaced => $num_items_replaced, + num_items_errored => $num_items_errored, + num_ignored => $num_ignored + }; + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; + + $self->finish($data); +} + +=head3 enqueue + +Enqueue the new job + +=cut + +sub enqueue { + my ( $self, $args) = @_; + + $self->SUPER::enqueue({ + job_size => 0, # unknown for now + job_args => $args + }); +} + +1; diff --git a/Koha/BackgroundJob/MARCImportRevertBatch.pm b/Koha/BackgroundJob/MARCImportRevertBatch.pm new file mode 100644 index 0000000000..a1cdaeb28b --- /dev/null +++ b/Koha/BackgroundJob/MARCImportRevertBatch.pm @@ -0,0 +1,110 @@ +package Koha::BackgroundJob::MARCImportRevertBatch; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; +use Try::Tiny; + +use base 'Koha::BackgroundJob'; + +use C4::ImportBatch qw( + BatchRevertRecords +); + +=head1 NAME + +Koha::BackgroundJob::MARCImportRevertBatch - Revert a batch + +This is a subclass of Koha::BackgroundJob. + +=head1 API + +=head2 Class methods + +=head3 job_type + +Define the job type of this job: marc_import_revert_batch + +=cut + +sub job_type { + return 'marc_import_revert_batch'; +} + +=head3 process + +Revert a batch + +=cut + +sub process { + my ( $self, $args ) = @_; + + $self->start; + + my $import_batch_id = $args->{import_batch_id}; + + my @messages; + my $job_progress = 0; + my ( + $num_deleted, $num_errors, $num_reverted, + $num_items_deleted, $num_ignored + ); + + try { + ( + $num_deleted, $num_errors, $num_reverted, + $num_items_deleted, $num_ignored + ) = BatchRevertRecords( $import_batch_id, 50, + sub { my $job_progress = shift; $self->progress( $job_progress )->store } ); + } + catch { + warn $_; + die "Something terrible has happened!" + if ( $_ =~ /Rollback failed/ ); # Rollback failed + }; + + my $report = { + num_deleted => $num_deleted, + num_items_deleted => $num_items_deleted, + num_errors => $num_errors, + num_reverted => $num_reverted, + num_ignored => $num_ignored, + }; + + my $data = $self->decoded_data; + $data->{messages} = \@messages; + $data->{report} = $report; + + $self->finish($data); +} + +=head3 enqueue + +Enqueue the new job + +=cut + +sub enqueue { + my ( $self, $args) = @_; + + $self->SUPER::enqueue({ + job_size => 0, # unknown for now + job_args => $args + }); +} + +1; diff --git a/debian/templates/apache-shared-intranet-plack.conf b/debian/templates/apache-shared-intranet-plack.conf index 8428e03a50..b09b62cb6e 100644 --- a/debian/templates/apache-shared-intranet-plack.conf +++ b/debian/templates/apache-shared-intranet-plack.conf @@ -14,7 +14,6 @@ ProxyPass "/cgi-bin/koha/offline_circ/process_koc.pl" "!" ProxyPass "/cgi-bin/koha/tools/background-job-progress.pl" "!" ProxyPass "/cgi-bin/koha/tools/export.pl" "!" - ProxyPass "/cgi-bin/koha/tools/manage-marc-import.pl" "!" ProxyPass "/cgi-bin/koha/tools/upload-cover-image.pl" "!" ProxyPass "/cgi-bin/koha/svc/cataloguing/metasearch" "!" diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_commit_batch.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_commit_batch.inc new file mode 100644 index 0000000000..ff32dbcc56 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_commit_batch.inc @@ -0,0 +1,42 @@ +[% USE Koha %] + +[% BLOCK report %] + [% SET report = job.report %] + [% IF report %] +
Completed import of records
+ + + + + + + + + + + + + + [% IF ( report.record_type == 'biblio' ) %] + + + + + + + + + + + + + [% END %] +
Number of records added[% report.num_added | html %]
Number of records updated[% report.num_updated | html %]
Number of records ignored[% report.num_ignored | html %]
Number of items added[% report.num_items_added | html %]
Number of items replaced[% report.num_items_replaced | html %]
Number of items ignored because of duplicate barcode[% report.num_items_errored | html %]
+ [% END %] +[% END %] + +[% BLOCK detail %] +[% END %] + +[% BLOCK js %] +[% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_revert_batch.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_revert_batch.inc new file mode 100644 index 0000000000..2ad8117ded --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/marc_import_revert_batch.inc @@ -0,0 +1,38 @@ +[% USE Koha %] + +[% BLOCK report %] + [% SET report = job.report %] + [% IF report %] +
Success: Import reversed
+ + + + + + [% IF ( report.record_type == 'biblio' ) %] + + + + + + + + + [% END %] + + + + + + + + +
Number of records deleted[% report.num_deleted | html %]
Number of items deleted[% report.num_items_deleted | html %]
Number of records not deleted due to items on loan[% report.num_errors | html %]
Number of records changed back[% report.num_reverted | html %]
Number of records ignored[% report.num_ignored | html %]
+ [% END %] +[% END %] + +[% BLOCK detail %] +[% END %] + +[% BLOCK js %] +[% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/stage_marc_for_import.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/stage_marc_for_import.inc index 11cde88c1f..0347d2d37f 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/stage_marc_for_import.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/background_jobs/stage_marc_for_import.inc @@ -31,6 +31,9 @@
  • New label batch created: # [% report.label_batch | html %]
  • [% END %] +

    + View batch +

    [% IF report.basketno && report.booksellerid %]

    Add staged files to basket diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt index 637683d61b..0da09327d3 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt @@ -43,6 +43,10 @@ Holds queue update [% CASE 'stage_marc_for_import' %] Staged MARC records for import + [% CASE 'marc_import_commit_batch' %] + Import MARC records + [% CASE 'marc_import_revert_batch' %] + Revert import MARC records [% CASE %]Unknown job type '[% job_type | html %]' [% END %] diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt index 7cac215413..2efba77b09 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt @@ -31,7 +31,7 @@ [% IF ( import_batch_id ) %]

  • - Manage staged MARC records + Manage staged MARC records
  • @@ -57,8 +57,30 @@ › Batch [% import_batch_id | html %] [% END %] + [% FOREACH message IN messages %] + [% IF message.type == 'success' %] +
    + [% ELSIF message.type == 'warning' %] +
    + [% ELSIF message.type == 'error' %] +
    + [% END %] + [% IF message.code == 'cannot_enqueue_job' %] + Cannot enqueue this job. + [% END %] + [% IF message.error %] + (The error was: [% message.error | html %], see the Koha log file for more information). + [% END %] +
    + [% END %] + + [% IF job_enqueued %] +
    - [% IF ( label_batch_msg ) %] + [% ELSIF ( label_batch_msg ) %] [% IF ( alert ) %]
    [% ELSE %] @@ -82,18 +104,16 @@
    Import batch deleted successfully
    [% END %] - [% UNLESS ( batch_list ) %] - [% UNLESS ( batch_info ) %] -
    -

    No records have been staged.

    -

    Stage MARC records for import.

    -
    - [% END %] + [% UNLESS batch_list || batch_info || job_enqueued %] +
    +

    No records have been staged.

    +

    Stage MARC records for import.

    +
    [% END %] [% IF ( batch_info ) %] [% IF ( can_commit ) %] -
    + @@ -121,6 +141,7 @@
    Changed item processing option
    [% END %] + [% UNLESS job_enqueued %]
    1. File name: [% file_name | html %]
    2. @@ -236,7 +257,7 @@
      [% IF ( can_commit ) %] - + @@ -261,7 +282,7 @@
      [% END # /IF can_commit %] [% IF ( can_revert ) %] - + @@ -274,66 +295,7 @@
    [% END # /IF can_revert %]
    - - [% IF ( did_commit ) %] -
    Completed import of records
    - - - - - - - - - - - - - - [% IF ( record_type == 'biblio' ) %] - - - - - - - - - - - - - [% END %] -
    Number of records added[% num_added | html %]
    Number of records updated[% num_updated | html %]
    Number of records ignored[% num_ignored | html %]
    Number of items added[% num_items_added | html %]
    Number of items replaced[% num_items_replaced | html %]
    Number of items ignored because of duplicate barcode[% num_items_errored | html %]
    - [% END #/ IF did_commit %] - - [% IF ( did_revert ) %] -
    Success: Import reversed
    - - - - - - [% IF ( record_type == 'biblio' ) %] - - - - - - - - - [% END %] - - - - - - - - -
    Number of records deleted[% num_deleted | html %]
    Number of items deleted[% num_items_deleted | html %]
    Number of records not deleted due to items on loan[% num_errors | html %]
    Number of records changed back[% num_reverted | html %]
    Number of records ignored[% num_ignored | html %]
    - [% END # /IF did_revert%] + [% END %] [% END # /IF batch_info %] @@ -360,7 +322,7 @@ [% FOREACH batch_lis IN batch_list %] [% batch_lis.import_batch_id | html %] - [% batch_lis.file_name | html %] + [% batch_lis.file_name | html %] [% batch_lis.profile | html %] [% batch_lis.comments | html %] [% IF ( batch_lis.record_type == 'auth' ) %]Authority[% ELSE %]Bibliographic[% END %] @@ -385,12 +347,12 @@ [% batch_lis.num_records | html %] [% batch_lis.num_items | html %] [% IF ( batch_lis.num_items && batch_lis.import_status == 'imported' ) %] - (Create label batch) + (Create label batch) [% END %] [% IF ( batch_lis.can_clean ) %] - + @@ -457,7 +419,6 @@ [% MACRO jsinclude BLOCK %] [% Asset.js("js/tools-menu.js") | $raw %] [% Asset.js("lib/jquery/plugins/humanmsg.js") | $raw %] - [% Asset.js("js/background-job-progressbar.js") | $raw %] [% INCLUDE 'datatables.inc' %]