Bug 27783: Rename queues and adjust currently defined jobs
[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 use JSON qw( decode_json encode_json );
20
21 use C4::MarcModificationTemplates qw( ModifyRecordWithTemplate );
22 use C4::AuthoritiesMarc;
23 use Koha::BackgroundJobs;
24 use Koha::DateUtils qw( dt_from_string );
25 use Koha::MetadataRecord::Authority;
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     my $job_progress = 0;
63     $self->started_on(dt_from_string)
64         ->progress($job_progress)
65         ->status('started')
66         ->store;
67
68     my $mmtid = $args->{mmtid};
69     my @record_ids = @{ $args->{record_ids} };
70
71     my $report = {
72         total_records => scalar @record_ids,
73         total_success => 0,
74     };
75     my @messages;
76     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
77         next unless $record_id;
78         # Authorities
79         my $authid = $record_id;
80         my $error = eval {
81             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
82             my $record = $authority->record;
83             ModifyRecordWithTemplate( $mmtid, $record );
84             ModAuthority( $authid, $record, $authority->authtypecode );
85         };
86         if ( $error and $error != $authid or $@ ) {
87             push @messages, {
88                 type => 'error',
89                 code => 'authority_not_modified',
90                 authid => $authid,
91                 error => ($@ ? "$@" : 0),
92             };
93         } else {
94             push @messages, {
95                 type => 'success',
96                 code => 'authority_modified',
97                 authid => $authid,
98             };
99             $report->{total_success}++;
100         }
101         $self->progress( ++$job_progress )->store;
102     }
103
104     my $job_data = decode_json $self->data;
105     $job_data->{messages} = \@messages;
106     $job_data->{report} = $report;
107
108     $self->ended_on(dt_from_string)
109         ->data(encode_json $job_data);
110     $self->status('finished') if $self->status ne 'cancelled';
111     $self->store;
112
113 }
114
115 =head3 enqueue
116
117 Enqueue the new job
118
119 =cut
120
121 sub enqueue {
122     my ( $self, $args) = @_;
123
124     # TODO Raise exception instead
125     return unless exists $args->{mmtid};
126     return unless exists $args->{record_ids};
127
128     my $mmtid = $args->{mmtid};
129     my @record_ids = @{ $args->{record_ids} };
130
131     $self->SUPER::enqueue({
132         job_size => scalar @record_ids,
133         job_args => {mmtid => $mmtid, record_ids => \@record_ids,},
134         queue    => 'long_tasks',
135     });
136 }
137
138 1;