-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBase.pm
2235 lines (1858 loc) · 68.5 KB
/
Base.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Koha::Illbackends::BLDSS::Base;
# Copyright PTFS Europe 2014, 2018
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
#
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with Koha; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Modern::Perl;
use Carp;
use File::Basename qw( dirname );
use C4::Installer;
use Koha::Libraries;
use Clone qw( clone );
use Locale::Country;
use XML::LibXML;
use MARC::Record;
use C4::Context;
use C4::Biblio qw( AddBiblio );
use C4::Charset qw( MarcToUTF8Record );
use Koha::Illrequest::Config;
use Koha::Illbackends::BLDSS::BLDSS::API;
use Koha::Illbackends::BLDSS::BLDSS::Config;
use Koha::Illbackends::BLDSS::BLDSS::XML;
use Try::Tiny;
use CGI;
use URI::Escape;
use YAML;
use JSON qw( to_json );
# We will be implementing the Abstract interface.
#use base qw(Koha::ILLRequest::Abstract);
## Every backend should have a version
our $VERSION = "00.00.00";
=head1 NAME
Koha::Illbackends::BLDSS::Base - Koha ILL Backend: BLDSS
=head1 SYNOPSIS
=head1 DESCRIPTION
A first stub file to help to split out BLDSS specific logic from the Abstract
ILL Interface.
=head1 API
=head2 Class Methods
=cut
=head3 new
=cut
sub new {
my ( $class, $params ) = @_;
my $framework =
defined $params->{config}->{configuration}->{raw_config}->{framework} ?
$params->{config}->{configuration}->{raw_config}->{framework} :
'FA';
my $self = {
keywords => [ "name", "accessor", "inSummary", "many" ],
framework => $framework
};
bless( $self, $class );
my $config =
Koha::Illbackends::BLDSS::BLDSS::Config->new( $params->{config} );
my $api = Koha::Illbackends::BLDSS::BLDSS::API->new($config);
$self->{cgi} = new CGI;
$self->_config($config);
$self->_api($api);
$self->_key_map;
$self->_logger( $params->{logger} ) if ( $params->{logger} );
$self->{templates} = { 'BLDSS_STATUS_CHECK' => dirname(__FILE__)
. '/intra-includes/log/bldss_status_check.tt' };
return $self;
}
=head _key_map
my $key_map = $bldss->_key_map;
Initialiser for our key_map
=cut
sub _key_map {
my ($self) = @_;
# Map item level metadata from form supplied
# keys to BLDSS metadata keys
$self->{key_map} = {
title => './metadata/titleLevel/title',
author => './metadata/titleLevel/author',
publisher => './metadata/titleLevel/publisher',
isbn => './metadata/titleLevel/isbn',
issn => './metadata/titleLevel/issn',
edition => './metadata/itemLevel/edition',
year => './metadata/itemLevel/year',
item_year => './metadata/itemLevel/year',
item_volume => './metadata/itemLevel/volume',
item_issue => './metadata/itemLevel/issue',
item_part => './metadata/itemLevel/part',
item_edition => './metadata/itemLevel/edition',
item_season => './metadata/itemLevel/season',
item_month => './metadata/itemLevel/month',
item_day => './metadata/itemLevel/day',
item_special_issue => './metadata/itemLevel/specialissue',
interest_title => './metadata/itemOfInterestLevel/title',
interest_author => './metadata/itemOfInterestLevel/author',
pages => './metadata/itemOfInterestLevel/pages',
};
return $self->{key_map};
}
=head3 _api
my $api = $bldss->_api($api);
my $api = $bldss->_api;
Getter/Setter for our API object.
=cut
sub _api {
my ( $self, $api ) = @_;
$self->{api} = $api if ($api);
return $self->{api};
}
=head3 _logger
my $logger = $bldss->_logger($logger);
my $logger = $bldss->_logger;
Getter/Setter for our Logger object.
=cut
sub _logger {
my ( $self, $logger ) = @_;
$self->{logger} = $logger if ($logger);
return $self->{logger};
}
=head3 _config
my $config = $bldss->_config($config);
my $config = $bldss->_config;
Getter/Setter for our config object.
=cut
sub _config {
my ( $self, $config ) = @_;
$self->{config} = $config if ($config);
return $self->{config};
}
=head3 status_graph
=cut
sub status_graph {
return {
EDITITEM => {
prev_actions => ['NEW'],
id => 'EDITITEM',
name => 'Edited item metadata',
ui_method_name => 'Edit item metadata',
method => 'edititem',
next_actions => [],
ui_method_icon => 'fa-edit',
},
STAT => {
prev_actions => ['REQ'],
id => 'STAT',
name => 'British Library Status',
ui_method_name => 'Check British Library status',
method => 'status',
next_actions => [],
ui_method_icon => 'fa-search',
},
MIG => {
prev_actions => [ 'NEW', 'REQREV', 'QUEUED', ],
id => 'MIG',
name => 'Backend Migration',
ui_method_name => 'Switch provider',
method => 'migrate',
next_actions => [],
ui_method_icon => 'fa-search',
},
};
}
sub name {
return "BLDSS";
}
=head3 capabilities
$capability = $backend->capabilities($name);
Return the sub implementing a capability selected by NAME, or 0 if that
capability is not implemented.
=cut
sub capabilities {
my ( $self, $name ) = @_;
my ($query) = @_;
my $capabilities = {
# The unmediated operation is just invoking confirm for BLDSS.
unmediated_ill => sub { $self->unmediated_confirm(@_); },
# Migrate
migrate => sub { $self->migrate(@_); },
};
return $capabilities->{$name};
}
=head3 metadata
Return a hashref containing canonical values from the key/value
illrequestattributes store.
In BLDSS we provide the following k/v fields:
- Title
- Author
- UIN
- Year
=cut
sub metadata {
my ( $self, $request ) = @_;
my $attrs = $request->illrequestattributes;
my $return = { UIN => $attrs->find( { type => './uin' } )->value };
$return->{Title} =
$attrs->find( { type => './metadata/titleLevel/title' } )
? $attrs->find( { type => './metadata/titleLevel/title' } )->value
: undef;
$return->{Author} =
$attrs->find( { type => './metadata/titleLevel/author' } )
? $attrs->find( { type => './metadata/titleLevel/author' } )->value
: undef;
$return->{Publisher} =
$attrs->find( { type => './metadata/titleLevel/publisher' } )
? $attrs->find( { type => './metadata/titleLevel/publisher' } )->value
: undef;
$return->{"Shelf mark"} =
$attrs->find( { type => './metadata/titleLevel/shelfmark' } )
? $attrs->find( { type => './metadata/titleLevel/shelfmark' } )->value
: undef;
$return->{Year} =
$attrs->find( { type => './metadata/itemLevel/year' } )
? $attrs->find( { type => './metadata/itemLevel/year' } )->value
: undef;
$return->{Issue} =
$attrs->find( { type => './metadata/itemLevel/issue' } )
? $attrs->find( { type => './metadata/itemLevel/issue' } )->value
: undef;
$return->{Volume} =
$attrs->find( { type => './metadata/itemLevel/volume' } )
? $attrs->find( { type => './metadata/itemLevel/volume' } )->value
: undef;
$return->{"Item part title"} =
$attrs->find( { type => './metadata/itemOfInterestLevel/title' } )
? $attrs->find( { type => './metadata/itemOfInterestLevel/title' } )
->value
: undef;
$return->{"Item part pages"} =
$attrs->find( { type => './metadata/itemOfInterestLevel/pages' } )
? $attrs->find( { type => './metadata/itemOfInterestLevel/pages' } )
->value
: undef;
$return->{"Item part author"} =
$attrs->find( { type => './metadata/itemOfInterestLevel/author' } )
? $attrs->find( { type => './metadata/itemOfInterestLevel/author' } )
->value
: undef;
return $return;
}
#### Standard Method Calls
=head3 confirm
my $response = $BLDSS->confirm( $record, $status, $params );
Return an ILL standard response for the confirm method call.
For BLDSS, this is a composite method, consisting of 3 stages: availability
lookup, pricing lookup and finally commit. Constructing the relevant standard
responses is carried out by the helper procedures `availability', `prices' and
`create_order' respectively.
=cut
sub confirm {
my ( $self, $params ) = @_;
my $stage = $params->{other}->{stage};
if ( !$stage || 'availability' eq $stage ) {
return $self->availability($params);
}
elsif ( 'pricing' eq $stage ) {
return $self->prices($params);
}
elsif ( 'commit' eq $stage ) {
return $self->create_order($params);
}
else {
die "Confirm Unexpected Stage";
}
}
=head3 unmediated_confirm
my $response = $BLDSS->unmediated_confirm( $params );
Return an ILL standard response for the confirm method call, in the case of
an unmediated workflow.
In the context of BLDSS this involves reading the default order preferences
from the configuration file, or falling back on the standard ones, and using
that to "create the order".
=cut
sub unmediated_confirm {
my ( $self, $params ) = @_;
# Directly invoke return create_order.
# It contains logic to load request details (speed, quality...) from
# the configuration file, using getDefaultFormat.
return $self->create_order($params);
}
=head3 create
my $response = $BLDSS->create( $params );
Return an ILL standard response for the create method call.
For BLDSS, this is a composite method.
=cut
sub create {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $stage = $other->{stage};
my $response = {
backend => $self->name,
method => 'create',
stage => $stage,
branchcode => $other->{branchcode},
cardnumber => $other->{cardnumber},
status => '',
message => '',
error => 0
};
# Check for borrowernumber
if ( !$other->{borrowernumber} && defined( $other->{cardnumber} ) ) {
$response->{cardnumber} = $other->{cardnumber};
# 'cardnumber' here could also be a surname (or in the case of
# search it will be a borrowernumber).
my ( $brw_count, $brw ) =
_validate_borrower( $other->{'cardnumber'}, $stage );
if ( $brw_count == 0 ) {
$response->{status} = "invalid_borrower";
$response->{value} = $params;
$response->{stage} = "init";
$response->{error} = 1;
return $response;
}
elsif ( $brw_count > 1 ) {
# We must select a specific borrower out of our options.
$params->{brw} = $brw;
$response->{value} = $params;
$response->{stage} = "borrowers";
$response->{error} = 0;
return $response;
}
else {
$other->{borrowernumber} = $brw->borrowernumber;
#$params->{other}->{borrowernumber} = $brw->borrowernumber;
}
}
$response->{borrowernumber} = $other->{borrowernumber};
# Initiate process
if ( !$stage || 'init' eq $stage ) {
# We just need to request the snippet that builds the Creation
# interface.
$response->{stage} = 'init';
$response->{value} = $params;
return $response;
}
# Validate form and perform search if valid
elsif ( 'validate' eq $stage ) {
if ( _fail( $other->{'branchcode'} ) ) {
$response->{status} = "missing_branch";
$response->{error} = 1;
$response->{stage} = 'init';
$response->{value} = $params;
return $response;
}
elsif ( !Koha::Libraries->find( $other->{'branchcode'} ) ) {
$response->{status} = "invalid_branch";
$response->{error} = 1;
$response->{stage} = 'init';
$response->{value} = $params;
return $response;
}
else {
$response->{stage} = 'search_results';
$response->{query} = $other->{query};
$response->{params} = $params;
# Perform the search!
my $results = $self->_search($params);
# Merge and return
$response = { %{$response}, %{$results} };
return $response;
}
}
# Load next results page
elsif ( 'search_results' eq $stage ) {
$response->{stage} = 'search_results';
$response->{query} = $other->{query};
$response->{params} = $params;
# Continue search!
my $results = $self->_search($params);
# Merge and return
return { %{$response}, %{$results} };
}
# Create request from search result
elsif ( 'commit' eq $stage ) {
# We should have the data we need for an API derived Record.
# ...Populate Illrequest
my $request = $params->{request};
my @read_write = $self->{cgi}->multi_param('read_write');
my $patron = Koha::Patrons->find( $other->{borrowernumber} );
my $bldss_result = $self->_find( $other->{uin} );
# If this is a 'book' or 'journal' request ask the user if they wish
# to add further details to turn it into a chapter or issue request.
if ( $bldss_result->{'./type'}->{value} =~ /book|journal|newspaper/ ) {
# Augment bldss_result with submitted details
if ( $other->{complete} ) {
foreach my $key ( keys %{ $self->{key_map} } ) {
my $value = $self->{key_map}->{$key};
if ( !length $bldss_result->{$value}->{value}
&& length $other->{$key} > 0 )
{
$bldss_result->{$value}->{value} = $other->{$key};
}
}
}
# Request more details
else {
$response->{stage} = 'extra_details';
$response->{params} = $other;
$response->{value} = {
type => $bldss_result->{'./type'}->{value},
# titleLevel
title =>
$bldss_result->{'./metadata/titleLevel/title'}->{value},
author =>
$bldss_result->{'./metadata/titleLevel/author'}->{value},
publisher =>
$bldss_result->{'./metadata/titleLevel/publisher'}
->{value},
isbn =>
$bldss_result->{'./metadata/titleLevel/isbn'}->{value},
issn =>
$bldss_result->{'./metadata/titleLevel/issn'}->{value},
# itemLevel
edition =>
$bldss_result->{'./metadata/itemLevel/edition'}->{value},
year =>
$bldss_result->{'./metadata/itemLevel/year'}->{value}
};
return $response;
}
}
my $biblionumber = $self->bldss2biblio($bldss_result);
$request->biblio_id($biblionumber) unless !$biblionumber;
$request->borrowernumber( $patron->borrowernumber );
$request->branchcode( $other->{branchcode} );
$request->medium( $other->{type} );
$request->status('NEW');
$request->backend( $self->name );
$request->placed( DateTime->now );
$request->updated( DateTime->now );
$request->store;
# Store the request attributes
$self->create_illrequestattributes( $bldss_result, $request,
\@read_write );
# Add original query details to result for storage
$self->_store_search( $request, $bldss_result, $other );
# Return
return {
status => "",
message => "",
error => 0,
value => {},
method => "create",
stage => "commit",
next => "illview",
};
}
# Catch unexpected stage
else {
die "Create Unexpected Stage";
}
}
=head3 edititem
Edit the read-write illrequest attribute fields for a request
=cut
sub edititem {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $stage = $other->{stage} ? $other->{stage} : 'form';
my $response = {
params => $params,
backend => $self->name,
method => 'edititem',
illrequest_id => $other->{illrequest_id},
stage => $stage,
error => 0,
status => '',
message => '',
};
# Don't allow editing of submitted requests
$response->{method} = 'illlist' if $params->{request}->status ne 'NEW';
if ( $stage eq 'form' ) {
# Map the BLDSS keys into form keys
my %rev = reverse %{ $self->{key_map} };
# Attributes for this request
my $attr = $params->{request}->illrequestattributes->unblessed;
# Prepare our return
my $out = {};
foreach my $meta ( @{$attr} ) {
if ( $rev{ $meta->{type} } ) {
$out->{ $rev{ $meta->{type} } } = $meta->{value};
}
elsif ( $meta->{type} eq './type' ) {
$out->{type} = $meta->{value};
}
}
$response->{value} = $out;
return $response;
}
elsif ( $stage eq 'commit' ) {
my $request = $params->{request};
my $passed = $params->{other};
my @read_write = $self->{cgi}->multi_param('read_write');
# Update the writeable fields that we've been passed
foreach my $attr (@read_write) {
my $bldss_key = $self->{key_map}->{$attr};
my $value = $passed->{$attr};
if ( $bldss_key && $value ) {
my $current_attr = Koha::Illrequestattributes->find(
{
illrequest_id => $request->id,
type => $bldss_key
}
);
if ($current_attr) {
$current_attr->value($value);
$current_attr->store;
}
}
}
$response->{method} = 'illlist';
return $response;
}
else {
return $response;
}
}
=head3 migrate
Migrate a request into or out of this backend.
=cut
sub migrate {
my ( $self, $params ) = @_;
my $other = $params->{other};
my $stage = $other->{stage} // 'immigrate';
my $step = $other->{step};
# Recieve a new request from another backend and suppliment it with
# anything we require speficifcally for this backend.
if ( $stage eq 'immigrate' ) {
$other->{stage} = $stage;
my $response = {
backend => $self->name,
method => 'migrate',
stage => $stage,
illrequest_id => $other->{illrequest_id},
status => '',
message => '',
error => 0
};
$response->{branchcode} = $other->{branchcode} if $other->{branchcode};
$response->{borrowernumber} = $other->{borrowernumber}
if $other->{borrowernumber};
# Initiate immigration search
if ( !$step || 'init' eq $step ) {
# Fetch original request details
my $original_request =
Koha::Illrequests->find( $other->{illrequest_id} );
# Collect parameters
$other->{step} = 'search_results';
$response->{step} = 'search_results';
$response->{query} = $other->{query};
$response->{params} = $params;
$other->{borrowernumber} = $original_request->borrowernumber;
$response->{borrowernumber} = $original_request->borrowernumber;
$other->{cardnumber} = $original_request->patron->cardnumber;
$response->{cardnumber} = $original_request->patron->cardnumber;
$other->{branchcode} = $original_request->branchcode;
$response->{branchcode} = $original_request->branchcode;
# Initiate search with details from last request
my @recognised_attributes = (qw/isbn issn article_title title author srchany/);
my $original_attributes =
$original_request->illrequestattributes->search(
{ type => { '-in' => \@recognised_attributes } } );
my $search_attributes =
{ map { $_->type => $_->value }
( $original_attributes->as_list ) };
$params->{other} = { %{ $params->{other} }, %{$search_attributes} };
if (
my $query = $original_request->illrequestattributes->find(
{ type => 'srchany' }
)
)
{
$params->{other}->{query} = $query->value;
}
# Perform a search
my $results = $self->_search($params);
# Merge and return
return { %{$response}, %{$results} };
}
# Load next results page
elsif ( 'search_results' eq $step ) {
$response->{step} = 'search_results';
$response->{query} = $other->{query};
$response->{params} = $params;
# Continue search!
my $results = $self->_search($params);
# Merge and return
return { %{$response}, %{$results} };
}
# Create request from search results
elsif ( 'commit' eq $step ) {
my $request = $params->{request};
my $bldss_result = $self->_find( $other->{uin} );
# Merge original request details as required
my $original_request =
Koha::Illrequests->find( $other->{illrequest_id} );
my @interesting_fields =
(qw/title article_author article_title title author edition year pages/);
my $original_attributes = {
map { $_->type => $_->value } (
$original_request->illrequestattributes->search(
{ type => { '-in' => \@interesting_fields } }
)->as_list
)
};
if ( exists $original_attributes->{'article_title'} ) {
# itemLevel
$bldss_result->{'./metadata/itemLevel/year'} //=
$original_request->{year}
if exists $original_request->{year};
$bldss_result->{'./metadata/itemLevel/volume'} //=
$original_request->{volume}
if exists $original_request->{volume};
$bldss_result->{'./metadata/itemLevel/issue'} //=
$original_request->{issue}
if exists $original_request->{issue};
$bldss_result->{'./metadata/itemLevel/edition'} //=
$original_request->{edition}
if exists $original_request->{edition};
# itemOfInterestLevel
$bldss_result->{'./metadata/itemOfInterestLevel/title'} //=
$original_attributes->{article_title}
if exists $original_attributes->{article_title};
$bldss_result->{'./metadata/itemOfInterestLevel/author'} //=
$original_attributes->{article_author}
if exists $original_attributes->{article_author};
$bldss_result->{'./metadata/itemOfInterestLevel/pages'} //=
$original_attributes->{pages}
if exists $original_attributes->{pages};
}
# Add temporary bib record
my $biblionumber = $self->bldss2biblio($bldss_result);
# Store request
$request->borrowernumber( $other->{borrowernumber} );
$request->branchcode( $other->{branchcode} );
$request->status('NEW');
$request->backend( $self->name );
$request->placed( DateTime->now );
$request->updated( DateTime->now );
$request->biblio_id($biblionumber) unless !$biblionumber;
$request->store;
# ...Add original query details to result for storage
$self->_store_search( $request, $bldss_result, $other );
# Store the request attributes
$bldss_result->{migrated_from}->{value} = $other->{illrequest_id};
$self->create_illrequestattributes( $bldss_result, $request );
return {
error => 0,
status => '',
message => '',
method => 'migrate',
stage => 'commit',
next => 'emigrate',
value => $params,
};
}
# Catch unexpected step
else {
die "Immigate Unexpected Step";
}
}
# Cleanup any outstanding work and close the request.
elsif ( $stage eq 'emigrate' ) {
my $new_request = $params->{request};
my $from_id = $new_request->illrequestattributes->find(
{ type => 'migrated_from' } )->value;
my $request = Koha::Illrequests->find($from_id);
# Cancel the original request if required
if ( $request->status eq 'REQ' ) {
# FIXME: Add Error Handling Here
$self->_process(
$self->_api->cancel_order( $params->{request} ) );
}
# Update original request to cancelled
$request->status("REQREV");
$request->orderid(undef);
$request->store;
return {
error => 0,
status => '',
message => '',
method => 'migrate',
stage => 'commit',
value => $params,
};
}
}
=head3 cancel
my $response = $BLDSS->cancel( $record, $status, $params );
Return an ILL standard response for the cancel method call.
=cut
sub cancel {
my ( $self, $params ) = @_;
my $response =
$self->_process(
$self->_api->cancel_order( $params->{request} ) );
if ( $response->{error} ) {
$response->{method} = 'cancel';
$response->{stage} = 'init';
return $response;
}
return { method => 'cancel', stage => 'commit', next => 'illview', };
}
=head3 status
my $response = $BLDSS->status( $record, $status, $params );
Return an ILL standard response for the status method call.
=cut
sub status {
my ( $self, $params ) = @_;
my $stage = $params->{other}->{stage};
if ( !$stage || $stage eq 'init' ) {
my $status =
$self->_process( $self->_api->order( $params->{request} ) );
$status->{method} = "status";
$status->{stage} = "show_status";
# Log this check if appropriate
if ( $self->_logger ) {
my $logger = $self->_logger;
# TODO: This is a transitionary measure, we have removed set_data
# in Bug 20750, so calls to it won't work. But since 20750 is
# currently only in 19.05, they only won't work in earlier
# versions. So we're temporarily going to allow for both cases
if ($logger->can('set_data')) {
$logger->set_data(
{
actionname => 'BLDSS_STATUS_CHECK',
objectnumber => $params->{request}->id,
infos => to_json(
{
log_origin => $self->name,
response =>
$status->{value}->result->orderline->overallStatus
}
)
}
);
$logger->log_something();
} else {
$logger->log_something({
actionname => 'BLDSS_STATUS_CHECK',
objectnumber => $params->{request}->id,
infos => to_json(
{
log_origin => $self->name,
response =>
$status->{value}->result->orderline->overallStatus
}
)
});
}
}
return $status;
}
else {
# Assume stage is commit, we just return.
return {
status => "",
message => "",
error => 0,
value => {},
method => "status",
next => "illview",
stage => "commit",
};
}
}
=head3 get_log_template_path
my $path = $BLDSS->get_log_template_path($action);
Given an action, return the path to the template for displaying
that action log
=cut
sub get_log_template_path {
my ( $self, $action ) = @_;
return $self->{templates}->{$action};
}
#### Helpers
=head3 bldss2biblio
my $biblionumber = $BLDSS->bldss2biblio($result);
Create a basic biblio record for the passed BLDSS API result
=cut
sub bldss2biblio {
my ( $self, $result ) = @_;
# We only want to create biblios for books
return undef unless $result->{'./type'}->{value} eq 'book';
# We're going to try and populate author, title & ISBN
my $author = $result->{'./metadata/titleLevel/author'}->{value};
my $title = $result->{'./metadata/titleLevel/title'}->{value};
my $isbn = $result->{'./metadata/titleLevel/isbn'}->{value};
# Create the MARC::Record object and populate
my $record = MARC::Record->new();
# Fix character set where appropriate
my $marcflavour = C4::Context->preference('marcflavour') || 'MARC21';
if ($record->encoding() eq 'MARC-8') {
($record) = MarcToUTF8Record($record, $marcflavour);
}
if ($isbn) {
my @isbns = split /\|/, $isbn;
for my $each (@isbns) {
my $marc_isbn = MARC::Field->new( '020', '', '', a => $each );
$record->append_fields($marc_isbn);
}
}
if ($author) {
my $marc_author = MARC::Field->new( '100', '1', '', a => $author );
$record->append_fields($marc_author);
}
if ($title) {
my $marc_title = MARC::Field->new( '245', '0', '0', a => $title );
$record->append_fields($marc_title);
}
# Suppress the record
$self->_set_suppression($record);