]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm
onward and upward
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Z3950.pm
1 #!/usr/bin/perl
2 package OpenILS::Application::Search::Z3950;
3 use strict; use warnings;
4 use base qw/OpenSRF::Application/;
5
6
7 use Net::Z3950;
8 use MARC::Record;
9 use MARC::File::XML;
10 use OpenSRF::Utils::SettingsClient;
11
12 use OpenILS::Utils::FlatXML;
13 use OpenILS::Application::Cat::Utils;
14 use OpenILS::Application::AppUtils;
15
16 my $utils = "OpenILS::Application::Cat::Utils";
17 my $apputils = "OpenILS::Application::AppUtils";
18
19 use OpenILS::Utils::ModsParser;
20 use Data::Dumper;
21
22 my $output = "USMARC"; # only support output for now
23 my $host;
24 my $port;
25 my $database;
26 my $attr;
27 my $username;
28 my $password;
29
30 my $settings_client;
31
32 sub initialize {
33         $settings_client = OpenSRF::Utils::SettingsClient->new();
34         $host                   = $settings_client->config_value("z3950", "oclc", "host");
35         $port                   = $settings_client->config_value("z3950", "oclc", "port");
36         $database       = $settings_client->config_value("z3950", "oclc", "db");
37         $attr                   = $settings_client->config_value("z3950", "oclc", "attr");
38         $username       = $settings_client->config_value("z3950", "oclc", "username");
39         $password       = $settings_client->config_value("z3950", "oclc", "password");
40 }
41
42
43 __PACKAGE__->register_method(
44         method  => "z39_search_by_string",
45         api_name        => "open-ils.search.z3950.raw_string",
46 );
47
48 sub z39_search_by_string {
49
50         my( $self, $client, $server, 
51                         $port, $db, $search, $user, $pw ) = @_;
52
53         throw OpenSRF::EX::InvalidArg unless( 
54                         $server and $port and $db and $search);
55
56
57         warn "Z39.50 search for $search\n";
58
59         $user ||= "";
60         $pw     ||= "";
61
62         my $conn = new Net::Z3950::Connection(
63                 $server, $port, 
64                 databaseName                            => $db, 
65                 user                                                    => $user,
66                 password                                                => $pw,
67                 preferredRecordSyntax   => $output, 
68         );
69
70
71         my $rs = $conn->search( $search );
72         if(!$rs) {
73                 throw OpenSRF::EX::ERROR ("z39 search failed"); 
74         }
75
76         my $records = [];
77         my $hash = {};
78
79         $hash->{count} =  $rs->size();
80         warn "Z3950 Search recovered " . $hash->{count} . " records\n";
81
82         # until there is a more graceful way to handle this
83         if($hash->{count} > 20) { return $hash; }
84
85
86         for( my $x = 0; $x != $hash->{count}; $x++ ) {
87                 warn "Churning on z39 record count $x\n";
88
89                 my $rec = $rs->record($x+1);
90                 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
91
92                 my $marcxml = $marc->as_xml();
93                 my $flat = OpenILS::Utils::FlatXML->new( xml => $marcxml ); 
94                 my $doc = $flat->xml_to_doc();
95
96
97                 if( $doc->documentElement->nodeName =~ /collection/io ) {
98                         $doc->setDocumentElement( $doc->documentElement->firstChild );
99                         $doc->documentElement->setNamespace(
100                                         "http://www.loc.gov/MARC21/slim", undef, 1);
101                 }
102
103                 warn $doc->toString . "\n";
104
105                 my $nodes = $flat->xmldoc_to_nodeset($doc);
106
107                 warn "turning nodeset into tree\n";
108                 my $tree = $utils->nodeset2tree( $nodes->nodeset );
109
110                 push @$records, $tree;
111         }
112
113         $hash->{records} = $records;
114         return $hash;
115
116 }
117
118
119 __PACKAGE__->register_method(
120         method  => "import_search",
121         api_name        => "open-ils.search.z3950.import",
122 );
123
124 sub import_search {
125         my($self, $client, $user_session, $string) = @_;
126
127         my $user_obj = 
128                 $apputils->check_user_session( $user_session ); #throws EX on error
129
130         return $self->z39_search_by_string(
131                 $client, $host, $port, $database, 
132                         "\@attr 1=$attr \"$string\"", $username, $password );
133 }
134
135
136
137
138 1;