1 package OpenILS::Application::Search;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
5 use OpenILS::Utils::Fieldmapper;
6 use Time::HiRes qw(time);
7 use OpenILS::Application::Cat::Utils;
9 use OpenSRF::EX qw(:try);
11 # used for cat search classes
12 my $cat_search_hash = {
15 { tag => "100", subfield => "a"} ,
16 { tag => "700", subfield => "a"},
20 { tag => "245", subfield => "a"},
21 { tag => "242", subfield => "a"},
22 { tag => "240", subfield => "a"},
23 { tag => "210", subfield => "a"},
27 { tag => "650", subfield => "_" },
31 { tag => "035", subfield => "_" },
37 __PACKAGE__->register_method(
38 method => "cat_biblio_search_tcn",
39 api_name => "open-ils.search.cat.biblio.tcn",
41 note => "Searches biblio information by search class",
44 sub cat_biblio_search_tcn {
46 my( $self, $client, $tcn ) = @_;
48 $tcn =~ s/.*?(\w+)\s*$/$1/o;
49 warn "Searching TCN $tcn\n";
51 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
52 my $request = $session->request(
53 "open-ils.storage.biblio.record_entry.search.tcn_value", $tcn );
54 my $response = $request->recv();
57 unless ($response) { return []; }
59 if($response->isa("OpenSRF::EX")) {
60 throw $response ($response->stringify);
63 my $record_entry = $response->content;
65 for my $record (@$record_entry) {
66 push @ids, $record->id;
69 return _records_to_mods( @ids );
73 __PACKAGE__->register_method(
74 method => "cat_biblio_search_class",
75 api_name => "open-ils.search.cat.biblio.class",
77 note => "Searches biblio information by search class",
80 sub cat_biblio_search_class {
81 my( $self, $client, $class, $sort, $string ) = @_;
83 warn "Starting search " . time() . "\n";
87 warn "Searching $class, $sort, $string\n";
89 warn "Looking up method: " . time() . "\n";
91 my $method = $self->method_lookup("open-ils.search.biblio.marc");
93 throw OpenSRF::EX::PANIC
94 ("Can't lookup method 'open-ils.search.biblio.marc'");
97 warn "Running: " . time() . "\n";
99 my ($records) = $method->run( $cat_search_hash->{$class}, $string );
103 for my $i (@$records) { push @ids, $i->[0]; }
105 warn "Found Id's: @ids " . time() . "\n";
107 return _records_to_mods(@ids);
113 __PACKAGE__->register_method(
114 method => "biblio_search_marc",
115 api_name => "open-ils.search.biblio.marc",
117 note => "Searches biblio information by marc tag",
120 sub biblio_search_marc {
122 my( $self, $client, $search_hash, $string ) = @_;
124 my $session = OpenSRF::AppSession->create("open-ils.storage");
125 my $request = $session->request(
126 "open-ils.storage.metabib.full_rec.search_fts.index_vector", $search_hash, $string );
128 my $response = $request->recv();
129 if($response and $response->isa("OpenSRF::EX")) {
130 throw $response ($response->stringify);
133 my $data = $response->content;
137 $session->disconnect();
149 # ---------------------------------------------------------------------------
150 # takes a list of record id's and turns the docs into friendly
152 # ---------------------------------------------------------------------------
153 sub _records_to_mods {
159 my $session = OpenSRF::AppSession->create("open-ils.storage");
160 my $request = $session->request(
161 "open-ils.storage.biblio.record_marc.batch.retrieve", @ids );
163 my $last_content = undef;
165 while( my $response = $request->recv() ) {
167 if( $last_content ) {
168 my $u = OpenILS::Application::Cat::Utils->new();
169 $u->start_mods_batch( $last_content->marc );
170 my $mods = $u->finish_mods_batch();
171 $mods->{doc_id} = $last_content->id();
172 warn "Turning doc " . $mods->{doc_id} . " into MODS\n";
173 $last_content = undef;
174 push @results, $mods;
177 next unless $response;
179 if($response->isa("OpenSRF::EX")) {
180 throw $response ($response->stringify);
183 $last_content = $response->content;
187 if( $last_content ) {
188 my $u = OpenILS::Application::Cat::Utils->new();
189 $u->start_mods_batch( $last_content->marc );
190 my $mods = $u->finish_mods_batch();
191 $mods->{doc_id} = $last_content->id();
192 push @results, $mods;
197 $session->disconnect();