Bug 22417: Remove $dbh in Koha::BackgroundJob::*
[koha.git] / Koha / BackgroundJob / BatchUpdateAuthority.pm
blob24f1e9a400037f8e6ed33fd5fa272c0e89243235
1 package Koha::BackgroundJob::BatchUpdateAuthority;
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 C4::MarcModificationTemplates;
22 use C4::AuthoritiesMarc;
23 use Koha::BackgroundJobs;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::MetadataRecord::Authority;
27 use base 'Koha::BackgroundJob';
29 =head1 NAME
31 Koha::BackgroundJob::BatchUpdateAuthority - Batch update authorities
33 This is a subclass of Koha::BackgroundJob.
35 =head1 API
37 =head2 Class methods
39 =head3 job_type
41 Define the job type of this job: batch_authority_record_modification
43 =cut
45 sub job_type {
46 return 'batch_authority_record_modification';
49 =head3 process
51 Process the modification.
53 =cut
55 sub process {
56 my ( $self, $args ) = @_;
58 my $job = Koha::BackgroundJobs->find( $args->{job_id} );
60 if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
61 return;
64 my $job_progress = 0;
65 $job->started_on(dt_from_string)
66 ->progress($job_progress)
67 ->status('started')
68 ->store;
70 my $mmtid = $args->{mmtid};
71 my @record_ids = @{ $args->{record_ids} };
73 my $report = {
74 total_records => scalar @record_ids,
75 total_success => 0,
77 my @messages;
78 RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
79 next unless $record_id;
80 # Authorities
81 my $authid = $record_id;
82 my $error = eval {
83 my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
84 my $record = $authority->record;
85 ModifyRecordWithTemplate( $mmtid, $record );
86 ModAuthority( $authid, $record, $authority->authtypecode );
88 if ( $error and $error != $authid or $@ ) {
89 push @messages, {
90 type => 'error',
91 code => 'authority_not_modified',
92 authid => $authid,
93 error => ($@ ? $@ : 0),
95 } else {
96 push @messages, {
97 type => 'success',
98 code => 'authority_modified',
99 authid => $authid,
101 $report->{total_success}++;
103 $job->progress( ++$job_progress )->store;
106 my $job_data = decode_json $job->data;
107 $job_data->{messages} = \@messages;
108 $job_data->{report} = $report;
110 $job->ended_on(dt_from_string)
111 ->data(encode_json $job_data);
112 $job->status('finished') if $job->status ne 'cancelled';
113 $job->store;
117 =head3 enqueue
119 Enqueue the new job
121 =cut
123 sub enqueue {
124 my ( $self, $args) = @_;
126 # TODO Raise exception instead
127 return unless exists $args->{mmtid};
128 return unless exists $args->{record_ids};
130 my $mmtid = $args->{mmtid};
131 my @record_ids = @{ $args->{record_ids} };
133 $self->SUPER::enqueue({
134 job_size => scalar @record_ids,
135 job_args => {mmtid => $mmtid, record_ids => \@record_ids,}