Bug 20780: Add handling for AcqItemSetSubfieldsWhenRecieved in EDI
[koha.git] / Koha / Z3950Responder / GenericSession.pm
1 #!/usr/bin/perl
2
3 package Koha::Z3950Responder::GenericSession;
4
5 # Copyright The National Library of Finland 2018
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 base qw( Koha::Z3950Responder::Session );
25
26 use Koha::Logger;
27 use Koha::SearchEngine::Search;
28 use Koha::SearchEngine::QueryBuilder;
29 use Koha::Z3950Responder::RPN;
30
31 =head1 NAME
32
33 Koha::Z3950Responder::genericSession
34
35 =head1 SYNOPSIS
36
37 Backend-agnostic session class that uses C<Koha::Session> as the base class. Utilizes
38 C<Koha::SearchEngine> for the actual functionality.
39
40 =head2 INSTANCE METHODS
41
42 =head3 start_search
43
44     my ($resultset, $hits) = $self->start_search( $args, $self->{server}->{num_to_prefetch} );
45
46 Perform a search using C<Koha::SearchEngine>'s QueryBuilder and Search.
47
48 =cut
49
50 sub start_search {
51     my ( $self, $args, $num_to_prefetch ) = @_;
52
53     if (!defined $self->{'attribute_mappings'}) {
54         require YAML;
55         $self->{'attribute_mappings'} = YAML::LoadFile($self->{server}->{config_dir} . 'attribute_mappings.yaml');
56     }
57
58     my $database = $args->{DATABASES}->[0];
59     my $builder = Koha::SearchEngine::QueryBuilder->new({ index => $database });
60     my $searcher = Koha::SearchEngine::Search->new({ index => $database });
61
62     my $built_query;
63     my $query = $args->{RPN}->{'query'}->to_koha($self->{'attribute_mappings'}->{$database});
64     $self->log_debug("    parsed search: $query");
65     my @operands = $query;
66     (undef, $built_query) = $builder->build_query_compat( undef, \@operands, undef, undef, undef, 0);
67
68     my ($error, $marcresults, $hits ) = $searcher->simple_search_compat($built_query, 0, $num_to_prefetch);
69     if (defined $error) {
70         $self->set_error($args, $self->ERR_SEARCH_FAILED, 'Search failed');
71         return;
72     }
73
74     my $resultset = {
75         query => $built_query,
76         database => $database,
77         cached_offset => 0,
78         cached_results => $marcresults,
79         hits => $hits
80     };
81
82     return ($resultset, $hits);
83 }
84
85 =head3 fetch_record
86
87     my $record = $self->fetch_record( $resultset, $args, $offset, $server->{num_to_prefetch} );
88
89 Fetch a record from SearchEngine. Caches records in session to avoid too many fetches.
90
91 =cut
92
93 sub fetch_record {
94     my ( $self, $resultset, $args, $index, $num_to_prefetch ) = @_;
95
96     # Fetch more records if necessary
97     my $offset = $args->{OFFSET} - 1;
98     if ($offset < $resultset->{cached_offset} || $offset >= $resultset->{cached_offset} + $num_to_prefetch) {
99         $self->log_debug("    fetch uncached, fetching $num_to_prefetch records starting at $offset");
100         my $searcher = Koha::SearchEngine::Search->new({ index => $resultset->{'database'} });
101         my ($error, $marcresults, $num_hits ) = $searcher->simple_search_compat($resultset->{'query'}, $offset, $num_to_prefetch);
102         if (defined $error) {
103             $self->set_error($args, $self->ERR_TEMPORARY_ERROR, 'Fetch failed');
104             return;
105         }
106
107         $resultset->{cached_offset} = $offset;
108         $resultset->{cached_results} = $marcresults;
109     }
110     return $resultset->{cached_results}[$offset - $resultset->{cached_offset}];
111 }
112
113 1;