1 package OpenILS::Utils::ZClient;
2 use UNIVERSAL::require;
4 use overload 'bool' => sub { return $_[0]->{connection} ? 1 : 0 };
6 our $conn_class = 'ZOOM::Connection';
7 our $imp_class = 'ZOOM';
10 # Detect the installed z client, prefering ZOOM.
11 if (!$imp_class->use()) {
13 $imp_class = 'Net::Z3950'; # Try Net::Z3950
14 if ($imp_class->use()) {
16 # Tell 'new' how to build the connection
17 $conn_class = 'Net::Z3950::Connection';
20 die "Cannot load a z39.50 client implementation! Please install either ZOOM or Net::Z3950.\n";
24 # 'new' is called thusly:
25 # my $conn = OpenILS::Utils::ZClient->new( $host, $port, databaseName => $db, user => $username )
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;
36 return bless { connection => $conn_class->new(@_) } => __PACKAGE__;
41 my $r = $imp_class eq 'Net::Z3950' ?
42 $self->{connection}->search( @_ ) :
43 $self->{connection}->search_pqf( @_ );
45 return OpenILS::Utils::ZClient::ResultSet->new( $r );
50 if ($imp_class eq 'Net::Z3950') {
51 if (defined $$list[0]{_async_index}) {
52 return 0 if ($$list[0]{_async_index} == @$list);
53 return ++$$list[0]{_async_index};
55 return $$list[0]{_async_index} = 1;
59 return ZOOM::event([map { ($_->{result}) } @$list]);
62 *{__PACKAGE__ . '::search_pqf'} = \&search;
67 my $method = $AUTOLOAD;
68 $method =~ s/.*://; # strip fully-qualified portion
70 return $self->{connection}->$method( @_ );
73 #-------------------------------------------------------------------------------
74 package OpenILS::Utils::ZClient::ResultSet;
82 if ($class ne __PACKAGE__) { # NOT called OO-ishly
83 # put the first param back if called like OpenILS::Utils::ZClient::ResultSet::new()
84 unshift @args, $class;
88 return bless { result => $args[0] } => __PACKAGE__;
94 my $r = $imp_class eq 'Net::Z3950' ?
95 $self->{result}->record( ++$offset ) :
96 $self->{result}->record( $offset );
98 return OpenILS::Utils::ZClient::Record->new( $r );
103 return OpenILS::Utils::ZClient::Event::EVENT_END if ($imp_class eq 'Net::Z3950');
104 $self->{result}->last_event();
110 my $method = $AUTOLOAD;
111 $method =~ s/.*://; # strip fully-qualified portion
113 return $self->{result}->$method( @_ );
116 #-------------------------------------------------------------------------------
117 package OpenILS::Utils::ZClient::Record;
125 if ($class ne __PACKAGE__) { # NOT called OO-ishly
126 # put the first param back if called like OpenILS::Utils::ZClient::ResultSet::new()
127 unshift @args, $class;
131 return bless { record => shift() } => __PACKAGE__;
136 return $OpenILS::Utils::ZClient::imp_class eq 'Net::Z3950' ?
137 $self->{record}->rawdata( @_ ) :
138 $self->{record}->raw( @_ );
141 *{__PACKAGE__ . '::raw'} = \&rawdata;
146 my $method = $AUTOLOAD;
147 $method =~ s/.*://; # strip fully-qualified portion
149 return $self->{record}->$method( @_ );
152 #-------------------------------------------------------------------------------
153 package OpenILS::Utils::ZClient::Event;
163 sub RECV_RECORD { 8 }
164 sub RECV_SEARCH { 9 }