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) : [];
719 my $r = $e->retrieve_biblio_record_entry($i);
720 push( @$subrecs, $r ) if $r;
725 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
726 $u->push_mods_batch( $_->marc ) if $_->marc;
730 # finish up and send to the client
731 my $mods = $u->finish_mods_batch();
732 $mods->doc_id($mrid);
733 $client->respond_complete($mods);
736 # now update the mods string in the db
737 my $string = JSON->perl2JSON($mods->decast);
740 #$e = OpenILS::Utils::Editor->new(xact => 1);
741 $e = new_editor(xact => 1);
742 $e->update_metabib_metarecord($mr)
743 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
752 # converts a mr id into a list of record ids
754 __PACKAGE__->register_method(
755 method => "biblio_mrid_to_record_ids",
756 api_name => "open-ils.search.biblio.metarecord_to_records",
759 __PACKAGE__->register_method(
760 method => "biblio_mrid_to_record_ids",
761 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
764 sub biblio_mrid_to_record_ids {
765 my( $self, $client, $mrid, $args ) = @_;
767 my $format = $$args{format};
768 my $org = $$args{org};
769 my $depth = $$args{depth};
771 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
772 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
773 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
775 return { count => scalar(@$recs), ids => $recs };
779 __PACKAGE__->register_method(
780 method => "biblio_record_to_marc_html",
781 api_name => "open-ils.search.biblio.record.html" );
783 my $parser = XML::LibXML->new();
784 my $xslt = XML::LibXSLT->new();
787 my $settings_client = OpenSRF::Utils::SettingsClient->new();
788 sub biblio_record_to_marc_html {
789 my( $self, $client, $recordid ) = @_;
792 my $dir = $settings_client->config_value( "dirs", "xsl" );
793 my $xsl = $settings_client->config_value(
794 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
796 $xsl = $parser->parse_file("$dir/$xsl");
797 $marc_sheet = $xslt->parse_stylesheet( $xsl );
801 my $record = $apputils->simple_scalar_request(
803 "open-ils.cstore.direct.biblio.record_entry.retrieve",
806 my $xmldoc = $parser->parse_string($record->marc);
807 my $html = $marc_sheet->transform($xmldoc);
808 $html = $html->toString();
815 __PACKAGE__->register_method(
816 method => "retrieve_all_copy_locations",
817 api_name => "open-ils.search.config.copy_location.retrieve.all" );
819 my $shelving_locations;
820 sub retrieve_all_copy_locations {
821 my( $self, $client ) = @_;
822 if(!$shelving_locations) {
823 $shelving_locations = $apputils->simple_scalar_request(
825 "open-ils.cstore.direct.asset.copy_location.search.atomic",
826 { id => { "!=" => undef } }
829 return $shelving_locations;
835 __PACKAGE__->register_method(
836 method => "retrieve_all_copy_statuses",
837 api_name => "open-ils.search.config.copy_status.retrieve.all" );
840 sub retrieve_all_copy_statuses {
841 my( $self, $client ) = @_;
842 return $copy_statuses if $copy_statuses;
843 return $copy_statuses =
844 new_editor()->retrieve_all_config_copy_status();
848 __PACKAGE__->register_method(
849 method => "copy_counts_per_org",
850 api_name => "open-ils.search.biblio.copy_counts.retrieve");
852 __PACKAGE__->register_method(
853 method => "copy_counts_per_org",
854 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
856 sub copy_counts_per_org {
857 my( $self, $client, $record_id ) = @_;
859 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
861 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
862 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
864 my $counts = $apputils->simple_scalar_request(
865 "open-ils.storage", $method, $record_id );
867 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
872 __PACKAGE__->register_method(
873 method => "copy_count_summary",
874 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
875 notes => <<" NOTES");
876 returns an array of these:
877 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
878 where statusx is a copy status name. the statuses are sorted
882 sub copy_count_summary {
883 my( $self, $client, $rid, $org, $depth ) = @_;
886 return $U->storagereq(
887 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
893 __PACKAGE__->register_method(
894 method => "multiclass_search",
895 api_name => "open-ils.search.biblio.multiclass",
896 notes => <<" NOTES");
897 Performs a multiclass search
898 PARAMS( searchBlob, org_unit, format, limit )
899 where searchBlob is defined like this:
901 "title" : { "term" : "water" },
902 "author" : { "term" : "smith" },
907 __PACKAGE__->register_method(
908 method => "multiclass_search",
909 api_name => "open-ils.search.biblio.multiclass.staff",
910 notes => "see open-ils.search.biblio.multiclass" );
912 sub multiclass_search {
913 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
915 $logger->debug("Performing multiclass search with org => $orgid, " .
916 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
918 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
919 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
922 my $records = $apputils->simplereq(
923 'open-ils.storage', $meth,
924 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
929 if( ref($records) and $records->[0] and
930 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
932 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
934 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
935 return { ids => $recs, count => $count };
941 __PACKAGE__->register_method(
942 method => "multiclass_search",
943 api_name => "open-ils.search.biblio.multiclass",
945 Performs a multiclass search
946 @param args A names hash of arguments:
947 org_unit : The org to focus the search on
948 depth : The search depth
951 offset : Search offset
952 searches : A named hash of searches which has the following format:
954 "title" : { "term" : "water" },
955 "author" : { "term" : "smith" },
958 @return { ids : <array of ids>, count : hitcount }
962 __PACKAGE__->register_method(
963 method => "multiclass_search",
964 api_name => "open-ils.search.biblio.multiclass.staff",
965 notes => q/@see open-ils.search.biblio.multiclass/ );
967 sub multiclass_search {
968 my( $self, $client, $args ) = @_;
970 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
971 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
972 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
974 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
979 if( ref($records) and $records->[0] and
980 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
982 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
984 return { ids => $recs, count => $count };
991 __PACKAGE__->register_method(
992 method => "marc_search",
993 api_name => "open-ils.search.biblio.marc.staff");
995 __PACKAGE__->register_method(
996 method => "marc_search",
997 api_name => "open-ils.search.biblio.marc",
998 notes => <<" NOTES");
1000 open-ils.storage.biblio.full_rec.multi_search.atomic
1001 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1002 "limit":5,"sort":"author","item_type":"g"}
1006 my( $self, $conn, $args, $limit, $offset ) = @_;
1008 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1009 $method .= ".staff" if $self->api_name =~ /staff/;
1010 $method .= ".atomic";
1016 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1017 my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
1019 my $recs = search_cache($ckey, $offset, $limit);
1022 $recs = $U->storagereq($method, %$args) || [];
1024 put_cache($ckey, scalar(@$recs), $recs);
1025 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1032 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1033 my @recs = map { $_->[0] } @$recs;
1035 return { ids => \@recs, count => $count };
1039 __PACKAGE__->register_method(
1040 method => "biblio_search_isbn",
1041 api_name => "open-ils.search.biblio.isbn",
1044 sub biblio_search_isbn {
1045 my( $self, $client, $isbn ) = @_;
1046 $logger->debug("Searching ISBN $isbn");
1047 my $e = new_editor();
1048 my $recs = $U->storagereq(
1049 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1050 return { ids => $recs, count => scalar(@$recs) };
1054 __PACKAGE__->register_method(
1055 method => "biblio_search_issn",
1056 api_name => "open-ils.search.biblio.issn",
1059 sub biblio_search_issn {
1060 my( $self, $client, $issn ) = @_;
1061 $logger->debug("Searching ISSN $issn");
1062 my $e = new_editor();
1063 my $recs = $U->storagereq(
1064 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1065 return { ids => $recs, count => scalar(@$recs) };
1071 __PACKAGE__->register_method(
1072 method => "fetch_mods_by_copy",
1073 api_name => "open-ils.search.biblio.mods_from_copy",
1076 sub fetch_mods_by_copy {
1077 my( $self, $client, $copyid ) = @_;
1078 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1079 return $evt if $evt;
1080 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1081 return $apputils->record_to_mvr($record);
1086 # -------------------------------------------------------------------------------------
1088 __PACKAGE__->register_method(
1089 method => "cn_browse",
1090 api_name => "open-ils.search.callnumber.browse.target",
1091 notes => "Starts a callnumber browse"
1094 __PACKAGE__->register_method(
1095 method => "cn_browse",
1096 api_name => "open-ils.search.callnumber.browse.page_up",
1097 notes => "Returns the previous page of callnumbers",
1100 __PACKAGE__->register_method(
1101 method => "cn_browse",
1102 api_name => "open-ils.search.callnumber.browse.page_down",
1103 notes => "Returns the next page of callnumbers",
1107 # RETURNS array of arrays like so: label, owning_lib, record, id
1109 my( $self, $client, @params ) = @_;
1112 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1113 if( $self->api_name =~ /target/ );
1114 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1115 if( $self->api_name =~ /page_up/ );
1116 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1117 if( $self->api_name =~ /page_down/ );
1119 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1121 # -------------------------------------------------------------------------------------
1123 __PACKAGE__->register_method(
1124 method => "fetch_cn",
1125 api_name => "open-ils.search.callnumber.retrieve",
1126 notes => "retrieves a callnumber based on ID",
1130 my( $self, $client, $id ) = @_;
1131 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1132 return $evt if $evt;
1136 __PACKAGE__->register_method (
1137 method => "fetch_copy_by_cn",
1138 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1140 Returns an array of copy id's by callnumber id
1141 @param cnid The callnumber id
1142 @return An array of copy ids
1146 sub fetch_copy_by_cn {
1147 my( $self, $conn, $cnid ) = @_;
1148 return $U->cstorereq(
1149 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1150 { call_number => $cnid, deleted => 'f' } );
1153 __PACKAGE__->register_method (
1154 method => 'fetch_cn_by_info',
1155 api_name => 'open-ils.search.call_number.retrieve_by_info',
1157 @param label The callnumber label
1158 @param record The record the cn is attached to
1159 @param org The owning library of the cn
1160 @return The callnumber object
1165 sub fetch_cn_by_info {
1166 my( $self, $conn, $label, $record, $org ) = @_;
1167 return $U->cstorereq(
1168 'open-ils.cstore.direct.asset.call_number.search',
1169 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1176 __PACKAGE__->register_method (
1177 method => 'bib_extras',
1178 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1179 __PACKAGE__->register_method (
1180 method => 'bib_extras',
1181 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1182 __PACKAGE__->register_method (
1183 method => 'bib_extras',
1184 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1185 __PACKAGE__->register_method (
1186 method => 'bib_extras',
1187 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1192 my $e = new_editor();
1194 return $e->retrieve_all_config_lit_form_map()
1195 if( $self->api_name =~ /lit_form/ );
1197 return $e->retrieve_all_config_item_form_map()
1198 if( $self->api_name =~ /item_form_map/ );
1200 return $e->retrieve_all_config_item_type_map()
1201 if( $self->api_name =~ /item_type_map/ );
1203 return $e->retrieve_all_config_audience_map()
1204 if( $self->api_name =~ /audience_map/ );
1211 __PACKAGE__->register_method(
1212 method => 'fetch_slim_record',
1213 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1215 Returns a biblio.record_entry without the attached marcxml
1219 sub fetch_slim_record {
1220 my( $self, $conn, $ids ) = @_;
1222 #my $editor = OpenILS::Utils::Editor->new;
1223 my $editor = new_editor();
1226 return $editor->event unless
1227 my $r = $editor->retrieve_biblio_record_entry($_);
1236 __PACKAGE__->register_method(
1237 method => 'rec_to_mr_rec_descriptors',
1238 api_name => 'open-ils.search.metabib.record_to_descriptors',
1240 specialized method...
1241 Given a biblio record id or a metarecord id,
1242 this returns a list of metabib.record_descriptor
1243 objects that live within the same metarecord
1244 @param args Object of args including:
1248 sub rec_to_mr_rec_descriptors {
1249 my( $self, $conn, $args ) = @_;
1251 my $rec = $$args{record};
1252 my $mrec = $$args{metarecord};
1253 my $item_forms = $$args{item_forms};
1254 my $item_types = $$args{item_types};
1255 my $item_lang = $$args{item_lang};
1257 my $e = new_editor();
1261 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1262 return $e->event unless @$map;
1263 $mrec = $$map[0]->metarecord;
1266 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1267 return $e->event unless @$recs;
1269 my @recs = map { $_->source } @$recs;
1270 my $search = { record => \@recs };
1271 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1272 $search->{item_type} = $item_types if $item_types and @$item_types;
1273 $search->{item_lang} = $item_lang if $item_lang;
1275 my $desc = $e->search_metabib_record_descriptor($search);
1277 return { metarecord => $mrec, descriptors => $desc };
1283 __PACKAGE__->register_method(
1284 method => 'copies_created_on',
1288 sub copies_created_on {
1289 my( $self, $conn, $auth, $org, $date ) = @_;
1290 my $e = new_editor(authtoken=>$auth);
1291 return $e->event unless $e->checkauth;
1295 __PACKAGE__->register_method(
1296 method => 'fetch_age_protect',
1297 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1300 sub fetch_age_protect {
1301 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1305 __PACKAGE__->register_method(
1306 method => 'copies_by_cn_label',
1307 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1310 __PACKAGE__->register_method(
1311 method => 'copies_by_cn_label',
1312 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1315 sub copies_by_cn_label {
1316 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1317 my $e = new_editor();
1318 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1319 return [] unless @$cns;
1321 # show all non-deleted copies in the staff client ...
1322 if ($self->api_name =~ /staff$/o) {
1323 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1326 # ... otherwise, grab the copies ...
1327 my $copies = $e->search_asset_copy(
1328 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1329 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1333 # ... and test for location and status visibility
1334 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];