+ $AUTOLOAD =~ s/.*:://; # Chop off the package name,
+ # leaving only the function name.
+ return $self->config($AUTOLOAD);
+}
+
+=head2 Zconn
+
+ $Zconn = C4::Context->Zconn
+
+Returns a connection to the Zebra database for the current
+context. If no connection has yet been made, this method
+creates one and connects.
+
+C<$self>
+
+C<$server> one of the servers defined in the koha-conf.xml file
+
+C<$async> whether this is a asynchronous connection
+
+C<$auth> whether this connection has rw access (1) or just r access (0 or NULL)
+
+
+=cut
+
+sub Zconn {
+ my $self=shift;
+ my $server=shift;
+ my $async=shift;
+ my $auth=shift;
+ my $piggyback=shift;
+ my $syntax=shift;
+ if ( defined($context->{"Zconn"}->{$server}) && (0 == $context->{"Zconn"}->{$server}->errcode()) ) {
+ return $context->{"Zconn"}->{$server};
+ # No connection object or it died. Create one.
+ }else {
+ # release resources if we're closing a connection and making a new one
+ # FIXME: this needs to be smarter -- an error due to a malformed query or
+ # a missing index does not necessarily require us to close the connection
+ # and make a new one, particularly for a batch job. However, at
+ # first glance it does not look like there's a way to easily check
+ # the basic health of a ZOOM::Connection
+ $context->{"Zconn"}->{$server}->destroy() if defined($context->{"Zconn"}->{$server});
+
+ $context->{"Zconn"}->{$server} = &_new_Zconn($server,$async,$auth,$piggyback,$syntax);
+ return $context->{"Zconn"}->{$server};
+ }
+}
+
+=head2 _new_Zconn
+
+$context->{"Zconn"} = &_new_Zconn($server,$async);
+
+Internal function. Creates a new database connection from the data given in the current context and returns it.
+
+C<$server> one of the servers defined in the koha-conf.xml file
+
+C<$async> whether this is a asynchronous connection
+
+C<$auth> whether this connection has rw access (1) or just r access (0 or NULL)
+
+=cut
+
+sub _new_Zconn {
+ my ($server,$async,$auth,$piggyback,$syntax) = @_;
+
+ my $tried=0; # first attempt
+ my $Zconn; # connection object
+ $server = "biblioserver" unless $server;
+ $syntax = "usmarc" unless $syntax;
+
+ my $host = $context->{'listen'}->{$server}->{'content'};
+ my $servername = $context->{"config"}->{$server};
+ my $user = $context->{"serverinfo"}->{$server}->{"user"};
+ my $password = $context->{"serverinfo"}->{$server}->{"password"};
+ $auth = 1 if($user && $password);
+ retry:
+ eval {
+ # set options
+ my $o = new ZOOM::Options();
+ $o->option(user=>$user) if $auth;
+ $o->option(password=>$password) if $auth;
+ $o->option(async => 1) if $async;
+ $o->option(count => $piggyback) if $piggyback;
+ $o->option(cqlfile=> $context->{"server"}->{$server}->{"cql2rpn"});
+ $o->option(cclfile=> $context->{"serverinfo"}->{$server}->{"ccl2rpn"});
+ $o->option(preferredRecordSyntax => $syntax);
+ $o->option(elementSetName => "F"); # F for 'full' as opposed to B for 'brief'
+ $o->option(databaseName => ($servername?$servername:"biblios"));
+
+ # create a new connection object
+ $Zconn= create ZOOM::Connection($o);
+
+ # forge to server
+ $Zconn->connect($host, 0);
+
+ # check for errors and warn
+ if ($Zconn->errcode() !=0) {
+ warn "something wrong with the connection: ". $Zconn->errmsg();
+ }
+
+ };
+# if ($@) {
+# # Koha manages the Zebra server -- this doesn't work currently for me because of permissions issues
+# # Also, I'm skeptical about whether it's the best approach
+# warn "problem with Zebra";
+# if ( C4::Context->preference("ManageZebra") ) {
+# if ($@->code==10000 && $tried==0) { ##No connection try restarting Zebra
+# $tried=1;
+# warn "trying to restart Zebra";
+# my $res=system("zebrasrv -f $ENV{'KOHA_CONF'} >/koha/log/zebra-error.log");
+# goto "retry";
+# } else {
+# warn "Error ", $@->code(), ": ", $@->message(), "\n";
+# $Zconn="error";
+# return $Zconn;
+# }
+# }
+# }
+ return $Zconn;