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