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 my $utils = "OpenILS::Application::Cat::Utils";
16 use OpenILS::Utils::ModsParser;
18 my $output = "USMARC"; # only support output for now
29 $settings_client = OpenSRF::Utils::SettingsClient->new();
30 $host = $settings_client->config_value("z3950", "oclc", "host");
31 $port = $settings_client->config_value("z3950", "oclc", "port");
32 $database = $settings_client->config_value("z3950", "oclc", "db");
33 $attr = $settings_client->config_value("z3950", "oclc", "attr");
34 $username = $settings_client->config_value("z3950", "oclc", "username");
35 $password = $settings_client->config_value("z3950", "oclc", "password");
39 __PACKAGE__->register_method(
40 method => "z39_search_by_string",
41 api_name => "open-ils.search.z3950.raw_string",
44 sub z39_search_by_string {
46 my( $self, $client, $server,
47 $port, $db, $search, $user, $pw ) = @_;
49 throw OpenSRF::EX::InvalidArg unless(
50 $server and $port and $db and $search);
53 warn "Z39.50 search for $search\n";
58 my $conn = new Net::Z3950::Connection(
63 preferredRecordSyntax => $output,
67 my $rs = $conn->search( $search );
69 throw OpenSRF::EX::ERROR ("z39 search failed");
75 $hash->{count} = $rs->size();
76 warn "Z3950 Search recovered " . $hash->{count} . " records\n";
78 # until there is a more graceful way to handle this
79 if($hash->{count} > 20) { return $hash; }
82 for( my $x = 0; $x != $hash->{count}; $x++ ) {
83 my $rec = $rs->record($x+1);
84 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
86 my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marc->as_xml() );
87 warn "turning nodeset into tree\n";
88 my $tree = $utils->nodeset2tree( $nodes->nodeset );
90 push @$records, $tree;
93 $hash->{records} = $records;
99 __PACKAGE__->register_method(
100 method => "import_search",
101 api_name => "open-ils.search.z3950.import",
105 my($self, $client, $string) = @_;
107 return $self->z39_search_by_string(
108 $client, $host, $port, $database,
109 "\@attr 1=$attr \"$string\"", $username, $password );