Bug 33974: (QA follow-up) Remove superflous import
[koha.git] / Koha / BackgroundJob / BatchUpdateAuthority.pm
1 package Koha::BackgroundJob::BatchUpdateAuthority;
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
20 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
21 use C4::AuthoritiesMarc qw( ModAuthority );
22 use Koha::BackgroundJobs;
23 use Koha::MetadataRecord::Authority;
24 use Koha::SearchEngine;
25 use Koha::SearchEngine::Indexer;
26
27 use base 'Koha::BackgroundJob';
28
29 =head1 NAME
30
31 Koha::BackgroundJob::BatchUpdateAuthority - Batch update authorities
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: batch_authority_record_modification
42
43 =cut
44
45 sub job_type {
46     return 'batch_authority_record_modification';
47 }
48
49 =head3 process
50
51 Process the modification.
52
53 =cut
54
55 sub process {
56     my ( $self, $args ) = @_;
57
58     if ( $self->status eq 'cancelled' ) {
59         return;
60     }
61
62     $self->start;
63
64     my $mmtid = $args->{mmtid};
65     my @record_ids = @{ $args->{record_ids} };
66
67     my $report = {
68         total_records => scalar @record_ids,
69         total_success => 0,
70     };
71     my @messages;
72     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
73         next unless $record_id;
74         # Authorities
75         my $authid = $record_id;
76         my $error = eval {
77             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
78             my $record = $authority->record;
79             ModifyRecordWithTemplate( $mmtid, $record );
80             ModAuthority( $authid, $record, $authority->authtypecode, { skip_record_index => 1 } );
81         };
82         if ( $error and $error != $authid or $@ ) {
83             push @messages, {
84                 type => 'error',
85                 code => 'authority_not_modified',
86                 authid => $authid,
87                 error => ($@ ? "$@" : 0),
88             };
89         } else {
90             push @messages, {
91                 type => 'success',
92                 code => 'authority_modified',
93                 authid => $authid,
94             };
95             $report->{total_success}++;
96         }
97         $self->step;
98     }
99
100     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
101     $indexer->index_records( \@record_ids, "specialUpdate", "authorityserver" );
102
103     my $data = $self->decoded_data;
104     $data->{messages} = \@messages;
105     $data->{report}   = $report;
106
107     $self->finish( $data );
108
109 }
110
111 =head3 enqueue
112
113 Enqueue the new job
114
115 =cut
116
117 sub enqueue {
118     my ( $self, $args) = @_;
119
120     # TODO Raise exception instead
121     return unless exists $args->{mmtid};
122     return unless exists $args->{record_ids};
123
124     my $mmtid = $args->{mmtid};
125     my @record_ids = @{ $args->{record_ids} };
126
127     $self->SUPER::enqueue({
128         job_size  => scalar @record_ids,
129         job_args  => {mmtid => $mmtid, record_ids => \@record_ids,},
130         job_queue => 'long_tasks',
131     });
132 }
133
134 1;