1 package Koha::BackgroundJob::StageMARCForImport;
3 # This file is part of Koha.
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.
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.
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>.
21 use base 'Koha::BackgroundJob';
25 use C4::ImportBatch qw(
26 RecordsFromMARCXMLFile
27 RecordsFromISO2709File
32 SetImportBatchOverlayAction
33 SetImportBatchNoMatchAction
34 SetImportBatchItemAction
39 Koha::BackgroundJob::StageMARCForImport - Stage MARC records for import
41 This is a subclass of Koha::BackgroundJob.
49 Define the job type of this job: stage_marc_for_import
54 return 'stage_marc_for_import';
59 Stage the MARC records for import.
64 my ( $self, $args ) = @_;
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};
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 = "";
91 my $schema = Koha::Database->new->schema;
93 $schema->storage->txn_begin;
95 my ( $errors, $marcrecords );
96 if ( $format eq 'MARCXML' ) {
97 ( $errors, $marcrecords ) =
98 C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding );
100 elsif ( $format eq 'ISO2709' ) {
101 ( $errors, $marcrecords ) =
102 C4::ImportBatch::RecordsFromISO2709File( $filepath, $record_type,
105 else { # plugin based
108 C4::ImportBatch::RecordsFromMarcPlugin( $filepath, $format,
112 $self->size(scalar @$marcrecords)->store;
114 ( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords(
115 $record_type, $encoding,
116 $marcrecords, $filename,
117 $marc_modification_template, $comments,
121 my $job_progress = shift;
125 $self->progress( int($job_progress) )->store;
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' });
135 my $ibatch = Koha::ImportBatches->find($batch_id);
136 $ibatch->set( { profile_id => $profile_id } )->store;
140 my $matcher = C4::Matcher->fetch($matcher_id);
141 if ( defined $matcher ) {
142 $checked_matches = 1;
143 $matcher_code = $matcher->code();
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;
155 $schema->storage->txn_rollback;
158 $schema->storage->txn_commit;
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' });
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,
183 my $data = $self->decoded_data;
184 $data->{messages} = \@messages;
185 $data->{report} = $report;
187 $self->finish($data);
197 my ( $self, $args) = @_;
199 # FIXME: no $args validation
200 $self->SUPER::enqueue({
201 job_size => 0, # TODO Unknown for now?
203 job_queue => 'long_tasks',