]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
moved to server side caching of opac search results - sped up the details page by...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Biblio.pm
1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5
6 use JSON;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Utils::ModsParser;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Utils::Editor q/:funcs/;
11 use OpenSRF::Utils::Cache;
12
13 use OpenSRF::Utils::Logger qw/:logger/;
14
15
16 use JSON;
17
18 use Time::HiRes qw(time);
19 use OpenSRF::EX qw(:try);
20 use Digest::MD5 qw(md5_hex);
21
22 use XML::LibXML;
23 use XML::LibXSLT;
24
25 use Data::Dumper;
26 $Data::Dumper::Indent = 0;
27
28 use OpenILS::Application::AppUtils;
29 my $apputils = "OpenILS::Application::AppUtils";
30 my $U = $apputils;
31
32 my $pfx = "open-ils.search";
33
34 my $cache;
35 sub initialize {
36         $cache = OpenSRF::Utils::Cache->new('global');
37 }
38
39
40
41 # ---------------------------------------------------------------------------
42 # takes a list of record id's and turns the docs into friendly 
43 # mods structures. Creates one MODS structure for each doc id.
44 # ---------------------------------------------------------------------------
45 sub _records_to_mods {
46         my @ids = @_;
47         
48         my @results;
49         my @marcxml_objs;
50
51         my $session = OpenSRF::AppSession->create("open-ils.storage");
52         my $request = $session->request(
53                         "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
54
55         while( my $resp = $request->recv ) {
56                 my $content = $resp->content;
57                 my $u = OpenILS::Utils::ModsParser->new();
58                 $u->start_mods_batch( $content->marc );
59                 my $mods = $u->finish_mods_batch();
60                 $mods->doc_id($content->id());
61                 $mods->tcn($content->tcn_value);
62                 push @results, $mods;
63         }
64
65         $session->disconnect();
66         return \@results;
67 }
68
69 __PACKAGE__->register_method(
70         method  => "record_id_to_mods",
71         api_name        => "open-ils.search.biblio.record.mods.retrieve",
72         argc            => 1, 
73         note            => "Provide ID, we provide the mods"
74 );
75
76 # converts a record into a mods object with copy counts attached
77 sub record_id_to_mods {
78
79         my( $self, $client, $org_id, $id ) = @_;
80
81         my $mods_list = _records_to_mods( $id );
82         my $mods_obj = $mods_list->[0];
83         my $cmethod = $self->method_lookup(
84                         "open-ils.search.biblio.record.copy_count");
85         my ($count) = $cmethod->run($org_id, $id);
86         $mods_obj->copy_count($count);
87
88         return $mods_obj;
89 }
90
91
92
93 __PACKAGE__->register_method(
94         method  => "record_id_to_mods_slim",
95         api_name        => "open-ils.search.biblio.record.mods_slim.retrieve",
96         argc            => 1, 
97         note            => "Provide ID, we provide the mods"
98 );
99
100 # converts a record into a mods object with NO copy counts attached
101 sub record_id_to_mods_slim {
102         my( $self, $client, $id ) = @_;
103         return undef unless defined $id;
104
105         if(ref($id) and ref($id) == 'ARRAY') {
106                 return _records_to_mods( @$id );
107         }
108         my $mods_list = _records_to_mods( $id );
109         my $mods_obj = $mods_list->[0];
110         return $mods_obj;
111 }
112
113
114 # Returns the number of copies attached to a record based on org location
115 __PACKAGE__->register_method(
116         method  => "record_id_to_copy_count",
117         api_name        => "open-ils.search.biblio.record.copy_count",
118 );
119
120 __PACKAGE__->register_method(
121         method  => "record_id_to_copy_count",
122         api_name        => "open-ils.search.biblio.record.copy_count.staff",
123 );
124
125 __PACKAGE__->register_method(
126         method  => "record_id_to_copy_count",
127         api_name        => "open-ils.search.biblio.metarecord.copy_count",
128 );
129
130 __PACKAGE__->register_method(
131         method  => "record_id_to_copy_count",
132         api_name        => "open-ils.search.biblio.metarecord.copy_count.staff",
133 );
134 sub record_id_to_copy_count {
135         my( $self, $client, $org_id, $record_id, $format ) = @_;
136
137         return [] unless $record_id;
138         $format = undef if (!$format or $format eq 'all');
139
140         my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
141         my $key = "record";
142
143         if($self->api_name =~ /metarecord/) {
144                 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
145                 $key = "metarecord";
146         }
147
148         $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
149
150         my $count = $U->storagereq( $method, 
151                 org_unit => $org_id, $key => $record_id, format => $format );
152
153         return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
154 }
155
156
157
158
159 __PACKAGE__->register_method(
160         method  => "biblio_search_tcn",
161         api_name        => "open-ils.search.biblio.tcn",
162         argc            => 3, 
163         note            => "Retrieve a record by TCN",
164 );
165
166 sub biblio_search_tcn {
167
168         my( $self, $client, $tcn ) = @_;
169
170         $tcn =~ s/.*?(\w+)\s*$/$1/o;
171
172         my $e = new_editor();
173         my $recs = $e->search_biblio_record_entry(
174                 {deleted => 'f', tcn_value => $tcn}, {idlist =>1});
175         
176         return { count => scalar(@$recs), ids => $recs };
177 }
178
179
180 # --------------------------------------------------------------------------------
181
182 __PACKAGE__->register_method(
183         method  => "biblio_barcode_to_copy",
184         api_name        => "open-ils.search.asset.copy.find_by_barcode",);
185 sub biblio_barcode_to_copy { 
186         my( $self, $client, $barcode ) = @_;
187         my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
188         return $evt if $evt;
189         return $copy;
190 }
191
192 __PACKAGE__->register_method(
193         method  => "biblio_id_to_copy",
194         api_name        => "open-ils.search.asset.copy.batch.retrieve",);
195 sub biblio_id_to_copy { 
196         my( $self, $client, $ids ) = @_;
197         $logger->info("Fetching copies @$ids");
198         return $U->storagereq(
199                 "open-ils.storage.direct.asset.copy.batch.retrieve.atomic", @$ids );
200 }
201
202
203 __PACKAGE__->register_method(
204         method  => "copy_retrieve", 
205         api_name        => "open-ils.search.asset.copy.retrieve",);
206 sub copy_retrieve {
207         my( $self, $client, $cid ) = @_;
208         my( $copy, $evt ) = $U->fetch_copy($cid);
209         return $evt if $evt;
210         return $copy;
211 }
212
213 __PACKAGE__->register_method(
214         method  => "volume_retrieve", 
215         api_name        => "open-ils.search.asset.call_number.retrieve");
216 sub volume_retrieve {
217         my( $self, $client, $vid ) = @_;
218         my $e = new_editor();
219         my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
220         return $vol;
221 }
222
223 __PACKAGE__->register_method(
224         method  => "fleshed_copy_retrieve_batch",
225         api_name        => "open-ils.search.asset.copy.fleshed.batch.retrieve");
226
227 sub fleshed_copy_retrieve_batch { 
228         my( $self, $client, $ids ) = @_;
229         $logger->info("Fetching fleshed copies @$ids");
230         return $U->storagereq(
231                 "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic", @$ids );
232 }
233
234
235 __PACKAGE__->register_method(
236         method  => "fleshed_copy_retrieve",
237         api_name        => "open-ils.search.asset.copy.fleshed.retrieve",);
238
239 sub fleshed_copy_retrieve { 
240         my( $self, $client, $id ) = @_;
241         my( $c, $e) = $U->fetch_fleshed_copy($id);
242         return $e if $e;
243         return $c;
244 }
245
246
247
248 __PACKAGE__->register_method(
249         method  => "biblio_barcode_to_title",
250         api_name        => "open-ils.search.biblio.find_by_barcode",
251 );
252
253 sub biblio_barcode_to_title {
254         my( $self, $client, $barcode ) = @_;
255
256         my $title = $apputils->simple_scalar_request(
257                 "open-ils.storage",
258                 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
259
260         return { ids => [ $title->id ], count => 1 } if $title;
261         return { count => 0 };
262 }
263
264
265 __PACKAGE__->register_method(
266         method  => "biblio_copy_to_mods",
267         api_name        => "open-ils.search.biblio.copy.mods.retrieve",
268 );
269
270 # takes a copy object and returns it fleshed mods object
271 sub biblio_copy_to_mods {
272         my( $self, $client, $copy ) = @_;
273
274         my $volume = $U->storagereq( 
275                 "open-ils.storage.direct.asset.call_number.retrieve",
276                 $copy->call_number() );
277
278         my $mods = _records_to_mods($volume->record());
279         $mods = shift @$mods;
280         $volume->copies([$copy]);
281         push @{$mods->call_numbers()}, $volume;
282
283         return $mods;
284 }
285
286
287 # ----------------------------------------------------------------------------
288 # These are the main OPAC search methods
289 # ----------------------------------------------------------------------------
290
291 __PACKAGE__->register_method(
292         method          => 'the_quest_for_knowledge',
293         api_name                => 'open-ils.search.biblio.multiclass',
294         signature       => q/
295                 Performs a multi class bilbli or metabib search
296                 @param searchhash A search object layed out like so:
297                         searches : { "$class" : "$value", ...}
298                         org_unit : The org id to focus the search at
299                         depth           : The org depth
300                         limit           : The search limit
301                         offset  : The search offset
302                         format  : The MARC format
303                         sort            : What field to sort the results on [ author | title | pubdate ]
304                         sort_dir        : What direction do we sort? [ asc | desc ]
305                 @return An object of the form 
306                         { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
307         /
308 );
309
310 __PACKAGE__->register_method(
311         method          => 'the_quest_for_knowledge',
312         api_name                => 'open-ils.search.biblio.multiclass.staff',
313         signature       => q/@see open-ils.search.biblio.multiclass/);
314 __PACKAGE__->register_method(
315         method          => 'the_quest_for_knowledge',
316         api_name                => 'open-ils.search.metabib.multiclass',
317         signature       => q/@see open-ils.search.biblio.multiclass/);
318 __PACKAGE__->register_method(
319         method          => 'the_quest_for_knowledge',
320         api_name                => 'open-ils.search.metabib.multiclass.staff',
321         signature       => q/@see open-ils.search.biblio.multiclass/);
322
323
324
325 sub the_quest_for_knowledge {
326         my( $self, $conn, $searchhash ) = @_;
327
328         my $method = 'open-ils.storage.biblio.multiclass.search_fts';
329         my $ismeta = 0;
330         my @recs;
331
332         my $maxlimit = 500;
333
334         my $o = $searchhash->{offset} || 0;
335         my $l = $searchhash->{limit} || 10;
336         $searchhash->{offset} = 0;
337         $searchhash->{limit} = 0;
338
339         my $ckey = $pfx . md5_hex($method . JSON->perl2JSON($searchhash));
340
341         $searchhash->{offset} = $o;
342         $searchhash->{limit} = $l;
343
344
345         if($self->api_name =~ /metabib/) {
346                 $ismeta = 1;
347                 $method =~ s/biblio/metabib/o;
348         }
349
350         # don't cache past max limit
351         my $result = ($o < $maxlimit) ? search_cache($ckey, $o, $l) : undef; 
352         my $docache = 1;
353
354         if(!$result) {
355
356                 $method .= ".staff" if($self->api_name =~ /staff/);
357                 $method .= ".atomic";
358         
359                 for (keys %$searchhash) { 
360                         delete $$searchhash{$_} unless defined $$searchhash{$_}; }
361         
362                 $searchhash->{limit} = $maxlimit;
363                 $result = new_editor()->request( $method, %$searchhash );
364
365
366         } else { 
367                 $logger->debug("cache returned results: " . JSON->perl2JSON($result));
368                 $docache = 0; 
369         }
370
371         return {count => 0} unless ($result && $$result[0]);
372
373         for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
374
375         if( $docache ) {
376                 $logger->debug("putting search cache $ckey\n");
377                 $cache->put_cache($ckey, \@recs, 900);
378         }
379
380         return { ids => \@recs, 
381                 count => ($ismeta) ? $result->[0]->[3] : $result->[0]->[2] };
382 }
383
384
385
386 #$cache_handle->put_cache( "_open-ils_seed_$username", $seed, 30 );
387 #my $current_seed = $cache_handle->get_cache("_open-ils_seed_$username");
388 #$cache_handle->delete_cache( "_open-ils_seed_$username" );
389
390 sub search_cache {
391
392         my $key         = shift;
393         my $offset      = shift;
394         my $limit       = shift;
395
396         $logger->debug("fetching search cache $key\n");
397
398         my $data = $cache->get_cache($key);
399         return undef unless $data;
400
401         $logger->debug("search_cache found some data: o=$offset, l=$limit");
402
403         #$data = JSON->JSON2perl($data);
404         return undef unless $data and ref $data eq 'ARRAY';
405         return undef unless $$data[$offset] and $$data[$offset+($limit-1)];
406
407         $logger->debug("search_cache found data..$offset - " . ($offset + ($limit - 1)));
408         return [ @$data[$offset..($offset+$limit)] ];
409 }
410
411
412
413
414
415
416 __PACKAGE__->register_method(
417         method  => "biblio_mrid_to_modsbatch_batch",
418         api_name        => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
419
420 sub biblio_mrid_to_modsbatch_batch {
421         my( $self, $client, $mrids) = @_;
422         warn "Performing mrid_to_modsbatch_batch...";
423         my @mods;
424         my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
425         for my $id (@$mrids) {
426                 next unless defined $id;
427                 my ($m) = $method->run($id);
428                 push @mods, $m;
429         }
430         return \@mods;
431 }
432
433
434 __PACKAGE__->register_method(
435         method  => "biblio_mrid_to_modsbatch",
436         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
437         notes           => <<"  NOTES");
438         Returns the mvr associated with a given metarecod. If none exists, 
439         it is created.
440         NOTES
441
442 __PACKAGE__->register_method(
443         method  => "biblio_mrid_to_modsbatch",
444         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
445         notes           => <<"  NOTES");
446         Returns the mvr associated with a given metarecod. If none exists, 
447         it is created.
448         NOTES
449
450 sub biblio_mrid_to_modsbatch {
451         my( $self, $client, $mrid ) = @_;
452
453         warn "Grabbing mvr for $mrid\n";
454
455         my ($mr, $evt) = _grab_metarecord($mrid);
456         return $evt unless $mr;
457
458         if( my $m = $self->biblio_mrid_check_mvr($client, $mr)) {
459                 return $m;
460         }
461
462         return $self->biblio_mrid_make_modsbatch( $client, $mr ); 
463 }
464
465 # converts a metarecord to an mvr
466 sub _mr_to_mvr {
467         my $mr = shift;
468         my $perl = JSON->JSON2perl($mr->mods());
469         return Fieldmapper::metabib::virtual_record->new($perl);
470 }
471
472 # checks to see if a metarecord has mods, if so returns true;
473
474 __PACKAGE__->register_method(
475         method  => "biblio_mrid_check_mvr",
476         api_name        => "open-ils.search.biblio.metarecord.mods_slim.check",
477         notes           => <<"  NOTES");
478         Takes a metarecord ID or a metarecord object and returns true
479         if the metarecord already has an mvr associated with it.
480         NOTES
481
482 sub biblio_mrid_check_mvr {
483         my( $self, $client, $mrid ) = @_;
484         my $mr; 
485
486         my $evt;
487         if(ref($mrid)) { $mr = $mrid; } 
488         else { ($mr, $evt) = _grab_metarecord($mrid); }
489         return $evt if $evt;
490
491         warn "Checking mvr for mr " . $mr->id . "\n";
492
493         return _mr_to_mvr($mr) if $mr->mods();
494         return undef;
495 }
496
497 sub _grab_metarecord {
498         my $mrid = shift;
499         my $e = OpenILS::Utils::Editor->new;
500         my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
501         return ($mr);
502 }
503
504
505 __PACKAGE__->register_method(
506         method  => "biblio_mrid_make_modsbatch",
507         api_name        => "open-ils.search.biblio.metarecord.mods_slim.create",
508         notes           => <<"  NOTES");
509         Takes either a metarecord ID or a metarecord object.
510         Forces the creations of an mvr for the given metarecord.
511         The created mvr is returned.
512         NOTES
513
514 sub biblio_mrid_make_modsbatch {
515         my( $self, $client, $mrid ) = @_;
516
517         my $e = OpenILS::Utils::Editor->new;
518
519         my $mr;
520         if( ref($mrid) ) {
521                 $mr = $mrid;
522                 $mrid = $mr->id;
523         } else {
524                 $mr = $e->retrieve_metabib_metarecord($mrid) 
525                         or return $e->event;
526         }
527
528         my $masterid = $mr->master_record;
529         $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
530
531         my $ids = $e->request(
532                 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
533         return undef unless @$ids;
534
535         my $master = $e->retrieve_biblio_record_entry($masterid)
536                 or return $e->event;
537
538         # start the mods batch
539         my $u = OpenILS::Utils::ModsParser->new();
540         $u->start_mods_batch( $master->marc );
541
542         # grab all of the sub-records and shove them into the batch
543         my @ids = grep { $_ ne $masterid } @$ids;
544         my $subrecs = $e->batch_retrieve_biblio_record_entry(\@ids);
545
546         for(@$subrecs) {
547                 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
548                 $u->push_mods_batch( $_->marc ) if $_->marc;
549         }
550
551
552         # finish up and send to the client
553         my $mods = $u->finish_mods_batch();
554         $mods->doc_id($mrid);
555         $client->respond_complete($mods);
556
557
558         # now update the mods string in the db
559         my $string = JSON->perl2JSON($mods->decast);
560         $mr->mods($string);
561
562         $e = OpenILS::Utils::Editor->new(xact => 1);
563         $e->update_metabib_metarecord($mr) 
564                 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
565         $e->finish;
566
567         return undef;
568 }
569
570
571
572
573 # converts a mr id into a list of record ids
574
575 __PACKAGE__->register_method(
576         method  => "biblio_mrid_to_record_ids",
577         api_name        => "open-ils.search.biblio.metarecord_to_records",
578 );
579
580 __PACKAGE__->register_method(
581         method  => "biblio_mrid_to_record_ids",
582         api_name        => "open-ils.search.biblio.metarecord_to_records.staff",
583 );
584
585 sub biblio_mrid_to_record_ids {
586         my( $self, $client, $mrid, $args ) = @_;
587
588         my $format      = $$args{format};
589         my $org         = $$args{org};
590         my $depth       = $$args{depth};
591
592         my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
593         $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o; 
594         my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
595
596         return { count => scalar(@$recs), ids => $recs };
597 }
598
599
600 __PACKAGE__->register_method(
601         method  => "biblio_record_to_marc_html",
602         api_name        => "open-ils.search.biblio.record.html" );
603
604 my $parser              = XML::LibXML->new();
605 my $xslt                        = XML::LibXSLT->new();
606 my $marc_sheet;
607
608 my $settings_client = OpenSRF::Utils::SettingsClient->new();
609 sub biblio_record_to_marc_html {
610         my( $self, $client, $recordid ) = @_;
611
612         if( !$marc_sheet ) {
613                 my $dir = $settings_client->config_value( "dirs", "xsl" );
614                 my $xsl = $settings_client->config_value(
615                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
616
617                 $xsl = $parser->parse_file("$dir/$xsl");
618                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
619         }
620
621
622         my $record = $apputils->simple_scalar_request(
623                 "open-ils.storage", 
624                 "open-ils.storage.direct.biblio.record_entry.retrieve",
625                 $recordid );
626
627         my $xmldoc = $parser->parse_string($record->marc);
628         my $html = $marc_sheet->transform($xmldoc);
629         $html = $html->toString();
630         return $html;
631
632 }
633
634
635 =head duplicate
636 __PACKAGE__->register_method(
637         method  => "retrieve_all_copy_locations",
638         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
639
640 my $shelving_locations;
641 sub retrieve_all_copy_locations {
642         my( $self, $client ) = @_;
643         if(!$shelving_locations) {
644                 $shelving_locations = $apputils->simple_scalar_request(
645                         "open-ils.storage", 
646                         "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
647         }
648         return $shelving_locations;
649 }
650 =cut
651
652
653
654 __PACKAGE__->register_method(
655         method  => "retrieve_all_copy_statuses",
656         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
657
658 my $copy_statuses;
659 sub retrieve_all_copy_statuses {
660         my( $self, $client ) = @_;
661         if(!$copy_statuses) {
662                 $copy_statuses = $apputils->simple_scalar_request(
663                         "open-ils.storage",
664                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
665         }
666         return $copy_statuses;
667 }
668
669
670 __PACKAGE__->register_method(
671         method  => "copy_counts_per_org",
672         api_name        => "open-ils.search.biblio.copy_counts.retrieve");
673
674 __PACKAGE__->register_method(
675         method  => "copy_counts_per_org",
676         api_name        => "open-ils.search.biblio.copy_counts.retrieve.staff");
677
678 sub copy_counts_per_org {
679         my( $self, $client, $record_id ) = @_;
680
681         warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
682
683         my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
684         if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
685
686         my $counts = $apputils->simple_scalar_request(
687                 "open-ils.storage", $method, $record_id );
688
689         $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
690         return $counts;
691 }
692
693
694 __PACKAGE__->register_method(
695         method          => "copy_count_summary",
696         api_name        => "open-ils.search.biblio.copy_counts.summary.retrieve",
697         notes           => <<"  NOTES");
698         returns an array of these:
699                 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
700                 where statusx is a copy status name.  the statuses are sorted
701                 by id.
702         NOTES
703
704 sub copy_count_summary {
705         my( $self, $client, $rid, $org, $depth ) = @_;
706         $org ||= 1;
707         $depth ||= 0;
708         return $U->storagereq(
709                 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
710 }
711
712
713
714 =head
715 __PACKAGE__->register_method(
716         method          => "multiclass_search",
717         api_name        => "open-ils.search.biblio.multiclass",
718         notes           => <<"  NOTES");
719                 Performs a multiclass search
720                 PARAMS( searchBlob, org_unit, format, limit ) 
721                 where searchBlob is defined like this:
722                         { 
723                                 "title" : { "term" : "water" }, 
724                                 "author" : { "term" : "smith" }, 
725                                 ... 
726                         }
727         NOTES
728
729 __PACKAGE__->register_method(
730         method          => "multiclass_search",
731         api_name        => "open-ils.search.biblio.multiclass.staff",
732         notes           => "see open-ils.search.biblio.multiclass" );
733
734 sub multiclass_search {
735         my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
736
737         $logger->debug("Performing multiclass search with org => $orgid, " .
738                 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
739
740         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
741         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
742
743
744         my $records = $apputils->simplereq(
745                 'open-ils.storage', $meth, 
746                  org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
747
748         my $count = 0;
749         my $recs = [];
750
751         if( ref($records) and $records->[0] and 
752                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
753
754         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
755
756         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
757         return { ids => $recs, count => $count };
758 }
759 =cut
760
761
762 =head comment-1
763 __PACKAGE__->register_method(
764         method          => "multiclass_search",
765         api_name                => "open-ils.search.biblio.multiclass",
766         signature       => q/
767                 Performs a multiclass search
768                 @param args A names hash of arguments:
769                         org_unit : The org to focus the search on
770                         depth           : The search depth
771                         format  : Item format
772                         limit           : Return limit
773                         offset  : Search offset
774                         searches : A named hash of searches which has the following format:
775                                 { 
776                                         "title" : { "term" : "water" }, 
777                                         "author" : { "term" : "smith" }, 
778                                         ... 
779                                 }
780                 @return { ids : <array of ids>, count : hitcount }
781         /
782 );
783
784 __PACKAGE__->register_method(
785         method          => "multiclass_search",
786         api_name                => "open-ils.search.biblio.multiclass.staff",
787         notes           => q/@see open-ils.search.biblio.multiclass/ );
788
789 sub multiclass_search {
790         my( $self, $client, $args ) = @_;
791
792         $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
793         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
794         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
795
796         my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
797
798         my $count = 0;
799         my $recs = [];
800
801         if( ref($records) and $records->[0] and 
802                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
803
804         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
805
806         return { ids => $recs, count => $count };
807 }
808
809 =cut
810
811
812
813 __PACKAGE__->register_method(
814         method          => "marc_search",
815         api_name        => "open-ils.search.biblio.marc.staff");
816
817 __PACKAGE__->register_method(
818         method          => "marc_search",
819         api_name        => "open-ils.search.biblio.marc",
820         notes           => <<"  NOTES");
821                 Example:
822                 open-ils.storage.biblio.full_rec.multi_search.atomic 
823                 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
824         "limit":5,"sort":"author","item_type":"g"}
825         NOTES
826
827 sub marc_search {
828         my( $self, $conn, $args, $limit, $offset ) = @_;
829
830         my $method = 'open-ils.storage.biblio.full_rec.multi_search';
831         $method .= ".staff" if $self->api_name =~ /staff/;
832         $method .= ".atomic";
833
834         $limit ||= 10;
835         $offset ||= 0;
836
837         my $ckey = $pfx . md5_hex($method . JSON->perl2JSON($args));
838         my $recs = search_cache($ckey, $offset, $limit);
839
840         if(!$recs) {
841                 $recs = new_editor()->request($method, %$args);
842                 $cache->put_cache($ckey, $recs, 900);
843                 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
844         }
845
846         my $count = 0;
847         $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
848         my @recs = map { $_->[0] } @$recs;
849
850         return { ids => \@recs, count => $count };
851 }
852
853
854 __PACKAGE__->register_method(
855         method  => "biblio_search_isbn",
856         api_name        => "open-ils.search.biblio.isbn",
857 );
858
859 sub biblio_search_isbn { 
860         my( $self, $client, $isbn ) = @_;
861         $logger->debug("Searching ISBN $isbn");
862         my $e = new_editor();
863         my $recs = $e->request(
864                 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
865         return { ids => $recs, count => scalar(@$recs) };
866 }
867
868
869 __PACKAGE__->register_method(
870         method  => "biblio_search_issn",
871         api_name        => "open-ils.search.biblio.issn",
872 );
873
874 sub biblio_search_issn { 
875         my( $self, $client, $issn ) = @_;
876         $logger->debug("Searching ISSN $issn");
877         my $e = new_editor();
878         my $recs = $e->request(
879                 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
880         return { ids => $recs, count => scalar(@$recs) };
881 }
882
883
884
885
886 __PACKAGE__->register_method(
887         method  => "fetch_mods_by_copy",
888         api_name        => "open-ils.search.biblio.mods_from_copy",
889 );
890
891 sub fetch_mods_by_copy {
892         my( $self, $client, $copyid ) = @_;
893         my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
894         return $evt if $evt;
895         return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
896         return $apputils->record_to_mvr($record);
897 }
898
899
900
901 # -------------------------------------------------------------------------------------
902
903 __PACKAGE__->register_method(
904         method  => "cn_browse",
905         api_name        => "open-ils.search.callnumber.browse.target",
906         notes           => "Starts a callnumber browse"
907         );
908
909 __PACKAGE__->register_method(
910         method  => "cn_browse",
911         api_name        => "open-ils.search.callnumber.browse.page_up",
912         notes           => "Returns the previous page of callnumbers", 
913         );
914
915 __PACKAGE__->register_method(
916         method  => "cn_browse",
917         api_name        => "open-ils.search.callnumber.browse.page_down",
918         notes           => "Returns the next page of callnumbers", 
919         );
920
921
922 # RETURNS array of arrays like so: label, owning_lib, record, id
923 sub cn_browse {
924         my( $self, $client, @params ) = @_;
925         my $method;
926
927         $method = 'open-ils.storage.asset.call_number.browse.target.atomic' 
928                 if( $self->api_name =~ /target/ );
929         $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
930                 if( $self->api_name =~ /page_up/ );
931         $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
932                 if( $self->api_name =~ /page_down/ );
933
934         return $apputils->simplereq( 'open-ils.storage', $method, @params );
935 }
936 # -------------------------------------------------------------------------------------
937
938 __PACKAGE__->register_method(
939         method => "fetch_cn",
940         api_name => "open-ils.search.callnumber.retrieve",
941         notes           => "retrieves a callnumber based on ID",
942         );
943
944 sub fetch_cn {
945         my( $self, $client, $id ) = @_;
946         my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
947         return $evt if $evt;
948         return $cn;
949 }
950
951 __PACKAGE__->register_method (
952         method          => "fetch_copy_by_cn",
953         api_name                => 'open-ils.search.copies_by_call_number.retrieve',
954         signature       => q/
955                 Returns an array of copy id's by callnumber id
956                 @param cnid The callnumber id
957                 @return An array of copy ids
958         /
959 );
960
961 sub fetch_copy_by_cn {
962         my( $self, $conn, $cnid ) = @_;
963         return $U->storagereq(
964                 'open-ils.storage.id_list.asset.copy.search_where.atomic', 
965                 { call_number => $cnid, deleted => 'f' } );
966 }
967
968 __PACKAGE__->register_method (
969         method          => 'fetch_cn_by_info',
970         api_name                => 'open-ils.search.call_number.retrieve_by_info',
971         signature       => q/
972                 @param label The callnumber label
973                 @param record The record the cn is attached to
974                 @param org The owning library of the cn
975                 @return The callnumber object
976         /
977 );
978
979
980 sub fetch_cn_by_info {
981         my( $self, $conn, $label, $record, $org ) = @_;
982         return $U->storagereq(
983                 'open-ils.storage.direct.asset.call_number.search_where',
984                 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
985 }
986
987
988                 
989
990
991 __PACKAGE__->register_method (
992         method => 'bib_extras',
993         api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
994 __PACKAGE__->register_method (
995         method => 'bib_extras',
996         api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
997 __PACKAGE__->register_method (
998         method => 'bib_extras',
999         api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1000 __PACKAGE__->register_method (
1001         method => 'bib_extras',
1002         api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1003
1004 sub bib_extras {
1005         my $self = shift;
1006         
1007         return $U->storagereq(
1008                 'open-ils.storage.direct.config.lit_form_map.retrieve.all.atomic')
1009                         if( $self->api_name =~ /lit_form/ );
1010
1011         return $U->storagereq(
1012                 'open-ils.storage.direct.config.item_form_map.retrieve.all.atomic')
1013                         if( $self->api_name =~ /item_form_map/ );
1014
1015         return $U->storagereq(
1016                 'open-ils.storage.direct.config.item_type_map.retrieve.all.atomic')
1017                         if( $self->api_name =~ /item_type_map/ );
1018
1019         return $U->storagereq(
1020                 'open-ils.storage.direct.config.audience_map.retrieve.all.atomic')
1021                         if( $self->api_name =~ /audience/ );
1022
1023         return [];
1024 }
1025
1026
1027
1028 __PACKAGE__->register_method(
1029         method  => 'fetch_slim_record',
1030         api_name        => 'open-ils.search.biblio.record_entry.slim.retrieve',
1031         signature=> q/
1032                 Returns a biblio.record_entry without the attached marcxml
1033         /
1034 );
1035
1036 sub fetch_slim_record {
1037         my( $self, $conn, $ids ) = @_;
1038
1039         my $editor = OpenILS::Utils::Editor->new;
1040         my @res;
1041         for( @$ids ) {
1042                 return $editor->event unless
1043                         my $r = $editor->retrieve_biblio_record_entry($_);
1044                 $r->clear_marc;
1045                 push(@res, $r);
1046         }
1047         return \@res;
1048 }
1049
1050
1051
1052
1053
1054
1055
1056 1;
1057
1058