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, $org_id, $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;
70 my $record_list = _records_to_mods( @ids );
72 for my $rec (@$record_list) {
73 $client->respond($rec);
75 #return _records_to_mods( @ids );
82 __PACKAGE__->register_method(
83 method => "cat_biblio_search_class",
84 api_name => "open-ils.search.cat.biblio.class",
86 note => "Searches biblio information by search class",
89 sub cat_biblio_search_class {
91 my( $self, $client, $org_id, $class, $sort, $string ) = @_;
93 throw OpenSRF::EX::InvalidArg
94 ("Not enough args to open-ils.search.cat.biblio.class")
95 unless( defined($org_id) and $class and $sort and $string );
100 my $method = $self->method_lookup("open-ils.search.biblio.marc");
102 throw OpenSRF::EX::PANIC
103 ("Can't lookup method 'open-ils.search.biblio.marc'");
106 my ($records) = $method->run( $cat_search_hash->{$class}, $string );
109 for my $i (@$records) { push @ids, $i->[0]; }
111 my $mods_list = _records_to_mods( @ids );
113 # ---------------------------------------------------------------
114 # append copy count information to the mods objects
115 my $session = OpenSRF::AppSession->create("open-ils.storage");
117 my $request = $session->request(
118 "open-ils.storage.biblio.record_copy_count.batch", $org_id, @ids );
122 warn "receiving copy counts for doc $id\n";
124 my $response = $request->recv();
125 next unless $response;
127 if( $response and UNIVERSAL::isa($response, "Error")) {
128 throw $response ($response->stringify);
131 my $count = $response->content;
132 my $mods_obj = undef;
133 for my $m (@$mods_list) {
134 $mods_obj = $m if ($m->{doc_id} == $id)
137 $mods_obj->{copy_count} = $count;
140 $client->respond( $mods_obj );
146 $session->disconnect();
148 # ---------------------------------------------------------------
160 __PACKAGE__->register_method(
161 method => "biblio_search_marc",
162 api_name => "open-ils.search.biblio.marc",
164 note => "Searches biblio information by marc tag",
167 sub biblio_search_marc {
169 my( $self, $client, $search_hash, $string ) = @_;
171 my $session = OpenSRF::AppSession->create("open-ils.storage");
172 my $request = $session->request(
173 "open-ils.storage.metabib.full_rec.search_fts.index_vector", $search_hash, $string );
175 my $response = $request->recv();
176 if($response and $response->isa("OpenSRF::EX")) {
177 throw $response ($response->stringify);
180 my $data = $response->content;
184 $session->disconnect();
194 __PACKAGE__->register_method(
195 method => "get_org_tree",
196 api_name => "open-ils.search.actor.org_tree.retrieve",
198 note => "Returns the entire org tree structure",
203 my( $self, $client, $user_session ) = @_;
205 if( $user_session ) {
208 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
211 my $session = OpenSRF::AppSession->create("open-ils.storage");
212 my $request = $session->request(
213 "open-ils.storage.actor.org_unit.retrieve", $user_obj->home_ou );
214 my $response = $request->recv();
217 throw OpenSRF::EX::ERROR (
218 "No response from storage for org_unit retrieve");
220 if(UNIVERSAL::isa($response,"Error")) {
221 throw $response ($response->stringify);
224 my $home_ou = $response->content;
226 # XXX grab descendants and build org tree from them
228 my $request = $session->request(
229 "open-ils.storage.actor.org_unit_descendants" );
230 my $response = $request->recv();
232 throw OpenSRF::EX::ERROR (
233 "No response from storage for org_unit retrieve");
235 if(UNIVERSAL::isa($response,"Error")) {
236 throw $response ($response->stringify);
239 my $descendants = $response->content;
242 $session->disconnect();
248 return OpenILS::Application::AppUtils->get_org_tree();
253 __PACKAGE__->register_method(
254 method => "copy_count_by_org_unit",
255 api_name => "open-ils.search.copy_count_by_location",
257 note => "Searches biblio information by marc tag",
260 sub copy_count_by_org_unit {
261 my( $self, $client, $org_id, @record_ids ) = @_;
263 my $session = OpenSRF::AppSession->create("open-ils.storage");
264 my $request = $session->request(
265 "open-ils.storage.biblio.record_copy_count.batch", $org_id, @record_ids );
267 for my $id (@record_ids) {
269 my $response = $request->recv();
270 next unless $response;
272 if( $response and UNIVERSAL::isa($response, "Error")) {
273 throw $response ($response->stringify);
276 my $count = $response->content;
277 $client->respond( { record => $id, count => $count } );
281 $session->disconnect();
293 # ---------------------------------------------------------------------------
294 # takes a list of record id's and turns the docs into friendly
296 # ---------------------------------------------------------------------------
297 sub _records_to_mods {
303 my $session = OpenSRF::AppSession->create("open-ils.storage");
304 my $request = $session->request(
305 "open-ils.storage.biblio.record_marc.batch.retrieve", @ids );
307 my $last_content = undef;
309 while( my $response = $request->recv() ) {
311 if( $last_content ) {
312 my $u = OpenILS::Application::Cat::Utils->new();
313 $u->start_mods_batch( $last_content->marc );
314 my $mods = $u->finish_mods_batch();
315 $mods->{doc_id} = $last_content->id();
316 warn "Turning doc " . $mods->{doc_id} . " into MODS\n";
317 $last_content = undef;
318 push @results, $mods;
321 next unless $response;
323 if($response->isa("OpenSRF::EX")) {
324 throw $response ($response->stringify);
327 $last_content = $response->content;
331 if( $last_content ) {
332 my $u = OpenILS::Application::Cat::Utils->new();
333 $u->start_mods_batch( $last_content->marc );
334 my $mods = $u->finish_mods_batch();
335 $mods->{doc_id} = $last_content->id();
336 push @results, $mods;
341 $session->disconnect();