made the z searches more generic by allowing a service name to be provided
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 21 Feb 2006 16:04:39 +0000 (16:04 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Tue, 21 Feb 2006 16:04:39 +0000 (16:04 +0000)
to the queries

git-svn-id: svn://svn.open-ils.org/ILS/trunk@3143 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/extras/ils_events.xml
Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm

index 79f8657..eeeba68 100644 (file)
                </desc>
        </event>
 
+       <event code='1003' textcode='Z3950_SEARCH_FAILED'>
+               <desc>The Z search did not succeed</desc>
+       </event>
+
        <event code='1200' textcode='USERNAME_EXISTS'>
                <desc>
                        The given username exists in the database
index d513f23..19ec0f8 100755 (executable)
@@ -12,6 +12,7 @@ use OpenSRF::Utils::SettingsClient;
 use OpenILS::Utils::FlatXML;
 use OpenILS::Application::Cat::Utils;
 use OpenILS::Application::AppUtils;
+use OpenILS::Event;
 
 use OpenSRF::Utils::Logger qw/$logger/;
 
@@ -19,6 +20,7 @@ use OpenSRF::EX qw(:try);
 
 my $utils = "OpenILS::Application::Cat::Utils";
 my $apputils = "OpenILS::Application::AppUtils";
+my $U = $apputils;
 
 use OpenILS::Utils::ModsParser;
 use Data::Dumper;
@@ -27,23 +29,41 @@ my $output = "USMARC"; # only support output for now
 my $host;
 my $port;
 my $database;
-my $attr;
+my $tcnattr;
+my $isbnattr;
 my $username;
 my $password;
+my $defserv;
 
 my $settings_client;
 
 sub initialize {
        $settings_client = OpenSRF::Utils::SettingsClient->new();
-       $host                   = $settings_client->config_value("z3950", "oclc", "host");
-       $port                   = $settings_client->config_value("z3950", "oclc", "port");
-       $database       = $settings_client->config_value("z3950", "oclc", "db");
-       $attr                   = $settings_client->config_value("z3950", "oclc", "attr");
-       $username       = $settings_client->config_value("z3950", "oclc", "username");
-       $password       = $settings_client->config_value("z3950", "oclc", "password");
-
-       $logger->info("z3950:  Search App connecting:  host=$host, port=$port, ".
-               "db=$database, attr=$attr, username=$username, password=$password" );
+
+       $defserv                = $settings_client->config_value("z3950", "default" );
+
+       ( $host, $port, $database, $username, $password ) = _load_settings($defserv);
+       $tcnattr                = $settings_client->config_value("z3950", $defserv, "tcnattr");
+       $isbnattr       = $settings_client->config_value("z3950", $defserv, "isbnattr");
+
+       $logger->info("z3950: Loading Defaults: service=$defserv, host=$host, port=$port, ".
+               "db=$database, tcnattr=$tcnattr, isbnattr=$isbnattr, username=$username, password=$password" );
+}
+
+sub _load_settings {
+       my $service = shift;
+
+       if( $service eq $defserv and $host ) {
+               return ( $host, $port, $database, $username, $password );
+       }
+
+       return (
+               $settings_client->config_value("z3950", $service, "host"),
+               $settings_client->config_value("z3950", $service, "port"),
+               $settings_client->config_value("z3950", $service, "db"),
+               $settings_client->config_value("z3950", $service, "username"),
+               $settings_client->config_value("z3950", $service, "password"),
+       );
 }
 
 
@@ -53,20 +73,22 @@ __PACKAGE__->register_method(
 );
 
 sub marcxml_to_brn {
-
        my( $self, $client, $marcxml ) = @_;
 
        my $tree;
        my $err;
 
+       # Strip the namespace info from the <collection> node and shove it into
+       # the <record> node, if the collection node exists
+       my ($ns) = ( $marcxml =~ /<collection(.*)?>/og );
+       $logger->info("marcxml_to_brn extracted namespace info: $ns") if $ns;
+       $marcxml =~ s/<collection(.*)?>//og;
+       $marcxml =~ s/<\/collection>//og;
+       $marcxml =~ s/<record>/<record $ns>/og if $ns;
+
        my $flat = OpenILS::Utils::FlatXML->new( xml => $marcxml ); 
        my $doc = $flat->xml_to_doc();
 
-       if( $doc->documentElement->nodeName =~ /collection/io ) {
-               $doc->setDocumentElement( $doc->documentElement->firstChild );
-               $doc->documentElement->setNamespace(
-                               "http://www.loc.gov/MARC21/slim", undef, 1);
-       }
        $logger->debug("z3950: Turning doc into a nodeset...");
 
        try {
@@ -92,31 +114,45 @@ __PACKAGE__->register_method(
 
 sub z39_search_by_string {
 
-       my( $self, $client, $server, 
-                       $port, $db, $search, $user, $pw ) = @_;
+       my( $self, $connection, $authtoken, $params ) = @_;
+       my( $hst, $prt, $db, $usr, $pw );
+
+
+       my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
+       return $evt if $evt;
+       my $service = $$params{service};
+       my $search      = $$params{search};
+
+       if( $service ) {
+               ($hst, $prt, $db, $usr, $pw ) = _load_settings($$params{service});
+       } else {
+               $hst    = $$params{host};
+               $prt    = $$params{prt};
+               $db     = $$params{db};
+               $usr    = $$params{username};
+               $pw     = $$params{password};
+               $service = "(custom)";
+       }
 
-       throw OpenSRF::EX::InvalidArg unless( 
-                       $server and $port and $db and $search);
 
+       $logger->info("z3950:  Search App connecting:  service=$service, ".
+               "host=$hst, port=$prt, db=$db, username=$usr, password=$pw, search=$search" );
 
-       $logger->info("Z3950: searching for $search");
+       return OpenILS::Event->new('BAD_PARAMS') unless ($hst and $prt and $db);
 
-       $user ||= "";
-       $pw     ||= "";
+       $usr ||= ""; $pw        ||= "";
 
        my $conn = new Net::Z3950::Connection(
-               $server, $port, 
+               $hst, $prt, 
                databaseName                            => $db, 
-               user                                                    => $user,
+               user                                                    => $usr,
                password                                                => $pw,
                preferredRecordSyntax   => $output, 
        );
 
 
        my $rs = $conn->search( $search );
-       if(!$rs) {
-               throw OpenSRF::EX::ERROR ("z39 search failed"); 
-       }
+       return OpenILS::Event->new('Z3950_SEARCH_FAILED') unless $rs;
 
        # We want nice full records
        $rs->option(elementSetName => "f");
@@ -125,7 +161,7 @@ sub z39_search_by_string {
        my $hash = {};
 
        $hash->{count} =  $rs->size();
-       $logger->info("Z3950: Search recovered " . $hash->{count} . " records");
+       $logger->info("z3950: Search recovered " . $hash->{count} . " records");
 
        # until there is a more graceful way to handle this
        if($hash->{count} > 20) { return $hash; }
@@ -155,22 +191,68 @@ sub z39_search_by_string {
 
 
 __PACKAGE__->register_method(
-       method  => "import_search",
-       api_name        => "open-ils.search.z3950.import",
+       method  => "tcn_search",
+       api_name        => "open-ils.search.z3950.tcn",
+);
+
+sub tcn_search {
+       my($self, $connection, $authtoken, $tcn, $service) = @_;
+
+       my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
+       return $evt if $evt;
+       $service ||= $defserv;
+
+       my $attr = $settings_client->config_value("z3950", $service, "tcnattr");
+
+       $logger->info("z3950: Searching for TCN $tcn");
+
+       return $self->z39_search_by_string(
+               $connection, $authtoken, {
+                       search => "\@attr 1=$attr \"$tcn\"", 
+                       service => $service });
+}
+
+
+__PACKAGE__->register_method(
+       method  => "isbn_search",
+       api_name        => "open-ils.search.z3950.isbn",
 );
 
-sub import_search {
-       my($self, $client, $user_session, $string) = @_;
+sub isbn_search {
+       my( $self, $connection, $authtoken, $isbn, $service ) = @_;
+
+       my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
+       return $evt if $evt;
+       $service ||= $defserv;
 
-       my $user_obj = 
-               $apputils->check_user_session( $user_session ); #throws EX on error
+       my $attr = $settings_client->config_value("z3950", $service, "isbnattr");
+
+       $logger->info("z3950: Performing ISBN search : $isbn");
 
        return $self->z39_search_by_string(
-               $client, $host, $port, $database, 
-                       "\@attr 1=$attr \"$string\"", $username, $password );
+               $connection, $authtoken, {
+                       search => "\@attr 1=$attr \"$isbn\"", 
+                       service => $service });
+}
+
+
+__PACKAGE__->register_method(
+       method  => "query_interfaces",
+       api_name        => "open-ils.search.z3950.services.retrieve",
+);
+
+sub query_interfaces {
+       my( $self, $client, $authtoken ) = @_;
+       my( $requestor, $evt ) = $U->checksesperm($authtoken, 'REMOTE_Z3950_QUERY');
+
+       my $services = $settings_client->config_value("z3950");
+       $services = { $services } unless ref($services);
+
+       return [ grep { $_ ne 'default' } keys %$services ];
 }
 
 
 
 
+
 1;