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);
262 __PACKAGE__->register_method(
263 method => 'fleshed_by_barcode',
264 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",);
265 sub fleshed_by_barcode {
266 my( $self, $conn, $barcode ) = @_;
267 my $e = new_editor();
268 my $copyid = $e->search_asset_copy(
269 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
271 return $self->fleshed_copy_retrieve2($conn, $copyid);
275 __PACKAGE__->register_method(
276 method => "fleshed_copy_retrieve2",
277 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
279 sub fleshed_copy_retrieve2 {
280 my( $self, $client, $id ) = @_;
281 my $e = new_editor();
282 my $copy = $e->retrieve_asset_copy(
288 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
289 ascecm => [ qw/ stat_cat stat_cat_entry / ],
293 ) or return $e->event;
295 # For backwards compatibility
296 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
298 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
300 $e->search_action_circulation(
302 { target_copy => $copy->id },
304 order_by => { circ => 'xact_start desc' },
316 __PACKAGE__->register_method(
317 method => 'flesh_copy_custom',
318 api_name => 'open-ils.search.asset.copy.fleshed.custom'
321 sub flesh_copy_custom {
322 my( $self, $conn, $copyid, $fields ) = @_;
323 my $e = new_editor();
324 my $copy = $e->retrieve_asset_copy(
334 ) or return $e->event;
343 __PACKAGE__->register_method(
344 method => "biblio_barcode_to_title",
345 api_name => "open-ils.search.biblio.find_by_barcode",
348 sub biblio_barcode_to_title {
349 my( $self, $client, $barcode ) = @_;
351 my $title = $apputils->simple_scalar_request(
353 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
355 return { ids => [ $title->id ], count => 1 } if $title;
356 return { count => 0 };
360 __PACKAGE__->register_method(
361 method => "biblio_copy_to_mods",
362 api_name => "open-ils.search.biblio.copy.mods.retrieve",
365 # takes a copy object and returns it fleshed mods object
366 sub biblio_copy_to_mods {
367 my( $self, $client, $copy ) = @_;
369 my $volume = $U->cstorereq(
370 "open-ils.cstore.direct.asset.call_number.retrieve",
371 $copy->call_number() );
373 my $mods = _records_to_mods($volume->record());
374 $mods = shift @$mods;
375 $volume->copies([$copy]);
376 push @{$mods->call_numbers()}, $volume;
382 # ----------------------------------------------------------------------------
383 # These are the main OPAC search methods
384 # ----------------------------------------------------------------------------
386 __PACKAGE__->register_method(
387 method => 'the_quest_for_knowledge',
388 api_name => 'open-ils.search.biblio.multiclass',
390 Performs a multi class bilbli or metabib search
391 @param searchhash A search object layed out like so:
392 searches : { "$class" : "$value", ...}
393 org_unit : The org id to focus the search at
394 depth : The org depth
395 limit : The search limit
396 offset : The search offset
397 format : The MARC format
398 sort : What field to sort the results on [ author | title | pubdate ]
399 sort_dir : What direction do we sort? [ asc | desc ]
400 @return An object of the form
401 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
405 __PACKAGE__->register_method(
406 method => 'the_quest_for_knowledge',
407 api_name => 'open-ils.search.biblio.multiclass.staff',
408 signature => q/@see open-ils.search.biblio.multiclass/);
409 __PACKAGE__->register_method(
410 method => 'the_quest_for_knowledge',
411 api_name => 'open-ils.search.metabib.multiclass',
412 signature => q/@see open-ils.search.biblio.multiclass/);
413 __PACKAGE__->register_method(
414 method => 'the_quest_for_knowledge',
415 api_name => 'open-ils.search.metabib.multiclass.staff',
416 signature => q/@see open-ils.search.biblio.multiclass/);
420 sub the_quest_for_knowledge {
421 my( $self, $conn, $searchhash, $docache ) = @_;
423 return { count => 0 } unless $searchhash and
424 ref $searchhash->{searches} eq 'HASH';
426 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
430 if($self->api_name =~ /metabib/) {
432 $method =~ s/biblio/metabib/o;
436 my $offset = $searchhash->{offset} || 0;
437 my $limit = $searchhash->{limit} || 10;
438 my $end = $offset + $limit - 1;
440 # do some simple sanity checking
441 if(!$searchhash->{searches} or
443 !$searchhash->{searches}->{title} and
444 !$searchhash->{searches}->{author} and
445 !$searchhash->{searches}->{subject} and
446 !$searchhash->{searches}->{series} and
447 !$searchhash->{searches}->{keyword} ) ) {
448 return { count => 0 };
453 $searchhash->{offset} = 0;
454 $searchhash->{limit} = $maxlimit;
456 return { count => 0 } if $offset > $maxlimit;
459 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
460 my $s = JSON->perl2JSON(\@search);
461 my $ckey = $pfx . md5_hex($method . $s);
463 $logger->info("bib search for: $s");
465 $searchhash->{limit} -= $offset;
468 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
472 $method .= ".staff" if($self->api_name =~ /staff/);
473 $method .= ".atomic";
475 for (keys %$searchhash) {
476 delete $$searchhash{$_}
477 unless defined $$searchhash{$_};
480 $result = $U->storagereq( $method, %$searchhash );
486 return {count => 0} unless ($result && $$result[0]);
488 #for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
491 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
496 # If we didn't get this data from the cache, put it into the cache
497 # then return the correct offset of records
498 $logger->debug("putting search cache $ckey\n");
499 put_cache($ckey, $count, \@recs);
502 for ($offset..$end) {
503 last unless $recs[$_];
508 #$logger->debug("cache done .. returning $offset..$end : " . JSON->perl2JSON(\@recs));
511 return { ids => \@recs, count => $count };
522 my $end = $offset + $limit - 1;
524 $logger->debug("searching cache for $key : $start..$end\n");
526 return undef unless $cache;
527 my $data = $cache->get_cache($key);
529 return undef unless $data;
531 my $count = $data->[0];
534 return undef unless $offset < $count;
538 for( my $i = $offset; $i <= $end; $i++ ) {
539 last unless my $d = $$data[$i];
543 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
550 my( $key, $count, $data ) = @_;
551 return undef unless $cache;
552 $logger->debug("search_cache putting ".
553 scalar(@$data)." items at key $key with timeout $cache_timeout");
554 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
562 __PACKAGE__->register_method(
563 method => "biblio_mrid_to_modsbatch_batch",
564 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
566 sub biblio_mrid_to_modsbatch_batch {
567 my( $self, $client, $mrids) = @_;
568 warn "Performing mrid_to_modsbatch_batch...";
570 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
571 for my $id (@$mrids) {
572 next unless defined $id;
573 my ($m) = $method->run($id);
580 __PACKAGE__->register_method(
581 method => "biblio_mrid_to_modsbatch",
582 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
583 notes => <<" NOTES");
584 Returns the mvr associated with a given metarecod. If none exists,
588 __PACKAGE__->register_method(
589 method => "biblio_mrid_to_modsbatch",
590 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
591 notes => <<" NOTES");
592 Returns the mvr associated with a given metarecod. If none exists,
596 sub biblio_mrid_to_modsbatch {
597 my( $self, $client, $mrid, $args) = @_;
599 warn "Grabbing mvr for $mrid\n";
601 my ($mr, $evt) = _grab_metarecord($mrid);
602 return $evt unless $mr;
604 my $mvr = $self->biblio_mrid_check_mvr($client, $mr);
605 $mvr = $self->biblio_mrid_make_modsbatch( $client, $mr ) unless $mvr;
607 return $mvr unless ref($args);
609 # Here we find the lead record appropriate for the given filters
610 # and use that for the title and author of the metarecord
611 my $format = $$args{format};
612 my $org = $$args{org};
613 my $depth = $$args{depth};
615 return $mvr unless $format or $org or $depth;
617 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
618 $method = "$method.staff" if $self->api_name =~ /staff/o;
620 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
622 if( my $mods = $U->record_to_mvr($rec) ) {
624 $mvr->title($mods->title);
625 $mvr->title($mods->author);
626 $logger->debug("mods_slim updating title and ".
627 "author in mvr with ".$mods->title." : ".$mods->author);
633 # converts a metarecord to an mvr
636 my $perl = JSON->JSON2perl($mr->mods());
637 return Fieldmapper::metabib::virtual_record->new($perl);
640 # checks to see if a metarecord has mods, if so returns true;
642 __PACKAGE__->register_method(
643 method => "biblio_mrid_check_mvr",
644 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
645 notes => <<" NOTES");
646 Takes a metarecord ID or a metarecord object and returns true
647 if the metarecord already has an mvr associated with it.
650 sub biblio_mrid_check_mvr {
651 my( $self, $client, $mrid ) = @_;
655 if(ref($mrid)) { $mr = $mrid; }
656 else { ($mr, $evt) = _grab_metarecord($mrid); }
659 warn "Checking mvr for mr " . $mr->id . "\n";
661 return _mr_to_mvr($mr) if $mr->mods();
665 sub _grab_metarecord {
667 #my $e = OpenILS::Utils::Editor->new;
668 my $e = new_editor();
669 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
674 __PACKAGE__->register_method(
675 method => "biblio_mrid_make_modsbatch",
676 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
677 notes => <<" NOTES");
678 Takes either a metarecord ID or a metarecord object.
679 Forces the creations of an mvr for the given metarecord.
680 The created mvr is returned.
683 sub biblio_mrid_make_modsbatch {
684 my( $self, $client, $mrid ) = @_;
686 #my $e = OpenILS::Utils::Editor->new;
687 my $e = new_editor();
694 $mr = $e->retrieve_metabib_metarecord($mrid)
698 my $masterid = $mr->master_record;
699 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
701 my $ids = $U->storagereq(
702 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
703 return undef unless @$ids;
705 my $master = $e->retrieve_biblio_record_entry($masterid)
708 # start the mods batch
709 my $u = OpenILS::Utils::ModsParser->new();
710 $u->start_mods_batch( $master->marc );
712 # grab all of the sub-records and shove them into the batch
713 my @ids = grep { $_ ne $masterid } @$ids;
714 my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
717 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
718 $u->push_mods_batch( $_->marc ) if $_->marc;
722 # finish up and send to the client
723 my $mods = $u->finish_mods_batch();
724 $mods->doc_id($mrid);
725 $client->respond_complete($mods);
728 # now update the mods string in the db
729 my $string = JSON->perl2JSON($mods->decast);
732 #$e = OpenILS::Utils::Editor->new(xact => 1);
733 $e = new_editor(xact => 1);
734 $e->update_metabib_metarecord($mr)
735 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
744 # converts a mr id into a list of record ids
746 __PACKAGE__->register_method(
747 method => "biblio_mrid_to_record_ids",
748 api_name => "open-ils.search.biblio.metarecord_to_records",
751 __PACKAGE__->register_method(
752 method => "biblio_mrid_to_record_ids",
753 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
756 sub biblio_mrid_to_record_ids {
757 my( $self, $client, $mrid, $args ) = @_;
759 my $format = $$args{format};
760 my $org = $$args{org};
761 my $depth = $$args{depth};
763 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
764 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
765 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
767 return { count => scalar(@$recs), ids => $recs };
771 __PACKAGE__->register_method(
772 method => "biblio_record_to_marc_html",
773 api_name => "open-ils.search.biblio.record.html" );
775 my $parser = XML::LibXML->new();
776 my $xslt = XML::LibXSLT->new();
779 my $settings_client = OpenSRF::Utils::SettingsClient->new();
780 sub biblio_record_to_marc_html {
781 my( $self, $client, $recordid ) = @_;
784 my $dir = $settings_client->config_value( "dirs", "xsl" );
785 my $xsl = $settings_client->config_value(
786 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
788 $xsl = $parser->parse_file("$dir/$xsl");
789 $marc_sheet = $xslt->parse_stylesheet( $xsl );
793 my $record = $apputils->simple_scalar_request(
795 "open-ils.cstore.direct.biblio.record_entry.retrieve",
798 my $xmldoc = $parser->parse_string($record->marc);
799 my $html = $marc_sheet->transform($xmldoc);
800 $html = $html->toString();
807 __PACKAGE__->register_method(
808 method => "retrieve_all_copy_locations",
809 api_name => "open-ils.search.config.copy_location.retrieve.all" );
811 my $shelving_locations;
812 sub retrieve_all_copy_locations {
813 my( $self, $client ) = @_;
814 if(!$shelving_locations) {
815 $shelving_locations = $apputils->simple_scalar_request(
817 "open-ils.cstore.direct.asset.copy_location.search.atomic",
818 { id => { "!=" => undef } }
821 return $shelving_locations;
827 __PACKAGE__->register_method(
828 method => "retrieve_all_copy_statuses",
829 api_name => "open-ils.search.config.copy_status.retrieve.all" );
832 sub retrieve_all_copy_statuses {
833 my( $self, $client ) = @_;
834 return $copy_statuses if $copy_statuses;
835 return $copy_statuses =
836 new_editor()->retrieve_all_config_copy_status();
840 __PACKAGE__->register_method(
841 method => "copy_counts_per_org",
842 api_name => "open-ils.search.biblio.copy_counts.retrieve");
844 __PACKAGE__->register_method(
845 method => "copy_counts_per_org",
846 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
848 sub copy_counts_per_org {
849 my( $self, $client, $record_id ) = @_;
851 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
853 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
854 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
856 my $counts = $apputils->simple_scalar_request(
857 "open-ils.storage", $method, $record_id );
859 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
864 __PACKAGE__->register_method(
865 method => "copy_count_summary",
866 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
867 notes => <<" NOTES");
868 returns an array of these:
869 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
870 where statusx is a copy status name. the statuses are sorted
874 sub copy_count_summary {
875 my( $self, $client, $rid, $org, $depth ) = @_;
878 return $U->storagereq(
879 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
885 __PACKAGE__->register_method(
886 method => "multiclass_search",
887 api_name => "open-ils.search.biblio.multiclass",
888 notes => <<" NOTES");
889 Performs a multiclass search
890 PARAMS( searchBlob, org_unit, format, limit )
891 where searchBlob is defined like this:
893 "title" : { "term" : "water" },
894 "author" : { "term" : "smith" },
899 __PACKAGE__->register_method(
900 method => "multiclass_search",
901 api_name => "open-ils.search.biblio.multiclass.staff",
902 notes => "see open-ils.search.biblio.multiclass" );
904 sub multiclass_search {
905 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
907 $logger->debug("Performing multiclass search with org => $orgid, " .
908 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
910 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
911 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
914 my $records = $apputils->simplereq(
915 'open-ils.storage', $meth,
916 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
921 if( ref($records) and $records->[0] and
922 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
924 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
926 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
927 return { ids => $recs, count => $count };
933 __PACKAGE__->register_method(
934 method => "multiclass_search",
935 api_name => "open-ils.search.biblio.multiclass",
937 Performs a multiclass search
938 @param args A names hash of arguments:
939 org_unit : The org to focus the search on
940 depth : The search depth
943 offset : Search offset
944 searches : A named hash of searches which has the following format:
946 "title" : { "term" : "water" },
947 "author" : { "term" : "smith" },
950 @return { ids : <array of ids>, count : hitcount }
954 __PACKAGE__->register_method(
955 method => "multiclass_search",
956 api_name => "open-ils.search.biblio.multiclass.staff",
957 notes => q/@see open-ils.search.biblio.multiclass/ );
959 sub multiclass_search {
960 my( $self, $client, $args ) = @_;
962 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
963 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
964 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
966 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
971 if( ref($records) and $records->[0] and
972 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
974 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
976 return { ids => $recs, count => $count };
983 __PACKAGE__->register_method(
984 method => "marc_search",
985 api_name => "open-ils.search.biblio.marc.staff");
987 __PACKAGE__->register_method(
988 method => "marc_search",
989 api_name => "open-ils.search.biblio.marc",
990 notes => <<" NOTES");
992 open-ils.storage.biblio.full_rec.multi_search.atomic
993 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
994 "limit":5,"sort":"author","item_type":"g"}
998 my( $self, $conn, $args, $limit, $offset ) = @_;
1000 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1001 $method .= ".staff" if $self->api_name =~ /staff/;
1002 $method .= ".atomic";
1008 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1009 my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
1011 my $recs = search_cache($ckey, $offset, $limit);
1014 $recs = $U->storagereq($method, %$args);
1015 put_cache($ckey, scalar(@$recs), $recs);
1016 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1020 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1021 my @recs = map { $_->[0] } @$recs;
1023 return { ids => \@recs, count => $count };
1027 __PACKAGE__->register_method(
1028 method => "biblio_search_isbn",
1029 api_name => "open-ils.search.biblio.isbn",
1032 sub biblio_search_isbn {
1033 my( $self, $client, $isbn ) = @_;
1034 $logger->debug("Searching ISBN $isbn");
1035 my $e = new_editor();
1036 my $recs = $U->storagereq(
1037 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1038 return { ids => $recs, count => scalar(@$recs) };
1042 __PACKAGE__->register_method(
1043 method => "biblio_search_issn",
1044 api_name => "open-ils.search.biblio.issn",
1047 sub biblio_search_issn {
1048 my( $self, $client, $issn ) = @_;
1049 $logger->debug("Searching ISSN $issn");
1050 my $e = new_editor();
1051 my $recs = $U->storagereq(
1052 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1053 return { ids => $recs, count => scalar(@$recs) };
1059 __PACKAGE__->register_method(
1060 method => "fetch_mods_by_copy",
1061 api_name => "open-ils.search.biblio.mods_from_copy",
1064 sub fetch_mods_by_copy {
1065 my( $self, $client, $copyid ) = @_;
1066 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1067 return $evt if $evt;
1068 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1069 return $apputils->record_to_mvr($record);
1074 # -------------------------------------------------------------------------------------
1076 __PACKAGE__->register_method(
1077 method => "cn_browse",
1078 api_name => "open-ils.search.callnumber.browse.target",
1079 notes => "Starts a callnumber browse"
1082 __PACKAGE__->register_method(
1083 method => "cn_browse",
1084 api_name => "open-ils.search.callnumber.browse.page_up",
1085 notes => "Returns the previous page of callnumbers",
1088 __PACKAGE__->register_method(
1089 method => "cn_browse",
1090 api_name => "open-ils.search.callnumber.browse.page_down",
1091 notes => "Returns the next page of callnumbers",
1095 # RETURNS array of arrays like so: label, owning_lib, record, id
1097 my( $self, $client, @params ) = @_;
1100 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1101 if( $self->api_name =~ /target/ );
1102 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1103 if( $self->api_name =~ /page_up/ );
1104 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1105 if( $self->api_name =~ /page_down/ );
1107 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1109 # -------------------------------------------------------------------------------------
1111 __PACKAGE__->register_method(
1112 method => "fetch_cn",
1113 api_name => "open-ils.search.callnumber.retrieve",
1114 notes => "retrieves a callnumber based on ID",
1118 my( $self, $client, $id ) = @_;
1119 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1120 return $evt if $evt;
1124 __PACKAGE__->register_method (
1125 method => "fetch_copy_by_cn",
1126 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1128 Returns an array of copy id's by callnumber id
1129 @param cnid The callnumber id
1130 @return An array of copy ids
1134 sub fetch_copy_by_cn {
1135 my( $self, $conn, $cnid ) = @_;
1136 return $U->cstorereq(
1137 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1138 { call_number => $cnid, deleted => 'f' } );
1141 __PACKAGE__->register_method (
1142 method => 'fetch_cn_by_info',
1143 api_name => 'open-ils.search.call_number.retrieve_by_info',
1145 @param label The callnumber label
1146 @param record The record the cn is attached to
1147 @param org The owning library of the cn
1148 @return The callnumber object
1153 sub fetch_cn_by_info {
1154 my( $self, $conn, $label, $record, $org ) = @_;
1155 return $U->cstorereq(
1156 'open-ils.cstore.direct.asset.call_number.search',
1157 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1164 __PACKAGE__->register_method (
1165 method => 'bib_extras',
1166 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1167 __PACKAGE__->register_method (
1168 method => 'bib_extras',
1169 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1170 __PACKAGE__->register_method (
1171 method => 'bib_extras',
1172 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1173 __PACKAGE__->register_method (
1174 method => 'bib_extras',
1175 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1180 my $e = new_editor();
1182 return $e->retrieve_all_config_lit_form_map()
1183 if( $self->api_name =~ /lit_form/ );
1185 return $e->retrieve_all_config_item_form_map()
1186 if( $self->api_name =~ /item_form_map/ );
1188 return $e->retrieve_all_config_item_type_map()
1189 if( $self->api_name =~ /item_type_map/ );
1191 return $e->retrieve_all_config_audience_map()
1192 if( $self->api_name =~ /audience_map/ );
1199 __PACKAGE__->register_method(
1200 method => 'fetch_slim_record',
1201 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1203 Returns a biblio.record_entry without the attached marcxml
1207 sub fetch_slim_record {
1208 my( $self, $conn, $ids ) = @_;
1210 #my $editor = OpenILS::Utils::Editor->new;
1211 my $editor = new_editor();
1214 return $editor->event unless
1215 my $r = $editor->retrieve_biblio_record_entry($_);
1224 __PACKAGE__->register_method(
1225 method => 'rec_to_mr_rec_descriptors',
1226 api_name => 'open-ils.search.metabib.record_to_descriptors',
1228 specialized method...
1229 Given a biblio record id or a metarecord id,
1230 this returns a list of metabib.record_descriptor
1231 objects that live within the same metarecord
1232 @param args Object of args including:
1236 sub rec_to_mr_rec_descriptors {
1237 my( $self, $conn, $args ) = @_;
1239 my $rec = $$args{record};
1240 my $mrec = $$args{metarecord};
1241 my $item_forms = $$args{item_forms};
1242 my $item_types = $$args{item_types};
1243 my $item_lang = $$args{item_lang};
1245 my $e = new_editor();
1249 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1250 return $e->event unless @$map;
1251 $mrec = $$map[0]->metarecord;
1254 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1255 return $e->event unless @$recs;
1257 my @recs = map { $_->source } @$recs;
1258 my $search = { record => \@recs };
1259 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1260 $search->{item_type} = $item_types if $item_types and @$item_types;
1261 $search->{item_lang} = $item_lang if $item_lang;
1263 my $desc = $e->search_metabib_record_descriptor($search);
1265 return { metarecord => $mrec, descriptors => $desc };
1271 __PACKAGE__->register_method(
1272 method => 'copies_created_on',
1276 sub copies_created_on {
1277 my( $self, $conn, $auth, $org, $date ) = @_;
1278 my $e = new_editor(authtoken=>$auth);
1279 return $e->event unless $e->checkauth;
1283 __PACKAGE__->register_method(
1284 method => 'fetch_age_protect',
1285 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1288 sub fetch_age_protect {
1289 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1293 __PACKAGE__->register_method(
1294 method => 'copies_by_cn_label',
1295 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1298 sub copies_by_cn_label {
1299 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1300 my $e = new_editor();
1301 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1302 return [] unless @$cns;
1303 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});