View | Details | Raw Unified | Return to bug 22417
Collapse All | Expand All

(-)a/Koha/BackgroundJob/BatchUpdateBiblio.pm (-1 / +97 lines)
Line 0 Link Here
0
- 
1
package Koha::BackgroundJob::BatchUpdateBiblio;
2
3
use Modern::Perl;
4
use Koha::BackgroundJobs;
5
use Koha::DateUtils qw( dt_from_string );
6
use JSON qw( encode_json decode_json );
7
use Net::RabbitFoot;
8
9
use base 'Koha::BackgroundJob';
10
11
our $channel;
12
sub process {
13
    my ( $self, $args, $channel ) = @_;
14
15
    my $job_type = $args->{job_type};
16
17
    return unless exists $args->{job_id};
18
19
    my $job = Koha::BackgroundJobs->find( $args->{job_id} );
20
21
    my $job_progress = 0;
22
    $job->started_on(dt_from_string)
23
        ->progress($job_progress)
24
        ->status('started')
25
        ->store;
26
27
    my $mmtid = $args->{mmtid};
28
    my $record_type = $args->{record_type};
29
    my @record_ids = @{ $args->{record_ids} };
30
31
    my $report = {
32
        total_records => 0,
33
        total_success => 0,
34
    };
35
    my @messages;
36
    my $dbh = C4::Context->dbh;
37
    $dbh->{RaiseError} = 1;
38
    RECORD_IDS: for my $biblionumber ( sort { $a <=> $b } @record_ids ) {
39
        $report->{total_records}++;
40
        next unless $biblionumber;
41
42
        # Modify the biblio
43
        my $error = eval {
44
            my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
45
            C4::MarcModificationTemplates::ModifyRecordWithTemplate( $mmtid, $record );
46
            my $frameworkcode = C4::Biblio::GetFrameworkCode( $biblionumber );
47
            C4::Biblio::ModBiblio( $record, $biblionumber, $frameworkcode );
48
        };
49
        if ( $error and $error != 1 or $@ ) { # ModBiblio returns 1 if everything as gone well
50
            push @messages, {
51
                type => 'error',
52
                code => 'biblio_not_modified',
53
                biblionumber => $biblionumber,
54
                error => ($@ ? $@ : $error),
55
            };
56
        } else {
57
            push @messages, {
58
                type => 'success',
59
                code => 'biblio_modified',
60
                biblionumber => $biblionumber,
61
            };
62
            $report->{total_success}++;
63
        }
64
        $job->progress( ++$job_progress )->store;
65
    }
66
67
    my $job_data = decode_json $job->data;
68
    $job_data->{messages} = \@messages;
69
    $job_data->{report} = $report;
70
71
    $job->ended_on(dt_from_string)
72
        ->status('finished')
73
        ->data(encode_json $job_data)
74
        ->store;
75
    $channel->ack(); # FIXME Is that ok even on failure?
76
}
77
78
sub enqueue {
79
    my ( $self, $args) = @_;
80
81
    # TODO Raise exception instead
82
    return unless exists $args->{mmtid};
83
    return unless exists $args->{record_type}; #FIXME RMME
84
    return unless exists $args->{record_ids};
85
86
    my $mmtid = $args->{mmtid};
87
    my $record_type = $args->{record_type};
88
    my @record_ids = @{ $args->{record_ids} };
89
90
    $self->SUPER::enqueue({
91
        job_type => 'batch_biblio_record_modification', # FIXME Must be a global const
92
        job_size => scalar @record_ids,
93
        job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}
94
    });
95
}
96
97
1;

Return to bug 22417