2 package OpenILS::Application::Search::Z3950;
3 use strict; use warnings;
4 use base qw/OpenSRF::Application/;
10 use OpenSRF::Utils::SettingsClient;
12 use OpenILS::Utils::FlatXML;
13 use OpenILS::Application::Cat::Utils;
14 use OpenILS::Application::AppUtils;
16 use OpenSRF::Utils::Logger qw/$logger/;
18 use OpenSRF::EX qw(:try);
20 my $utils = "OpenILS::Application::Cat::Utils";
21 my $apputils = "OpenILS::Application::AppUtils";
23 use OpenILS::Utils::ModsParser;
26 my $output = "USMARC"; # only support output for now
37 $settings_client = OpenSRF::Utils::SettingsClient->new();
38 $host = $settings_client->config_value("z3950", "oclc", "host");
39 $port = $settings_client->config_value("z3950", "oclc", "port");
40 $database = $settings_client->config_value("z3950", "oclc", "db");
41 $attr = $settings_client->config_value("z3950", "oclc", "attr");
42 $username = $settings_client->config_value("z3950", "oclc", "username");
43 $password = $settings_client->config_value("z3950", "oclc", "password");
45 $logger->info("z3950: Search App connecting: host=$host, port=$port, ".
46 "db=$database, attr=$attr, username=$username, password=$password" );
50 __PACKAGE__->register_method(
51 method => "z39_search_by_string",
52 api_name => "open-ils.search.z3950.raw_string",
55 sub z39_search_by_string {
57 my( $self, $client, $server,
58 $port, $db, $search, $user, $pw ) = @_;
60 throw OpenSRF::EX::InvalidArg unless(
61 $server and $port and $db and $search);
64 $logger->info("Z3950: searching for $search");
69 my $conn = new Net::Z3950::Connection(
74 preferredRecordSyntax => $output,
78 my $rs = $conn->search( $search );
80 throw OpenSRF::EX::ERROR ("z39 search failed");
86 $hash->{count} = $rs->size();
87 $logger->info("Z3950: Search recovered " . $hash->{count} . " records");
89 # until there is a more graceful way to handle this
90 if($hash->{count} > 20) { return $hash; }
93 for( my $x = 0; $x != $hash->{count}; $x++ ) {
94 $logger->debug("z3950: Churning on z39 record count $x");
96 my $rec = $rs->record($x+1);
97 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
99 my $marcxml = $marc->as_xml();
100 my $flat = OpenILS::Utils::FlatXML->new( xml => $marcxml );
101 my $doc = $flat->xml_to_doc();
104 if( $doc->documentElement->nodeName =~ /collection/io ) {
105 $doc->setDocumentElement( $doc->documentElement->firstChild );
106 $doc->documentElement->setNamespace(
107 "http://www.loc.gov/MARC21/slim", undef, 1);
110 $logger->debug("z3950: Turning doc into a nodeset...");
116 my $nodes = OpenILS::Utils::FlatXML->new->xmldoc_to_nodeset($doc);
117 $logger->debug("z3950: turning nodeset into tree");
118 $tree = $utils->nodeset2tree( $nodes->nodeset );
124 $logger->error("z3950: Error turning doc into nodeset/node tree: $err");
126 push @$records, $tree;
131 $hash->{records} = $records;
137 __PACKAGE__->register_method(
138 method => "import_search",
139 api_name => "open-ils.search.z3950.import",
143 my($self, $client, $user_session, $string) = @_;
146 $apputils->check_user_session( $user_session ); #throws EX on error
148 return $self->z39_search_by_string(
149 $client, $host, $port, $database,
150 "\@attr 1=$attr \"$string\"", $username, $password );