]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
commit ;)
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search.pm
1 package OpenILS::Application::Search;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5 use OpenILS::Utils::Fieldmapper;
6 use Time::HiRes qw(time);
7 use OpenILS::Application::Cat::Utils;
8
9 use OpenSRF::EX qw(:try);
10
11 # used for cat search classes
12 my $cat_search_hash =  {
13
14         author => [ 
15                 { tag => "100", subfield => "a"} ,
16                 { tag => "700", subfield => "a"}, 
17         ],
18
19         title => [ 
20                 { tag => "245", subfield => "a"},
21                 { tag => "242", subfield => "a"}, 
22                 { tag => "240", subfield => "a"},
23                 { tag => "210", subfield => "a"},
24         ],
25
26         subject => [ 
27                 { tag => "650", subfield => "_" }, 
28         ],
29
30         tcn     => [
31                 { tag => "035", subfield => "_" },
32         ],
33
34 };
35
36
37 __PACKAGE__->register_method(
38         method  => "cat_biblio_search_tcn",
39         api_name        => "open-ils.search.cat.biblio.tcn",
40         argc            => 3, 
41         note            => "Searches biblio information by search class",
42 );
43
44 sub cat_biblio_search_tcn {
45
46         my( $self, $client, $org_id, $tcn ) = @_;
47
48         $tcn =~ s/.*?(\w+)\s*$/$1/o;
49         warn "Searching TCN $tcn\n";
50
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();
55
56
57         unless ($response) { return []; }
58
59         if($response->isa("OpenSRF::EX")) {
60                 throw $response ($response->stringify);
61         }
62
63         my $record_entry = $response->content;
64         my @ids;
65         for my $record (@$record_entry) {
66                 push @ids, $record->id;
67         }
68
69
70         my $record_list = _records_to_mods( @ids );
71
72         for my $rec (@$record_list) {
73                 $client->respond($rec);
74         }
75 #return _records_to_mods( @ids );
76
77
78         return undef;
79
80 }
81
82 __PACKAGE__->register_method(
83         method  => "cat_biblio_search_class",
84         api_name        => "open-ils.search.cat.biblio.class",
85         argc            => 3, 
86         note            => "Searches biblio information by search class",
87 );
88
89 sub cat_biblio_search_class {
90
91         my( $self, $client, $org_id, $class, $sort, $string ) = @_;
92
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 );
96
97
98         my $search_hash;
99
100         my $method = $self->method_lookup("open-ils.search.biblio.marc");
101         if(!$method) {
102                 throw OpenSRF::EX::PANIC 
103                         ("Can't lookup method 'open-ils.search.biblio.marc'");
104         }
105
106         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
107
108         my @ids;
109         for my $i (@$records) { push @ids, $i->[0]; }
110
111         my $mods_list = _records_to_mods( @ids );
112
113         # ---------------------------------------------------------------
114         # append copy count information to the mods objects
115         my $session = OpenSRF::AppSession->create("open-ils.storage");
116
117         my $request = $session->request(
118                 "open-ils.storage.biblio.record_copy_count.batch",  $org_id, @ids );
119
120         for my $id (@ids) {
121
122                 warn "receiving copy counts for doc $id\n";
123
124                 my $response = $request->recv();
125                 next unless $response;
126
127                 if( $response and UNIVERSAL::isa($response, "Error")) {
128                         throw $response ($response->stringify);
129                 }
130
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)
135                 }
136                 if($mods_obj) {
137                         $mods_obj->{copy_count} = $count;
138                 }
139
140                 $client->respond( $mods_obj );
141
142         }       
143         $request->finish();
144
145         $session->finish();
146         $session->disconnect();
147         $session->kill_me();
148         # ---------------------------------------------------------------
149
150 #       return $mods_list;
151
152         return undef;
153
154 }
155
156
157 __PACKAGE__->register_method(
158         method  => "cat_biblio_search_class_stream",
159         api_name        => "open-ils.search.cat.biblio.class.stream",
160         argc            => 3, 
161         note            => "Searches biblio information by search class",
162 );
163
164 sub cat_biblio_search_class_stream {
165
166         my( $self, $client, $org_id, $class, $sort, $string ) = @_;
167
168         throw OpenSRF::EX::InvalidArg 
169                 ("Not enough args to open-ils.search.cat.biblio.class")
170                         unless( defined($org_id) and $class and $sort and $string );
171
172
173         my $search_hash;
174
175         my $method = $self->method_lookup("open-ils.search.biblio.marc");
176         if(!$method) {
177                 throw OpenSRF::EX::PANIC 
178                         ("Can't lookup method 'open-ils.search.biblio.marc'");
179         }
180
181         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
182
183         my @ids;
184         for my $i (@$records) { push @ids, $i->[0]; }
185
186         my $mods_list = _records_to_mods( @ids );
187
188         # ---------------------------------------------------------------
189         # append copy count information to the mods objects
190         my $session = OpenSRF::AppSession->create("open-ils.storage");
191
192         my $request = $session->request(
193                 "open-ils.storage.biblio.record_copy_count.batch",  $org_id, @ids );
194
195         for my $id (@ids) {
196
197                 warn "receiving copy counts for doc $id\n";
198
199                 my $response = $request->recv();
200                 next unless $response;
201
202                 if( $response and UNIVERSAL::isa($response, "Error")) {
203                         throw $response ($response->stringify);
204                 }
205
206                 my $count = $response->content;
207                 my $mods_obj = undef;
208                 for my $m (@$mods_list) {
209                         $mods_obj = $m if ($m->{doc_id} == $id)
210                 }
211                 if($mods_obj) {
212                         $mods_obj->{copy_count} = $count;
213                 }
214
215                 $client->respond( $mods_obj );
216
217         }       
218         $request->finish();
219
220         $session->finish();
221         $session->disconnect();
222         $session->kill_me();
223         # ---------------------------------------------------------------
224
225 }
226
227
228 __PACKAGE__->register_method(
229         method  => "biblio_search_marc",
230         api_name        => "open-ils.search.biblio.marc",
231         argc            => 1, 
232         note            => "Searches biblio information by marc tag",
233 );
234
235 sub biblio_search_marc {
236
237         my( $self, $client, $search_hash, $string ) = @_;
238
239         my $session = OpenSRF::AppSession->create("open-ils.storage");
240         my $request = $session->request( 
241                         "open-ils.storage.metabib.full_rec.search_fts.index_vector", $search_hash, $string );
242
243         my $response = $request->recv();
244         if($response and $response->isa("OpenSRF::EX")) {
245                 throw $response ($response->stringify);
246         }
247
248         my $data = $response->content;
249
250         $request->finish();
251         $session->finish();
252         $session->disconnect();
253         $session->kill_me();
254
255         return $data;
256
257 }
258
259
260
261
262 __PACKAGE__->register_method(
263         method  => "get_org_tree",
264         api_name        => "open-ils.search.actor.org_tree.retrieve",
265         argc            => 1, 
266         note            => "Returns the entire org tree structure",
267 );
268
269 sub get_org_tree {
270
271         my( $self, $client, $user_session ) = @_;
272
273         if( $user_session ) {
274
275                 my $user_obj = 
276                         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
277
278                 
279                 my $session = OpenSRF::AppSession->create("open-ils.storage");
280                 my $request = $session->request( 
281                                 "open-ils.storage.actor.org_unit.retrieve", $user_obj->home_ou );
282                 my $response = $request->recv();
283
284                 if(!$response) { 
285                         throw OpenSRF::EX::ERROR (
286                                         "No response from storage for org_unit retrieve");
287                 }
288                 if(UNIVERSAL::isa($response,"Error")) {
289                         throw $response ($response->stringify);
290                 }
291
292                 my $home_ou = $response->content;
293
294                 # XXX grab descendants and build org tree from them
295 =head asdf
296                 my $request = $session->request( 
297                                 "open-ils.storage.actor.org_unit_descendants" );
298                 my $response = $request->recv();
299                 if(!$response) { 
300                         throw OpenSRF::EX::ERROR (
301                                         "No response from storage for org_unit retrieve");
302                 }
303                 if(UNIVERSAL::isa($response,"Error")) {
304                         throw $response ($response->stringify);
305                 }
306
307                 my $descendants = $response->content;
308 =cut
309
310                 $session->disconnect();
311                 $session->kill_me();
312
313                 return $home_ou;
314         }
315
316         return OpenILS::Application::AppUtils->get_org_tree();
317 }
318
319
320
321 __PACKAGE__->register_method(
322         method  => "copy_count_by_org_unit",
323         api_name        => "open-ils.search.copy_count_by_location",
324         argc            => 1, 
325         note            => "Searches biblio information by marc tag",
326 );
327
328 sub copy_count_by_org_unit {
329         my( $self, $client, $org_id, @record_ids ) = @_;
330
331         my $session = OpenSRF::AppSession->create("open-ils.storage");
332         my $request = $session->request(
333                 "open-ils.storage.biblio.record_copy_count.batch",  $org_id, @record_ids );
334
335         for my $id (@record_ids) {
336
337                 my $response = $request->recv();
338                 next unless $response;
339
340                 if( $response and UNIVERSAL::isa($response, "Error")) {
341                         throw $response ($response->stringify);
342                 }
343
344                 my $count = $response->content;
345                 $client->respond( { record => $id, count => $count } );
346         }
347
348         $request->finish();
349         $session->disconnect();
350         $session->kill_me();
351         return undef;
352 }
353
354
355
356
357
358
359
360
361 # ---------------------------------------------------------------------------
362 # takes a list of record id's and turns the docs into friendly 
363 # mods structures.
364 # ---------------------------------------------------------------------------
365 sub _records_to_mods {
366         my @ids = @_;
367         
368         my @results;
369         my @marcxml_objs;
370
371         my $session = OpenSRF::AppSession->create("open-ils.storage");
372         my $request = $session->request(
373                         "open-ils.storage.biblio.record_marc.batch.retrieve",  @ids );
374
375         my $last_content = undef;
376
377         while( my $response = $request->recv() ) {
378
379                 if( $last_content ) {
380                         my $u = OpenILS::Application::Cat::Utils->new();
381                         $u->start_mods_batch( $last_content->marc );
382                         my $mods = $u->finish_mods_batch();
383                         $mods->{doc_id} = $last_content->id();
384                         warn "Turning doc " . $mods->{doc_id} . " into MODS\n";
385                         $last_content = undef;
386                         push @results, $mods;
387                 }
388
389                 next unless $response;
390
391                 if($response->isa("OpenSRF::EX")) {
392                         throw $response ($response->stringify);
393                 }
394
395                 $last_content = $response->content;
396
397         }
398
399         if( $last_content ) {
400                 my $u = OpenILS::Application::Cat::Utils->new();
401                 $u->start_mods_batch( $last_content->marc );
402                 my $mods = $u->finish_mods_batch();
403                 $mods->{doc_id} = $last_content->id();
404                 push @results, $mods;
405         }
406
407         $request->finish();
408         $session->finish();
409         $session->disconnect();
410         $session->kill_me();
411
412         return \@results;
413
414 }
415
416
417
418
419 1;