|
Lines 19-24
package Koha::ImportBatch;
Link Here
|
| 19 |
# <http://www.gnu.org/licenses> |
19 |
# <http://www.gnu.org/licenses> |
| 20 |
|
20 |
|
| 21 |
use Modern::Perl; |
21 |
use Modern::Perl; |
|
|
22 |
use Try::Tiny; |
| 23 |
|
| 24 |
use Koha::Database; |
| 25 |
use Koha::ImportBatches; |
| 26 |
use C4::Matcher; |
| 27 |
use C4::ImportBatch qw( |
| 28 |
RecordsFromMARCXMLFile |
| 29 |
RecordsFromISO2709File |
| 30 |
RecordsFromMarcPlugin |
| 31 |
BatchStageMarcRecords |
| 32 |
BatchFindDuplicates |
| 33 |
SetImportBatchMatcher |
| 34 |
SetImportBatchOverlayAction |
| 35 |
SetImportBatchNoMatchAction |
| 36 |
SetImportBatchItemAction |
| 37 |
); |
| 22 |
|
38 |
|
| 23 |
use base qw(Koha::Object); |
39 |
use base qw(Koha::Object); |
| 24 |
|
40 |
|
|
Lines 30-35
Koha::ImportBatch - Koha ImportBatch Object class
Link Here
|
| 30 |
|
46 |
|
| 31 |
=head2 Class Methods |
47 |
=head2 Class Methods |
| 32 |
|
48 |
|
|
|
49 |
=head3 new_from_file |
| 50 |
|
| 51 |
Koha::ImportBatch->new_from_file($args); |
| 52 |
|
| 53 |
This method is used to create a new Koha::ImportBatch object from a file. |
| 54 |
If being called from a background job, $args->{job} must be set. |
| 55 |
|
| 56 |
=cut |
| 57 |
|
| 58 |
sub new_from_file { |
| 59 |
my ( $self, $args ) = @_; |
| 60 |
|
| 61 |
my $job = $args->{job}; |
| 62 |
my $record_type = $args->{record_type}; |
| 63 |
my $encoding = $args->{encoding}; |
| 64 |
my $format = $args->{format}; |
| 65 |
my $filepath = $args->{filepath}; |
| 66 |
my $filename = $args->{filename}; |
| 67 |
my $marc_modification_template = $args->{marc_modification_template}; |
| 68 |
my $comments = $args->{comments}; |
| 69 |
my $parse_items = $args->{parse_items}; |
| 70 |
my $matcher_id = $args->{matcher_id}; |
| 71 |
my $overlay_action = $args->{overlay_action}; |
| 72 |
my $nomatch_action = $args->{nomatch_action}; |
| 73 |
my $item_action = $args->{item_action}; |
| 74 |
my $vendor_id = $args->{vendor_id}; |
| 75 |
my $basket_id = $args->{basket_id}; |
| 76 |
my $profile_id = $args->{profile_id}; |
| 77 |
|
| 78 |
my @messages; |
| 79 |
my ( $batch_id, $num_valid, $num_items, @import_errors ); |
| 80 |
my $num_with_matches = 0; |
| 81 |
my $checked_matches = 0; |
| 82 |
my $matcher_failed = 0; |
| 83 |
my $matcher_code = ""; |
| 84 |
|
| 85 |
my $schema = Koha::Database->new->schema; |
| 86 |
try { |
| 87 |
$schema->storage->txn_begin; |
| 88 |
|
| 89 |
my ( $errors, $marcrecords ); |
| 90 |
if ( $format eq 'MARCXML' ) { |
| 91 |
( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding ); |
| 92 |
} elsif ( $format eq 'ISO2709' ) { |
| 93 |
( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File( |
| 94 |
$filepath, $record_type, |
| 95 |
$encoding |
| 96 |
); |
| 97 |
} else { # plugin based |
| 98 |
$errors = []; |
| 99 |
$marcrecords = C4::ImportBatch::RecordsFromMarcPlugin( |
| 100 |
$filepath, $format, |
| 101 |
$encoding |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
$job->size( scalar @$marcrecords )->store if $job; |
| 106 |
|
| 107 |
( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords( |
| 108 |
$record_type, $encoding, |
| 109 |
$marcrecords, $filename, |
| 110 |
$marc_modification_template, $comments, |
| 111 |
'', $parse_items, |
| 112 |
0, 50, |
| 113 |
sub { |
| 114 |
my $job_progress = shift; |
| 115 |
if ($matcher_id) { |
| 116 |
$job_progress /= 2; |
| 117 |
} |
| 118 |
$job->progress( int($job_progress) )->store if $job; |
| 119 |
} |
| 120 |
); |
| 121 |
if ( $num_valid && $job ) { |
| 122 |
$job->set( { progress => $num_valid, size => $num_valid } ); |
| 123 |
} else { # We must assume that something went wrong here |
| 124 |
$job->set( { progress => 0, status => 'failed' } ); |
| 125 |
} |
| 126 |
|
| 127 |
if ($profile_id) { |
| 128 |
my $ibatch = Koha::ImportBatches->find($batch_id); |
| 129 |
$ibatch->set( { profile_id => $profile_id } )->store; |
| 130 |
} |
| 131 |
|
| 132 |
if ($matcher_id) { |
| 133 |
my $matcher = C4::Matcher->fetch($matcher_id); |
| 134 |
if ( defined $matcher ) { |
| 135 |
$checked_matches = 1; |
| 136 |
$matcher_code = $matcher->code(); |
| 137 |
$num_with_matches = BatchFindDuplicates( |
| 138 |
$batch_id, $matcher, 10, 50, |
| 139 |
sub { my $job_progress = shift; $job->progress($job_progress)->store if $job } |
| 140 |
); |
| 141 |
SetImportBatchMatcher( $batch_id, $matcher_id ); |
| 142 |
SetImportBatchOverlayAction( $batch_id, $overlay_action ); |
| 143 |
SetImportBatchNoMatchAction( $batch_id, $nomatch_action ); |
| 144 |
SetImportBatchItemAction( $batch_id, $item_action ); |
| 145 |
$schema->storage->txn_commit; |
| 146 |
} else { |
| 147 |
$matcher_failed = 1; |
| 148 |
$schema->storage->txn_rollback; |
| 149 |
} |
| 150 |
} else { |
| 151 |
$schema->storage->txn_commit; |
| 152 |
} |
| 153 |
} catch { |
| 154 |
warn $_; |
| 155 |
$schema->storage->txn_rollback; |
| 156 |
die "Something terrible has happened!" |
| 157 |
if ( $_ =~ /Rollback failed/ ); # TODO Check test: Rollback failed |
| 158 |
$job->set( { progress => 0, status => 'failed' } ) if $job; |
| 159 |
}; |
| 160 |
|
| 161 |
my $report = { |
| 162 |
staged => $num_valid, |
| 163 |
matched => $num_with_matches, |
| 164 |
num_items => $num_items, |
| 165 |
import_errors => scalar(@import_errors), |
| 166 |
total => $num_valid + scalar(@import_errors), |
| 167 |
checked_matches => $checked_matches, |
| 168 |
matcher_failed => $matcher_failed, |
| 169 |
matcher_code => $matcher_code, |
| 170 |
import_batch_id => $batch_id, |
| 171 |
vendor_id => $vendor_id, |
| 172 |
basket_id => $basket_id, |
| 173 |
}; |
| 174 |
|
| 175 |
return { report => $report, messages => \@messages }; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 33 |
=head3 _type |
179 |
=head3 _type |
| 34 |
|
180 |
|
| 35 |
=cut |
181 |
=cut |