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

(-)a/Koha/REST/V1/ERM/EHoldings/Titles.pm (+13 lines)
Lines 122-125 sub import_from_list { Link Here
122
122
123
    return Koha::REST::V1::ERM::EHoldings::Titles::Local::import_from_list($c);
123
    return Koha::REST::V1::ERM::EHoldings::Titles::Local::import_from_list($c);
124
}
124
}
125
126
=head3 import_from_kbart_file
127
128
Controller function that handles importing a kbart file
129
130
=cut
131
132
sub import_from_kbart_file {
133
    my $c = shift->openapi->valid_input or return;
134
135
    return Koha::REST::V1::ERM::EHoldings::Titles::Local::import_from_kbart_file($c);
136
}
137
125
1;
138
1;
(-)a/Koha/REST/V1/ERM/EHoldings/Titles/Local.pm (+74 lines)
Lines 21-29 use Mojo::Base 'Mojolicious::Controller'; Link Here
21
21
22
use Koha::ERM::EHoldings::Titles;
22
use Koha::ERM::EHoldings::Titles;
23
use Koha::BackgroundJob::CreateEHoldingsFromBiblios;
23
use Koha::BackgroundJob::CreateEHoldingsFromBiblios;
24
use Koha::BackgroundJob::ImportKBARTFile;
24
25
25
use Scalar::Util qw( blessed );
26
use Scalar::Util qw( blessed );
26
use Try::Tiny qw( catch try );
27
use Try::Tiny qw( catch try );
28
use MIME::Base64 qw( decode_base64 encode_base64 );
29
use POSIX qw( floor );
27
30
28
=head1 API
31
=head1 API
29
32
Lines 277-280 sub import_from_list { Link Here
277
    };
280
    };
278
}
281
}
279
282
283
284
=head3 import_from_kbart_file
285
286
=cut
287
288
sub import_from_kbart_file {
289
    my $c = shift or return;
290
291
    my $file = $c->req->json;
292
293
    return try {
294
        my @job_ids;
295
        my @invalid_columns;
296
        my $max_allowed_packet = C4::Context->dbh->selectrow_array(q{SELECT @@max_allowed_packet});
297
        my $file_content       = defined( $file->{file_content} ) ? decode_base64( $file->{file_content} ) : "";
298
        $file_content =~ s/\n/\r/g;
299
        my @lines          = split /\r/, $file_content;
300
        my @column_headers = split /\t/, $lines[0];
301
        shift @lines;    # Remove headers row
302
        my @remove_null_lines = grep $_ ne '', @lines;
303
304
        # Check that the column headers in the file match the standardised KBART phase II columns
305
        # If not, return a warning before the job is queued
306
        my @valid_headers = Koha::BackgroundJob::ImportKBARTFile::get_valid_headers();
307
        foreach my $header (@column_headers) {
308
            if ( !grep { $_ eq $header } @valid_headers ) {
309
                push @invalid_columns, $header;
310
            }
311
        }
312
        return $c->render(
313
            status  => 201,
314
            openapi => { invalid_columns => \@invalid_columns, valid_columns => \@valid_headers }
315
        ) if scalar(@invalid_columns) > 0;
316
317
        my $file_size = length($file_content);
318
319
        # If the file is too large, we can break the file into smaller chunks and enqueue one job per chunk
320
        if ( $file_size > $max_allowed_packet ) {
321
322
            my $max_number_of_lines = Koha::BackgroundJob::ImportKBARTFile::calculate_chunked_file_size(
323
                $file_size, $max_allowed_packet,
324
                scalar(@remove_null_lines)
325
            );
326
            my @chunked_files;
327
            push @chunked_files, [ splice @remove_null_lines, 0, $max_number_of_lines ] while @remove_null_lines;
328
329
            foreach my $chunk (@chunked_files) {
330
                unshift( @{$chunk}, join( "\t", @column_headers ) );
331
                my $chunked_file = {
332
                    filename     => $file->{filename},
333
                    file_content => encode_base64( join( "\r", @{$chunk} ) )
334
                };
335
                my $params = { file => $chunked_file };
336
                my $job_id = Koha::BackgroundJob::ImportKBARTFile->new->enqueue($params);
337
                push @job_ids, $job_id;
338
            }
339
        } else {
340
            my $params = { file => $file };
341
            my $job_id = Koha::BackgroundJob::ImportKBARTFile->new->enqueue($params);
342
            push @job_ids, $job_id;
343
        }
344
345
        return $c->render(
346
            status  => 201,
347
            openapi => { job_ids => \@job_ids }
348
        );
349
    } catch {
350
        $c->unhandled_exception($_);
351
    };
352
}
353
280
1;
354
1;
(-)a/api/v1/swagger/paths/erm_eholdings_titles.yaml (+70 lines)
Lines 493-495 Link Here
493
    x-koha-authorization:
493
    x-koha-authorization:
494
      permissions:
494
      permissions:
495
        erm: 1
495
        erm: 1
496
/erm/eholdings/local/titles/import_kbart:
497
  post:
498
    x-mojo-to: ERM::EHoldings::Titles#import_from_kbart_file
499
    operationId: importErmEHoldingsTitlesFromKbart
500
    tags:
501
      - erm_eholdings_titles
502
    summary: Import local titles from a KBART file
503
    consumes:
504
      - application/json
505
    produces:
506
      - application/json
507
    parameters:
508
      - description: The file to import
509
        in: body
510
        name: body
511
        required: true
512
        schema:
513
          type: object
514
          properties:
515
            filename:
516
              type: string
517
            file_content:
518
              type: string
519
          additionalProperties: false
520
    responses:
521
      201:
522
        description: Successfully enqueued the import job
523
        schema:
524
          type: object
525
          properties:
526
            job_ids:
527
              type: array
528
            invalid_columns:
529
              type: array
530
            valid_columns:
531
              type: array
532
          additionalProperties: false
533
      400:
534
        description: Bad parameter
535
        schema:
536
          $ref: "../swagger.yaml#/definitions/error"
537
      401:
538
        description: Authentication required
539
        schema:
540
          $ref: "../swagger.yaml#/definitions/error"
541
      403:
542
        description: Access forbidden
543
        schema:
544
          $ref: "../swagger.yaml#/definitions/error"
545
      404:
546
        description: Ressource not found
547
        schema:
548
          $ref: "../swagger.yaml#/definitions/error"
549
      409:
550
        description: Conflict in creating resource
551
        schema:
552
          $ref: "../swagger.yaml#/definitions/error"
553
      500:
554
        description: |-
555
          Internal server error. Possible `error_code` attribute values:
556
          * `internal_server_error`
557
        schema:
558
          $ref: "../swagger.yaml#/definitions/error"
559
      503:
560
        description: Under maintenance
561
        schema:
562
          $ref: "../swagger.yaml#/definitions/error"
563
    x-koha-authorization:
564
      permissions:
565
        erm: 1
(-)a/api/v1/swagger/swagger.yaml (-1 / +2 lines)
Lines 305-310 paths: Link Here
305
    $ref: "./paths/erm_eholdings_titles.yaml#/~1erm~1eholdings~1{provider}~1titles"
305
    $ref: "./paths/erm_eholdings_titles.yaml#/~1erm~1eholdings~1{provider}~1titles"
306
  /erm/eholdings/local/titles/import:
306
  /erm/eholdings/local/titles/import:
307
    $ref: ./paths/erm_eholdings_titles.yaml#/~1erm~1eholdings~1local~1titles~1import
307
    $ref: ./paths/erm_eholdings_titles.yaml#/~1erm~1eholdings~1local~1titles~1import
308
  /erm/eholdings/local/titles/import_kbart:
309
    $ref: ./paths/erm_eholdings_titles.yaml#/~1erm~1eholdings~1local~1titles~1import_kbart
308
  "/erm/eholdings/{provider}/titles/{title_id}":
310
  "/erm/eholdings/{provider}/titles/{title_id}":
309
    $ref: "./paths/erm_eholdings_titles.yaml#/~1erm~1eholdings~1{provider}~1titles~1{title_id}"
311
    $ref: "./paths/erm_eholdings_titles.yaml#/~1erm~1eholdings~1{provider}~1titles~1{title_id}"
310
  "/erm/eholdings/{provider}/titles/{title_id}/resources":
312
  "/erm/eholdings/{provider}/titles/{title_id}/resources":
311
- 

Return to bug 34788