Bug 22417: Remove $dbh in Koha::BackgroundJob::*
[koha.git] / Koha / BackgroundJob / BatchUpdateBiblio.pm
blob7cb7969474df9ada10b2dbecd4ccecdfc98c675d
1 package Koha::BackgroundJob::BatchUpdateBiblio;
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
19 use JSON qw( encode_json decode_json );
21 use Koha::BackgroundJobs;
22 use Koha::DateUtils qw( dt_from_string );
23 use C4::Biblio;
24 use C4::MarcModificationTemplates;
26 use base 'Koha::BackgroundJob';
28 =head1 NAME
30 Koha::BackgroundJob::BatchUpdateBiblio - Batch update bibliographic records
32 This is a subclass of Koha::BackgroundJob.
34 =head1 API
36 =head2 Class methods
38 =head3 job_type
40 Define the job type of this job: batch_biblio_record_modification
42 =cut
44 sub job_type {
45 return 'batch_biblio_record_modification';
48 =head3 process
50 Process the modification.
52 =cut
54 sub process {
55 my ( $self, $args ) = @_;
57 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
59 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
60 return;
63 # FIXME If the job has already been started, but started again (worker has been restart for instance)
64 # Then we will start from scratch and so double process the same records
66 my $job_progress = 0;
67 $job->started_on(dt_from_string)
68 ->progress($job_progress)
69 ->status('started')
70 ->store;
72 my $mmtid = $args->{mmtid};
73 my @record_ids = @{ $args->{record_ids} };
75 my $report = {
76 total_records => scalar @record_ids,
77 total_success => 0,
79 my @messages;
80 RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
82 last if $job->get_from_storage->status eq 'cancelled';
84 next unless $biblionumber;
86 # Modify the biblio
87 my $error = eval {
88 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
89 C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
90 my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
91 C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
93 if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
94 push @messages, {
95 type => 'error',
96 code => 'biblio_not_modified',
97 biblionumber => $biblionumber,
98 error => ($@ ? $@ : $error),
100 } else {
101 push @messages, {
102 type => 'success',
103 code => 'biblio_modified',
104 biblionumber => $biblionumber,
106 $report->{total_success}++;
108 $job->progress( ++$job_progress )->store;
111 my $job_data = decode_json $job->data;
112 $job_data->{messages} = \@messages;
113 $job_data->{report} = $report;
115 $job->ended_on(dt_from_string)
116 ->data(encode_json $job_data);
117 $job->status('finished') if $job->status ne 'cancelled';
118 $job->store;
121 =head3 enqueue
123 Enqueue the new job
125 =cut
127 sub enqueue {
128 my ( $self, $args) = @_;
130 # TODO Raise exception instead
131 return unless exists $args->{mmtid};
132 return unless exists $args->{record_ids};
134 my $mmtid = $args->{mmtid};
135 my @record_ids = @{ $args->{record_ids} };
137 $self->SUPER::enqueue({
138 job_size => scalar @record_ids,
139 job_args => {mmtid => $mmtid, record_ids => \@record_ids,}