View | Details | Raw Unified | Return to bug 34355
Collapse All | Expand All

(-)a/Koha/BackgroundJob/StageMARCForImport.pm (-116 / +4 lines)
Lines 66-189 sub process { Link Here
66
66
67
    $self->start;
67
    $self->start;
68
68
69
    my $record_type                = $args->{record_type};
69
    my $import_args = { %$args, job => $self };
70
    my $encoding                   = $args->{encoding};
70
    my $result = Koha::ImportBatch->new_from_file($import_args);
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};
84
85
    my @messages;
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     = "";
91
92
    my $schema = Koha::Database->new->schema;
93
    try {
94
        $schema->storage->txn_begin;
95
96
        my ( $errors, $marcrecords );
97
        if ( $format eq 'MARCXML' ) {
98
            ( $errors, $marcrecords ) =
99
              C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding );
100
        }
101
        elsif ( $format eq 'ISO2709' ) {
102
            ( $errors, $marcrecords ) =
103
              C4::ImportBatch::RecordsFromISO2709File( $filepath, $record_type,
104
                $encoding );
105
        }
106
        else {    # plugin based
107
            $errors = [];
108
            $marcrecords =
109
              C4::ImportBatch::RecordsFromMarcPlugin( $filepath, $format,
110
                $encoding );
111
        }
112
113
        $self->size(scalar @$marcrecords)->store;
114
115
        ( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords(
116
            $record_type,                $encoding,
117
            $marcrecords,                $filename,
118
            $marc_modification_template, $comments,
119
            '',                          $parse_items,
120
            0,                           50,
121
            sub {
122
                my $job_progress = shift;
123
                if ($matcher_id) {
124
                    $job_progress /= 2;
125
                }
126
                $self->progress( int($job_progress) )->store;
127
            }
128
        );
129
        if( $num_valid ) {
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' });
133
        }
134
135
        if ($profile_id) {
136
            my $ibatch = Koha::ImportBatches->find($batch_id);
137
            $ibatch->set( { profile_id => $profile_id } )->store;
138
        }
139
140
        if ($matcher_id) {
141
            my $matcher = C4::Matcher->fetch($matcher_id);
142
            if ( defined $matcher ) {
143
                $checked_matches = 1;
144
                $matcher_code    = $matcher->code();
145
                $num_with_matches =
146
                  BatchFindDuplicates( $batch_id, $matcher, 10, 50,
147
                    sub { my $job_progress = shift; $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;
153
            }
154
            else {
155
                $matcher_failed = 1;
156
                $schema->storage->txn_rollback;
157
            }
158
        } else {
159
            $schema->storage->txn_commit;
160
        }
161
    }
162
    catch {
163
        warn $_;
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' });
168
    };
169
170
    my $report = {
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,
182
    };
183
71
184
    my $data = $self->decoded_data;
72
    my $data = $self->decoded_data;
185
    $data->{messages} = \@messages;
73
    $data->{messages} = $result->{messages};
186
    $data->{report}   = $report;
74
    $data->{report}   = $result->{report};
187
75
188
    $self->finish($data);
76
    $self->finish($data);
189
}
77
}
(-)a/Koha/ImportBatch.pm (+146 lines)
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
(-)a/Koha/MarcOrder.pm (-75 / +3 lines)
Lines 251-337 sub _create_basket_for_file { Link Here
251
251
252
    $file->_stage_file($params)
252
    $file->_stage_file($params)
253
253
254
    Stages a file directly using parameters from a marc ordering account and without using the background job
254
    Stages a file directly using parameters from a marc ordering account
255
    This function is a mirror of Koha::BackgroundJob::StageMARCForImport->process but with the background job functionality removed
256
255
257
=cut
256
=cut
258
257
259
sub _stage_file {
258
sub _stage_file {
260
    my ($args) = @_;
259
    my ($args) = @_;
261
260
262
    my $record_type                = $args->{record_type};
261
    my $result = Koha::ImportBatch->new_from_file($args);
263
    my $encoding                   = $args->{encoding};
264
    my $format                     = $args->{format};
265
    my $filepath                   = $args->{filepath};
266
    my $filename                   = $args->{filename};
267
    my $marc_modification_template = $args->{marc_modification_template};
268
    my $comments                   = $args->{comments};
269
    my $parse_items                = $args->{parse_items};
270
    my $matcher_id                 = $args->{matcher_id};
271
    my $overlay_action             = $args->{overlay_action};
272
    my $nomatch_action             = $args->{nomatch_action};
273
    my $item_action                = $args->{item_action};
274
275
    my ( $batch_id, $num_valid, $num_items, @import_errors );
276
    my $num_with_matches = 0;
277
    my $checked_matches  = 0;
278
    my $matcher_failed   = 0;
279
    my $matcher_code     = "";
280
281
    my $schema = Koha::Database->new->schema;
282
    try {
283
        $schema->storage->txn_begin;
284
285
        my ( $errors, $marcrecords );
286
        if ( $format eq 'MARCXML' ) {
287
            ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromMARCXMLFile( $filepath, $encoding );
288
        } elsif ( $format eq 'ISO2709' ) {
289
            ( $errors, $marcrecords ) = C4::ImportBatch::RecordsFromISO2709File(
290
                $filepath, $record_type,
291
                $encoding
292
            );
293
        } else {    # plugin based
294
            $errors      = [];
295
            $marcrecords = C4::ImportBatch::RecordsFromMarcPlugin(
296
                $filepath, $format,
297
                $encoding
298
            );
299
        }
300
301
        ( $batch_id, $num_valid, $num_items, @import_errors ) = BatchStageMarcRecords(
302
            $record_type,                $encoding,
303
            $marcrecords,                $filename,
304
            $marc_modification_template, $comments,
305
            '',                          $parse_items,
306
            0
307
        );
308
309
        if ($matcher_id) {
310
            my $matcher = C4::Matcher->fetch($matcher_id);
311
            if ( defined $matcher ) {
312
                $checked_matches  = 1;
313
                $matcher_code     = $matcher->code();
314
                $num_with_matches = BatchFindDuplicates( $batch_id, $matcher, 10 );
315
                SetImportBatchMatcher( $batch_id, $matcher_id );
316
                SetImportBatchOverlayAction( $batch_id, $overlay_action );
317
                SetImportBatchNoMatchAction( $batch_id, $nomatch_action );
318
                SetImportBatchItemAction( $batch_id, $item_action );
319
                $schema->storage->txn_commit;
320
            } else {
321
                $matcher_failed = 1;
322
                $schema->storage->txn_rollback;
323
            }
324
        } else {
325
            $schema->storage->txn_commit;
326
        }
327
262
328
        return $batch_id;
263
    return $result->{report}->{import_batch_id};
329
    } catch {
330
        warn $_;
331
        $schema->storage->txn_rollback;
332
        die "Something terrible has happened!"
333
            if ( $_ =~ /Rollback failed/ );    # TODO Check test: Rollback failed
334
    };
335
}
264
}
336
265
337
=head3 _get_syspref_mappings
266
=head3 _get_syspref_mappings
338
- 

Return to bug 34355