]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Z3950.pm
0d061feafd911ae3b9102cf32a524a7c97507c97
[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 use OpenSRF::Utils::Logger qw/$logger/;
17
18 use OpenSRF::EX qw(:try);
19
20 my $utils = "OpenILS::Application::Cat::Utils";
21 my $apputils = "OpenILS::Application::AppUtils";
22
23 use OpenILS::Utils::ModsParser;
24 use Data::Dumper;
25
26 my $output = "USMARC"; # only support output for now
27 my $host;
28 my $port;
29 my $database;
30 my $attr;
31 my $username;
32 my $password;
33
34 my $settings_client;
35
36 sub initialize {
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");
44
45         $logger->info("z3950:  Search App connecting:  host=$host, port=$port, ".
46                 "db=$database, attr=$attr, username=$username, password=$password" );
47 }
48
49
50 __PACKAGE__->register_method(
51         method  => "z39_search_by_string",
52         api_name        => "open-ils.search.z3950.raw_string",
53 );
54
55 sub z39_search_by_string {
56
57         my( $self, $client, $server, 
58                         $port, $db, $search, $user, $pw ) = @_;
59
60         throw OpenSRF::EX::InvalidArg unless( 
61                         $server and $port and $db and $search);
62
63
64         $logger->info("Z3950: searching for $search");
65
66         $user ||= "";
67         $pw     ||= "";
68
69         my $conn = new Net::Z3950::Connection(
70                 $server, $port, 
71                 databaseName                            => $db, 
72                 user                                                    => $user,
73                 password                                                => $pw,
74                 preferredRecordSyntax   => $output, 
75         );
76
77
78         my $rs = $conn->search( $search );
79         if(!$rs) {
80                 throw OpenSRF::EX::ERROR ("z39 search failed"); 
81         }
82
83         my $records = [];
84         my $hash = {};
85
86         $hash->{count} =  $rs->size();
87         $logger->info("Z3950: Search recovered " . $hash->{count} . " records");
88
89         # until there is a more graceful way to handle this
90         if($hash->{count} > 20) { return $hash; }
91
92
93         for( my $x = 0; $x != $hash->{count}; $x++ ) {
94                 $logger->debug("z3950: Churning on z39 record count $x");
95
96                 my $rec = $rs->record($x+1);
97                 my $marc = MARC::Record->new_from_usmarc($rec->rawdata());
98
99                 my $marcxml = $marc->as_xml();
100                 my $flat = OpenILS::Utils::FlatXML->new( xml => $marcxml ); 
101                 my $doc = $flat->xml_to_doc();
102
103
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);
108                 }
109
110                 $logger->debug("z3950: Turning doc into a nodeset...");
111
112                 my $tree;
113                 my $err;
114
115                 try {
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 );
119                 } catch Error with {
120                         $err = shift;
121                 };
122
123                 if($err) {
124                         $logger->error("z3950: Error turning doc into nodeset/node tree: $err");
125                 } else {
126                         my $mods;
127                         
128                         my $u = OpenILS::Utils::ModsParser->new();
129                         $u->start_mods_batch( $marcxml );
130                         $mods = $u->finish_mods_batch();
131
132                         push @$records, { 'mvr' => $mods, 'brn' => $tree };
133
134                         #push @$records, $tree;
135                 }
136
137         }
138
139         $logger->debug("z3950: got here near the end with " . scalar(@$records) . " records." );
140
141         $hash->{records} = $records;
142         return $hash;
143
144 }
145
146
147 __PACKAGE__->register_method(
148         method  => "import_search",
149         api_name        => "open-ils.search.z3950.import",
150 );
151
152 sub import_search {
153         my($self, $client, $user_session, $string) = @_;
154
155         my $user_obj = 
156                 $apputils->check_user_session( $user_session ); #throws EX on error
157
158         return $self->z39_search_by_string(
159                 $client, $host, $port, $database, 
160                         "\@attr 1=$attr \"$string\"", $username, $password );
161 }
162
163
164
165
166 1;