Bug 32394: (follow-up) Add param for Koha::BackgroundJob::StageMARCForImport->enqueue
[koha-ffzg.git] / Koha / BackgroundJob / StageMARCForImport.pm
1 package Koha::BackgroundJob::StageMARCForImport;
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 Try::Tiny;
20
21 use base 'Koha::BackgroundJob';
22
23 use Koha::Database;
24 use C4::Matcher;
25 use C4::ImportBatch qw(
26     RecordsFromMARCXMLFile
27     RecordsFromISO2709File
28     RecordsFromMarcPlugin
29     BatchStageMarcRecords
30     BatchFindDuplicates
31     SetImportBatchMatcher
32     SetImportBatchOverlayAction
33     SetImportBatchNoMatchAction
34     SetImportBatchItemAction
35 );
36
37 =head1 NAME
38
39 Koha::BackgroundJob::StageMARCForImport - Stage MARC records for import
40
41 This is a subclass of Koha::BackgroundJob.
42
43 =head1 API
44
45 =head2 Class methods
46
47 =head3 job_type
48
49 Define the job type of this job: stage_marc_for_import
50
51 =cut
52
53 sub job_type {
54     return 'stage_marc_for_import';
55 }
56
57 =head3 process
58
59 Stage the MARC records for import.
60
61 =cut
62
63 sub process {
64     my ( $self, $args ) = @_;
65
66     $self->start;
67
68     my $record_type                = $args->{record_type};
69     my $encoding                   = $args->{encoding};
70     my $format                     = $args->{format};
71     my $filepath                   = $args->{filepath};
72     my $filename                   = $args->{filename};
73     my $marc_modification_template = $args->{marc_modification_template};
74     my $comments                   = $args->{comments};
75     my $parse_items                = $args->{parse_items};
76     my $matcher_id                 = $args->{matcher_id};
77     my $overlay_action             = $args->{overlay_action};
78     my $nomatch_action             = $args->{nomatch_action};
79     my $item_action                = $args->{item_action};
80     my $vendor_id                  = $args->{vendor_id};
81     my $basket_id                  = $args->{basket_id};
82     my $profile_id                 = $args->{profile_id};
83
84     my @messages;
85     my ( $batch_id, $num_valid, $num_items, @import_errors );
86     my $num_with_matches = 0;
87     my $checked_matches  = 0;
88     my $matcher_failed   = 0;
89     my $matcher_code     = "";
90
91     my $schema = Koha::Database->new->schema;
92     try {
93         $schema->storage->txn_begin;
94
95         my ( $errors, $marcrecords );
96         if ( $format eq 'MARCXML' ) {
97             ( $errors, $marcrecords ) =
98               C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding );
99         }
100         elsif ( $format eq 'ISO2709' ) {
101             ( $errors, $marcrecords ) =
102               C4::ImportBatch::RecordsFromISO2709File( $filepath, $record_type,
103                 $encoding );
104         }
105         else {    # plugin based
106             $errors = [];
107             $marcrecords =
108               C4::ImportBatch::RecordsFromMarcPlugin( $filepath, $format,
109                 $encoding );
110         }
111
112         $self->size(scalar @$marcrecords)->store;
113
114         ( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords(
115             $record_type,                $encoding,
116             $marcrecords,                $filename,
117             $marc_modification_template, $comments,
118             '',                          $parse_items,
119             0,                           50,
120             sub {
121                 my $job_progress = shift;
122                 if ($matcher_id) {
123                     $job_progress /= 2;
124                 }
125                 $self->progress( int($job_progress) )->store;
126             }
127         );
128         if( $num_valid ) {
129             $self->set({ progress => $num_valid, size => $num_valid });
130         } else { # We must assume that something went wrong here
131             $self->set({ progress => 0, status => 'failed' });
132         }
133
134         if ($profile_id) {
135             my $ibatch = Koha::ImportBatches->find($batch_id);
136             $ibatch->set( { profile_id => $profile_id } )->store;
137         }
138
139         if ($matcher_id) {
140             my $matcher = C4::Matcher->fetch($matcher_id);
141             if ( defined $matcher ) {
142                 $checked_matches = 1;
143                 $matcher_code    = $matcher->code();
144                 $num_with_matches =
145                   BatchFindDuplicates( $batch_id, $matcher, 10, 50,
146                     sub { my $job_progress = shift; $self->progress( $self->progress + $job_progress )->store } );
147                 SetImportBatchMatcher( $batch_id, $matcher_id );
148                 SetImportBatchOverlayAction( $batch_id, $overlay_action );
149                 SetImportBatchNoMatchAction( $batch_id, $nomatch_action );
150                 SetImportBatchItemAction( $batch_id, $item_action );
151                 $schema->storage->txn_commit;
152             }
153             else {
154                 $matcher_failed = 1;
155                 $schema->storage->txn_rollback;
156             }
157         } else {
158             $schema->storage->txn_commit;
159         }
160     }
161     catch {
162         warn $_;
163         $schema->storage->txn_rollback;
164         die "Something terrible has happened!"
165           if ( $_ =~ /Rollback failed/ );    # TODO Check test: Rollback failed
166         $self->set({ progress => 0, status => 'failed' });
167     };
168
169     my $report = {
170         staged          => $num_valid,
171         matched         => $num_with_matches,
172         num_items       => $num_items,
173         import_errors   => scalar(@import_errors),
174         total           => $num_valid + scalar(@import_errors),
175         checked_matches => $checked_matches,
176         matcher_failed  => $matcher_failed,
177         matcher_code    => $matcher_code,
178         import_batch_id => $batch_id,
179         vendor_id       => $vendor_id,
180         basket_id       => $basket_id,
181     };
182
183     my $data = $self->decoded_data;
184     $data->{messages} = \@messages;
185     $data->{report}   = $report;
186
187     $self->finish($data);
188 }
189
190 =head3 enqueue
191
192 Enqueue the new job
193
194 =cut
195
196 sub enqueue {
197     my ( $self, $args) = @_;
198
199     # FIXME: no $args validation
200     $self->SUPER::enqueue({
201         job_size  => 0, # TODO Unknown for now?
202         job_args  => $args,
203         job_queue => 'long_tasks',
204     });
205 }
206
207 1;