Bug 20780: Add handling for AcqItemSetSubfieldsWhenRecieved in EDI
[koha.git] / Koha / Z3950Responder.pm
1 #!/usr/bin/perl
2
3 package Koha::Z3950Responder;
4
5 # Copyright ByWater Solutions 2016
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
12 # version.
13 #
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22 use Modern::Perl;
23
24 use C4::Biblio qw( GetMarcFromKohaField );
25 use C4::Koha qw( GetAuthorisedValues );
26
27 use Net::Z3950::SimpleServer;
28
29 =head1 NAME
30
31 Koha::Z3950Responder - Main class for interfacing with Net::Z3950::SimpleServer
32
33 =head1 SYNOPSIS
34
35     use Koha::Z3950Responder;
36
37     my $z = Koha::Z3950Responder->new( {
38         add_item_status_subfield => 1,
39         add_status_multi_subfield => 1,
40         debug => 0,
41         num_to_prefetch => 20,
42         config_dir => '/home/koha/etc',
43         yaz_options => [ ],
44     } );
45
46     $z->start();
47
48 =head1 DESCRIPTION
49
50 A daemon class that interfaces with Net::Z3950::SimpleServer to provider Z39.50/SRU
51 service. Uses a Session class for the actual functionality.
52
53 =head1 METHODS
54
55 =head2 INSTANCE METHODS
56
57 =head3 new
58
59     $self->new({
60         add_item_status_subfield => 1
61     });
62
63 =cut
64
65 sub new {
66     my ( $class, $config ) = @_;
67
68     my ($item_tag, $itemnumber_subfield) = GetMarcFromKohaField( "items.itemnumber" );
69
70     # We hardcode the strings for English so SOMETHING will work if the authorized value doesn't exist.
71     my $status_strings = {
72         AVAILABLE => 'Available',
73         CHECKED_OUT => 'Checked Out',
74         LOST => 'Lost',
75         NOT_FOR_LOAN => 'Not for Loan',
76         DAMAGED => 'Damaged',
77         WITHDRAWN => 'Withdrawn',
78         IN_TRANSIT => 'In Transit',
79         ON_HOLD => 'On Hold',
80     };
81
82     foreach my $val ( @{ GetAuthorisedValues( 'Z3950_STATUS' ) } ) {
83         $status_strings->{ $val->{authorised_value} } = $val->{lib};
84     }
85
86     my $self = {
87         %$config,
88         item_tag => $item_tag,
89         itemnumber_subfield => $itemnumber_subfield,
90         status_strings => $status_strings,
91     };
92
93     # If requested, turn on debugging.
94     if ( $self->{debug} ) {
95         # Turn on single-process mode.
96         unshift @{ $self->{yaz_options} }, '-S';
97     } else {
98         # Turn off Yaz's built-in logging apart from fatal errors (can be turned back on if desired).
99         unshift @{ $self->{yaz_options} }, '-v', 'none,fatal';
100     }
101
102     # Set main config for SRU support
103     unshift @{ $self->{yaz_options} }, '-f', $self->{config_dir} . 'config.xml' if $self->{config_dir};
104
105     # Set num to prefetch if not passed
106     $self->{num_to_prefetch} //= 20;
107
108     $self->{server} = Net::Z3950::SimpleServer->new(
109         INIT => sub { $self->init_handler(@_) },
110         SEARCH => sub { $self->search_handler(@_) },
111         FETCH => sub { $self->fetch_handler(@_) },
112         CLOSE => sub { $self->close_handler(@_) },
113     );
114
115     return bless( $self, $class );
116 }
117
118 =head3 start
119
120     $z->start();
121
122 Start the daemon and begin serving requests. Does not return unless initialization fails or a
123 fatal error occurs.
124
125 =cut
126
127 sub start {
128     my ( $self ) = @_;
129
130     $self->{server}->launch_server( 'Koha::Z3950Responder', @{ $self->{yaz_options} } )
131 }
132
133 =head2 CALLBACKS
134
135 These methods are SimpleServer callbacks bound to this Z3950Responder object.
136 It's worth noting that these callbacks don't return anything; they both
137 receive and return data in the $args hashref.
138
139 =head3 init_handler
140
141 Callback that is called when a new connection is initialized
142
143 =cut
144
145 sub init_handler {
146     # Called when the client first connects.
147     my ( $self, $args ) = @_;
148
149     # This holds all of the per-connection state.
150     my $session;
151     if (C4::Context->preference('SearchEngine') eq 'Zebra') {
152         use Koha::Z3950Responder::ZebraSession;
153         $session = Koha::Z3950Responder::ZebraSession->new({
154             server => $self,
155             peer => $args->{PEER_NAME},
156         });
157     } else {
158         use Koha::Z3950Responder::GenericSession;
159         $session = Koha::Z3950Responder::GenericSession->new({
160             server => $self,
161             peer => $args->{PEER_NAME}
162         });
163     }
164
165     $args->{HANDLE} = $session;
166
167     $args->{IMP_NAME} = "Koha";
168     $args->{IMP_VER} = Koha::version;
169 }
170
171 =head3 search_handler
172
173 Callback that is called when a new search is performed
174
175 =cut
176
177 sub search_handler {
178     my ( $self, $args ) = @_;
179
180     $args->{HANDLE}->search_handler($args);
181 }
182
183 =head3 fetch_handler
184
185 Callback that is called when records are requested
186
187 =cut
188
189 sub fetch_handler {
190     my ( $self, $args ) = @_;
191
192     $args->{HANDLE}->fetch_handler( $args );
193 }
194
195 =head3 close_handler
196
197 Callback that is called when a session is terminated
198
199 =cut
200
201 sub close_handler {
202     my ( $self, $args ) = @_;
203
204     $args->{HANDLE}->close_handler( $args );
205 }
206
207 1;