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

(-)a/Koha/BackgroundJob/BatchUpdateAuthority.pm (-1 / +98 lines)
Line 0 Link Here
0
- 
1
package Koha::BackgroundJob::BatchUpdateAuthority;
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 $record_id ( sort { $a <=> $b } @record_ids ) {
39
        $report->{total_records}++;
40
        next unless $record_id;
41
        # Authorities
42
        my $authid = $record_id;
43
        my $error = eval {
44
            my $authority = Koha::MetadataRecord::Authority->get_from_authid( $authid );
45
            my $record = $authority->record;
46
            ModifyRecordWithTemplate( $mmtid, $record );
47
            ModAuthority( $authid, $record, $authority->authtypecode );
48
        };
49
        if ( $error and $error != $authid or $@ ) {
50
            push @messages, {
51
                type => 'error',
52
                code => 'authority_not_modified',
53
                authid => $authid,
54
                error => ($@ ? $@ : 0),
55
            };
56
        } else {
57
            push @messages, {
58
                type => 'success',
59
                code => 'authority_modified',
60
                authid => $authid,
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
76
    $channel->ack(); # FIXME Is that ok even on failure?
77
}
78
79
sub enqueue {
80
    my ( $self, $args) = @_;
81
82
    # TODO Raise exception instead
83
    return unless exists $args->{mmtid};
84
    return unless exists $args->{record_type}; #FIXME RMME
85
    return unless exists $args->{record_ids};
86
87
    my $mmtid = $args->{mmtid};
88
    my $record_type = $args->{record_type};
89
    my @record_ids = @{ $args->{record_ids} };
90
91
    $self->SUPER::enqueue({
92
        job_type => 'batch_record_modification',
93
        job_size => scalar @record_ids,
94
        job_args => {mmtid => $mmtid, record_type => $record_type, record_ids => \@record_ids,}
95
    });
96
}
97
98
1;

Return to bug 22417