Bug 31028: Add new Koha::Object(s) classes
[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::DateUtils qw( dt_from_string );
24 use Koha::MetadataRecord::Authority;
25 use Koha::SearchEngine;
26 use Koha::SearchEngine::Indexer;
27
28 use base 'Koha::BackgroundJob';
29
30 =head1 NAME
31
32 Koha::BackgroundJob::BatchUpdateAuthority - Batch update authorities
33
34 This is a subclass of Koha::BackgroundJob.
35
36 =head1 API
37
38 =head2 Class methods
39
40 =head3 job_type
41
42 Define the job type of this job: batch_authority_record_modification
43
44 =cut
45
46 sub job_type {
47     return 'batch_authority_record_modification';
48 }
49
50 =head3 process
51
52 Process the modification.
53
54 =cut
55
56 sub process {
57     my ( $self, $args ) = @_;
58
59     if ( $self->status eq 'cancelled' ) {
60         return;
61     }
62
63     my $job_progress = 0;
64     $self->started_on(dt_from_string)
65         ->progress($job_progress)
66         ->status('started')
67         ->store;
68
69     my $mmtid = $args->{mmtid};
70     my @record_ids = @{ $args->{record_ids} };
71
72     my $report = {
73         total_records => scalar @record_ids,
74         total_success => 0,
75     };
76     my @messages;
77     RECORD_IDS: for my $record_id ( sort { $a <=> $b } @record_ids ) {
78         next unless $record_id;
79         # Authorities
80         my $authid = $record_id;
81         my $error = eval {
82             my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
83             my $record = $authority->record;
84             ModifyRecordWithTemplate( $mmtid, $record );
85             ModAuthority( $authid, $record, $authority->authtypecode, { skip_record_index => 1 } );
86         };
87         if ( $error and $error != $authid or $@ ) {
88             push @messages, {
89                 type => 'error',
90                 code => 'authority_not_modified',
91                 authid => $authid,
92                 error => ($@ ? "$@" : 0),
93             };
94         } else {
95             push @messages, {
96                 type => 'success',
97                 code => 'authority_modified',
98                 authid => $authid,
99             };
100             $report->{total_success}++;
101         }
102         $self->progress( ++$job_progress )->store;
103     }
104
105     my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::AUTHORITIES_INDEX });
106     $indexer->index_records( \@record_ids, "specialUpdate", "authorityserver" );
107
108     my $json = $self->json;
109     my $job_data = $json->decode($self->data);
110     $job_data->{messages} = \@messages;
111     $job_data->{report} = $report;
112
113     $self->ended_on(dt_from_string)
114         ->data($json->encode($job_data));
115     $self->status('finished') if $self->status ne 'cancelled';
116     $self->store;
117
118 }
119
120 =head3 enqueue
121
122 Enqueue the new job
123
124 =cut
125
126 sub enqueue {
127     my ( $self, $args) = @_;
128
129     # TODO Raise exception instead
130     return unless exists $args->{mmtid};
131     return unless exists $args->{record_ids};
132
133     my $mmtid = $args->{mmtid};
134     my @record_ids = @{ $args->{record_ids} };
135
136     $self->SUPER::enqueue({
137         job_size  => scalar @record_ids,
138         job_args  => {mmtid => $mmtid, record_ids => \@record_ids,},
139         job_queue => 'long_tasks',
140     });
141 }
142
143 1;