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, $include_deleted ) = @_;
180 $tcn =~ s/.*?(\w+)\s*$/$1/o;
182 my $e = new_editor();
183 my $search = {tcn_value => $tcn};
184 $search->{deleted} = 'f' unless $include_deleted;
185 my $recs = $e->search_biblio_record_entry( $search, {idlist =>1} );
187 return { count => scalar(@$recs), ids => $recs };
191 # --------------------------------------------------------------------------------
193 __PACKAGE__->register_method(
194 method => "biblio_barcode_to_copy",
195 api_name => "open-ils.search.asset.copy.find_by_barcode",);
196 sub biblio_barcode_to_copy {
197 my( $self, $client, $barcode ) = @_;
198 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
203 __PACKAGE__->register_method(
204 method => "biblio_id_to_copy",
205 api_name => "open-ils.search.asset.copy.batch.retrieve",);
206 sub biblio_id_to_copy {
207 my( $self, $client, $ids ) = @_;
208 $logger->info("Fetching copies @$ids");
209 return $U->cstorereq(
210 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
214 __PACKAGE__->register_method(
215 method => "copy_retrieve",
216 api_name => "open-ils.search.asset.copy.retrieve",);
218 my( $self, $client, $cid ) = @_;
219 my( $copy, $evt ) = $U->fetch_copy($cid);
224 __PACKAGE__->register_method(
225 method => "volume_retrieve",
226 api_name => "open-ils.search.asset.call_number.retrieve");
227 sub volume_retrieve {
228 my( $self, $client, $vid ) = @_;
229 my $e = new_editor();
230 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
234 __PACKAGE__->register_method(
235 method => "fleshed_copy_retrieve_batch",
236 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
238 sub fleshed_copy_retrieve_batch {
239 my( $self, $client, $ids ) = @_;
240 $logger->info("Fetching fleshed copies @$ids");
241 return $U->cstorereq(
242 "open-ils.cstore.direct.asset.copy.search.atomic",
245 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
250 __PACKAGE__->register_method(
251 method => "fleshed_copy_retrieve",
252 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
254 sub fleshed_copy_retrieve {
255 my( $self, $client, $id ) = @_;
256 my( $c, $e) = $U->fetch_fleshed_copy($id);
263 __PACKAGE__->register_method(
264 method => 'fleshed_by_barcode',
265 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",);
266 sub fleshed_by_barcode {
267 my( $self, $conn, $barcode ) = @_;
268 my $e = new_editor();
269 my $copyid = $e->search_asset_copy(
270 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
272 return $self->fleshed_copy_retrieve2($conn, $copyid);
276 __PACKAGE__->register_method(
277 method => "fleshed_copy_retrieve2",
278 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
280 sub fleshed_copy_retrieve2 {
281 my( $self, $client, $id ) = @_;
282 my $e = new_editor();
283 my $copy = $e->retrieve_asset_copy(
289 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
290 ascecm => [ qw/ stat_cat stat_cat_entry / ],
294 ) or return $e->event;
296 # For backwards compatibility
297 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
299 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
301 $e->search_action_circulation(
303 { target_copy => $copy->id },
305 order_by => { circ => 'xact_start desc' },
317 __PACKAGE__->register_method(
318 method => 'flesh_copy_custom',
319 api_name => 'open-ils.search.asset.copy.fleshed.custom'
322 sub flesh_copy_custom {
323 my( $self, $conn, $copyid, $fields ) = @_;
324 my $e = new_editor();
325 my $copy = $e->retrieve_asset_copy(
335 ) or return $e->event;
344 __PACKAGE__->register_method(
345 method => "biblio_barcode_to_title",
346 api_name => "open-ils.search.biblio.find_by_barcode",
349 sub biblio_barcode_to_title {
350 my( $self, $client, $barcode ) = @_;
352 my $title = $apputils->simple_scalar_request(
354 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
356 return { ids => [ $title->id ], count => 1 } if $title;
357 return { count => 0 };
360 __PACKAGE__->register_method(
361 method => 'title_id_by_item_barcode',
362 api_name => 'open-ils.search.bib_id.by_barcode'
365 sub title_id_by_item_barcode {
366 my( $self, $conn, $barcode ) = @_;
367 my $e = new_editor();
368 my $copies = $e->search_asset_copy(
370 { deleted => 'f', barcode => $barcode },
374 acp => [ 'call_number' ],
381 return $e->event unless @$copies;
382 return $$copies[0]->call_number->record->id;
386 __PACKAGE__->register_method(
387 method => "biblio_copy_to_mods",
388 api_name => "open-ils.search.biblio.copy.mods.retrieve",
391 # takes a copy object and returns it fleshed mods object
392 sub biblio_copy_to_mods {
393 my( $self, $client, $copy ) = @_;
395 my $volume = $U->cstorereq(
396 "open-ils.cstore.direct.asset.call_number.retrieve",
397 $copy->call_number() );
399 my $mods = _records_to_mods($volume->record());
400 $mods = shift @$mods;
401 $volume->copies([$copy]);
402 push @{$mods->call_numbers()}, $volume;
408 # ----------------------------------------------------------------------------
409 # These are the main OPAC search methods
410 # ----------------------------------------------------------------------------
412 __PACKAGE__->register_method(
413 method => 'the_quest_for_knowledge',
414 api_name => 'open-ils.search.biblio.multiclass',
416 Performs a multi class bilbli or metabib search
417 @param searchhash A search object layed out like so:
418 searches : { "$class" : "$value", ...}
419 org_unit : The org id to focus the search at
420 depth : The org depth
421 limit : The search limit
422 offset : The search offset
423 format : The MARC format
424 sort : What field to sort the results on [ author | title | pubdate ]
425 sort_dir : What direction do we sort? [ asc | desc ]
426 @return An object of the form
427 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
431 __PACKAGE__->register_method(
432 method => 'the_quest_for_knowledge',
433 api_name => 'open-ils.search.biblio.multiclass.staff',
434 signature => q/@see open-ils.search.biblio.multiclass/);
435 __PACKAGE__->register_method(
436 method => 'the_quest_for_knowledge',
437 api_name => 'open-ils.search.metabib.multiclass',
438 signature => q/@see open-ils.search.biblio.multiclass/);
439 __PACKAGE__->register_method(
440 method => 'the_quest_for_knowledge',
441 api_name => 'open-ils.search.metabib.multiclass.staff',
442 signature => q/@see open-ils.search.biblio.multiclass/);
446 sub the_quest_for_knowledge {
447 my( $self, $conn, $searchhash, $docache ) = @_;
449 return { count => 0 } unless $searchhash and
450 ref $searchhash->{searches} eq 'HASH';
452 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
456 if($self->api_name =~ /metabib/) {
458 $method =~ s/biblio/metabib/o;
462 my $offset = $searchhash->{offset} || 0;
463 my $limit = $searchhash->{limit} || 10;
464 my $end = $offset + $limit - 1;
466 # do some simple sanity checking
467 if(!$searchhash->{searches} or
469 !$searchhash->{searches}->{title} and
470 !$searchhash->{searches}->{author} and
471 !$searchhash->{searches}->{subject} and
472 !$searchhash->{searches}->{series} and
473 !$searchhash->{searches}->{keyword} ) ) {
474 return { count => 0 };
479 $searchhash->{offset} = 0;
480 $searchhash->{limit} = $maxlimit;
482 return { count => 0 } if $offset > $maxlimit;
485 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
486 my $s = JSON->perl2JSON(\@search);
487 my $ckey = $pfx . md5_hex($method . $s);
489 $logger->info("bib search for: $s");
491 $searchhash->{limit} -= $offset;
494 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
498 $method .= ".staff" if($self->api_name =~ /staff/);
499 $method .= ".atomic";
501 for (keys %$searchhash) {
502 delete $$searchhash{$_}
503 unless defined $$searchhash{$_};
506 $result = $U->storagereq( $method, %$searchhash );
512 return {count => 0} unless ($result && $$result[0]);
514 #for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
517 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
522 # If we didn't get this data from the cache, put it into the cache
523 # then return the correct offset of records
524 $logger->debug("putting search cache $ckey\n");
525 put_cache($ckey, $count, \@recs);
528 for ($offset..$end) {
529 last unless $recs[$_];
534 #$logger->debug("cache done .. returning $offset..$end : " . JSON->perl2JSON(\@recs));
537 return { ids => \@recs, count => $count };
548 my $end = $offset + $limit - 1;
550 $logger->debug("searching cache for $key : $start..$end\n");
552 return undef unless $cache;
553 my $data = $cache->get_cache($key);
555 return undef unless $data;
557 my $count = $data->[0];
560 return undef unless $offset < $count;
564 for( my $i = $offset; $i <= $end; $i++ ) {
565 last unless my $d = $$data[$i];
569 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
576 my( $key, $count, $data ) = @_;
577 return undef unless $cache;
578 $logger->debug("search_cache putting ".
579 scalar(@$data)." items at key $key with timeout $cache_timeout");
580 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
588 __PACKAGE__->register_method(
589 method => "biblio_mrid_to_modsbatch_batch",
590 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
592 sub biblio_mrid_to_modsbatch_batch {
593 my( $self, $client, $mrids) = @_;
594 warn "Performing mrid_to_modsbatch_batch...";
596 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
597 for my $id (@$mrids) {
598 next unless defined $id;
599 my ($m) = $method->run($id);
606 __PACKAGE__->register_method(
607 method => "biblio_mrid_to_modsbatch",
608 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
609 notes => <<" NOTES");
610 Returns the mvr associated with a given metarecod. If none exists,
614 __PACKAGE__->register_method(
615 method => "biblio_mrid_to_modsbatch",
616 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
617 notes => <<" NOTES");
618 Returns the mvr associated with a given metarecod. If none exists,
622 sub biblio_mrid_to_modsbatch {
623 my( $self, $client, $mrid, $args) = @_;
625 warn "Grabbing mvr for $mrid\n";
627 my ($mr, $evt) = _grab_metarecord($mrid);
628 return $evt unless $mr;
630 my $mvr = $self->biblio_mrid_check_mvr($client, $mr);
631 $mvr = $self->biblio_mrid_make_modsbatch( $client, $mr ) unless $mvr;
633 return $mvr unless ref($args);
635 # Here we find the lead record appropriate for the given filters
636 # and use that for the title and author of the metarecord
637 my $format = $$args{format};
638 my $org = $$args{org};
639 my $depth = $$args{depth};
641 return $mvr unless $format or $org or $depth;
643 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
644 $method = "$method.staff" if $self->api_name =~ /staff/o;
646 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
648 if( my $mods = $U->record_to_mvr($rec) ) {
650 $mvr->title($mods->title);
651 $mvr->title($mods->author);
652 $logger->debug("mods_slim updating title and ".
653 "author in mvr with ".$mods->title." : ".$mods->author);
659 # converts a metarecord to an mvr
662 my $perl = JSON->JSON2perl($mr->mods());
663 return Fieldmapper::metabib::virtual_record->new($perl);
666 # checks to see if a metarecord has mods, if so returns true;
668 __PACKAGE__->register_method(
669 method => "biblio_mrid_check_mvr",
670 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
671 notes => <<" NOTES");
672 Takes a metarecord ID or a metarecord object and returns true
673 if the metarecord already has an mvr associated with it.
676 sub biblio_mrid_check_mvr {
677 my( $self, $client, $mrid ) = @_;
681 if(ref($mrid)) { $mr = $mrid; }
682 else { ($mr, $evt) = _grab_metarecord($mrid); }
685 warn "Checking mvr for mr " . $mr->id . "\n";
687 return _mr_to_mvr($mr) if $mr->mods();
691 sub _grab_metarecord {
693 #my $e = OpenILS::Utils::Editor->new;
694 my $e = new_editor();
695 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
700 __PACKAGE__->register_method(
701 method => "biblio_mrid_make_modsbatch",
702 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
703 notes => <<" NOTES");
704 Takes either a metarecord ID or a metarecord object.
705 Forces the creations of an mvr for the given metarecord.
706 The created mvr is returned.
709 sub biblio_mrid_make_modsbatch {
710 my( $self, $client, $mrid ) = @_;
712 #my $e = OpenILS::Utils::Editor->new;
713 my $e = new_editor();
720 $mr = $e->retrieve_metabib_metarecord($mrid)
724 my $masterid = $mr->master_record;
725 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
727 my $ids = $U->storagereq(
728 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
729 return undef unless @$ids;
731 my $master = $e->retrieve_biblio_record_entry($masterid)
734 # start the mods batch
735 my $u = OpenILS::Utils::ModsParser->new();
736 $u->start_mods_batch( $master->marc );
738 # grab all of the sub-records and shove them into the batch
739 my @ids = grep { $_ ne $masterid } @$ids;
740 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
745 my $r = $e->retrieve_biblio_record_entry($i);
746 push( @$subrecs, $r ) if $r;
751 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
752 $u->push_mods_batch( $_->marc ) if $_->marc;
756 # finish up and send to the client
757 my $mods = $u->finish_mods_batch();
758 $mods->doc_id($mrid);
759 $client->respond_complete($mods);
762 # now update the mods string in the db
763 my $string = JSON->perl2JSON($mods->decast);
766 #$e = OpenILS::Utils::Editor->new(xact => 1);
767 $e = new_editor(xact => 1);
768 $e->update_metabib_metarecord($mr)
769 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
778 # converts a mr id into a list of record ids
780 __PACKAGE__->register_method(
781 method => "biblio_mrid_to_record_ids",
782 api_name => "open-ils.search.biblio.metarecord_to_records",
785 __PACKAGE__->register_method(
786 method => "biblio_mrid_to_record_ids",
787 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
790 sub biblio_mrid_to_record_ids {
791 my( $self, $client, $mrid, $args ) = @_;
793 my $format = $$args{format};
794 my $org = $$args{org};
795 my $depth = $$args{depth};
797 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
798 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
799 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
801 return { count => scalar(@$recs), ids => $recs };
805 __PACKAGE__->register_method(
806 method => "biblio_record_to_marc_html",
807 api_name => "open-ils.search.biblio.record.html" );
809 my $parser = XML::LibXML->new();
810 my $xslt = XML::LibXSLT->new();
813 my $settings_client = OpenSRF::Utils::SettingsClient->new();
814 sub biblio_record_to_marc_html {
815 my( $self, $client, $recordid ) = @_;
818 my $dir = $settings_client->config_value( "dirs", "xsl" );
819 my $xsl = $settings_client->config_value(
820 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
822 $xsl = $parser->parse_file("$dir/$xsl");
823 $marc_sheet = $xslt->parse_stylesheet( $xsl );
827 my $record = $apputils->simple_scalar_request(
829 "open-ils.cstore.direct.biblio.record_entry.retrieve",
832 my $xmldoc = $parser->parse_string($record->marc);
833 my $html = $marc_sheet->transform($xmldoc);
834 $html = $html->toString();
841 __PACKAGE__->register_method(
842 method => "retrieve_all_copy_locations",
843 api_name => "open-ils.search.config.copy_location.retrieve.all" );
845 my $shelving_locations;
846 sub retrieve_all_copy_locations {
847 my( $self, $client ) = @_;
848 if(!$shelving_locations) {
849 $shelving_locations = $apputils->simple_scalar_request(
851 "open-ils.cstore.direct.asset.copy_location.search.atomic",
852 { id => { "!=" => undef } }
855 return $shelving_locations;
861 __PACKAGE__->register_method(
862 method => "retrieve_all_copy_statuses",
863 api_name => "open-ils.search.config.copy_status.retrieve.all" );
866 sub retrieve_all_copy_statuses {
867 my( $self, $client ) = @_;
868 return $copy_statuses if $copy_statuses;
869 return $copy_statuses =
870 new_editor()->retrieve_all_config_copy_status();
874 __PACKAGE__->register_method(
875 method => "copy_counts_per_org",
876 api_name => "open-ils.search.biblio.copy_counts.retrieve");
878 __PACKAGE__->register_method(
879 method => "copy_counts_per_org",
880 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
882 sub copy_counts_per_org {
883 my( $self, $client, $record_id ) = @_;
885 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
887 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
888 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
890 my $counts = $apputils->simple_scalar_request(
891 "open-ils.storage", $method, $record_id );
893 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
898 __PACKAGE__->register_method(
899 method => "copy_count_summary",
900 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
901 notes => <<" NOTES");
902 returns an array of these:
903 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
904 where statusx is a copy status name. the statuses are sorted
908 sub copy_count_summary {
909 my( $self, $client, $rid, $org, $depth ) = @_;
912 return $U->storagereq(
913 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
919 __PACKAGE__->register_method(
920 method => "multiclass_search",
921 api_name => "open-ils.search.biblio.multiclass",
922 notes => <<" NOTES");
923 Performs a multiclass search
924 PARAMS( searchBlob, org_unit, format, limit )
925 where searchBlob is defined like this:
927 "title" : { "term" : "water" },
928 "author" : { "term" : "smith" },
933 __PACKAGE__->register_method(
934 method => "multiclass_search",
935 api_name => "open-ils.search.biblio.multiclass.staff",
936 notes => "see open-ils.search.biblio.multiclass" );
938 sub multiclass_search {
939 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
941 $logger->debug("Performing multiclass search with org => $orgid, " .
942 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
944 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
945 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
948 my $records = $apputils->simplereq(
949 'open-ils.storage', $meth,
950 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
955 if( ref($records) and $records->[0] and
956 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
958 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
960 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
961 return { ids => $recs, count => $count };
967 __PACKAGE__->register_method(
968 method => "multiclass_search",
969 api_name => "open-ils.search.biblio.multiclass",
971 Performs a multiclass search
972 @param args A names hash of arguments:
973 org_unit : The org to focus the search on
974 depth : The search depth
977 offset : Search offset
978 searches : A named hash of searches which has the following format:
980 "title" : { "term" : "water" },
981 "author" : { "term" : "smith" },
984 @return { ids : <array of ids>, count : hitcount }
988 __PACKAGE__->register_method(
989 method => "multiclass_search",
990 api_name => "open-ils.search.biblio.multiclass.staff",
991 notes => q/@see open-ils.search.biblio.multiclass/ );
993 sub multiclass_search {
994 my( $self, $client, $args ) = @_;
996 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
997 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
998 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1000 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1005 if( ref($records) and $records->[0] and
1006 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1008 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1010 return { ids => $recs, count => $count };
1017 __PACKAGE__->register_method(
1018 method => "marc_search",
1019 api_name => "open-ils.search.biblio.marc.staff");
1021 __PACKAGE__->register_method(
1022 method => "marc_search",
1023 api_name => "open-ils.search.biblio.marc",
1024 notes => <<" NOTES");
1026 open-ils.storage.biblio.full_rec.multi_search.atomic
1027 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1028 "limit":5,"sort":"author","item_type":"g"}
1032 my( $self, $conn, $args, $limit, $offset ) = @_;
1034 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1035 $method .= ".staff" if $self->api_name =~ /staff/;
1036 $method .= ".atomic";
1042 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1043 my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
1045 my $recs = search_cache($ckey, $offset, $limit);
1048 $recs = $U->storagereq($method, %$args) || [];
1050 put_cache($ckey, scalar(@$recs), $recs);
1051 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1058 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1059 my @recs = map { $_->[0] } @$recs;
1061 return { ids => \@recs, count => $count };
1065 __PACKAGE__->register_method(
1066 method => "biblio_search_isbn",
1067 api_name => "open-ils.search.biblio.isbn",
1070 sub biblio_search_isbn {
1071 my( $self, $client, $isbn ) = @_;
1072 $logger->debug("Searching ISBN $isbn");
1073 my $e = new_editor();
1074 my $recs = $U->storagereq(
1075 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1076 return { ids => $recs, count => scalar(@$recs) };
1080 __PACKAGE__->register_method(
1081 method => "biblio_search_issn",
1082 api_name => "open-ils.search.biblio.issn",
1085 sub biblio_search_issn {
1086 my( $self, $client, $issn ) = @_;
1087 $logger->debug("Searching ISSN $issn");
1088 my $e = new_editor();
1089 my $recs = $U->storagereq(
1090 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1091 return { ids => $recs, count => scalar(@$recs) };
1097 __PACKAGE__->register_method(
1098 method => "fetch_mods_by_copy",
1099 api_name => "open-ils.search.biblio.mods_from_copy",
1102 sub fetch_mods_by_copy {
1103 my( $self, $client, $copyid ) = @_;
1104 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1105 return $evt if $evt;
1106 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1107 return $apputils->record_to_mvr($record);
1112 # -------------------------------------------------------------------------------------
1114 __PACKAGE__->register_method(
1115 method => "cn_browse",
1116 api_name => "open-ils.search.callnumber.browse.target",
1117 notes => "Starts a callnumber browse"
1120 __PACKAGE__->register_method(
1121 method => "cn_browse",
1122 api_name => "open-ils.search.callnumber.browse.page_up",
1123 notes => "Returns the previous page of callnumbers",
1126 __PACKAGE__->register_method(
1127 method => "cn_browse",
1128 api_name => "open-ils.search.callnumber.browse.page_down",
1129 notes => "Returns the next page of callnumbers",
1133 # RETURNS array of arrays like so: label, owning_lib, record, id
1135 my( $self, $client, @params ) = @_;
1138 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1139 if( $self->api_name =~ /target/ );
1140 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1141 if( $self->api_name =~ /page_up/ );
1142 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1143 if( $self->api_name =~ /page_down/ );
1145 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1147 # -------------------------------------------------------------------------------------
1149 __PACKAGE__->register_method(
1150 method => "fetch_cn",
1151 api_name => "open-ils.search.callnumber.retrieve",
1152 notes => "retrieves a callnumber based on ID",
1156 my( $self, $client, $id ) = @_;
1157 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1158 return $evt if $evt;
1162 __PACKAGE__->register_method (
1163 method => "fetch_copy_by_cn",
1164 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1166 Returns an array of copy id's by callnumber id
1167 @param cnid The callnumber id
1168 @return An array of copy ids
1172 sub fetch_copy_by_cn {
1173 my( $self, $conn, $cnid ) = @_;
1174 return $U->cstorereq(
1175 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1176 { call_number => $cnid, deleted => 'f' } );
1179 __PACKAGE__->register_method (
1180 method => 'fetch_cn_by_info',
1181 api_name => 'open-ils.search.call_number.retrieve_by_info',
1183 @param label The callnumber label
1184 @param record The record the cn is attached to
1185 @param org The owning library of the cn
1186 @return The callnumber object
1191 sub fetch_cn_by_info {
1192 my( $self, $conn, $label, $record, $org ) = @_;
1193 return $U->cstorereq(
1194 'open-ils.cstore.direct.asset.call_number.search',
1195 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1202 __PACKAGE__->register_method (
1203 method => 'bib_extras',
1204 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1205 __PACKAGE__->register_method (
1206 method => 'bib_extras',
1207 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1208 __PACKAGE__->register_method (
1209 method => 'bib_extras',
1210 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1211 __PACKAGE__->register_method (
1212 method => 'bib_extras',
1213 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1218 my $e = new_editor();
1220 return $e->retrieve_all_config_lit_form_map()
1221 if( $self->api_name =~ /lit_form/ );
1223 return $e->retrieve_all_config_item_form_map()
1224 if( $self->api_name =~ /item_form_map/ );
1226 return $e->retrieve_all_config_item_type_map()
1227 if( $self->api_name =~ /item_type_map/ );
1229 return $e->retrieve_all_config_audience_map()
1230 if( $self->api_name =~ /audience_map/ );
1237 __PACKAGE__->register_method(
1238 method => 'fetch_slim_record',
1239 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1241 Returns a biblio.record_entry without the attached marcxml
1245 sub fetch_slim_record {
1246 my( $self, $conn, $ids ) = @_;
1248 #my $editor = OpenILS::Utils::Editor->new;
1249 my $editor = new_editor();
1252 return $editor->event unless
1253 my $r = $editor->retrieve_biblio_record_entry($_);
1262 __PACKAGE__->register_method(
1263 method => 'rec_to_mr_rec_descriptors',
1264 api_name => 'open-ils.search.metabib.record_to_descriptors',
1266 specialized method...
1267 Given a biblio record id or a metarecord id,
1268 this returns a list of metabib.record_descriptor
1269 objects that live within the same metarecord
1270 @param args Object of args including:
1274 sub rec_to_mr_rec_descriptors {
1275 my( $self, $conn, $args ) = @_;
1277 my $rec = $$args{record};
1278 my $mrec = $$args{metarecord};
1279 my $item_forms = $$args{item_forms};
1280 my $item_types = $$args{item_types};
1281 my $item_lang = $$args{item_lang};
1283 my $e = new_editor();
1287 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1288 return $e->event unless @$map;
1289 $mrec = $$map[0]->metarecord;
1292 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1293 return $e->event unless @$recs;
1295 my @recs = map { $_->source } @$recs;
1296 my $search = { record => \@recs };
1297 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1298 $search->{item_type} = $item_types if $item_types and @$item_types;
1299 $search->{item_lang} = $item_lang if $item_lang;
1301 my $desc = $e->search_metabib_record_descriptor($search);
1303 return { metarecord => $mrec, descriptors => $desc };
1309 __PACKAGE__->register_method(
1310 method => 'copies_created_on',
1314 sub copies_created_on {
1315 my( $self, $conn, $auth, $org, $date ) = @_;
1316 my $e = new_editor(authtoken=>$auth);
1317 return $e->event unless $e->checkauth;
1321 __PACKAGE__->register_method(
1322 method => 'fetch_age_protect',
1323 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1326 sub fetch_age_protect {
1327 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1331 __PACKAGE__->register_method(
1332 method => 'copies_by_cn_label',
1333 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1336 __PACKAGE__->register_method(
1337 method => 'copies_by_cn_label',
1338 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1341 sub copies_by_cn_label {
1342 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1343 my $e = new_editor();
1344 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1345 return [] unless @$cns;
1347 # show all non-deleted copies in the staff client ...
1348 if ($self->api_name =~ /staff$/o) {
1349 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1352 # ... otherwise, grab the copies ...
1353 my $copies = $e->search_asset_copy(
1354 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1355 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1359 # ... and test for location and status visibility
1360 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];