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 C4::ImportBatch qw(
25 RecordsFromMARCXMLFile
26 RecordsFromISO2709File
31 SetImportBatchOverlayAction
32 SetImportBatchNoMatchAction
33 SetImportBatchItemAction
38 Koha::BackgroundJob::StageMARCForImport - Stage MARC records for import
40 This is a subclass of Koha::BackgroundJob.
48 Define the job type of this job: stage_marc_for_import
53 return 'stage_marc_for_import';
58 Stage the MARC records for import.
63 my ( $self, $args ) = @_;
67 my $record_type = $args->{record_type};
68 my $encoding = $args->{encoding};
69 my $format = $args->{format};
70 my $filepath = $args->{filepath};
71 my $filename = $args->{filename};
72 my $marc_modification_template = $args->{marc_modification_template};
73 my $comments = $args->{comments};
74 my $parse_items = $args->{parse_items};
75 my $matcher_id = $args->{matcher_id};
76 my $overlay_action = $args->{overlay_action};
77 my $nomatch_action = $args->{nomatch_action};
78 my $item_action = $args->{item_action};
79 my $vendor_id = $args->{vendor_id};
80 my $basket_id = $args->{basket_id};
81 my $profile_id = $args->{profile_id};
84 my ( $batch_id, $num_valid, $num_items, @import_errors );
85 my $num_with_matches = 0;
86 my $checked_matches = 0;
87 my $matcher_failed = 0;
88 my $matcher_code = "";
91 my $schema = Koha::Database->new->schema;
92 $schema->storage->txn_begin;
94 my ( $errors, $marcrecords );
95 if ( $format eq 'MARCXML' ) {
96 ( $errors, $marcrecords ) =
97 C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding );
99 elsif ( $format eq 'ISO2709' ) {
100 ( $errors, $marcrecords ) =
101 C4::ImportBatch::RecordsFromISO2709File( $filepath, $record_type,
104 else { # plugin based
107 C4::ImportBatch::RecordsFromMarcPlugin( $filepath, $format,
111 $self->size(scalar @$marcrecords)->store;
113 ( $batch_id, $num_valid, $num_items, @import_errors ) =
114 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;
130 my $ibatch = Koha::ImportBatches->find($batch_id);
131 $ibatch->set( { profile_id => $profile_id } )->store;
135 my $matcher = C4::Matcher->fetch($matcher_id);
136 if ( defined $matcher ) {
137 $checked_matches = 1;
138 $matcher_code = $matcher->code();
140 BatchFindDuplicates( $batch_id, $matcher, 10, 50,
141 sub { my $job_progress = shift; $self->progress( $self->progress + $job_progress )->store } );
142 SetImportBatchMatcher( $batch_id, $matcher_id );
143 SetImportBatchOverlayAction( $batch_id, $overlay_action );
144 SetImportBatchNoMatchAction( $batch_id, $nomatch_action );
145 SetImportBatchItemAction( $batch_id, $item_action );
146 $schema->storage->txn_commit;
150 $schema->storage->txn_rollback;
153 $schema->storage->txn_commit;
158 die "Something terrible has happened!"
159 if ( $_ =~ /Rollback failed/ ); # Rollback failed
163 staged => $num_valid,
164 matched => $num_with_matches,
165 num_items => $num_items,
166 import_errors => scalar(@import_errors),
167 total => $num_valid + scalar(@import_errors),
168 checked_matches => $checked_matches,
169 matcher_failed => $matcher_failed,
170 matcher_code => $matcher_code,
171 import_batch_id => $batch_id,
172 vendor_id => $vendor_id,
173 basket_id => $basket_id,
176 my $data = $self->decoded_data;
177 $data->{messages} = \@messages;
178 $data->{report} = $report;
180 $self->finish($data);
190 my ( $self, $args) = @_;
192 $self->SUPER::enqueue({
193 job_size => 0, # unknown for now