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::Const qw/:const/;
30 use OpenILS::Application::AppUtils;
31 my $apputils = "OpenILS::Application::AppUtils";
34 my $pfx = "open-ils.search_";
40 $cache = OpenSRF::Utils::Cache->new('global');
41 my $sclient = OpenSRF::Utils::SettingsClient->new();
42 $cache_timeout = $sclient->config_value(
43 "apps", "open-ils.search", "app_settings", "cache_timeout" ) || 300;
44 $logger->info("Search cache timeout is $cache_timeout");
49 # ---------------------------------------------------------------------------
50 # takes a list of record id's and turns the docs into friendly
51 # mods structures. Creates one MODS structure for each doc id.
52 # ---------------------------------------------------------------------------
53 sub _records_to_mods {
59 my $session = OpenSRF::AppSession->create("open-ils.cstore");
60 my $request = $session->request(
61 "open-ils.cstore.direct.biblio.record_entry.search", { id => \@ids } );
63 while( my $resp = $request->recv ) {
64 my $content = $resp->content;
65 next if $content->id == OILS_PRECAT_RECORD;
66 my $u = OpenILS::Utils::ModsParser->new();
67 $u->start_mods_batch( $content->marc );
68 my $mods = $u->finish_mods_batch();
69 $mods->doc_id($content->id());
70 $mods->tcn($content->tcn_value);
74 $session->disconnect();
78 __PACKAGE__->register_method(
79 method => "record_id_to_mods",
80 api_name => "open-ils.search.biblio.record.mods.retrieve",
82 note => "Provide ID, we provide the mods"
85 # converts a record into a mods object with copy counts attached
86 sub record_id_to_mods {
88 my( $self, $client, $org_id, $id ) = @_;
90 my $mods_list = _records_to_mods( $id );
91 my $mods_obj = $mods_list->[0];
92 my $cmethod = $self->method_lookup(
93 "open-ils.search.biblio.record.copy_count");
94 my ($count) = $cmethod->run($org_id, $id);
95 $mods_obj->copy_count($count);
102 __PACKAGE__->register_method(
103 method => "record_id_to_mods_slim",
104 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
106 note => "Provide ID, we provide the mods"
109 # converts a record into a mods object with NO copy counts attached
110 sub record_id_to_mods_slim {
111 my( $self, $client, $id ) = @_;
112 return undef unless defined $id;
114 if(ref($id) and ref($id) == 'ARRAY') {
115 return _records_to_mods( @$id );
117 my $mods_list = _records_to_mods( $id );
118 my $mods_obj = $mods_list->[0];
119 return OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND') unless $mods_obj;
124 # Returns the number of copies attached to a record based on org location
125 __PACKAGE__->register_method(
126 method => "record_id_to_copy_count",
127 api_name => "open-ils.search.biblio.record.copy_count",
130 __PACKAGE__->register_method(
131 method => "record_id_to_copy_count",
132 api_name => "open-ils.search.biblio.record.copy_count.staff",
135 __PACKAGE__->register_method(
136 method => "record_id_to_copy_count",
137 api_name => "open-ils.search.biblio.metarecord.copy_count",
140 __PACKAGE__->register_method(
141 method => "record_id_to_copy_count",
142 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
144 sub record_id_to_copy_count {
145 my( $self, $client, $org_id, $record_id, $format ) = @_;
147 return [] unless $record_id;
148 $format = undef if (!$format or $format eq 'all');
150 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
153 if($self->api_name =~ /metarecord/) {
154 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
158 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
160 my $count = $U->storagereq( $method,
161 org_unit => $org_id, $key => $record_id, format => $format );
163 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
169 __PACKAGE__->register_method(
170 method => "biblio_search_tcn",
171 api_name => "open-ils.search.biblio.tcn",
173 note => "Retrieve a record by TCN",
176 sub biblio_search_tcn {
178 my( $self, $client, $tcn ) = @_;
180 $tcn =~ s/.*?(\w+)\s*$/$1/o;
182 my $e = new_editor();
183 my $recs = $e->search_biblio_record_entry(
184 {deleted => 'f', tcn_value => $tcn}, {idlist =>1});
186 return { count => scalar(@$recs), ids => $recs };
190 # --------------------------------------------------------------------------------
192 __PACKAGE__->register_method(
193 method => "biblio_barcode_to_copy",
194 api_name => "open-ils.search.asset.copy.find_by_barcode",);
195 sub biblio_barcode_to_copy {
196 my( $self, $client, $barcode ) = @_;
197 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
202 __PACKAGE__->register_method(
203 method => "biblio_id_to_copy",
204 api_name => "open-ils.search.asset.copy.batch.retrieve",);
205 sub biblio_id_to_copy {
206 my( $self, $client, $ids ) = @_;
207 $logger->info("Fetching copies @$ids");
208 return $U->cstorereq(
209 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
213 __PACKAGE__->register_method(
214 method => "copy_retrieve",
215 api_name => "open-ils.search.asset.copy.retrieve",);
217 my( $self, $client, $cid ) = @_;
218 my( $copy, $evt ) = $U->fetch_copy($cid);
223 __PACKAGE__->register_method(
224 method => "volume_retrieve",
225 api_name => "open-ils.search.asset.call_number.retrieve");
226 sub volume_retrieve {
227 my( $self, $client, $vid ) = @_;
228 my $e = new_editor();
229 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
233 __PACKAGE__->register_method(
234 method => "fleshed_copy_retrieve_batch",
235 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
237 sub fleshed_copy_retrieve_batch {
238 my( $self, $client, $ids ) = @_;
239 $logger->info("Fetching fleshed copies @$ids");
240 return $U->cstorereq(
241 "open-ils.cstore.direct.asset.copy.search.atomic",
244 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
249 __PACKAGE__->register_method(
250 method => "fleshed_copy_retrieve",
251 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
253 sub fleshed_copy_retrieve {
254 my( $self, $client, $id ) = @_;
255 my( $c, $e) = $U->fetch_fleshed_copy($id);
260 __PACKAGE__->register_method(
261 method => "fleshed_copy_retrieve2",
262 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
264 sub fleshed_copy_retrieve2 {
265 my( $self, $client, $id ) = @_;
266 my $e = new_editor();
267 my $copy = $e->retrieve_asset_copy(
273 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
274 ascecm => [ qw/ stat_cat stat_cat_entry / ],
278 ) or return $e->event;
280 # For backwards compatibility
281 $copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
283 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
285 $e->search_action_circulation(
287 { target_copy => $copy->id },
289 order_by => { circ => 'xact_start desc' },
304 __PACKAGE__->register_method(
305 method => "biblio_barcode_to_title",
306 api_name => "open-ils.search.biblio.find_by_barcode",
309 sub biblio_barcode_to_title {
310 my( $self, $client, $barcode ) = @_;
312 my $title = $apputils->simple_scalar_request(
314 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
316 return { ids => [ $title->id ], count => 1 } if $title;
317 return { count => 0 };
321 __PACKAGE__->register_method(
322 method => "biblio_copy_to_mods",
323 api_name => "open-ils.search.biblio.copy.mods.retrieve",
326 # takes a copy object and returns it fleshed mods object
327 sub biblio_copy_to_mods {
328 my( $self, $client, $copy ) = @_;
330 my $volume = $U->cstorereq(
331 "open-ils.cstore.direct.asset.call_number.retrieve",
332 $copy->call_number() );
334 my $mods = _records_to_mods($volume->record());
335 $mods = shift @$mods;
336 $volume->copies([$copy]);
337 push @{$mods->call_numbers()}, $volume;
343 # ----------------------------------------------------------------------------
344 # These are the main OPAC search methods
345 # ----------------------------------------------------------------------------
347 __PACKAGE__->register_method(
348 method => 'the_quest_for_knowledge',
349 api_name => 'open-ils.search.biblio.multiclass',
351 Performs a multi class bilbli or metabib search
352 @param searchhash A search object layed out like so:
353 searches : { "$class" : "$value", ...}
354 org_unit : The org id to focus the search at
355 depth : The org depth
356 limit : The search limit
357 offset : The search offset
358 format : The MARC format
359 sort : What field to sort the results on [ author | title | pubdate ]
360 sort_dir : What direction do we sort? [ asc | desc ]
361 @return An object of the form
362 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
366 __PACKAGE__->register_method(
367 method => 'the_quest_for_knowledge',
368 api_name => 'open-ils.search.biblio.multiclass.staff',
369 signature => q/@see open-ils.search.biblio.multiclass/);
370 __PACKAGE__->register_method(
371 method => 'the_quest_for_knowledge',
372 api_name => 'open-ils.search.metabib.multiclass',
373 signature => q/@see open-ils.search.biblio.multiclass/);
374 __PACKAGE__->register_method(
375 method => 'the_quest_for_knowledge',
376 api_name => 'open-ils.search.metabib.multiclass.staff',
377 signature => q/@see open-ils.search.biblio.multiclass/);
381 sub the_quest_for_knowledge {
382 my( $self, $conn, $searchhash, $docache ) = @_;
384 return { count => 0 } unless $searchhash and
385 ref $searchhash->{searches} eq 'HASH';
387 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
392 my $offset = $searchhash->{offset} || 0;
393 my $limit = $searchhash->{limit} || 10;
394 my $end = $offset + $limit - 1;
396 for( keys %{$searchhash->{searches}} ) {
400 # do some simple sanity checking
401 if(!$searchhash->{searches} or
403 !$searchhash->{searches}->{title} and
404 !$searchhash->{searches}->{author} and
405 !$searchhash->{searches}->{subject} and
406 !$searchhash->{searches}->{series} and
407 !$searchhash->{searches}->{keyword} ) ) {
408 return { count => 0 };
413 $searchhash->{offset} = 0;
414 $searchhash->{limit} = $maxlimit;
416 return { count => 0 } if $offset > $maxlimit;
419 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
420 my $s = JSON->perl2JSON(\@search);
421 my $ckey = $pfx . md5_hex($method . $s);
423 $logger->info("bib search for: $s");
425 $searchhash->{limit} -= $offset;
427 if($self->api_name =~ /metabib/) {
429 $method =~ s/biblio/metabib/o;
432 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
436 $method .= ".staff" if($self->api_name =~ /staff/);
437 $method .= ".atomic";
439 for (keys %$searchhash) {
440 delete $$searchhash{$_}
441 unless defined $$searchhash{$_};
444 $result = $U->storagereq( $method, %$searchhash );
450 return {count => 0} unless ($result && $$result[0]);
452 #for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
455 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
460 # If we didn't get this data from the cache, put it into the cache
461 # then return the correct offset of records
462 $logger->debug("putting search cache $ckey\n");
463 put_cache($ckey, $count, \@recs);
466 for ($offset..$end) {
467 last unless $recs[$_];
472 #$logger->debug("cache done .. returning $offset..$end : " . JSON->perl2JSON(\@recs));
475 return { ids => \@recs, count => $count };
486 my $end = $offset + $limit - 1;
488 $logger->debug("searching cache for $key : $start..$end\n");
490 return undef unless $cache;
491 my $data = $cache->get_cache($key);
493 return undef unless $data;
495 my $count = $data->[0];
498 return undef unless $offset < $count;
502 for( my $i = $offset; $i <= $end; $i++ ) {
503 last unless my $d = $$data[$i];
507 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
514 my( $key, $count, $data ) = @_;
515 return undef unless $cache;
516 $logger->debug("search_cache putting ".
517 scalar(@$data)." items at key $key with timeout $cache_timeout");
518 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
526 __PACKAGE__->register_method(
527 method => "biblio_mrid_to_modsbatch_batch",
528 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
530 sub biblio_mrid_to_modsbatch_batch {
531 my( $self, $client, $mrids) = @_;
532 warn "Performing mrid_to_modsbatch_batch...";
534 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
535 for my $id (@$mrids) {
536 next unless defined $id;
537 my ($m) = $method->run($id);
544 __PACKAGE__->register_method(
545 method => "biblio_mrid_to_modsbatch",
546 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
547 notes => <<" NOTES");
548 Returns the mvr associated with a given metarecod. If none exists,
552 __PACKAGE__->register_method(
553 method => "biblio_mrid_to_modsbatch",
554 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
555 notes => <<" NOTES");
556 Returns the mvr associated with a given metarecod. If none exists,
560 sub biblio_mrid_to_modsbatch {
561 my( $self, $client, $mrid, $args) = @_;
563 warn "Grabbing mvr for $mrid\n";
565 my ($mr, $evt) = _grab_metarecord($mrid);
566 return $evt unless $mr;
568 my $mvr = $self->biblio_mrid_check_mvr($client, $mr);
569 $mvr = $self->biblio_mrid_make_modsbatch( $client, $mr ) unless $mvr;
571 return $mvr unless ref($args);
573 # Here we find the lead record appropriate for the given filters
574 # and use that for the title and author of the metarecord
575 my $format = $$args{format};
576 my $org = $$args{org};
577 my $depth = $$args{depth};
579 return $mvr unless $format or $org or $depth;
581 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
582 $method = "$method.staff" if $self->api_name =~ /staff/o;
584 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
586 if( my $mods = $U->record_to_mvr($rec) ) {
588 $mvr->title($mods->title);
589 $mvr->title($mods->author);
590 $logger->debug("mods_slim updating title and ".
591 "author in mvr with ".$mods->title." : ".$mods->author);
597 # converts a metarecord to an mvr
600 my $perl = JSON->JSON2perl($mr->mods());
601 return Fieldmapper::metabib::virtual_record->new($perl);
604 # checks to see if a metarecord has mods, if so returns true;
606 __PACKAGE__->register_method(
607 method => "biblio_mrid_check_mvr",
608 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
609 notes => <<" NOTES");
610 Takes a metarecord ID or a metarecord object and returns true
611 if the metarecord already has an mvr associated with it.
614 sub biblio_mrid_check_mvr {
615 my( $self, $client, $mrid ) = @_;
619 if(ref($mrid)) { $mr = $mrid; }
620 else { ($mr, $evt) = _grab_metarecord($mrid); }
623 warn "Checking mvr for mr " . $mr->id . "\n";
625 return _mr_to_mvr($mr) if $mr->mods();
629 sub _grab_metarecord {
631 #my $e = OpenILS::Utils::Editor->new;
632 my $e = new_editor();
633 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
638 __PACKAGE__->register_method(
639 method => "biblio_mrid_make_modsbatch",
640 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
641 notes => <<" NOTES");
642 Takes either a metarecord ID or a metarecord object.
643 Forces the creations of an mvr for the given metarecord.
644 The created mvr is returned.
647 sub biblio_mrid_make_modsbatch {
648 my( $self, $client, $mrid ) = @_;
650 #my $e = OpenILS::Utils::Editor->new;
651 my $e = new_editor();
658 $mr = $e->retrieve_metabib_metarecord($mrid)
662 my $masterid = $mr->master_record;
663 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
665 my $ids = $U->storagereq(
666 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
667 return undef unless @$ids;
669 my $master = $e->retrieve_biblio_record_entry($masterid)
672 # start the mods batch
673 my $u = OpenILS::Utils::ModsParser->new();
674 $u->start_mods_batch( $master->marc );
676 # grab all of the sub-records and shove them into the batch
677 my @ids = grep { $_ ne $masterid } @$ids;
678 my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
681 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
682 $u->push_mods_batch( $_->marc ) if $_->marc;
686 # finish up and send to the client
687 my $mods = $u->finish_mods_batch();
688 $mods->doc_id($mrid);
689 $client->respond_complete($mods);
692 # now update the mods string in the db
693 my $string = JSON->perl2JSON($mods->decast);
696 #$e = OpenILS::Utils::Editor->new(xact => 1);
697 $e = new_editor(xact => 1);
698 $e->update_metabib_metarecord($mr)
699 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
708 # converts a mr id into a list of record ids
710 __PACKAGE__->register_method(
711 method => "biblio_mrid_to_record_ids",
712 api_name => "open-ils.search.biblio.metarecord_to_records",
715 __PACKAGE__->register_method(
716 method => "biblio_mrid_to_record_ids",
717 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
720 sub biblio_mrid_to_record_ids {
721 my( $self, $client, $mrid, $args ) = @_;
723 my $format = $$args{format};
724 my $org = $$args{org};
725 my $depth = $$args{depth};
727 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
728 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
729 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
731 return { count => scalar(@$recs), ids => $recs };
735 __PACKAGE__->register_method(
736 method => "biblio_record_to_marc_html",
737 api_name => "open-ils.search.biblio.record.html" );
739 my $parser = XML::LibXML->new();
740 my $xslt = XML::LibXSLT->new();
743 my $settings_client = OpenSRF::Utils::SettingsClient->new();
744 sub biblio_record_to_marc_html {
745 my( $self, $client, $recordid ) = @_;
748 my $dir = $settings_client->config_value( "dirs", "xsl" );
749 my $xsl = $settings_client->config_value(
750 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
752 $xsl = $parser->parse_file("$dir/$xsl");
753 $marc_sheet = $xslt->parse_stylesheet( $xsl );
757 my $record = $apputils->simple_scalar_request(
759 "open-ils.cstore.direct.biblio.record_entry.retrieve",
762 my $xmldoc = $parser->parse_string($record->marc);
763 my $html = $marc_sheet->transform($xmldoc);
764 $html = $html->toString();
771 __PACKAGE__->register_method(
772 method => "retrieve_all_copy_locations",
773 api_name => "open-ils.search.config.copy_location.retrieve.all" );
775 my $shelving_locations;
776 sub retrieve_all_copy_locations {
777 my( $self, $client ) = @_;
778 if(!$shelving_locations) {
779 $shelving_locations = $apputils->simple_scalar_request(
781 "open-ils.cstore.direct.asset.copy_location.search.atomic",
782 { id => { "!=" => undef } }
785 return $shelving_locations;
791 __PACKAGE__->register_method(
792 method => "retrieve_all_copy_statuses",
793 api_name => "open-ils.search.config.copy_status.retrieve.all" );
796 sub retrieve_all_copy_statuses {
797 my( $self, $client ) = @_;
798 return $copy_statuses if $copy_statuses;
799 return $copy_statuses =
800 new_editor()->retrieve_all_config_copy_status();
804 __PACKAGE__->register_method(
805 method => "copy_counts_per_org",
806 api_name => "open-ils.search.biblio.copy_counts.retrieve");
808 __PACKAGE__->register_method(
809 method => "copy_counts_per_org",
810 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
812 sub copy_counts_per_org {
813 my( $self, $client, $record_id ) = @_;
815 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
817 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
818 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
820 my $counts = $apputils->simple_scalar_request(
821 "open-ils.storage", $method, $record_id );
823 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
828 __PACKAGE__->register_method(
829 method => "copy_count_summary",
830 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
831 notes => <<" NOTES");
832 returns an array of these:
833 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
834 where statusx is a copy status name. the statuses are sorted
838 sub copy_count_summary {
839 my( $self, $client, $rid, $org, $depth ) = @_;
842 return $U->storagereq(
843 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
849 __PACKAGE__->register_method(
850 method => "multiclass_search",
851 api_name => "open-ils.search.biblio.multiclass",
852 notes => <<" NOTES");
853 Performs a multiclass search
854 PARAMS( searchBlob, org_unit, format, limit )
855 where searchBlob is defined like this:
857 "title" : { "term" : "water" },
858 "author" : { "term" : "smith" },
863 __PACKAGE__->register_method(
864 method => "multiclass_search",
865 api_name => "open-ils.search.biblio.multiclass.staff",
866 notes => "see open-ils.search.biblio.multiclass" );
868 sub multiclass_search {
869 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
871 $logger->debug("Performing multiclass search with org => $orgid, " .
872 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
874 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
875 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
878 my $records = $apputils->simplereq(
879 'open-ils.storage', $meth,
880 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
885 if( ref($records) and $records->[0] and
886 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
888 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
890 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
891 return { ids => $recs, count => $count };
897 __PACKAGE__->register_method(
898 method => "multiclass_search",
899 api_name => "open-ils.search.biblio.multiclass",
901 Performs a multiclass search
902 @param args A names hash of arguments:
903 org_unit : The org to focus the search on
904 depth : The search depth
907 offset : Search offset
908 searches : A named hash of searches which has the following format:
910 "title" : { "term" : "water" },
911 "author" : { "term" : "smith" },
914 @return { ids : <array of ids>, count : hitcount }
918 __PACKAGE__->register_method(
919 method => "multiclass_search",
920 api_name => "open-ils.search.biblio.multiclass.staff",
921 notes => q/@see open-ils.search.biblio.multiclass/ );
923 sub multiclass_search {
924 my( $self, $client, $args ) = @_;
926 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
927 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
928 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
930 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
935 if( ref($records) and $records->[0] and
936 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
938 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
940 return { ids => $recs, count => $count };
947 __PACKAGE__->register_method(
948 method => "marc_search",
949 api_name => "open-ils.search.biblio.marc.staff");
951 __PACKAGE__->register_method(
952 method => "marc_search",
953 api_name => "open-ils.search.biblio.marc",
954 notes => <<" NOTES");
956 open-ils.storage.biblio.full_rec.multi_search.atomic
957 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
958 "limit":5,"sort":"author","item_type":"g"}
962 my( $self, $conn, $args, $limit, $offset ) = @_;
964 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
965 $method .= ".staff" if $self->api_name =~ /staff/;
966 $method .= ".atomic";
972 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
973 my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
975 my $recs = search_cache($ckey, $offset, $limit);
978 $recs = $U->storagereq($method, %$args);
979 put_cache($ckey, scalar(@$recs), $recs);
980 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
984 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
985 my @recs = map { $_->[0] } @$recs;
987 return { ids => \@recs, count => $count };
991 __PACKAGE__->register_method(
992 method => "biblio_search_isbn",
993 api_name => "open-ils.search.biblio.isbn",
996 sub biblio_search_isbn {
997 my( $self, $client, $isbn ) = @_;
998 $logger->debug("Searching ISBN $isbn");
999 my $e = new_editor();
1000 my $recs = $U->storagereq(
1001 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1002 return { ids => $recs, count => scalar(@$recs) };
1006 __PACKAGE__->register_method(
1007 method => "biblio_search_issn",
1008 api_name => "open-ils.search.biblio.issn",
1011 sub biblio_search_issn {
1012 my( $self, $client, $issn ) = @_;
1013 $logger->debug("Searching ISSN $issn");
1014 my $e = new_editor();
1015 my $recs = $U->storagereq(
1016 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1017 return { ids => $recs, count => scalar(@$recs) };
1023 __PACKAGE__->register_method(
1024 method => "fetch_mods_by_copy",
1025 api_name => "open-ils.search.biblio.mods_from_copy",
1028 sub fetch_mods_by_copy {
1029 my( $self, $client, $copyid ) = @_;
1030 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1031 return $evt if $evt;
1032 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1033 return $apputils->record_to_mvr($record);
1038 # -------------------------------------------------------------------------------------
1040 __PACKAGE__->register_method(
1041 method => "cn_browse",
1042 api_name => "open-ils.search.callnumber.browse.target",
1043 notes => "Starts a callnumber browse"
1046 __PACKAGE__->register_method(
1047 method => "cn_browse",
1048 api_name => "open-ils.search.callnumber.browse.page_up",
1049 notes => "Returns the previous page of callnumbers",
1052 __PACKAGE__->register_method(
1053 method => "cn_browse",
1054 api_name => "open-ils.search.callnumber.browse.page_down",
1055 notes => "Returns the next page of callnumbers",
1059 # RETURNS array of arrays like so: label, owning_lib, record, id
1061 my( $self, $client, @params ) = @_;
1064 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1065 if( $self->api_name =~ /target/ );
1066 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1067 if( $self->api_name =~ /page_up/ );
1068 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1069 if( $self->api_name =~ /page_down/ );
1071 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1073 # -------------------------------------------------------------------------------------
1075 __PACKAGE__->register_method(
1076 method => "fetch_cn",
1077 api_name => "open-ils.search.callnumber.retrieve",
1078 notes => "retrieves a callnumber based on ID",
1082 my( $self, $client, $id ) = @_;
1083 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1084 return $evt if $evt;
1088 __PACKAGE__->register_method (
1089 method => "fetch_copy_by_cn",
1090 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1092 Returns an array of copy id's by callnumber id
1093 @param cnid The callnumber id
1094 @return An array of copy ids
1098 sub fetch_copy_by_cn {
1099 my( $self, $conn, $cnid ) = @_;
1100 return $U->cstorereq(
1101 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1102 { call_number => $cnid, deleted => 'f' } );
1105 __PACKAGE__->register_method (
1106 method => 'fetch_cn_by_info',
1107 api_name => 'open-ils.search.call_number.retrieve_by_info',
1109 @param label The callnumber label
1110 @param record The record the cn is attached to
1111 @param org The owning library of the cn
1112 @return The callnumber object
1117 sub fetch_cn_by_info {
1118 my( $self, $conn, $label, $record, $org ) = @_;
1119 return $U->cstorereq(
1120 'open-ils.cstore.direct.asset.call_number.search',
1121 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1128 __PACKAGE__->register_method (
1129 method => 'bib_extras',
1130 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1131 __PACKAGE__->register_method (
1132 method => 'bib_extras',
1133 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1134 __PACKAGE__->register_method (
1135 method => 'bib_extras',
1136 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1137 __PACKAGE__->register_method (
1138 method => 'bib_extras',
1139 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1144 my $e = new_editor();
1146 return $e->retrieve_all_config_lit_form_map()
1147 if( $self->api_name =~ /lit_form/ );
1149 return $e->retrieve_all_config_item_form_map()
1150 if( $self->api_name =~ /item_form_map/ );
1152 return $e->retrieve_all_config_item_type_map()
1153 if( $self->api_name =~ /item_type_map/ );
1155 return $e->retrieve_all_config_audience_map()
1156 if( $self->api_name =~ /audience_map/ );
1163 __PACKAGE__->register_method(
1164 method => 'fetch_slim_record',
1165 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1167 Returns a biblio.record_entry without the attached marcxml
1171 sub fetch_slim_record {
1172 my( $self, $conn, $ids ) = @_;
1174 #my $editor = OpenILS::Utils::Editor->new;
1175 my $editor = new_editor();
1178 return $editor->event unless
1179 my $r = $editor->retrieve_biblio_record_entry($_);
1188 __PACKAGE__->register_method(
1189 method => 'rec_to_mr_rec_descriptors',
1190 api_name => 'open-ils.search.metabib.record_to_descriptors',
1192 specialized method...
1193 Given a biblio record id or a metarecord id,
1194 this returns a list of metabib.record_descriptor
1195 objects that live within the same metarecord
1196 @param args Object of args including:
1200 sub rec_to_mr_rec_descriptors {
1201 my( $self, $conn, $args ) = @_;
1203 my $rec = $$args{record};
1204 my $mrec = $$args{metarecord};
1205 my $item_forms = $$args{item_forms};
1206 my $item_types = $$args{item_types};
1207 my $item_lang = $$args{item_lang};
1209 my $e = new_editor();
1213 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1214 return $e->event unless @$map;
1215 $mrec = $$map[0]->metarecord;
1218 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1219 return $e->event unless @$recs;
1221 my @recs = map { $_->source } @$recs;
1222 my $search = { record => \@recs };
1223 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1224 $search->{item_type} = $item_types if $item_types and @$item_types;
1225 $search->{item_lang} = $item_lang if $item_lang;
1227 my $desc = $e->search_metabib_record_descriptor($search);
1229 return { metarecord => $mrec, descriptors => $desc };
1235 __PACKAGE__->register_method(
1236 method => 'copies_created_on',
1240 sub copies_created_on {
1241 my( $self, $conn, $auth, $org, $date ) = @_;
1242 my $e = new_editor(authtoken=>$auth);
1243 return $e->event unless $e->checkauth;
1247 __PACKAGE__->register_method(
1248 method => 'fetch_age_protect',
1249 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1252 sub fetch_age_protect {
1253 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1257 __PACKAGE__->register_method(
1258 method => 'copies_by_cn_label',
1259 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1262 sub copies_by_cn_label {
1263 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1264 my $e = new_editor();
1265 my $cns = $e->search_asset_call_number({record => $record, label => $label}, {idlist=>1});
1266 return [] unless @$cns;
1267 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib}, {idlist=>1});