]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/ZClient.pm
abstraction layer to support both ZOOM and Net::Z3950
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / ZClient.pm
1 package OpenILS::Utils::ZClient;
2 use UNIVERSAL::require;
3
4 our $conn_class = 'ZOOM::Connection';
5 our $imp_class = 'ZOOM';
6
7 # Detect the installed z client, prefering ZOOM.
8 if (!$imp_class->use()) {
9
10         $imp_class = 'Net::Z3950';  # Try Net::Z3950
11         if ($imp_class->use()) {
12
13                 # Load the modules we're going to modify
14                 'Net::Z3950::Connection'->use();
15                 'Net::Z3950::ResultSet'->use();
16                 'Net::Z3950::Record'->use();
17
18                 # Tell 'new' how to build the connection
19                 $conn_class = 'Net::Z3950::Connection';
20                 
21                 # Now we're going to give Net::Z3950 a ZOOM-ish interface
22
23                 # Move 'record' out of the way ...
24                 *{'Net::Z3950::ResultSet::_real_record'}  = *{'Net::Z3950::ResultSet::record'};
25                 # ... and install a new version using the 0-based ZOOM semantics
26                 *{'Net::Z3950::ResultSet::record'}  = sub { return shift()->_real_record(shift() - 1); };
27
28                 # Alias 'search' with the ZOOM 'search_pqf' method
29                 *{'Net::Z3950::Connection::search_pqf'}  = *{'Net::Z3950::Connection::search'};
30
31                 # And finally, alias 'rawdata' with the ZOOM 'raw' method
32                 *{'Net::Z3950::Record::raw'}  = sub { return shift()->rawdata(@_); }
33
34         } else {
35                 die "Cannot load a z39.50 client implementation!  Please install either ZOOM or Net::Z3950.\n";
36         }
37 }
38
39 # 'new' is called thusly:
40 #  my $conn = OpenILS::Utils::ZClient->new( $host, $port, databaseName => $db, user => $username )
41
42 sub new {
43         my $class = shift();
44         my @args = @_;
45         if ($class ne __PACKAGE__) { # NOT called OO-ishly
46                 # put the first param back if called like OpenILS::Utils::ZClient::new()
47                 unshift @args, $class;
48         }
49
50         return $conn_class->new(@_);
51 }
52
53
54 1;
55