]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/ZClient.pm
typo; thanks Dan Scott
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / ZClient.pm
1 package OpenILS::Utils::ZClient;
2 use UNIVERSAL::require;
3
4 use overload 'bool' => sub { return $_[0]->{connection} ? 1 : 0 };
5
6 our $conn_class = 'ZOOM::Connection';
7 our $imp_class = 'ZOOM';
8 our $AUTOLOAD;
9
10 # Detect the installed z client, prefering ZOOM.
11 if (!$imp_class->use()) {
12
13         $imp_class = 'Net::Z3950';  # Try Net::Z3950
14         if ($imp_class->use()) {
15
16                 # Tell 'new' how to build the connection
17                 $conn_class = 'Net::Z3950::Connection';
18                 
19         } else {
20                 die "Cannot load a z39.50 client implementation!  Please install either ZOOM or Net::Z3950.\n";
21         }
22 }
23
24 # 'new' is called thusly:
25 #  my $conn = OpenILS::Utils::ZClient->new( $host, $port, databaseName => $db, user => $username )
26
27 sub new {
28         my $class = shift();
29         my @args = @_;
30
31         if ($class ne __PACKAGE__) { # NOT called OO-ishly
32                 # put the first param back if called like OpenILS::Utils::ZClient::new()
33                 unshift @args, $class;
34         }
35
36         return bless { connection => $conn_class->new(@_) } => __PACKAGE__;
37 }
38
39 sub search {
40         my $self = shift;
41         my $r =  $imp_class eq 'Net::Z3950' ?
42                 $self->{connection}->search( @_ ) :
43                 $self->{connection}->search_pqf( @_ );
44
45         return OpenILS::Utils::ZClient::ResultSet->new( $r );
46 }
47
48 *{__PACKAGE__ . '::search_pqf'} = \&search; 
49
50 sub AUTOLOAD {
51         my $self = shift;
52
53         my $method = $AUTOLOAD;
54         $method =~ s/.*://;   # strip fully-qualified portion
55
56         return $self->{connection}->$method( @_ );
57 }
58
59 #-------------------------------------------------------------------------------
60 package OpenILS::Utils::ZClient::ResultSet;
61
62 our $AUTOLOAD;
63
64 sub new {
65         my $class = shift;
66         my @args = @_;
67
68         if ($class ne __PACKAGE__) { # NOT called OO-ishly
69                 # put the first param back if called like OpenILS::Utils::ZClient::ResultSet::new()
70                 unshift @args, $class;
71         }
72
73
74         return bless { result => $args[0] } => __PACKAGE__;
75 }
76
77 sub record {
78         my $self = shift;
79         my $offset = shift;
80         my $r = $imp_class eq 'Net::Z3950' ?
81                 $self->{result}->record( ++$offset ) :
82                 $self->{result}->record( $offset );
83
84         return  OpenILS::Utils::ZClient::Record->new( $r );
85 }
86
87 sub AUTOLOAD {
88         my $self = shift;
89
90         my $method = $AUTOLOAD;
91         $method =~ s/.*://;   # strip fully-qualified portion
92
93         return $self->{result}->$method( @_ );
94 }
95
96 #-------------------------------------------------------------------------------
97 package OpenILS::Utils::ZClient::Record;
98
99 our $AUTOLOAD;
100
101 sub new {
102         my $class = shift;
103         my @args = @_;
104
105         if ($class ne __PACKAGE__) { # NOT called OO-ishly
106                 # put the first param back if called like OpenILS::Utils::ZClient::ResultSet::new()
107                 unshift @args, $class;
108         }
109
110
111         return bless { record => shift() } => __PACKAGE__;
112 }
113
114 sub rawdata {
115         my $self = shift;
116         return $OpenILS::Utils::ZClient::imp_class eq 'Net::Z3950' ?
117                 $self->{record}->rawdata( @_ ) :
118                 $self->{record}->raw( @_ );
119 }
120
121 *{__PACKAGE__ . '::raw'} = \&rawdata; 
122
123 sub AUTOLOAD {
124         my $self = shift;
125
126         my $method = $AUTOLOAD;
127         $method =~ s/.*://;   # strip fully-qualified portion
128
129         return $self->{record}->$method( @_ );
130 }
131
132
133 1;
134