Bug 30943: Adjust CreateEHoldingsFromBiblios
[koha.git] / Koha / BackgroundJob / MARCImportRevertBatch.pm
1 package Koha::BackgroundJob::MARCImportRevertBatch;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19 use Try::Tiny;
20
21 use base 'Koha::BackgroundJob';
22
23 use C4::ImportBatch qw(
24     BatchRevertRecords
25 );
26 use Koha::Database;
27 use Koha::Import::Records;
28
29 =head1 NAME
30
31 Koha::BackgroundJob::MARCImportRevertBatch - Revert a batch
32
33 This is a subclass of Koha::BackgroundJob.
34
35 =head1 API
36
37 =head2 Class methods
38
39 =head3 job_type
40
41 Define the job type of this job: marc_import_revert_batch
42
43 =cut
44
45 sub job_type {
46     return 'marc_import_revert_batch';
47 }
48
49 =head3 process
50
51 Revert a batch
52
53 =cut
54
55 sub process {
56     my ( $self, $args ) = @_;
57
58     $self->start;
59
60     my $import_batch_id = $args->{import_batch_id};
61
62     my @messages;
63     my $job_progress = 0;
64     my (
65         $num_deleted,       $num_errors, $num_reverted,
66         $num_items_deleted, $num_ignored
67     );
68
69     my $schema = Koha::Database->new->schema;
70     try {
71         $schema->storage->txn_begin;
72         ( $num_deleted, $num_errors, $num_reverted, $num_items_deleted, $num_ignored ) =
73           BatchRevertRecords( $import_batch_id ); # TODO BatchRevertRecords still needs a progress_callback
74         $schema->storage->txn_commit;
75
76         my $count = $num_deleted + $num_reverted;
77         if( $count ) {
78             $self->set({ progress => $count, size => $count });
79         } else { # TODO Nothing happened? Refine later
80             $self->set({ progress => 0, status => 'failed' });
81         }
82     }
83     catch {
84         warn $_;
85         $schema->storage->txn_rollback;
86         $self->set({ progress => 0, status => 'failed' });
87     };
88
89     my $report = {
90         num_deleted       => $num_deleted,
91         num_items_deleted => $num_items_deleted,
92         num_errors        => $num_errors,
93         num_reverted      => $num_reverted,
94         num_ignored       => $num_ignored,
95         import_batch_id   => $import_batch_id,
96     };
97
98     my $data = $self->decoded_data;
99     $data->{messages} = \@messages;
100     $data->{report}   = $report;
101
102     $self->finish($data);
103 }
104
105 =head3 enqueue
106
107 Enqueue the new job
108
109 =cut
110
111 sub enqueue {
112     my ( $self, $args) = @_;
113
114     $self->SUPER::enqueue({
115         job_size  => Koha::Import::Records->search({ import_batch_id => $args->{import_batch_id} })->count,
116         job_args  => $args,
117         job_queue => 'long_tasks',
118     });
119 }
120
121 1;