1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Utils::ModsParser;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Utils::CStoreEditor q/:funcs/;
11 use OpenSRF::Utils::Cache;
13 use OpenSRF::Utils::Logger qw/:logger/;
18 use Time::HiRes qw(time);
19 use OpenSRF::EX qw(:try);
20 use Digest::MD5 qw(md5_hex);
26 $Data::Dumper::Indent = 0;
28 use OpenILS::Application::AppUtils;
29 my $apputils = "OpenILS::Application::AppUtils";
32 my $pfx = "open-ils.search_";
38 $cache = OpenSRF::Utils::Cache->new('global');
39 my $sclient = OpenSRF::Utils::SettingsClient->new();
40 $cache_timeout = $sclient->config_value(
41 "apps", "open-ils.search", "app_settings", "cache_timeout" ) || 300;
42 $logger->info("Search cache timeout is $cache_timeout");
47 # ---------------------------------------------------------------------------
48 # takes a list of record id's and turns the docs into friendly
49 # mods structures. Creates one MODS structure for each doc id.
50 # ---------------------------------------------------------------------------
51 sub _records_to_mods {
57 my $session = OpenSRF::AppSession->create("open-ils.cstore");
58 my $request = $session->request(
59 "open-ils.cstore.direct.biblio.record_entry.search", { id => \@ids } );
61 while( my $resp = $request->recv ) {
62 my $content = $resp->content;
63 my $u = OpenILS::Utils::ModsParser->new();
64 $u->start_mods_batch( $content->marc );
65 my $mods = $u->finish_mods_batch();
66 $mods->doc_id($content->id());
67 $mods->tcn($content->tcn_value);
71 $session->disconnect();
75 __PACKAGE__->register_method(
76 method => "record_id_to_mods",
77 api_name => "open-ils.search.biblio.record.mods.retrieve",
79 note => "Provide ID, we provide the mods"
82 # converts a record into a mods object with copy counts attached
83 sub record_id_to_mods {
85 my( $self, $client, $org_id, $id ) = @_;
87 my $mods_list = _records_to_mods( $id );
88 my $mods_obj = $mods_list->[0];
89 my $cmethod = $self->method_lookup(
90 "open-ils.search.biblio.record.copy_count");
91 my ($count) = $cmethod->run($org_id, $id);
92 $mods_obj->copy_count($count);
99 __PACKAGE__->register_method(
100 method => "record_id_to_mods_slim",
101 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
103 note => "Provide ID, we provide the mods"
106 # converts a record into a mods object with NO copy counts attached
107 sub record_id_to_mods_slim {
108 my( $self, $client, $id ) = @_;
109 return undef unless defined $id;
111 if(ref($id) and ref($id) == 'ARRAY') {
112 return _records_to_mods( @$id );
114 my $mods_list = _records_to_mods( $id );
115 my $mods_obj = $mods_list->[0];
120 # Returns the number of copies attached to a record based on org location
121 __PACKAGE__->register_method(
122 method => "record_id_to_copy_count",
123 api_name => "open-ils.search.biblio.record.copy_count",
126 __PACKAGE__->register_method(
127 method => "record_id_to_copy_count",
128 api_name => "open-ils.search.biblio.record.copy_count.staff",
131 __PACKAGE__->register_method(
132 method => "record_id_to_copy_count",
133 api_name => "open-ils.search.biblio.metarecord.copy_count",
136 __PACKAGE__->register_method(
137 method => "record_id_to_copy_count",
138 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
140 sub record_id_to_copy_count {
141 my( $self, $client, $org_id, $record_id, $format ) = @_;
143 return [] unless $record_id;
144 $format = undef if (!$format or $format eq 'all');
146 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
149 if($self->api_name =~ /metarecord/) {
150 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
154 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
156 my $count = $U->storagereq( $method,
157 org_unit => $org_id, $key => $record_id, format => $format );
159 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
165 __PACKAGE__->register_method(
166 method => "biblio_search_tcn",
167 api_name => "open-ils.search.biblio.tcn",
169 note => "Retrieve a record by TCN",
172 sub biblio_search_tcn {
174 my( $self, $client, $tcn ) = @_;
176 $tcn =~ s/.*?(\w+)\s*$/$1/o;
178 my $e = new_editor();
179 my $recs = $e->search_biblio_record_entry(
180 {deleted => 'f', tcn_value => $tcn}, {idlist =>1});
182 return { count => scalar(@$recs), ids => $recs };
186 # --------------------------------------------------------------------------------
188 __PACKAGE__->register_method(
189 method => "biblio_barcode_to_copy",
190 api_name => "open-ils.search.asset.copy.find_by_barcode",);
191 sub biblio_barcode_to_copy {
192 my( $self, $client, $barcode ) = @_;
193 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
198 __PACKAGE__->register_method(
199 method => "biblio_id_to_copy",
200 api_name => "open-ils.search.asset.copy.batch.retrieve",);
201 sub biblio_id_to_copy {
202 my( $self, $client, $ids ) = @_;
203 $logger->info("Fetching copies @$ids");
204 return $U->cstorereq(
205 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
209 __PACKAGE__->register_method(
210 method => "copy_retrieve",
211 api_name => "open-ils.search.asset.copy.retrieve",);
213 my( $self, $client, $cid ) = @_;
214 my( $copy, $evt ) = $U->fetch_copy($cid);
219 __PACKAGE__->register_method(
220 method => "volume_retrieve",
221 api_name => "open-ils.search.asset.call_number.retrieve");
222 sub volume_retrieve {
223 my( $self, $client, $vid ) = @_;
224 my $e = new_editor();
225 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
229 __PACKAGE__->register_method(
230 method => "fleshed_copy_retrieve_batch",
231 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
233 sub fleshed_copy_retrieve_batch {
234 my( $self, $client, $ids ) = @_;
235 $logger->info("Fetching fleshed copies @$ids");
236 return $U->cstorereq(
237 "open-ils.cstore.direct.asset.copy.search.atomic",
240 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
245 __PACKAGE__->register_method(
246 method => "fleshed_copy_retrieve",
247 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
249 sub fleshed_copy_retrieve {
250 my( $self, $client, $id ) = @_;
251 my( $c, $e) = $U->fetch_fleshed_copy($id);
258 __PACKAGE__->register_method(
259 method => "biblio_barcode_to_title",
260 api_name => "open-ils.search.biblio.find_by_barcode",
263 sub biblio_barcode_to_title {
264 my( $self, $client, $barcode ) = @_;
266 my $title = $apputils->simple_scalar_request(
268 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
270 return { ids => [ $title->id ], count => 1 } if $title;
271 return { count => 0 };
275 __PACKAGE__->register_method(
276 method => "biblio_copy_to_mods",
277 api_name => "open-ils.search.biblio.copy.mods.retrieve",
280 # takes a copy object and returns it fleshed mods object
281 sub biblio_copy_to_mods {
282 my( $self, $client, $copy ) = @_;
284 my $volume = $U->cstorereq(
285 "open-ils.cstore.direct.asset.call_number.retrieve",
286 $copy->call_number() );
288 my $mods = _records_to_mods($volume->record());
289 $mods = shift @$mods;
290 $volume->copies([$copy]);
291 push @{$mods->call_numbers()}, $volume;
297 # ----------------------------------------------------------------------------
298 # These are the main OPAC search methods
299 # ----------------------------------------------------------------------------
301 __PACKAGE__->register_method(
302 method => 'the_quest_for_knowledge',
303 api_name => 'open-ils.search.biblio.multiclass',
305 Performs a multi class bilbli or metabib search
306 @param searchhash A search object layed out like so:
307 searches : { "$class" : "$value", ...}
308 org_unit : The org id to focus the search at
309 depth : The org depth
310 limit : The search limit
311 offset : The search offset
312 format : The MARC format
313 sort : What field to sort the results on [ author | title | pubdate ]
314 sort_dir : What direction do we sort? [ asc | desc ]
315 @return An object of the form
316 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
320 __PACKAGE__->register_method(
321 method => 'the_quest_for_knowledge',
322 api_name => 'open-ils.search.biblio.multiclass.staff',
323 signature => q/@see open-ils.search.biblio.multiclass/);
324 __PACKAGE__->register_method(
325 method => 'the_quest_for_knowledge',
326 api_name => 'open-ils.search.metabib.multiclass',
327 signature => q/@see open-ils.search.biblio.multiclass/);
328 __PACKAGE__->register_method(
329 method => 'the_quest_for_knowledge',
330 api_name => 'open-ils.search.metabib.multiclass.staff',
331 signature => q/@see open-ils.search.biblio.multiclass/);
335 sub the_quest_for_knowledge {
336 my( $self, $conn, $searchhash, $docache ) = @_;
338 return { count => 0 } unless $searchhash and
339 ref $searchhash->{searches} eq 'HASH';
341 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
346 my $offset = $searchhash->{offset} || 0;
347 my $limit = $searchhash->{limit} || 10;
348 my $end = $offset + $limit - 1;
352 $searchhash->{offset} = 0;
353 $searchhash->{limit} = $maxlimit;
355 return { count => 0 } if $offset > $maxlimit;
358 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
359 my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
361 $searchhash->{limit} -= $offset;
363 if($self->api_name =~ /metabib/) {
365 $method =~ s/biblio/metabib/o;
368 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
372 $method .= ".staff" if($self->api_name =~ /staff/);
373 $method .= ".atomic";
375 for (keys %$searchhash) {
376 delete $$searchhash{$_}
377 unless defined $$searchhash{$_};
380 $result = $U->storagereq( $method, %$searchhash );
386 return {count => 0} unless ($result && $$result[0]);
388 #for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
391 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
396 # If we didn't get this data from the cache, put it into the cache
397 # then return the correct offset of records
398 $logger->debug("putting search cache $ckey\n");
399 put_cache($ckey, $count, \@recs);
402 for ($offset..$end) {
403 last unless $recs[$_];
408 #$logger->debug("cache done .. returning $offset..$end : " . JSON->perl2JSON(\@recs));
411 return { ids => \@recs, count => $count };
422 my $end = $offset + $limit - 1;
424 $logger->debug("searching cache for $key : $start..$end\n");
426 return undef unless $cache;
427 my $data = $cache->get_cache($key);
429 return undef unless $data;
431 my $count = $data->[0];
434 return undef unless $offset < $count;
438 for( my $i = $offset; $i <= $end; $i++ ) {
439 last unless my $d = $$data[$i];
443 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
450 my( $key, $count, $data ) = @_;
451 return undef unless $cache;
452 $logger->debug("search_cache putting ".
453 scalar(@$data)." items at key $key with timeout $cache_timeout");
454 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
462 __PACKAGE__->register_method(
463 method => "biblio_mrid_to_modsbatch_batch",
464 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
466 sub biblio_mrid_to_modsbatch_batch {
467 my( $self, $client, $mrids) = @_;
468 warn "Performing mrid_to_modsbatch_batch...";
470 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
471 for my $id (@$mrids) {
472 next unless defined $id;
473 my ($m) = $method->run($id);
480 __PACKAGE__->register_method(
481 method => "biblio_mrid_to_modsbatch",
482 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
483 notes => <<" NOTES");
484 Returns the mvr associated with a given metarecod. If none exists,
488 __PACKAGE__->register_method(
489 method => "biblio_mrid_to_modsbatch",
490 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
491 notes => <<" NOTES");
492 Returns the mvr associated with a given metarecod. If none exists,
496 sub biblio_mrid_to_modsbatch {
497 my( $self, $client, $mrid ) = @_;
499 warn "Grabbing mvr for $mrid\n";
501 my ($mr, $evt) = _grab_metarecord($mrid);
502 return $evt unless $mr;
504 if( my $m = $self->biblio_mrid_check_mvr($client, $mr)) {
508 return $self->biblio_mrid_make_modsbatch( $client, $mr );
511 # converts a metarecord to an mvr
514 my $perl = JSON->JSON2perl($mr->mods());
515 return Fieldmapper::metabib::virtual_record->new($perl);
518 # checks to see if a metarecord has mods, if so returns true;
520 __PACKAGE__->register_method(
521 method => "biblio_mrid_check_mvr",
522 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
523 notes => <<" NOTES");
524 Takes a metarecord ID or a metarecord object and returns true
525 if the metarecord already has an mvr associated with it.
528 sub biblio_mrid_check_mvr {
529 my( $self, $client, $mrid ) = @_;
533 if(ref($mrid)) { $mr = $mrid; }
534 else { ($mr, $evt) = _grab_metarecord($mrid); }
537 warn "Checking mvr for mr " . $mr->id . "\n";
539 return _mr_to_mvr($mr) if $mr->mods();
543 sub _grab_metarecord {
545 #my $e = OpenILS::Utils::Editor->new;
546 my $e = new_editor();
547 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
552 __PACKAGE__->register_method(
553 method => "biblio_mrid_make_modsbatch",
554 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
555 notes => <<" NOTES");
556 Takes either a metarecord ID or a metarecord object.
557 Forces the creations of an mvr for the given metarecord.
558 The created mvr is returned.
561 sub biblio_mrid_make_modsbatch {
562 my( $self, $client, $mrid ) = @_;
564 #my $e = OpenILS::Utils::Editor->new;
565 my $e = new_editor();
572 $mr = $e->retrieve_metabib_metarecord($mrid)
576 my $masterid = $mr->master_record;
577 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
579 my $ids = $U->storagereq(
580 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
581 return undef unless @$ids;
583 my $master = $e->retrieve_biblio_record_entry($masterid)
586 # start the mods batch
587 my $u = OpenILS::Utils::ModsParser->new();
588 $u->start_mods_batch( $master->marc );
590 # grab all of the sub-records and shove them into the batch
591 my @ids = grep { $_ ne $masterid } @$ids;
592 my $subrecs = $e->batch_retrieve_biblio_record_entry(\@ids);
595 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
596 $u->push_mods_batch( $_->marc ) if $_->marc;
600 # finish up and send to the client
601 my $mods = $u->finish_mods_batch();
602 $mods->doc_id($mrid);
603 $client->respond_complete($mods);
606 # now update the mods string in the db
607 my $string = JSON->perl2JSON($mods->decast);
610 #$e = OpenILS::Utils::Editor->new(xact => 1);
611 $e = new_editor(xact => 1);
612 $e->update_metabib_metarecord($mr)
613 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
622 # converts a mr id into a list of record ids
624 __PACKAGE__->register_method(
625 method => "biblio_mrid_to_record_ids",
626 api_name => "open-ils.search.biblio.metarecord_to_records",
629 __PACKAGE__->register_method(
630 method => "biblio_mrid_to_record_ids",
631 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
634 sub biblio_mrid_to_record_ids {
635 my( $self, $client, $mrid, $args ) = @_;
637 my $format = $$args{format};
638 my $org = $$args{org};
639 my $depth = $$args{depth};
641 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
642 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
643 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
645 return { count => scalar(@$recs), ids => $recs };
649 __PACKAGE__->register_method(
650 method => "biblio_record_to_marc_html",
651 api_name => "open-ils.search.biblio.record.html" );
653 my $parser = XML::LibXML->new();
654 my $xslt = XML::LibXSLT->new();
657 my $settings_client = OpenSRF::Utils::SettingsClient->new();
658 sub biblio_record_to_marc_html {
659 my( $self, $client, $recordid ) = @_;
662 my $dir = $settings_client->config_value( "dirs", "xsl" );
663 my $xsl = $settings_client->config_value(
664 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
666 $xsl = $parser->parse_file("$dir/$xsl");
667 $marc_sheet = $xslt->parse_stylesheet( $xsl );
671 my $record = $apputils->simple_scalar_request(
673 "open-ils.cstore.direct.biblio.record_entry.retrieve",
676 my $xmldoc = $parser->parse_string($record->marc);
677 my $html = $marc_sheet->transform($xmldoc);
678 $html = $html->toString();
685 __PACKAGE__->register_method(
686 method => "retrieve_all_copy_locations",
687 api_name => "open-ils.search.config.copy_location.retrieve.all" );
689 my $shelving_locations;
690 sub retrieve_all_copy_locations {
691 my( $self, $client ) = @_;
692 if(!$shelving_locations) {
693 $shelving_locations = $apputils->simple_scalar_request(
695 "open-ils.cstore.direct.asset.copy_location.search.atomic",
696 { id => { "!=" => undef } }
699 return $shelving_locations;
705 __PACKAGE__->register_method(
706 method => "retrieve_all_copy_statuses",
707 api_name => "open-ils.search.config.copy_status.retrieve.all" );
710 sub retrieve_all_copy_statuses {
711 my( $self, $client ) = @_;
712 return $copy_statuses if $copy_statuses;
713 return $copy_statuses =
714 new_editor()->retrieve_all_config_copy_status();
718 __PACKAGE__->register_method(
719 method => "copy_counts_per_org",
720 api_name => "open-ils.search.biblio.copy_counts.retrieve");
722 __PACKAGE__->register_method(
723 method => "copy_counts_per_org",
724 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
726 sub copy_counts_per_org {
727 my( $self, $client, $record_id ) = @_;
729 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
731 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
732 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
734 my $counts = $apputils->simple_scalar_request(
735 "open-ils.storage", $method, $record_id );
737 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
742 __PACKAGE__->register_method(
743 method => "copy_count_summary",
744 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
745 notes => <<" NOTES");
746 returns an array of these:
747 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
748 where statusx is a copy status name. the statuses are sorted
752 sub copy_count_summary {
753 my( $self, $client, $rid, $org, $depth ) = @_;
756 return $U->storagereq(
757 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
763 __PACKAGE__->register_method(
764 method => "multiclass_search",
765 api_name => "open-ils.search.biblio.multiclass",
766 notes => <<" NOTES");
767 Performs a multiclass search
768 PARAMS( searchBlob, org_unit, format, limit )
769 where searchBlob is defined like this:
771 "title" : { "term" : "water" },
772 "author" : { "term" : "smith" },
777 __PACKAGE__->register_method(
778 method => "multiclass_search",
779 api_name => "open-ils.search.biblio.multiclass.staff",
780 notes => "see open-ils.search.biblio.multiclass" );
782 sub multiclass_search {
783 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
785 $logger->debug("Performing multiclass search with org => $orgid, " .
786 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
788 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
789 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
792 my $records = $apputils->simplereq(
793 'open-ils.storage', $meth,
794 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
799 if( ref($records) and $records->[0] and
800 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
802 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
804 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
805 return { ids => $recs, count => $count };
811 __PACKAGE__->register_method(
812 method => "multiclass_search",
813 api_name => "open-ils.search.biblio.multiclass",
815 Performs a multiclass search
816 @param args A names hash of arguments:
817 org_unit : The org to focus the search on
818 depth : The search depth
821 offset : Search offset
822 searches : A named hash of searches which has the following format:
824 "title" : { "term" : "water" },
825 "author" : { "term" : "smith" },
828 @return { ids : <array of ids>, count : hitcount }
832 __PACKAGE__->register_method(
833 method => "multiclass_search",
834 api_name => "open-ils.search.biblio.multiclass.staff",
835 notes => q/@see open-ils.search.biblio.multiclass/ );
837 sub multiclass_search {
838 my( $self, $client, $args ) = @_;
840 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
841 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
842 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
844 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
849 if( ref($records) and $records->[0] and
850 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
852 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
854 return { ids => $recs, count => $count };
861 __PACKAGE__->register_method(
862 method => "marc_search",
863 api_name => "open-ils.search.biblio.marc.staff");
865 __PACKAGE__->register_method(
866 method => "marc_search",
867 api_name => "open-ils.search.biblio.marc",
868 notes => <<" NOTES");
870 open-ils.storage.biblio.full_rec.multi_search.atomic
871 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
872 "limit":5,"sort":"author","item_type":"g"}
876 my( $self, $conn, $args, $limit, $offset ) = @_;
878 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
879 $method .= ".staff" if $self->api_name =~ /staff/;
880 $method .= ".atomic";
886 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
887 my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
889 my $recs = search_cache($ckey, $offset, $limit);
892 $recs = $U->storagereq($method, %$args);
893 put_cache($ckey, scalar(@$recs), $recs);
894 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
898 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
899 my @recs = map { $_->[0] } @$recs;
901 return { ids => \@recs, count => $count };
905 __PACKAGE__->register_method(
906 method => "biblio_search_isbn",
907 api_name => "open-ils.search.biblio.isbn",
910 sub biblio_search_isbn {
911 my( $self, $client, $isbn ) = @_;
912 $logger->debug("Searching ISBN $isbn");
913 my $e = new_editor();
914 my $recs = $U->storagereq(
915 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
916 return { ids => $recs, count => scalar(@$recs) };
920 __PACKAGE__->register_method(
921 method => "biblio_search_issn",
922 api_name => "open-ils.search.biblio.issn",
925 sub biblio_search_issn {
926 my( $self, $client, $issn ) = @_;
927 $logger->debug("Searching ISSN $issn");
928 my $e = new_editor();
929 my $recs = $U->storagereq(
930 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
931 return { ids => $recs, count => scalar(@$recs) };
937 __PACKAGE__->register_method(
938 method => "fetch_mods_by_copy",
939 api_name => "open-ils.search.biblio.mods_from_copy",
942 sub fetch_mods_by_copy {
943 my( $self, $client, $copyid ) = @_;
944 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
946 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
947 return $apputils->record_to_mvr($record);
952 # -------------------------------------------------------------------------------------
954 __PACKAGE__->register_method(
955 method => "cn_browse",
956 api_name => "open-ils.search.callnumber.browse.target",
957 notes => "Starts a callnumber browse"
960 __PACKAGE__->register_method(
961 method => "cn_browse",
962 api_name => "open-ils.search.callnumber.browse.page_up",
963 notes => "Returns the previous page of callnumbers",
966 __PACKAGE__->register_method(
967 method => "cn_browse",
968 api_name => "open-ils.search.callnumber.browse.page_down",
969 notes => "Returns the next page of callnumbers",
973 # RETURNS array of arrays like so: label, owning_lib, record, id
975 my( $self, $client, @params ) = @_;
978 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
979 if( $self->api_name =~ /target/ );
980 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
981 if( $self->api_name =~ /page_up/ );
982 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
983 if( $self->api_name =~ /page_down/ );
985 return $apputils->simplereq( 'open-ils.storage', $method, @params );
987 # -------------------------------------------------------------------------------------
989 __PACKAGE__->register_method(
990 method => "fetch_cn",
991 api_name => "open-ils.search.callnumber.retrieve",
992 notes => "retrieves a callnumber based on ID",
996 my( $self, $client, $id ) = @_;
997 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1002 __PACKAGE__->register_method (
1003 method => "fetch_copy_by_cn",
1004 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1006 Returns an array of copy id's by callnumber id
1007 @param cnid The callnumber id
1008 @return An array of copy ids
1012 sub fetch_copy_by_cn {
1013 my( $self, $conn, $cnid ) = @_;
1014 return $U->cstorereq(
1015 'open-ils.cstore.search.asset.copy.id_list.atomic',
1016 { call_number => $cnid, deleted => 'f' } );
1019 __PACKAGE__->register_method (
1020 method => 'fetch_cn_by_info',
1021 api_name => 'open-ils.search.call_number.retrieve_by_info',
1023 @param label The callnumber label
1024 @param record The record the cn is attached to
1025 @param org The owning library of the cn
1026 @return The callnumber object
1031 sub fetch_cn_by_info {
1032 my( $self, $conn, $label, $record, $org ) = @_;
1033 return $U->cstorereq(
1034 'open-ils.cstore.direct.asset.call_number.search',
1035 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1042 __PACKAGE__->register_method (
1043 method => 'bib_extras',
1044 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1045 __PACKAGE__->register_method (
1046 method => 'bib_extras',
1047 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1048 __PACKAGE__->register_method (
1049 method => 'bib_extras',
1050 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1051 __PACKAGE__->register_method (
1052 method => 'bib_extras',
1053 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1058 my $e = new_editor();
1060 return $e->retrieve_all_config_lit_form_map()
1061 if( $self->api_name =~ /lit_form/ );
1063 return $e->retrieve_all_config_item_form_map()
1064 if( $self->api_name =~ /item_form_map/ );
1066 return $e->retrieve_all_config_item_type_map()
1067 if( $self->api_name =~ /item_type_map/ );
1069 return $e->retrieve_all_config_audience_map()
1070 if( $self->api_name =~ /audience_map/ );
1077 __PACKAGE__->register_method(
1078 method => 'fetch_slim_record',
1079 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1081 Returns a biblio.record_entry without the attached marcxml
1085 sub fetch_slim_record {
1086 my( $self, $conn, $ids ) = @_;
1088 #my $editor = OpenILS::Utils::Editor->new;
1089 my $editor = new_editor();
1092 return $editor->event unless
1093 my $r = $editor->retrieve_biblio_record_entry($_);
1102 __PACKAGE__->register_method(
1103 method => 'rec_to_mr_rec_descriptors',
1104 api_name => 'open-ils.search.metabib.record_to_descriptors',
1106 specialized method...
1107 Given a biblio record id or a metarecord id,
1108 this returns a list of metabib.record_descriptor
1109 objects that live within the same metarecord
1110 @param args Object of args including:
1114 sub rec_to_mr_rec_descriptors {
1115 my( $self, $conn, $args ) = @_;
1117 my $rec = $$args{record};
1118 my $mrec = $$args{metarecord};
1119 my $item_forms = $$args{item_forms};
1120 my $item_types = $$args{item_types};
1121 my $item_lang = $$args{item_lang};
1123 my $e = new_editor();
1127 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1128 return $e->event unless @$map;
1129 $mrec = $$map[0]->metarecord;
1132 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1133 return $e->event unless @$recs;
1135 my @recs = map { $_->source } @$recs;
1136 my $search = { record => \@recs };
1137 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1138 $search->{item_type} = $item_types if $item_types and @$item_types;
1139 $search->{item_lang} = $item_lang if $item_lang;
1141 my $desc = $e->search_metabib_record_descriptor($search);
1143 return { metarecord => $mrec, descriptors => $desc };
1149 __PACKAGE__->register_method(
1150 method => 'copies_created_on',
1154 sub copies_created_on {
1155 my( $self, $conn, $auth, $org, $date ) = @_;
1156 my $e = new_editor(authtoken=>$auth);
1157 return $e->event unless $e->checkauth;