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 my $utils = "OpenILS::Application::Cat::Utils";
17 my $apputils = "OpenILS::Application::AppUtils";
19 use OpenILS::Utils::ModsParser;
21 my $output = "USMARC"; # only support output for now
32 $settings_client = OpenSRF::Utils::SettingsClient->new();
33 $host = $settings_client->config_value("z3950", "oclc", "host");
34 $port = $settings_client->config_value("z3950", "oclc", "port");
35 $database = $settings_client->config_value("z3950", "oclc", "db");
36 $attr = $settings_client->config_value("z3950", "oclc", "attr");
37 $username = $settings_client->config_value("z3950", "oclc", "username");
38 $password = $settings_client->config_value("z3950", "oclc", "password");
42 __PACKAGE__->register_method(
43 method => "z39_search_by_string",
44 api_name => "open-ils.search.z3950.raw_string",
47 sub z39_search_by_string {
49 my( $self, $client, $server,
50 $port, $db, $search, $user, $pw ) = @_;
52 throw OpenSRF::EX::InvalidArg unless(
53 $server and $port and $db and $search);
56 warn "Z39.50 search for $search\n";
61 my $conn = new Net::Z3950::Connection(
66 preferredRecordSyntax => $output,
70 my $rs = $conn->search( $search );
72 throw OpenSRF::EX::ERROR ("z39 search failed");
78 $hash->{count} = $rs->size();
79 warn "Z3950 Search recovered " . $hash->{count} . " records\n";
81 # until there is a more graceful way to handle this
82 if($hash->{count} > 20) { return $hash; }
85 for( my $x = 0; $x != $hash->{count}; $x++ ) {
86 my $rec = $rs->record($x+1);
87 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
89 my $nodes = OpenILS::Utils::FlatXML->new()->xml_to_nodeset( $marc->as_xml() );
90 warn "turning nodeset into tree\n";
91 my $tree = $utils->nodeset2tree( $nodes->nodeset );
93 push @$records, $tree;
96 $hash->{records} = $records;
102 __PACKAGE__->register_method(
103 method => "import_search",
104 api_name => "open-ils.search.z3950.import",
108 my($self, $client, $user_session, $string) = @_;
111 $apputils->check_user_session( $user_session ); #throws EX on error
113 return $self->z39_search_by_string(
114 $client, $host, $port, $database,
115 "\@attr 1=$attr \"$string\"", $username, $password );