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';
24 use Koha::ImportBatches;
26 use C4::ImportBatch qw(
27 RecordsFromMARCXMLFile
28 RecordsFromISO2709File
33 SetImportBatchOverlayAction
34 SetImportBatchNoMatchAction
35 SetImportBatchItemAction
40 Koha::BackgroundJob::StageMARCForImport - Stage MARC records for import
42 This is a subclass of Koha::BackgroundJob.
50 Define the job type of this job: stage_marc_for_import
55 return 'stage_marc_for_import';
60 Stage the MARC records for import.
65 my ( $self, $args ) = @_;
69 my $record_type = $args->{record_type};
70 my $encoding = $args->{encoding};
71 my $format = $args->{format};
72 my $filepath = $args->{filepath};
73 my $filename = $args->{filename};
74 my $marc_modification_template = $args->{marc_modification_template};
75 my $comments = $args->{comments};
76 my $parse_items = $args->{parse_items};
77 my $matcher_id = $args->{matcher_id};
78 my $overlay_action = $args->{overlay_action};
79 my $nomatch_action = $args->{nomatch_action};
80 my $item_action = $args->{item_action};
81 my $vendor_id = $args->{vendor_id};
82 my $basket_id = $args->{basket_id};
83 my $profile_id = $args->{profile_id};
86 my ( $batch_id, $num_valid, $num_items, @import_errors );
87 my $num_with_matches = 0;
88 my $checked_matches = 0;
89 my $matcher_failed = 0;
90 my $matcher_code = "";
92 my $schema = Koha::Database->new->schema;
94 $schema->storage->txn_begin;
96 my ( $errors, $marcrecords );
97 if ( $format eq 'MARCXML' ) {
98 ( $errors, $marcrecords ) =
99 C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding );
101 elsif ( $format eq 'ISO2709' ) {
102 ( $errors, $marcrecords ) =
103 C4::ImportBatch::RecordsFromISO2709File( $filepath, $record_type,
106 else { # plugin based
109 C4::ImportBatch::RecordsFromMarcPlugin( $filepath, $format,
113 $self->size(scalar @$marcrecords)->store;
115 ( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords(
116 $record_type, $encoding,
117 $marcrecords, $filename,
118 $marc_modification_template, $comments,
122 my $job_progress = shift;
126 $self->progress( int($job_progress) )->store;
130 $self->set({ progress => $num_valid, size => $num_valid });
131 } else { # We must assume that something went wrong here
132 $self->set({ progress => 0, status => 'failed' });
136 my $ibatch = Koha::ImportBatches->find($batch_id);
137 $ibatch->set( { profile_id => $profile_id } )->store;
141 my $matcher = C4::Matcher->fetch($matcher_id);
142 if ( defined $matcher ) {
143 $checked_matches = 1;
144 $matcher_code = $matcher->code();
146 BatchFindDuplicates( $batch_id, $matcher, 10, 50,
147 sub { my $job_progress = shift; $self->progress( $self->progress + $job_progress )->store } );
148 SetImportBatchMatcher( $batch_id, $matcher_id );
149 SetImportBatchOverlayAction( $batch_id, $overlay_action );
150 SetImportBatchNoMatchAction( $batch_id, $nomatch_action );
151 SetImportBatchItemAction( $batch_id, $item_action );
152 $schema->storage->txn_commit;
156 $schema->storage->txn_rollback;
159 $schema->storage->txn_commit;
164 $schema->storage->txn_rollback;
165 die "Something terrible has happened!"
166 if ( $_ =~ /Rollback failed/ ); # TODO Check test: Rollback failed
167 $self->set({ progress => 0, status => 'failed' });
171 staged => $num_valid,
172 matched => $num_with_matches,
173 num_items => $num_items,
174 import_errors => scalar(@import_errors),
175 total => $num_valid + scalar(@import_errors),
176 checked_matches => $checked_matches,
177 matcher_failed => $matcher_failed,
178 matcher_code => $matcher_code,
179 import_batch_id => $batch_id,
180 vendor_id => $vendor_id,
181 basket_id => $basket_id,
184 my $data = $self->decoded_data;
185 $data->{messages} = \@messages;
186 $data->{report} = $report;
188 $self->finish($data);
198 my ( $self, $args) = @_;
200 # FIXME: no $args validation
201 $self->SUPER::enqueue({
202 job_size => 0, # TODO Unknown for now?
204 job_queue => 'long_tasks',