]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
continuing
[working/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
158
159
160 __PACKAGE__->register_method(
161         method  => "biblio_search_marc",
162         api_name        => "open-ils.search.biblio.marc",
163         argc            => 1, 
164         note            => "Searches biblio information by marc tag",
165 );
166
167 sub biblio_search_marc {
168
169         my( $self, $client, $search_hash, $string ) = @_;
170
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 );
174
175         my $response = $request->recv();
176         if($response and $response->isa("OpenSRF::EX")) {
177                 throw $response ($response->stringify);
178         }
179
180         my $data = $response->content;
181
182         $request->finish();
183         $session->finish();
184         $session->disconnect();
185         $session->kill_me();
186
187         return $data;
188
189 }
190
191
192
193
194 __PACKAGE__->register_method(
195         method  => "get_org_tree",
196         api_name        => "open-ils.search.actor.org_tree.retrieve",
197         argc            => 1, 
198         note            => "Returns the entire org tree structure",
199 );
200
201 sub get_org_tree {
202
203         my( $self, $client, $user_session ) = @_;
204
205         if( $user_session ) {
206
207                 my $user_obj = 
208                         OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
209
210                 
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();
215
216                 if(!$response) { 
217                         throw OpenSRF::EX::ERROR (
218                                         "No response from storage for org_unit retrieve");
219                 }
220                 if(UNIVERSAL::isa($response,"Error")) {
221                         throw $response ($response->stringify);
222                 }
223
224                 my $home_ou = $response->content;
225
226                 # XXX grab descendants and build org tree from them
227 =head asdf
228                 my $request = $session->request( 
229                                 "open-ils.storage.actor.org_unit_descendants" );
230                 my $response = $request->recv();
231                 if(!$response) { 
232                         throw OpenSRF::EX::ERROR (
233                                         "No response from storage for org_unit retrieve");
234                 }
235                 if(UNIVERSAL::isa($response,"Error")) {
236                         throw $response ($response->stringify);
237                 }
238
239                 my $descendants = $response->content;
240 =cut
241
242                 $session->disconnect();
243                 $session->kill_me();
244
245                 return $home_ou;
246         }
247
248         return OpenILS::Application::AppUtils->get_org_tree();
249 }
250
251
252
253 __PACKAGE__->register_method(
254         method  => "copy_count_by_org_unit",
255         api_name        => "open-ils.search.copy_count_by_location",
256         argc            => 1, 
257         note            => "Searches biblio information by marc tag",
258 );
259
260 sub copy_count_by_org_unit {
261         my( $self, $client, $org_id, @record_ids ) = @_;
262
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 );
266
267         for my $id (@record_ids) {
268
269                 my $response = $request->recv();
270                 next unless $response;
271
272                 if( $response and UNIVERSAL::isa($response, "Error")) {
273                         throw $response ($response->stringify);
274                 }
275
276                 my $count = $response->content;
277                 $client->respond( { record => $id, count => $count } );
278         }
279
280         $request->finish();
281         $session->disconnect();
282         $session->kill_me();
283         return undef;
284 }
285
286
287
288
289
290
291
292
293 # ---------------------------------------------------------------------------
294 # takes a list of record id's and turns the docs into friendly 
295 # mods structures.
296 # ---------------------------------------------------------------------------
297 sub _records_to_mods {
298         my @ids = @_;
299         
300         my @results;
301         my @marcxml_objs;
302
303         my $session = OpenSRF::AppSession->create("open-ils.storage");
304         my $request = $session->request(
305                         "open-ils.storage.biblio.record_marc.batch.retrieve",  @ids );
306
307         my $last_content = undef;
308
309         while( my $response = $request->recv() ) {
310
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;
319                 }
320
321                 next unless $response;
322
323                 if($response->isa("OpenSRF::EX")) {
324                         throw $response ($response->stringify);
325                 }
326
327                 $last_content = $response->content;
328
329         }
330
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;
337         }
338
339         $request->finish();
340         $session->finish();
341         $session->disconnect();
342         $session->kill_me();
343
344         return \@results;
345
346 }
347
348
349
350
351 1;