Bug 27783: Rename queues and adjust currently defined jobs
[koha.git] / Koha / BackgroundJob / BatchUpdateItem.pm
1 package Koha::BackgroundJob::BatchUpdateItem;
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( encode_json decode_json );
20 use Encode qw( encode_utf8 );
21 use List::MoreUtils qw( uniq );
22 use Try::Tiny;
23
24 use MARC::Record;
25 use MARC::Field;
26
27 use C4::Biblio;
28 use C4::Items;
29
30 use Koha::DateUtils qw( dt_from_string );
31 use Koha::SearchEngine::Indexer;
32 use Koha::Items;
33 use Koha::UI::Table::Builder::Items;
34
35 use base 'Koha::BackgroundJob';
36
37 =head1 NAME
38
39 Koha::BackgroundJob::BatchUpdateItem - Background job derived class to process item modification in batch
40
41 =head1 API
42
43 =head2 Class methods
44
45 =head3 job_type
46
47 Define the job type of this job: batch_item_record_modification
48
49 =cut
50
51 sub job_type {
52     return 'batch_item_record_modification';
53 }
54
55 =head3 process
56
57     Koha::BackgroundJobs->find($id)->process(
58         {
59             record_ids => \@itemnumbers,
60             new_values => {
61                 itemnotes => $new_item_notes,
62                 k         => $k,
63             },
64             regex_mod => {
65                 itemnotes_nonpublic => {
66                     search => 'foo',
67                     replace => 'bar',
68                     modifiers => 'gi',
69                 },
70             },
71             exclude_from_local_holds_priority => 1|0
72         }
73     );
74
75 Process the modification.
76
77 new_values allows to set a new value for given fields.
78 The key can be one of the item's column name, or one subfieldcode of a MARC subfields not linked with a Koha field.
79
80 regex_mod allows to modify existing subfield's values using a regular expression.
81
82 =cut
83
84 sub process {
85     my ( $self, $args ) = @_;
86
87     if ( $self->status eq 'cancelled' ) {
88         return;
89     }
90
91     # FIXME If the job has already been started, but started again (worker has been restart for instance)
92     # Then we will start from scratch and so double process the same records
93
94     my $job_progress = 0;
95     $self->started_on(dt_from_string)->progress($job_progress)
96       ->status('started')->store;
97
98     my @record_ids = @{ $args->{record_ids} };
99     my $regex_mod  = $args->{regex_mod};
100     my $new_values = $args->{new_values};
101     my $exclude_from_local_holds_priority =
102       $args->{exclude_from_local_holds_priority};
103
104     my $report = {
105         total_records            => scalar @record_ids,
106         modified_fields          => 0,
107     };
108
109     try {
110         my ($results) =
111           Koha::Items->search( { itemnumber => \@record_ids } )
112           ->batch_update(
113             {
114                 regex_mod  => $regex_mod,
115                 new_values => $new_values,
116                 exclude_from_local_holds_priority =>
117                   $exclude_from_local_holds_priority,
118                 callback => sub {
119                     my ($progress) = @_;
120                     $self->progress($progress)->store;
121                 },
122             }
123           );
124         $report->{modified_itemnumbers} = $results->{modified_itemnumbers};
125         $report->{modified_fields}      = $results->{modified_fields};
126     }
127     catch {
128         warn $_;
129         die "Something terrible has happened!"
130           if ( $_ =~ /Rollback failed/ );    # Rollback failed
131     };
132
133     $self->discard_changes;
134     my $job_data = decode_json encode_utf8 $self->data;
135     $job_data->{report} = $report;
136
137     $self->ended_on(dt_from_string)->data( encode_json $job_data);
138     $self->status('finished') if $self->status ne 'cancelled';
139     $self->store;
140 }
141
142 =head3 enqueue
143
144 Enqueue the new job
145
146 =cut
147
148 sub enqueue {
149     my ( $self, $args ) = @_;
150
151     # TODO Raise exception instead
152     return unless exists $args->{record_ids};
153
154     my @record_ids = @{ $args->{record_ids} };
155
156     $self->SUPER::enqueue(
157         {
158             job_size => scalar @record_ids,
159             job_args => {%$args},
160             queue    => 'long_tasks',
161         }
162     );
163 }
164
165 =head3 additional_report
166
167 Sent the infos to generate the table containing the details of the modified items.
168
169 =cut
170
171 sub additional_report {
172     my ( $self, $args ) = @_;
173
174     my $itemnumbers = $self->report->{modified_itemnumbers};
175     if ( scalar(@$itemnumbers) > C4::Context->preference('MaxItemsToDisplayForBatchMod') ) {
176         return { too_many_items_display => 1 };
177     } else {
178         my $items_table =
179           Koha::UI::Table::Builder::Items->new( { itemnumbers => $itemnumbers } )
180           ->build_table;
181
182         return {
183             items            => $items_table->{items},
184             item_header_loop => $items_table->{headers},
185         };
186     }
187 }
188
189 1;