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 {
82 my( $self, $client, $class, $sort, $string ) = @_;
86 my $method = $self->method_lookup("open-ils.search.biblio.marc");
88 throw OpenSRF::EX::PANIC
89 ("Can't lookup method 'open-ils.search.biblio.marc'");
92 my ($records) = $method->run( $cat_search_hash->{$class}, $string );
95 for my $i (@$records) { push @ids, $i->[0]; }
96 return _records_to_mods(@ids);
102 __PACKAGE__->register_method(
103 method => "biblio_search_marc",
104 api_name => "open-ils.search.biblio.marc",
106 note => "Searches biblio information by marc tag",
109 sub biblio_search_marc {
111 my( $self, $client, $search_hash, $string ) = @_;
113 my $session = OpenSRF::AppSession->create("open-ils.storage");
114 my $request = $session->request(
115 "open-ils.storage.metabib.full_rec.search_fts.index_vector", $search_hash, $string );
117 my $response = $request->recv();
118 if($response and $response->isa("OpenSRF::EX")) {
119 throw $response ($response->stringify);
122 my $data = $response->content;
126 $session->disconnect();
136 __PACKAGE__->register_method(
137 method => "get_org_tree",
138 api_name => "open-ils.search.actor.org_tree.retrieve",
140 note => "Returns the entire org tree structure",
144 my( $self, $client ) = @_;
145 return OpenILS::Application::AppUtils->get_org_tree();
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();