Bug 22417: Fix the batch authority tool
[koha.git] / Koha / BackgroundJob / BatchUpdateBiblio.pm
1 package Koha::BackgroundJob::BatchUpdateBiblio;
2
3 use Modern::Perl;
4 use JSON qw( encode_json decode_json );
5
6 use Koha::BackgroundJobs;
7 use Koha::DateUtils qw( dt_from_string );
8 use C4::Biblio;
9 use C4::MarcModificationTemplates;
10
11 use base 'Koha::BackgroundJob';
12
13 sub job_type {
14     return 'batch_biblio_record_modification';
15 }
16
17 sub process {
18     my ( $self, $args ) = @_;
19
20     my $job_type = $args->{job_type};
21
22     my $job = Koha::BackgroundJobs->find( $args->{job_id} );
23
24     if ( !exists $args->{job_id} || !$job || $job->status eq 'cancelled' ) {
25         return;
26     }
27
28     # FIXME If the job has already been started, but started again (worker has been restart for instance)
29     # Then we will start from scratch and so double process the same records
30
31     my $job_progress = 0;
32     $job->started_on(dt_from_string)
33         ->progress($job_progress)
34         ->status('started')
35         ->store;
36
37     my $mmtid = $args->{mmtid};
38     my $record_type = $args->{record_type};
39     my @record_ids = @{ $args->{record_ids} };
40
41     my $report = {
42         total_records => scalar @record_ids,
43         total_success => 0,
44     };
45     my @messages;
46     my $dbh = C4::Context->dbh;
47     $dbh->{RaiseError} = 1;
48     RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
49
50         last if $job->get_from_storage->status eq 'cancelled';
51
52         next unless $biblionumber;
53
54         # Modify the biblio
55         my $error = eval {
56             my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
57             C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
58             my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
59             C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
60         };
61         if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
62             push @messages, {
63                 type => 'error',
64                 code => 'biblio_not_modified',
65                 biblionumber => $biblionumber,
66                 error => ($@ ? $@ : $error),
67             };
68         } else {
69             push @messages, {
70                 type => 'success',
71                 code => 'biblio_modified',
72                 biblionumber => $biblionumber,
73             };
74             $report->{total_success}++;
75         }
76         $job->progress( ++$job_progress )->store;
77     }
78
79     my $job_data = decode_json $job->data;
80     $job_data->{messages} = \@messages;
81     $job_data->{report} = $report;
82
83     $job->ended_on(dt_from_string)
84         ->data(encode_json $job_data);
85     $job->status('finished') if $job->status ne 'cancelled';
86     $job->store;
87 }
88
89 sub enqueue {
90     my ( $self, $args) = @_;
91
92     # TODO Raise exception instead
93     return unless exists $args->{mmtid};
94     return unless exists $args->{record_type}; #FIXME RMME
95     return unless exists $args->{record_ids};
96
97     my $mmtid = $args->{mmtid};
98     my $record_type = $args->{record_type};
99     my @record_ids = @{ $args->{record_ids} };
100
101     $self->SUPER::enqueue({
102         job_size => scalar @record_ids,
103         job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}
104     });
105 }
106
107 1;