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 };
361 __PACKAGE__->register_method(
362 method => "biblio_copy_to_mods",
363 api_name => "open-ils.search.biblio.copy.mods.retrieve",
366 # takes a copy object and returns it fleshed mods object
367 sub biblio_copy_to_mods {
368 my( $self, $client, $copy ) = @_;
370 my $volume = $U->cstorereq(
371 "open-ils.cstore.direct.asset.call_number.retrieve",
372 $copy->call_number() );
374 my $mods = _records_to_mods($volume->record());
375 $mods = shift @$mods;
376 $volume->copies([$copy]);
377 push @{$mods->call_numbers()}, $volume;
383 # ----------------------------------------------------------------------------
384 # These are the main OPAC search methods
385 # ----------------------------------------------------------------------------
387 __PACKAGE__->register_method(
388 method => 'the_quest_for_knowledge',
389 api_name => 'open-ils.search.biblio.multiclass',
391 Performs a multi class bilbli or metabib search
392 @param searchhash A search object layed out like so:
393 searches : { "$class" : "$value", ...}
394 org_unit : The org id to focus the search at
395 depth : The org depth
396 limit : The search limit
397 offset : The search offset
398 format : The MARC format
399 sort : What field to sort the results on [ author | title | pubdate ]
400 sort_dir : What direction do we sort? [ asc | desc ]
401 @return An object of the form
402 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
406 __PACKAGE__->register_method(
407 method => 'the_quest_for_knowledge',
408 api_name => 'open-ils.search.biblio.multiclass.staff',
409 signature => q/@see open-ils.search.biblio.multiclass/);
410 __PACKAGE__->register_method(
411 method => 'the_quest_for_knowledge',
412 api_name => 'open-ils.search.metabib.multiclass',
413 signature => q/@see open-ils.search.biblio.multiclass/);
414 __PACKAGE__->register_method(
415 method => 'the_quest_for_knowledge',
416 api_name => 'open-ils.search.metabib.multiclass.staff',
417 signature => q/@see open-ils.search.biblio.multiclass/);
421 sub the_quest_for_knowledge {
422 my( $self, $conn, $searchhash, $docache ) = @_;
424 return { count => 0 } unless $searchhash and
425 ref $searchhash->{searches} eq 'HASH';
427 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
431 if($self->api_name =~ /metabib/) {
433 $method =~ s/biblio/metabib/o;
437 my $offset = $searchhash->{offset} || 0;
438 my $limit = $searchhash->{limit} || 10;
439 my $end = $offset + $limit - 1;
441 # do some simple sanity checking
442 if(!$searchhash->{searches} or
444 !$searchhash->{searches}->{title} and
445 !$searchhash->{searches}->{author} and
446 !$searchhash->{searches}->{subject} and
447 !$searchhash->{searches}->{series} and
448 !$searchhash->{searches}->{keyword} ) ) {
449 return { count => 0 };
454 $searchhash->{offset} = 0;
455 $searchhash->{limit} = $maxlimit;
457 return { count => 0 } if $offset > $maxlimit;
460 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
461 my $s = JSON->perl2JSON(\@search);
462 my $ckey = $pfx . md5_hex($method . $s);
464 $logger->info("bib search for: $s");
466 $searchhash->{limit} -= $offset;
469 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
473 $method .= ".staff" if($self->api_name =~ /staff/);
474 $method .= ".atomic";
476 for (keys %$searchhash) {
477 delete $$searchhash{$_}
478 unless defined $$searchhash{$_};
481 $result = $U->storagereq( $method, %$searchhash );
487 return {count => 0} unless ($result && $$result[0]);
489 #for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
492 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
497 # If we didn't get this data from the cache, put it into the cache
498 # then return the correct offset of records
499 $logger->debug("putting search cache $ckey\n");
500 put_cache($ckey, $count, \@recs);
503 for ($offset..$end) {
504 last unless $recs[$_];
509 #$logger->debug("cache done .. returning $offset..$end : " . JSON->perl2JSON(\@recs));
512 return { ids => \@recs, count => $count };
523 my $end = $offset + $limit - 1;
525 $logger->debug("searching cache for $key : $start..$end\n");
527 return undef unless $cache;
528 my $data = $cache->get_cache($key);
530 return undef unless $data;
532 my $count = $data->[0];
535 return undef unless $offset < $count;
539 for( my $i = $offset; $i <= $end; $i++ ) {
540 last unless my $d = $$data[$i];
544 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
551 my( $key, $count, $data ) = @_;
552 return undef unless $cache;
553 $logger->debug("search_cache putting ".
554 scalar(@$data)." items at key $key with timeout $cache_timeout");
555 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
563 __PACKAGE__->register_method(
564 method => "biblio_mrid_to_modsbatch_batch",
565 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
567 sub biblio_mrid_to_modsbatch_batch {
568 my( $self, $client, $mrids) = @_;
569 warn "Performing mrid_to_modsbatch_batch...";
571 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
572 for my $id (@$mrids) {
573 next unless defined $id;
574 my ($m) = $method->run($id);
581 __PACKAGE__->register_method(
582 method => "biblio_mrid_to_modsbatch",
583 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
584 notes => <<" NOTES");
585 Returns the mvr associated with a given metarecod. If none exists,
589 __PACKAGE__->register_method(
590 method => "biblio_mrid_to_modsbatch",
591 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
592 notes => <<" NOTES");
593 Returns the mvr associated with a given metarecod. If none exists,
597 sub biblio_mrid_to_modsbatch {
598 my( $self, $client, $mrid, $args) = @_;
600 warn "Grabbing mvr for $mrid\n";
602 my ($mr, $evt) = _grab_metarecord($mrid);
603 return $evt unless $mr;
605 my $mvr = $self->biblio_mrid_check_mvr($client, $mr);
606 $mvr = $self->biblio_mrid_make_modsbatch( $client, $mr ) unless $mvr;
608 return $mvr unless ref($args);
610 # Here we find the lead record appropriate for the given filters
611 # and use that for the title and author of the metarecord
612 my $format = $$args{format};
613 my $org = $$args{org};
614 my $depth = $$args{depth};
616 return $mvr unless $format or $org or $depth;
618 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
619 $method = "$method.staff" if $self->api_name =~ /staff/o;
621 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
623 if( my $mods = $U->record_to_mvr($rec) ) {
625 $mvr->title($mods->title);
626 $mvr->title($mods->author);
627 $logger->debug("mods_slim updating title and ".
628 "author in mvr with ".$mods->title." : ".$mods->author);
634 # converts a metarecord to an mvr
637 my $perl = JSON->JSON2perl($mr->mods());
638 return Fieldmapper::metabib::virtual_record->new($perl);
641 # checks to see if a metarecord has mods, if so returns true;
643 __PACKAGE__->register_method(
644 method => "biblio_mrid_check_mvr",
645 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
646 notes => <<" NOTES");
647 Takes a metarecord ID or a metarecord object and returns true
648 if the metarecord already has an mvr associated with it.
651 sub biblio_mrid_check_mvr {
652 my( $self, $client, $mrid ) = @_;
656 if(ref($mrid)) { $mr = $mrid; }
657 else { ($mr, $evt) = _grab_metarecord($mrid); }
660 warn "Checking mvr for mr " . $mr->id . "\n";
662 return _mr_to_mvr($mr) if $mr->mods();
666 sub _grab_metarecord {
668 #my $e = OpenILS::Utils::Editor->new;
669 my $e = new_editor();
670 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
675 __PACKAGE__->register_method(
676 method => "biblio_mrid_make_modsbatch",
677 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
678 notes => <<" NOTES");
679 Takes either a metarecord ID or a metarecord object.
680 Forces the creations of an mvr for the given metarecord.
681 The created mvr is returned.
684 sub biblio_mrid_make_modsbatch {
685 my( $self, $client, $mrid ) = @_;
687 #my $e = OpenILS::Utils::Editor->new;
688 my $e = new_editor();
695 $mr = $e->retrieve_metabib_metarecord($mrid)
699 my $masterid = $mr->master_record;
700 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
702 my $ids = $U->storagereq(
703 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
704 return undef unless @$ids;
706 my $master = $e->retrieve_biblio_record_entry($masterid)
709 # start the mods batch
710 my $u = OpenILS::Utils::ModsParser->new();
711 $u->start_mods_batch( $master->marc );
713 # grab all of the sub-records and shove them into the batch
714 my @ids = grep { $_ ne $masterid } @$ids;
715 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
720 my $r = $e->retrieve_biblio_record_entry($i);
721 push( @$subrecs, $r ) if $r;
726 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
727 $u->push_mods_batch( $_->marc ) if $_->marc;
731 # finish up and send to the client
732 my $mods = $u->finish_mods_batch();
733 $mods->doc_id($mrid);
734 $client->respond_complete($mods);
737 # now update the mods string in the db
738 my $string = JSON->perl2JSON($mods->decast);
741 #$e = OpenILS::Utils::Editor->new(xact => 1);
742 $e = new_editor(xact => 1);
743 $e->update_metabib_metarecord($mr)
744 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
753 # converts a mr id into a list of record ids
755 __PACKAGE__->register_method(
756 method => "biblio_mrid_to_record_ids",
757 api_name => "open-ils.search.biblio.metarecord_to_records",
760 __PACKAGE__->register_method(
761 method => "biblio_mrid_to_record_ids",
762 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
765 sub biblio_mrid_to_record_ids {
766 my( $self, $client, $mrid, $args ) = @_;
768 my $format = $$args{format};
769 my $org = $$args{org};
770 my $depth = $$args{depth};
772 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
773 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
774 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
776 return { count => scalar(@$recs), ids => $recs };
780 __PACKAGE__->register_method(
781 method => "biblio_record_to_marc_html",
782 api_name => "open-ils.search.biblio.record.html" );
784 my $parser = XML::LibXML->new();
785 my $xslt = XML::LibXSLT->new();
788 my $settings_client = OpenSRF::Utils::SettingsClient->new();
789 sub biblio_record_to_marc_html {
790 my( $self, $client, $recordid ) = @_;
793 my $dir = $settings_client->config_value( "dirs", "xsl" );
794 my $xsl = $settings_client->config_value(
795 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
797 $xsl = $parser->parse_file("$dir/$xsl");
798 $marc_sheet = $xslt->parse_stylesheet( $xsl );
802 my $record = $apputils->simple_scalar_request(
804 "open-ils.cstore.direct.biblio.record_entry.retrieve",
807 my $xmldoc = $parser->parse_string($record->marc);
808 my $html = $marc_sheet->transform($xmldoc);
809 $html = $html->toString();
816 __PACKAGE__->register_method(
817 method => "retrieve_all_copy_locations",
818 api_name => "open-ils.search.config.copy_location.retrieve.all" );
820 my $shelving_locations;
821 sub retrieve_all_copy_locations {
822 my( $self, $client ) = @_;
823 if(!$shelving_locations) {
824 $shelving_locations = $apputils->simple_scalar_request(
826 "open-ils.cstore.direct.asset.copy_location.search.atomic",
827 { id => { "!=" => undef } }
830 return $shelving_locations;
836 __PACKAGE__->register_method(
837 method => "retrieve_all_copy_statuses",
838 api_name => "open-ils.search.config.copy_status.retrieve.all" );
841 sub retrieve_all_copy_statuses {
842 my( $self, $client ) = @_;
843 return $copy_statuses if $copy_statuses;
844 return $copy_statuses =
845 new_editor()->retrieve_all_config_copy_status();
849 __PACKAGE__->register_method(
850 method => "copy_counts_per_org",
851 api_name => "open-ils.search.biblio.copy_counts.retrieve");
853 __PACKAGE__->register_method(
854 method => "copy_counts_per_org",
855 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
857 sub copy_counts_per_org {
858 my( $self, $client, $record_id ) = @_;
860 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
862 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
863 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
865 my $counts = $apputils->simple_scalar_request(
866 "open-ils.storage", $method, $record_id );
868 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
873 __PACKAGE__->register_method(
874 method => "copy_count_summary",
875 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
876 notes => <<" NOTES");
877 returns an array of these:
878 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
879 where statusx is a copy status name. the statuses are sorted
883 sub copy_count_summary {
884 my( $self, $client, $rid, $org, $depth ) = @_;
887 return $U->storagereq(
888 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
894 __PACKAGE__->register_method(
895 method => "multiclass_search",
896 api_name => "open-ils.search.biblio.multiclass",
897 notes => <<" NOTES");
898 Performs a multiclass search
899 PARAMS( searchBlob, org_unit, format, limit )
900 where searchBlob is defined like this:
902 "title" : { "term" : "water" },
903 "author" : { "term" : "smith" },
908 __PACKAGE__->register_method(
909 method => "multiclass_search",
910 api_name => "open-ils.search.biblio.multiclass.staff",
911 notes => "see open-ils.search.biblio.multiclass" );
913 sub multiclass_search {
914 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
916 $logger->debug("Performing multiclass search with org => $orgid, " .
917 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
919 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
920 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
923 my $records = $apputils->simplereq(
924 'open-ils.storage', $meth,
925 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
930 if( ref($records) and $records->[0] and
931 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
933 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
935 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
936 return { ids => $recs, count => $count };
942 __PACKAGE__->register_method(
943 method => "multiclass_search",
944 api_name => "open-ils.search.biblio.multiclass",
946 Performs a multiclass search
947 @param args A names hash of arguments:
948 org_unit : The org to focus the search on
949 depth : The search depth
952 offset : Search offset
953 searches : A named hash of searches which has the following format:
955 "title" : { "term" : "water" },
956 "author" : { "term" : "smith" },
959 @return { ids : <array of ids>, count : hitcount }
963 __PACKAGE__->register_method(
964 method => "multiclass_search",
965 api_name => "open-ils.search.biblio.multiclass.staff",
966 notes => q/@see open-ils.search.biblio.multiclass/ );
968 sub multiclass_search {
969 my( $self, $client, $args ) = @_;
971 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
972 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
973 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
975 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
980 if( ref($records) and $records->[0] and
981 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
983 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
985 return { ids => $recs, count => $count };
992 __PACKAGE__->register_method(
993 method => "marc_search",
994 api_name => "open-ils.search.biblio.marc.staff");
996 __PACKAGE__->register_method(
997 method => "marc_search",
998 api_name => "open-ils.search.biblio.marc",
999 notes => <<" NOTES");
1001 open-ils.storage.biblio.full_rec.multi_search.atomic
1002 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1003 "limit":5,"sort":"author","item_type":"g"}
1007 my( $self, $conn, $args, $limit, $offset ) = @_;
1009 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1010 $method .= ".staff" if $self->api_name =~ /staff/;
1011 $method .= ".atomic";
1017 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1018 my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
1020 my $recs = search_cache($ckey, $offset, $limit);
1023 $recs = $U->storagereq($method, %$args) || [];
1025 put_cache($ckey, scalar(@$recs), $recs);
1026 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1033 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1034 my @recs = map { $_->[0] } @$recs;
1036 return { ids => \@recs, count => $count };
1040 __PACKAGE__->register_method(
1041 method => "biblio_search_isbn",
1042 api_name => "open-ils.search.biblio.isbn",
1045 sub biblio_search_isbn {
1046 my( $self, $client, $isbn ) = @_;
1047 $logger->debug("Searching ISBN $isbn");
1048 my $e = new_editor();
1049 my $recs = $U->storagereq(
1050 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1051 return { ids => $recs, count => scalar(@$recs) };
1055 __PACKAGE__->register_method(
1056 method => "biblio_search_issn",
1057 api_name => "open-ils.search.biblio.issn",
1060 sub biblio_search_issn {
1061 my( $self, $client, $issn ) = @_;
1062 $logger->debug("Searching ISSN $issn");
1063 my $e = new_editor();
1064 my $recs = $U->storagereq(
1065 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1066 return { ids => $recs, count => scalar(@$recs) };
1072 __PACKAGE__->register_method(
1073 method => "fetch_mods_by_copy",
1074 api_name => "open-ils.search.biblio.mods_from_copy",
1077 sub fetch_mods_by_copy {
1078 my( $self, $client, $copyid ) = @_;
1079 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1080 return $evt if $evt;
1081 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1082 return $apputils->record_to_mvr($record);
1087 # -------------------------------------------------------------------------------------
1089 __PACKAGE__->register_method(
1090 method => "cn_browse",
1091 api_name => "open-ils.search.callnumber.browse.target",
1092 notes => "Starts a callnumber browse"
1095 __PACKAGE__->register_method(
1096 method => "cn_browse",
1097 api_name => "open-ils.search.callnumber.browse.page_up",
1098 notes => "Returns the previous page of callnumbers",
1101 __PACKAGE__->register_method(
1102 method => "cn_browse",
1103 api_name => "open-ils.search.callnumber.browse.page_down",
1104 notes => "Returns the next page of callnumbers",
1108 # RETURNS array of arrays like so: label, owning_lib, record, id
1110 my( $self, $client, @params ) = @_;
1113 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1114 if( $self->api_name =~ /target/ );
1115 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1116 if( $self->api_name =~ /page_up/ );
1117 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1118 if( $self->api_name =~ /page_down/ );
1120 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1122 # -------------------------------------------------------------------------------------
1124 __PACKAGE__->register_method(
1125 method => "fetch_cn",
1126 api_name => "open-ils.search.callnumber.retrieve",
1127 notes => "retrieves a callnumber based on ID",
1131 my( $self, $client, $id ) = @_;
1132 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1133 return $evt if $evt;
1137 __PACKAGE__->register_method (
1138 method => "fetch_copy_by_cn",
1139 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1141 Returns an array of copy id's by callnumber id
1142 @param cnid The callnumber id
1143 @return An array of copy ids
1147 sub fetch_copy_by_cn {
1148 my( $self, $conn, $cnid ) = @_;
1149 return $U->cstorereq(
1150 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1151 { call_number => $cnid, deleted => 'f' } );
1154 __PACKAGE__->register_method (
1155 method => 'fetch_cn_by_info',
1156 api_name => 'open-ils.search.call_number.retrieve_by_info',
1158 @param label The callnumber label
1159 @param record The record the cn is attached to
1160 @param org The owning library of the cn
1161 @return The callnumber object
1166 sub fetch_cn_by_info {
1167 my( $self, $conn, $label, $record, $org ) = @_;
1168 return $U->cstorereq(
1169 'open-ils.cstore.direct.asset.call_number.search',
1170 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1177 __PACKAGE__->register_method (
1178 method => 'bib_extras',
1179 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1180 __PACKAGE__->register_method (
1181 method => 'bib_extras',
1182 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1183 __PACKAGE__->register_method (
1184 method => 'bib_extras',
1185 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1186 __PACKAGE__->register_method (
1187 method => 'bib_extras',
1188 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1193 my $e = new_editor();
1195 return $e->retrieve_all_config_lit_form_map()
1196 if( $self->api_name =~ /lit_form/ );
1198 return $e->retrieve_all_config_item_form_map()
1199 if( $self->api_name =~ /item_form_map/ );
1201 return $e->retrieve_all_config_item_type_map()
1202 if( $self->api_name =~ /item_type_map/ );
1204 return $e->retrieve_all_config_audience_map()
1205 if( $self->api_name =~ /audience_map/ );
1212 __PACKAGE__->register_method(
1213 method => 'fetch_slim_record',
1214 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1216 Returns a biblio.record_entry without the attached marcxml
1220 sub fetch_slim_record {
1221 my( $self, $conn, $ids ) = @_;
1223 #my $editor = OpenILS::Utils::Editor->new;
1224 my $editor = new_editor();
1227 return $editor->event unless
1228 my $r = $editor->retrieve_biblio_record_entry($_);
1237 __PACKAGE__->register_method(
1238 method => 'rec_to_mr_rec_descriptors',
1239 api_name => 'open-ils.search.metabib.record_to_descriptors',
1241 specialized method...
1242 Given a biblio record id or a metarecord id,
1243 this returns a list of metabib.record_descriptor
1244 objects that live within the same metarecord
1245 @param args Object of args including:
1249 sub rec_to_mr_rec_descriptors {
1250 my( $self, $conn, $args ) = @_;
1252 my $rec = $$args{record};
1253 my $mrec = $$args{metarecord};
1254 my $item_forms = $$args{item_forms};
1255 my $item_types = $$args{item_types};
1256 my $item_lang = $$args{item_lang};
1258 my $e = new_editor();
1262 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1263 return $e->event unless @$map;
1264 $mrec = $$map[0]->metarecord;
1267 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1268 return $e->event unless @$recs;
1270 my @recs = map { $_->source } @$recs;
1271 my $search = { record => \@recs };
1272 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1273 $search->{item_type} = $item_types if $item_types and @$item_types;
1274 $search->{item_lang} = $item_lang if $item_lang;
1276 my $desc = $e->search_metabib_record_descriptor($search);
1278 return { metarecord => $mrec, descriptors => $desc };
1284 __PACKAGE__->register_method(
1285 method => 'copies_created_on',
1289 sub copies_created_on {
1290 my( $self, $conn, $auth, $org, $date ) = @_;
1291 my $e = new_editor(authtoken=>$auth);
1292 return $e->event unless $e->checkauth;
1296 __PACKAGE__->register_method(
1297 method => 'fetch_age_protect',
1298 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1301 sub fetch_age_protect {
1302 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1306 __PACKAGE__->register_method(
1307 method => 'copies_by_cn_label',
1308 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1311 __PACKAGE__->register_method(
1312 method => 'copies_by_cn_label',
1313 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1316 sub copies_by_cn_label {
1317 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1318 my $e = new_editor();
1319 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1320 return [] unless @$cns;
1322 # show all non-deleted copies in the staff client ...
1323 if ($self->api_name =~ /staff$/o) {
1324 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1327 # ... otherwise, grab the copies ...
1328 my $copies = $e->search_asset_copy(
1329 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1330 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1334 # ... and test for location and status visibility
1335 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];