1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
6 use OpenSRF::Utils::JSON;
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;
14 use OpenSRF::Utils::Logger qw/:logger/;
17 use OpenSRF::Utils::JSON;
19 use Time::HiRes qw(time);
20 use OpenSRF::EX qw(:try);
21 use Digest::MD5 qw(md5_hex);
27 $Data::Dumper::Indent = 0;
29 use OpenILS::Const qw/:const/;
31 use OpenILS::Application::AppUtils;
32 my $apputils = "OpenILS::Application::AppUtils";
35 my $pfx = "open-ils.search_";
43 $cache = OpenSRF::Utils::Cache->new('global');
44 my $sclient = OpenSRF::Utils::SettingsClient->new();
45 $cache_timeout = $sclient->config_value(
46 "apps", "open-ils.search", "app_settings", "cache_timeout" ) || 300;
48 $superpage_size = $sclient->config_value(
49 "apps", "open-ils.search", "app_settings", "superpage_size" ) || 500;
51 $max_superpages = $sclient->config_value(
52 "apps", "open-ils.search", "app_settings", "max_superpages" ) || 20;
54 $logger->info("Search cache timeout is $cache_timeout, ".
55 " superpage_size is $superpage_size, max_superpages is $max_superpages");
60 # ---------------------------------------------------------------------------
61 # takes a list of record id's and turns the docs into friendly
62 # mods structures. Creates one MODS structure for each doc id.
63 # ---------------------------------------------------------------------------
64 sub _records_to_mods {
70 my $session = OpenSRF::AppSession->create("open-ils.cstore");
71 my $request = $session->request(
72 "open-ils.cstore.direct.biblio.record_entry.search", { id => \@ids } );
74 while( my $resp = $request->recv ) {
75 my $content = $resp->content;
76 next if $content->id == OILS_PRECAT_RECORD;
77 my $u = OpenILS::Utils::ModsParser->new();
78 $u->start_mods_batch( $content->marc );
79 my $mods = $u->finish_mods_batch();
80 $mods->doc_id($content->id());
81 $mods->tcn($content->tcn_value);
85 $session->disconnect();
89 __PACKAGE__->register_method(
90 method => "record_id_to_mods",
91 api_name => "open-ils.search.biblio.record.mods.retrieve",
93 note => "Provide ID, we provide the mods"
96 # converts a record into a mods object with copy counts attached
97 sub record_id_to_mods {
99 my( $self, $client, $org_id, $id ) = @_;
101 my $mods_list = _records_to_mods( $id );
102 my $mods_obj = $mods_list->[0];
103 my $cmethod = $self->method_lookup(
104 "open-ils.search.biblio.record.copy_count");
105 my ($count) = $cmethod->run($org_id, $id);
106 $mods_obj->copy_count($count);
113 __PACKAGE__->register_method(
114 method => "record_id_to_mods_slim",
116 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
118 note => "Provide ID, we provide the mods"
121 # converts a record into a mods object with NO copy counts attached
122 sub record_id_to_mods_slim {
123 my( $self, $client, $id ) = @_;
124 return undef unless defined $id;
126 if(ref($id) and ref($id) == 'ARRAY') {
127 return _records_to_mods( @$id );
129 my $mods_list = _records_to_mods( $id );
130 my $mods_obj = $mods_list->[0];
131 return OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND') unless $mods_obj;
137 __PACKAGE__->register_method(
138 method => "record_id_to_mods_slim_batch",
139 api_name => "open-ils.search.biblio.record.mods_slim.batch.retrieve",
142 sub record_id_to_mods_slim_batch {
143 my($self, $conn, $id_list) = @_;
144 $conn->respond(_records_to_mods($_)->[0]) for @$id_list;
149 # Returns the number of copies attached to a record based on org location
150 __PACKAGE__->register_method(
151 method => "record_id_to_copy_count",
152 api_name => "open-ils.search.biblio.record.copy_count",
155 __PACKAGE__->register_method(
156 method => "record_id_to_copy_count",
158 api_name => "open-ils.search.biblio.record.copy_count.staff",
161 __PACKAGE__->register_method(
162 method => "record_id_to_copy_count",
163 api_name => "open-ils.search.biblio.metarecord.copy_count",
166 __PACKAGE__->register_method(
167 method => "record_id_to_copy_count",
168 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
170 sub record_id_to_copy_count {
171 my( $self, $client, $org_id, $record_id, $format ) = @_;
173 return [] unless $record_id;
174 $format = undef if (!$format or $format eq 'all');
176 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
179 if($self->api_name =~ /metarecord/) {
180 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
184 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
186 my $count = $U->storagereq( $method,
187 org_unit => $org_id, $key => $record_id, format => $format );
189 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
195 __PACKAGE__->register_method(
196 method => "biblio_search_tcn",
197 api_name => "open-ils.search.biblio.tcn",
199 note => "Retrieve a record by TCN",
202 sub biblio_search_tcn {
204 my( $self, $client, $tcn, $include_deleted ) = @_;
206 $tcn =~ s/^\s+|\s+$//og;
208 my $e = new_editor();
209 my $search = {tcn_value => $tcn};
210 $search->{deleted} = 'f' unless $include_deleted;
211 my $recs = $e->search_biblio_record_entry( $search, {idlist =>1} );
213 return { count => scalar(@$recs), ids => $recs };
217 # --------------------------------------------------------------------------------
219 __PACKAGE__->register_method(
220 method => "biblio_barcode_to_copy",
221 api_name => "open-ils.search.asset.copy.find_by_barcode",);
222 sub biblio_barcode_to_copy {
223 my( $self, $client, $barcode ) = @_;
224 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
229 __PACKAGE__->register_method(
230 method => "biblio_id_to_copy",
231 api_name => "open-ils.search.asset.copy.batch.retrieve",);
232 sub biblio_id_to_copy {
233 my( $self, $client, $ids ) = @_;
234 $logger->info("Fetching copies @$ids");
235 return $U->cstorereq(
236 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
240 __PACKAGE__->register_method(
241 method => "biblio_id_to_uris",
242 api_name=> "open-ils.search.asset.uri.retrieve_by_bib",
246 @param BibID Which bib record contains the URIs
247 @param OrgID Where to look for URIs
248 @param OrgDepth Range adjustment for OrgID
249 @return A stream or list of 'auri' objects
253 sub biblio_id_to_uris {
254 my( $self, $client, $bib, $org, $depth ) = @_;
255 die "Org ID required" unless defined($org);
256 die "Bib ID required" unless defined($bib);
259 push @params, $depth if (defined $depth);
261 my $ids = $U->cstorereq( "open-ils.cstore.json_query.atomic",
262 { select => { auri => [ 'id' ] },
266 field => 'call_number',
272 filter => { active => 't' }
283 select => { aou => [ { column => 'id', transform => 'actor.org_unit_descendants', params => \@params, result_field => 'id' } ] },
285 where => { id => $org },
295 my $uris = $U->cstorereq(
296 "open-ils.cstore.direct.asset.uri.search.atomic",
297 { id => [ map { (values %$_) } @$ids ] }
300 $client->respond($_) for (@$uris);
306 __PACKAGE__->register_method(
307 method => "copy_retrieve",
308 api_name => "open-ils.search.asset.copy.retrieve",);
310 my( $self, $client, $cid ) = @_;
311 my( $copy, $evt ) = $U->fetch_copy($cid);
316 __PACKAGE__->register_method(
317 method => "volume_retrieve",
318 api_name => "open-ils.search.asset.call_number.retrieve");
319 sub volume_retrieve {
320 my( $self, $client, $vid ) = @_;
321 my $e = new_editor();
322 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
326 __PACKAGE__->register_method(
327 method => "fleshed_copy_retrieve_batch",
329 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
331 sub fleshed_copy_retrieve_batch {
332 my( $self, $client, $ids ) = @_;
333 $logger->info("Fetching fleshed copies @$ids");
334 return $U->cstorereq(
335 "open-ils.cstore.direct.asset.copy.search.atomic",
338 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
343 __PACKAGE__->register_method(
344 method => "fleshed_copy_retrieve",
345 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
347 sub fleshed_copy_retrieve {
348 my( $self, $client, $id ) = @_;
349 my( $c, $e) = $U->fetch_fleshed_copy($id);
356 __PACKAGE__->register_method(
357 method => 'fleshed_by_barcode',
358 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",
361 sub fleshed_by_barcode {
362 my( $self, $conn, $barcode ) = @_;
363 my $e = new_editor();
364 my $copyid = $e->search_asset_copy(
365 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
367 return fleshed_copy_retrieve2( $self, $conn, $copyid);
371 __PACKAGE__->register_method(
372 method => "fleshed_copy_retrieve2",
373 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",
377 sub fleshed_copy_retrieve2 {
378 my( $self, $client, $id ) = @_;
379 my $e = new_editor();
380 my $copy = $e->retrieve_asset_copy(
386 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
387 ascecm => [ qw/ stat_cat stat_cat_entry / ],
391 ) or return $e->event;
393 # For backwards compatibility
394 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
396 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
398 $e->search_action_circulation(
400 { target_copy => $copy->id },
402 order_by => { circ => 'xact_start desc' },
414 __PACKAGE__->register_method(
415 method => 'flesh_copy_custom',
416 api_name => 'open-ils.search.asset.copy.fleshed.custom',
420 sub flesh_copy_custom {
421 my( $self, $conn, $copyid, $fields ) = @_;
422 my $e = new_editor();
423 my $copy = $e->retrieve_asset_copy(
433 ) or return $e->event;
442 __PACKAGE__->register_method(
443 method => "biblio_barcode_to_title",
444 api_name => "open-ils.search.biblio.find_by_barcode",
447 sub biblio_barcode_to_title {
448 my( $self, $client, $barcode ) = @_;
450 my $title = $apputils->simple_scalar_request(
452 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
454 return { ids => [ $title->id ], count => 1 } if $title;
455 return { count => 0 };
458 __PACKAGE__->register_method(
459 method => 'title_id_by_item_barcode',
460 api_name => 'open-ils.search.bib_id.by_barcode',
464 sub title_id_by_item_barcode {
465 my( $self, $conn, $barcode ) = @_;
466 my $e = new_editor();
467 my $copies = $e->search_asset_copy(
469 { deleted => 'f', barcode => $barcode },
473 acp => [ 'call_number' ],
480 return $e->event unless @$copies;
481 return $$copies[0]->call_number->record->id;
485 __PACKAGE__->register_method(
486 method => "biblio_copy_to_mods",
487 api_name => "open-ils.search.biblio.copy.mods.retrieve",
490 # takes a copy object and returns it fleshed mods object
491 sub biblio_copy_to_mods {
492 my( $self, $client, $copy ) = @_;
494 my $volume = $U->cstorereq(
495 "open-ils.cstore.direct.asset.call_number.retrieve",
496 $copy->call_number() );
498 my $mods = _records_to_mods($volume->record());
499 $mods = shift @$mods;
500 $volume->copies([$copy]);
501 push @{$mods->call_numbers()}, $volume;
507 =head3 open-ils.search.biblio.multiclass.query (arghash, query, docache)
509 For arghash and docache, see B<open-ils.search.biblio.multiclass>.
511 The query argument is a string, but built like a hash with key: value pairs.
512 Recognized search keys include:
514 keyword (kw) - search keyword(s) *
515 author (au) - search author(s) *
516 name (au) - same as author *
517 title (ti) - search title *
518 subject (su) - search subject *
519 series (se) - search series *
520 lang - limit by language (specifiy multiple langs with lang:l1 lang:l2 ...)
521 site - search at specified org unit, corresponds to actor.org_unit.shortname
522 sort - sort type (title, author, pubdate)
523 dir - sort direction (asc, desc)
524 available - if set to anything other than "false" or "0", limits to available items
526 * Searching keyword, author, title, subject, and series supports additional search
527 subclasses, specified with a "|". For example, C<title|proper:gone with the wind>.
529 For more, see B<config.metabib_field>.
533 __PACKAGE__->register_method(
534 api_name => 'open-ils.search.biblio.multiclass.query',
535 method => 'multiclass_query',
537 desc => 'Perform a search query',
539 {name => 'arghash', desc => 'Arg hash (see open-ils.search.biblio.multiclass)', type => 'object'},
540 {name => 'query', desc => 'Raw human-readable query (see perldoc '. __PACKAGE__ .')', type => 'string'},
541 {name => 'docache', desc => 'Flag for caching (see open-ils.search.biblio.multiclass)', type => 'object'},
544 desc => 'Search results from query, like: { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }',
545 type => 'object', # TODO: update as miker's new elements are included
549 __PACKAGE__->register_method(
550 api_name => 'open-ils.search.biblio.multiclass.query.staff',
551 method => 'multiclass_query',
552 signature => '@see open-ils.search.biblio.multiclass.query'
554 __PACKAGE__->register_method(
555 api_name => 'open-ils.search.metabib.multiclass.query',
556 method => 'multiclass_query',
557 signature => '@see open-ils.search.biblio.multiclass.query'
559 __PACKAGE__->register_method(
560 api_name => 'open-ils.search.metabib.multiclass.query.staff',
561 method => 'multiclass_query',
562 signature => '@see open-ils.search.biblio.multiclass.query'
565 sub multiclass_query {
566 my($self, $conn, $arghash, $query, $docache) = @_;
568 $logger->debug("initial search query => $query");
569 my $orig_query = $query;
573 $query =~ s/^\s+//go;
575 # convert convenience classes (e.g. kw for keyword) to the full class name
576 $query =~ s/kw(:|\|)/keyword$1/go;
577 $query =~ s/ti(:|\|)/title$1/go;
578 $query =~ s/au(:|\|)/author$1/go;
579 $query =~ s/su(:|\|)/subject$1/go;
580 $query =~ s/se(:|\|)/series$1/go;
581 $query =~ s/name(:|\|)/author$1/og;
583 $logger->debug("cleansed query string => $query");
586 my $simple_class_re = qr/((?:\w+(?:\|\w+)?):[^:]+?)$/;
587 my $class_list_re = qr/(?:keyword|title|author|subject|series)/;
588 my $modifier_list_re = qr/(?:site|dir|sort|lang|available)/;
591 while ($query =~ s/$simple_class_re//so) {
594 my $where = index($qpart,':');
595 my $type = substr($qpart, 0, $where++);
596 my $value = substr($qpart, $where);
598 if ($type !~ /^(?:$class_list_re|$modifier_list_re)/o) {
599 $tmp_value = "$qpart $tmp_value";
603 if ($type =~ /$class_list_re/o ) {
604 $value .= $tmp_value;
608 next unless $type and $value;
610 $value =~ s/^\s*//og;
611 $value =~ s/\s*$//og;
612 $type = 'sort_dir' if $type eq 'dir';
614 if($type eq 'site') {
615 # 'site' is the org shortname. when using this, we also want
616 # to search at the requested org's depth
617 my $e = new_editor();
618 if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
619 $arghash->{org_unit} = $org->id if $org;
620 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
622 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
625 } elsif($type eq 'available') {
627 $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
629 } elsif($type eq 'lang') {
630 # collect languages into an array of languages
631 $arghash->{language} = [] unless $arghash->{language};
632 push(@{$arghash->{language}}, $value);
634 } elsif($type =~ /^sort/o) {
635 # sort and sort_dir modifiers
636 $arghash->{$type} = $value;
639 # append the search term to the term under construction
640 $search->{$type} = {} unless $search->{$type};
641 $search->{$type}->{term} =
642 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
646 $query .= " $tmp_value";
647 $query =~ s/\s+/ /go;
648 $query =~ s/^\s+//go;
649 $query =~ s/\s+$//go;
651 my $type = $arghash->{default_class} || 'keyword';
652 $type = ($type eq '-') ? 'keyword' : $type;
653 $type = ($type !~ /^(title|author|keyword|subject|series)(?:\|\w+)?$/o) ? 'keyword' : $type;
656 # This is the front part of the string before any special tokens were
657 # parsed OR colon-separated strings that do not denote a class.
658 # Add this data to the default search class
659 $search->{$type} = {} unless $search->{$type};
660 $search->{$type}->{term} =
661 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $query" : $query;
663 my $real_search = $arghash->{searches} = { $type => { term => $orig_query } };
665 # capture the original limit because the search method alters the limit internally
666 my $ol = $arghash->{limit};
668 my $sclient = OpenSRF::Utils::SettingsClient->new;
670 (my $method = $self->api_name) =~ s/\.query//o;
672 $method =~ s/multiclass/multiclass.staged/
673 if $sclient->config_value(apps => 'open-ils.search',
674 app_settings => 'use_staged_search') =~ /true/i;
676 $arghash->{preferred_language} = $U->get_org_locale($arghash->{org_unit})
677 unless $arghash->{preferred_language};
679 $method = $self->method_lookup($method);
680 my ($data) = $method->run($arghash, $docache);
682 $arghash->{searches} = $search if (!$data->{complex_query});
684 $arghash->{limit} = $ol if $ol;
685 $data->{compiled_search} = $arghash;
686 $data->{query} = $orig_query;
688 $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
693 __PACKAGE__->register_method(
694 method => 'cat_search_z_style_wrapper',
695 api_name => 'open-ils.search.biblio.zstyle',
697 signature => q/@see open-ils.search.biblio.multiclass/);
699 __PACKAGE__->register_method(
700 method => 'cat_search_z_style_wrapper',
701 api_name => 'open-ils.search.biblio.zstyle.staff',
703 signature => q/@see open-ils.search.biblio.multiclass/);
705 sub cat_search_z_style_wrapper {
708 my $authtoken = shift;
711 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
713 my $ou = $cstore->request(
714 'open-ils.cstore.direct.actor.org_unit.search',
715 { parent_ou => undef }
718 my $result = { service => 'native-evergreen-catalog', records => [] };
719 my $searchhash = { limit => $$args{limit}, offset => $$args{offset}, org_unit => $ou->id };
721 $$searchhash{searches}{title}{term} = $$args{search}{title} if $$args{search}{title};
722 $$searchhash{searches}{author}{term} = $$args{search}{author} if $$args{search}{author};
723 $$searchhash{searches}{subject}{term} = $$args{search}{subject} if $$args{search}{subject};
724 $$searchhash{searches}{keyword}{term} = $$args{search}{keyword} if $$args{search}{keyword};
726 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{tcn} if $$args{search}{tcn};
727 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{isbn} if $$args{search}{isbn};
728 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{issn} if $$args{search}{issn};
729 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{publisher} if $$args{search}{publisher};
730 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{pubdate} if $$args{search}{pubdate};
731 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{item_type} if $$args{search}{item_type};
733 my $list = the_quest_for_knowledge( $self, $client, $searchhash );
735 if ($list->{count} > 0) {
736 $result->{count} = $list->{count};
738 my $records = $cstore->request(
739 'open-ils.cstore.direct.biblio.record_entry.search.atomic',
740 { id => [ map { ( $_->[0] ) } @{$list->{ids}} ] }
743 for my $rec ( @$records ) {
745 my $u = OpenILS::Utils::ModsParser->new();
746 $u->start_mods_batch( $rec->marc );
747 my $mods = $u->finish_mods_batch();
749 push @{ $result->{records} }, { mvr => $mods, marcxml => $rec->marc, bibid => $rec->id };
755 $cstore->disconnect();
759 # ----------------------------------------------------------------------------
760 # These are the main OPAC search methods
761 # ----------------------------------------------------------------------------
763 __PACKAGE__->register_method(
764 method => 'the_quest_for_knowledge',
765 api_name => 'open-ils.search.biblio.multiclass',
767 Performs a multi class biblio or metabib search
768 @param searchhash A search object layed out like so:
769 searches : { "$class" : "$value", ...}
770 org_unit : The org id to focus the search at
771 depth : The org depth
772 limit : The search limit
773 offset : The search offset
774 format : The MARC format
775 sort : What field to sort the results on [ author | title | pubdate ]
776 sort_dir : What direction do we sort? [ asc | desc ]
777 @return An object of the form
778 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
782 __PACKAGE__->register_method(
783 method => 'the_quest_for_knowledge',
784 api_name => 'open-ils.search.biblio.multiclass.staff',
785 signature => q/@see open-ils.search.biblio.multiclass/
787 __PACKAGE__->register_method(
788 method => 'the_quest_for_knowledge',
789 api_name => 'open-ils.search.metabib.multiclass',
790 signature => q/@see open-ils.search.biblio.multiclass/
792 __PACKAGE__->register_method(
793 method => 'the_quest_for_knowledge',
794 api_name => 'open-ils.search.metabib.multiclass.staff',
795 signature => q/@see open-ils.search.biblio.multiclass/
798 sub the_quest_for_knowledge {
799 my( $self, $conn, $searchhash, $docache ) = @_;
801 return { count => 0 } unless $searchhash and
802 ref $searchhash->{searches} eq 'HASH';
804 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
808 if($self->api_name =~ /metabib/) {
810 $method =~ s/biblio/metabib/o;
814 my $offset = $searchhash->{offset} || 0;
815 my $limit = $searchhash->{limit} || 10;
816 my $end = $offset + $limit - 1;
818 # do some simple sanity checking
819 if(!$searchhash->{searches} or
820 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
821 return { count => 0 };
826 $searchhash->{offset} = 0;
827 $searchhash->{limit} = $maxlimit;
829 return { count => 0 } if $offset > $maxlimit;
832 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
833 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
834 my $ckey = $pfx . md5_hex($method . $s);
836 $logger->info("bib search for: $s");
838 $searchhash->{limit} -= $offset;
842 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
846 $method .= ".staff" if($self->api_name =~ /staff/);
847 $method .= ".atomic";
849 for (keys %$searchhash) {
850 delete $$searchhash{$_}
851 unless defined $$searchhash{$_};
854 $result = $U->storagereq( $method, %$searchhash );
861 return {count => 0} unless ($result && $$result[0]);
865 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
868 # If we didn't get this data from the cache, put it into the cache
869 # then return the correct offset of records
870 $logger->debug("putting search cache $ckey\n");
871 put_cache($ckey, $count, \@recs);
875 # if we have the full set of data, trim out
876 # the requested chunk based on limit and offset
878 for ($offset..$end) {
879 last unless $recs[$_];
885 return { ids => \@recs, count => $count };
889 __PACKAGE__->register_method(
890 method => 'staged_search',
891 api_name => 'open-ils.search.biblio.multiclass.staged');
892 __PACKAGE__->register_method(
893 method => 'staged_search',
894 api_name => 'open-ils.search.biblio.multiclass.staged.staff',
895 signature => q/@see open-ils.search.biblio.multiclass.staged/);
896 __PACKAGE__->register_method(
897 method => 'staged_search',
898 api_name => 'open-ils.search.metabib.multiclass.staged',
899 signature => q/@see open-ils.search.biblio.multiclass.staged/);
900 __PACKAGE__->register_method(
901 method => 'staged_search',
902 api_name => 'open-ils.search.metabib.multiclass.staged.staff',
903 signature => q/@see open-ils.search.biblio.multiclass.staged/);
906 my($self, $conn, $search_hash, $docache) = @_;
908 my $method = ($self->api_name =~ /metabib/) ?
909 'open-ils.storage.metabib.multiclass.staged.search_fts':
910 'open-ils.storage.biblio.multiclass.staged.search_fts';
912 $method .= '.staff' if $self->api_name =~ /staff$/;
913 $method .= '.atomic';
915 return {count => 0} unless (
917 $search_hash->{searches} and
918 scalar( keys %{$search_hash->{searches}} ));
921 my $user_offset = $search_hash->{offset} || 0; # user-specified offset
922 my $user_limit = $search_hash->{limit} || 10;
923 $user_offset = ($user_offset >= 0) ? $user_offset : 0;
924 $user_limit = ($user_limit >= 0) ? $user_limit : 10;
927 # we're grabbing results on a per-superpage basis, which means the
928 # limit and offset should coincide with superpage boundaries
929 $search_hash->{offset} = 0;
930 $search_hash->{limit} = $superpage_size;
932 # force a well-known check_limit
933 $search_hash->{check_limit} = $superpage_size;
934 # restrict total tested to superpage size * number of superpages
935 $search_hash->{core_limit} = $superpage_size * $max_superpages;
937 # Set the configured estimation strategy, defaults to 'inclusion'.
938 my $estimation_strategy = OpenSRF::Utils::SettingsClient
941 apps => 'open-ils.search', app_settings => 'estimation_strategy'
943 $search_hash->{estimation_strategy} = $estimation_strategy;
945 # pull any existing results from the cache
946 my $key = search_cache_key($method, $search_hash);
947 my $facet_key = $key.'_facets';
948 my $cache_data = $cache->get_cache($key) || {};
950 # keep retrieving results until we find enough to
951 # fulfill the user-specified limit and offset
952 my $all_results = [];
953 my $page; # current superpage
954 my $est_hit_count = 0;
955 my $current_page_summary = {};
956 my $global_summary = {checked => 0, visible => 0, excluded => 0, deleted => 0, total => 0};
957 my $is_real_hit_count = 0;
960 for($page = 0; $page < $max_superpages; $page++) {
962 my $data = $cache_data->{$page};
966 $logger->debug("staged search: analyzing superpage $page");
969 # this window of results is already cached
970 $logger->debug("staged search: found cached results");
971 $summary = $data->{summary};
972 $results = $data->{results};
975 # retrieve the window of results from the database
976 $logger->debug("staged search: fetching results from the database");
977 $search_hash->{skip_check} = $page * $superpage_size;
979 $results = $U->storagereq($method, %$search_hash);
980 $search_duration = time - $start;
981 $logger->info("staged search: DB call took $search_duration seconds and returned ".scalar(@$results)." rows, including summary");
982 $summary = shift(@$results);
985 $logger->info("search timed out: duration=$search_duration: params=".
986 OpenSRF::Utils::JSON->perl2JSON($search_hash));
990 my $hc = $summary->{estimated_hit_count} || $summary->{visible};
992 $logger->info("search returned 0 results: duration=$search_duration: params=".
993 OpenSRF::Utils::JSON->perl2JSON($search_hash));
996 # Create backwards-compatible result structures
997 if($self->api_name =~ /biblio/) {
998 $results = [map {[$_->{id}]} @$results];
1000 $results = [map {[$_->{id}, $_->{rel}, $_->{record}]} @$results];
1003 push @$new_ids, grep {defined($_)} map {$_->[0]} @$results;
1004 $results = [grep {defined $_->[0]} @$results];
1005 cache_staged_search_page($key, $page, $summary, $results) if $docache;
1008 $current_page_summary = $summary;
1010 # add the new set of results to the set under construction
1011 push(@$all_results, @$results);
1013 my $current_count = scalar(@$all_results);
1015 $est_hit_count = $summary->{estimated_hit_count} || $summary->{visible}
1018 $logger->debug("staged search: located $current_count, with estimated hits=".
1019 $summary->{estimated_hit_count}." : visible=".$summary->{visible}.", checked=".$summary->{checked});
1021 if (defined($summary->{estimated_hit_count})) {
1022 $global_summary->{checked} += $summary->{checked};
1023 $global_summary->{visible} += $summary->{visible};
1024 $global_summary->{excluded} += $summary->{excluded};
1025 $global_summary->{deleted} += $summary->{deleted};
1026 $global_summary->{total} = $summary->{total};
1029 # we've found all the possible hits
1030 last if $current_count == $summary->{visible}
1031 and not defined $summary->{estimated_hit_count};
1033 # we've found enough results to satisfy the requested limit/offset
1034 last if $current_count >= ($user_limit + $user_offset);
1036 # we've scanned all possible hits
1037 if($summary->{checked} < $superpage_size) {
1038 $est_hit_count = scalar(@$all_results);
1039 # we have all possible results in hand, so we know the final hit count
1040 $is_real_hit_count = 1;
1045 my @results = grep {defined $_} @$all_results[$user_offset..($user_offset + $user_limit - 1)];
1047 # refine the estimate if we have more than one superpage
1048 if ($page > 0 and not $is_real_hit_count) {
1049 if ($global_summary->{checked} >= $global_summary->{total}) {
1050 $est_hit_count = $global_summary->{visible};
1052 my $updated_hit_count = $U->storagereq(
1053 'open-ils.storage.fts_paging_estimate',
1054 $global_summary->{checked},
1055 $global_summary->{visible},
1056 $global_summary->{excluded},
1057 $global_summary->{deleted},
1058 $global_summary->{total}
1060 $est_hit_count = $updated_hit_count->{$estimation_strategy};
1064 $conn->respond_complete({
1065 count => $est_hit_count,
1066 core_limit => $search_hash->{core_limit},
1067 superpage_size => $search_hash->{check_limit},
1068 superpage_summary => $current_page_summary,
1069 facet_key => $facet_key,
1073 cache_facets($facet_key, $new_ids) if $docache;
1077 # creates a unique token to represent the query in the cache
1078 sub search_cache_key {
1080 my $search_hash = shift;
1082 for my $key (sort keys %$search_hash) {
1083 push(@sorted, ($key => $$search_hash{$key}))
1084 unless $key eq 'limit' or
1086 $key eq 'skip_check';
1088 my $s = OpenSRF::Utils::JSON->perl2JSON(\@sorted);
1089 return $pfx . md5_hex($method . $s);
1092 sub retrieve_cached_facets {
1097 return undef unless ($key =~ /_facets$/);
1099 return $cache->get_cache($key) || {};
1101 __PACKAGE__->register_method(
1102 method => "retrieve_cached_facets",
1103 api_name=> "open-ils.search.facet_cache.retrieve"
1108 # add facets for this search to the facet cache
1109 my($key, $results) = @_;
1110 my $data = $cache->get_cache($key);
1113 return undef unless (@$results);
1115 # The query we're constructing
1119 # count(distinct mfae.source)
1120 # from metabib.facet_entry mfae
1121 # join config.metabib_field cmf on (mfae.field = cmf.id)
1122 # where cmf.facet_field
1123 # and mfae.source in IDLIST
1126 my $facets = $U->cstorereq( "open-ils.cstore.json_query.atomic",
1132 transform => 'count',
1140 from => { mfae => 'cmf' },
1141 where => { '+cmf' => 'facet_field', '+mfae' => { source => $results } }
1145 for my $facet (@$facets) {
1146 next unless ($facet->{value});
1147 $data->{$facet->{id}}->{$facet->{value}} += $facet->{count};
1150 $logger->info("facet compilation: cached with key=$key");
1152 $cache->put_cache($key, $data, $cache_timeout);
1155 sub cache_staged_search_page {
1156 # puts this set of results into the cache
1157 my($key, $page, $summary, $results) = @_;
1158 my $data = $cache->get_cache($key);
1161 summary => $summary,
1165 $logger->info("staged search: cached with key=$key, superpage=$page, estimated=".
1166 $summary->{estimated_hit_count}.", visible=".$summary->{visible});
1168 $cache->put_cache($key, $data, $cache_timeout);
1176 my $start = $offset;
1177 my $end = $offset + $limit - 1;
1179 $logger->debug("searching cache for $key : $start..$end\n");
1181 return undef unless $cache;
1182 my $data = $cache->get_cache($key);
1184 return undef unless $data;
1186 my $count = $data->[0];
1189 return undef unless $offset < $count;
1192 for( my $i = $offset; $i <= $end; $i++ ) {
1193 last unless my $d = $$data[$i];
1194 push( @result, $d );
1197 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
1204 my( $key, $count, $data ) = @_;
1205 return undef unless $cache;
1206 $logger->debug("search_cache putting ".
1207 scalar(@$data)." items at key $key with timeout $cache_timeout");
1208 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
1216 __PACKAGE__->register_method(
1217 method => "biblio_mrid_to_modsbatch_batch",
1218 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
1220 sub biblio_mrid_to_modsbatch_batch {
1221 my( $self, $client, $mrids) = @_;
1222 warn "Performing mrid_to_modsbatch_batch...";
1224 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
1225 for my $id (@$mrids) {
1226 next unless defined $id;
1227 my ($m) = $method->run($id);
1234 __PACKAGE__->register_method(
1235 method => "biblio_mrid_to_modsbatch",
1236 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
1237 notes => <<" NOTES");
1238 Returns the mvr associated with a given metarecod. If none exists,
1242 __PACKAGE__->register_method(
1243 method => "biblio_mrid_to_modsbatch",
1244 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
1245 notes => <<" NOTES");
1246 Returns the mvr associated with a given metarecod. If none exists,
1250 sub biblio_mrid_to_modsbatch {
1251 my( $self, $client, $mrid, $args) = @_;
1253 warn "Grabbing mvr for $mrid\n";
1255 my ($mr, $evt) = _grab_metarecord($mrid);
1256 return $evt unless $mr;
1258 my $mvr = biblio_mrid_check_mvr($self, $client, $mr);
1259 $mvr = biblio_mrid_make_modsbatch( $self, $client, $mr ) unless $mvr;
1261 return $mvr unless ref($args);
1263 # Here we find the lead record appropriate for the given filters
1264 # and use that for the title and author of the metarecord
1265 my $format = $$args{format};
1266 my $org = $$args{org};
1267 my $depth = $$args{depth};
1269 return $mvr unless $format or $org or $depth;
1271 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
1272 $method = "$method.staff" if $self->api_name =~ /staff/o;
1274 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
1276 if( my $mods = $U->record_to_mvr($rec) ) {
1278 $mvr->title($mods->title);
1279 $mvr->title($mods->author);
1280 $logger->debug("mods_slim updating title and ".
1281 "author in mvr with ".$mods->title." : ".$mods->author);
1287 # converts a metarecord to an mvr
1290 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
1291 return Fieldmapper::metabib::virtual_record->new($perl);
1294 # checks to see if a metarecord has mods, if so returns true;
1296 __PACKAGE__->register_method(
1297 method => "biblio_mrid_check_mvr",
1298 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
1299 notes => <<" NOTES");
1300 Takes a metarecord ID or a metarecord object and returns true
1301 if the metarecord already has an mvr associated with it.
1304 sub biblio_mrid_check_mvr {
1305 my( $self, $client, $mrid ) = @_;
1309 if(ref($mrid)) { $mr = $mrid; }
1310 else { ($mr, $evt) = _grab_metarecord($mrid); }
1311 return $evt if $evt;
1313 warn "Checking mvr for mr " . $mr->id . "\n";
1315 return _mr_to_mvr($mr) if $mr->mods();
1319 sub _grab_metarecord {
1321 #my $e = OpenILS::Utils::Editor->new;
1322 my $e = new_editor();
1323 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
1328 __PACKAGE__->register_method(
1329 method => "biblio_mrid_make_modsbatch",
1330 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
1331 notes => <<" NOTES");
1332 Takes either a metarecord ID or a metarecord object.
1333 Forces the creations of an mvr for the given metarecord.
1334 The created mvr is returned.
1337 sub biblio_mrid_make_modsbatch {
1338 my( $self, $client, $mrid ) = @_;
1340 #my $e = OpenILS::Utils::Editor->new;
1341 my $e = new_editor();
1348 $mr = $e->retrieve_metabib_metarecord($mrid)
1349 or return $e->event;
1352 my $masterid = $mr->master_record;
1353 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
1355 my $ids = $U->storagereq(
1356 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
1357 return undef unless @$ids;
1359 my $master = $e->retrieve_biblio_record_entry($masterid)
1360 or return $e->event;
1362 # start the mods batch
1363 my $u = OpenILS::Utils::ModsParser->new();
1364 $u->start_mods_batch( $master->marc );
1366 # grab all of the sub-records and shove them into the batch
1367 my @ids = grep { $_ ne $masterid } @$ids;
1368 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
1373 my $r = $e->retrieve_biblio_record_entry($i);
1374 push( @$subrecs, $r ) if $r;
1379 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
1380 $u->push_mods_batch( $_->marc ) if $_->marc;
1384 # finish up and send to the client
1385 my $mods = $u->finish_mods_batch();
1386 $mods->doc_id($mrid);
1387 $client->respond_complete($mods);
1390 # now update the mods string in the db
1391 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
1394 #$e = OpenILS::Utils::Editor->new(xact => 1);
1395 $e = new_editor(xact => 1);
1396 $e->update_metabib_metarecord($mr)
1397 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
1406 # converts a mr id into a list of record ids
1408 __PACKAGE__->register_method(
1409 method => "biblio_mrid_to_record_ids",
1410 api_name => "open-ils.search.biblio.metarecord_to_records",
1413 __PACKAGE__->register_method(
1414 method => "biblio_mrid_to_record_ids",
1415 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
1418 sub biblio_mrid_to_record_ids {
1419 my( $self, $client, $mrid, $args ) = @_;
1421 my $format = $$args{format};
1422 my $org = $$args{org};
1423 my $depth = $$args{depth};
1425 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
1426 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
1427 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
1429 return { count => scalar(@$recs), ids => $recs };
1433 __PACKAGE__->register_method(
1434 method => "biblio_record_to_marc_html",
1435 api_name => "open-ils.search.biblio.record.html" );
1437 __PACKAGE__->register_method(
1438 method => "biblio_record_to_marc_html",
1439 api_name => "open-ils.search.authority.to_html" );
1441 my $parser = XML::LibXML->new();
1442 my $xslt = XML::LibXSLT->new();
1444 my $slim_marc_sheet;
1445 my $settings_client = OpenSRF::Utils::SettingsClient->new();
1447 sub biblio_record_to_marc_html {
1448 my($self, $client, $recordid, $slim, $marcxml) = @_;
1451 my $dir = $settings_client->config_value("dirs", "xsl");
1454 unless($slim_marc_sheet) {
1455 my $xsl = $settings_client->config_value(
1456 "apps", "open-ils.search", "app_settings", 'marc_html_xsl_slim');
1458 $xsl = $parser->parse_file("$dir/$xsl");
1459 $slim_marc_sheet = $xslt->parse_stylesheet($xsl);
1462 $sheet = $slim_marc_sheet;
1466 unless($marc_sheet) {
1467 my $xsl_key = ($slim) ? 'marc_html_xsl_slim' : 'marc_html_xsl';
1468 my $xsl = $settings_client->config_value(
1469 "apps", "open-ils.search", "app_settings", 'marc_html_xsl');
1470 $xsl = $parser->parse_file("$dir/$xsl");
1471 $marc_sheet = $xslt->parse_stylesheet($xsl);
1473 $sheet = $marc_sheet;
1478 my $e = new_editor();
1479 if($self->api_name =~ /authority/) {
1480 $record = $e->retrieve_authority_record_entry($recordid)
1481 or return $e->event;
1483 $record = $e->retrieve_biblio_record_entry($recordid)
1484 or return $e->event;
1486 $marcxml = $record->marc;
1489 my $xmldoc = $parser->parse_string($marcxml);
1490 my $html = $sheet->transform($xmldoc);
1491 return $html->documentElement->toString();
1496 __PACKAGE__->register_method(
1497 method => "retrieve_all_copy_statuses",
1498 api_name => "open-ils.search.config.copy_status.retrieve.all" );
1500 sub retrieve_all_copy_statuses {
1501 my( $self, $client ) = @_;
1502 return new_editor()->retrieve_all_config_copy_status();
1506 __PACKAGE__->register_method(
1507 method => "copy_counts_per_org",
1508 api_name => "open-ils.search.biblio.copy_counts.retrieve");
1510 __PACKAGE__->register_method(
1511 method => "copy_counts_per_org",
1512 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
1514 sub copy_counts_per_org {
1515 my( $self, $client, $record_id ) = @_;
1517 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1519 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1520 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1522 my $counts = $apputils->simple_scalar_request(
1523 "open-ils.storage", $method, $record_id );
1525 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1530 __PACKAGE__->register_method(
1531 method => "copy_count_summary",
1532 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1533 notes => <<" NOTES");
1534 returns an array of these:
1535 [ org_id, callnumber_label, <status1_count>, <status2_count>,...]
1536 where statusx is a copy status name. the statuses are sorted
1540 sub copy_count_summary {
1541 my( $self, $client, $rid, $org, $depth ) = @_;
1544 my $data = $U->storagereq(
1545 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1547 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1550 __PACKAGE__->register_method(
1551 method => "copy_location_count_summary",
1552 api_name => "open-ils.search.biblio.copy_location_counts.summary.retrieve",
1553 notes => <<" NOTES");
1554 returns an array of these:
1555 [ org_id, callnumber_label, copy_location, <status1_count>, <status2_count>,...]
1556 where statusx is a copy status name. the statuses are sorted
1560 sub copy_location_count_summary {
1561 my( $self, $client, $rid, $org, $depth ) = @_;
1564 my $data = $U->storagereq(
1565 'open-ils.storage.biblio.record_entry.status_copy_location_count.atomic', $rid, $org, $depth );
1567 return [ sort { $a->[1] cmp $b->[1] || $a->[2] cmp $b->[2] } @$data ];
1570 __PACKAGE__->register_method(
1571 method => "copy_count_location_summary",
1572 api_name => "open-ils.search.biblio.copy_counts.location.summary.retrieve",
1573 notes => <<" NOTES");
1574 returns an array of these:
1575 [ org_id, callnumber_label, <status1_count>, <status2_count>,...]
1576 where statusx is a copy status name. the statuses are sorted
1580 sub copy_count_location_summary {
1581 my( $self, $client, $rid, $org, $depth ) = @_;
1584 my $data = $U->storagereq(
1585 'open-ils.storage.biblio.record_entry.status_copy_location_count.atomic', $rid, $org, $depth );
1586 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1590 __PACKAGE__->register_method(
1591 method => "marc_search",
1592 api_name => "open-ils.search.biblio.marc.staff");
1594 __PACKAGE__->register_method(
1595 method => "marc_search",
1596 api_name => "open-ils.search.biblio.marc",
1597 notes => <<" NOTES");
1599 open-ils.storage.biblio.full_rec.multi_search.atomic
1600 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1601 "limit":5,"sort":"author","item_type":"g"}
1605 my( $self, $conn, $args, $limit, $offset ) = @_;
1607 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1608 $method .= ".staff" if $self->api_name =~ /staff/;
1609 $method .= ".atomic";
1615 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1616 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1618 my $recs = search_cache($ckey, $offset, $limit);
1621 $recs = $U->storagereq($method, %$args) || [];
1623 put_cache($ckey, scalar(@$recs), $recs);
1624 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1631 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1632 my @recs = map { $_->[0] } @$recs;
1634 return { ids => \@recs, count => $count };
1638 __PACKAGE__->register_method(
1639 method => "biblio_search_isbn",
1640 api_name => "open-ils.search.biblio.isbn",
1643 sub biblio_search_isbn {
1644 my( $self, $client, $isbn ) = @_;
1645 $logger->debug("Searching ISBN $isbn");
1646 my $e = new_editor();
1647 my $recs = $U->storagereq(
1648 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1649 return { ids => $recs, count => scalar(@$recs) };
1652 __PACKAGE__->register_method(
1653 method => "biblio_search_isbn_batch",
1654 api_name => "open-ils.search.biblio.isbn_list",
1657 sub biblio_search_isbn_batch {
1658 my( $self, $client, $isbn_list ) = @_;
1659 $logger->debug("Searching ISBNs @$isbn_list");
1660 my @recs = (); my %rec_set = ();
1661 foreach my $isbn ( @$isbn_list ) {
1662 foreach my $rec ( @{ $U->storagereq(
1663 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn )
1665 if (! $rec_set{ $rec }) {
1666 $rec_set{ $rec } = 1;
1671 return { ids => \@recs, count => scalar(@recs) };
1674 __PACKAGE__->register_method(
1675 method => "biblio_search_issn",
1676 api_name => "open-ils.search.biblio.issn",
1679 sub biblio_search_issn {
1680 my( $self, $client, $issn ) = @_;
1681 $logger->debug("Searching ISSN $issn");
1682 my $e = new_editor();
1684 my $recs = $U->storagereq(
1685 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1686 return { ids => $recs, count => scalar(@$recs) };
1692 __PACKAGE__->register_method(
1693 method => "fetch_mods_by_copy",
1694 api_name => "open-ils.search.biblio.mods_from_copy",
1697 sub fetch_mods_by_copy {
1698 my( $self, $client, $copyid ) = @_;
1699 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1700 return $evt if $evt;
1701 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1702 return $apputils->record_to_mvr($record);
1707 # -------------------------------------------------------------------------------------
1709 __PACKAGE__->register_method(
1710 method => "cn_browse",
1711 api_name => "open-ils.search.callnumber.browse.target",
1712 notes => "Starts a callnumber browse"
1715 __PACKAGE__->register_method(
1716 method => "cn_browse",
1717 api_name => "open-ils.search.callnumber.browse.page_up",
1718 notes => "Returns the previous page of callnumbers",
1721 __PACKAGE__->register_method(
1722 method => "cn_browse",
1723 api_name => "open-ils.search.callnumber.browse.page_down",
1724 notes => "Returns the next page of callnumbers",
1728 # RETURNS array of arrays like so: label, owning_lib, record, id
1730 my( $self, $client, @params ) = @_;
1733 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1734 if( $self->api_name =~ /target/ );
1735 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1736 if( $self->api_name =~ /page_up/ );
1737 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1738 if( $self->api_name =~ /page_down/ );
1740 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1742 # -------------------------------------------------------------------------------------
1744 __PACKAGE__->register_method(
1745 method => "fetch_cn",
1747 api_name => "open-ils.search.callnumber.retrieve",
1748 notes => "retrieves a callnumber based on ID",
1752 my( $self, $client, $id ) = @_;
1753 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1754 return $evt if $evt;
1758 __PACKAGE__->register_method (
1759 method => "fetch_copy_by_cn",
1760 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1762 Returns an array of copy id's by callnumber id
1763 @param cnid The callnumber id
1764 @return An array of copy ids
1768 sub fetch_copy_by_cn {
1769 my( $self, $conn, $cnid ) = @_;
1770 return $U->cstorereq(
1771 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1772 { call_number => $cnid, deleted => 'f' } );
1775 __PACKAGE__->register_method (
1776 method => 'fetch_cn_by_info',
1777 api_name => 'open-ils.search.call_number.retrieve_by_info',
1779 @param label The callnumber label
1780 @param record The record the cn is attached to
1781 @param org The owning library of the cn
1782 @return The callnumber object
1787 sub fetch_cn_by_info {
1788 my( $self, $conn, $label, $record, $org ) = @_;
1789 return $U->cstorereq(
1790 'open-ils.cstore.direct.asset.call_number.search',
1791 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1798 __PACKAGE__->register_method (
1799 method => 'bib_extras',
1800 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1801 __PACKAGE__->register_method (
1802 method => 'bib_extras',
1803 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1804 __PACKAGE__->register_method (
1805 method => 'bib_extras',
1806 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1807 __PACKAGE__->register_method (
1808 method => 'bib_extras',
1809 api_name => 'open-ils.search.biblio.bib_level_map.retrieve.all');
1810 __PACKAGE__->register_method (
1811 method => 'bib_extras',
1812 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1817 my $e = new_editor();
1819 return $e->retrieve_all_config_lit_form_map()
1820 if( $self->api_name =~ /lit_form/ );
1822 return $e->retrieve_all_config_item_form_map()
1823 if( $self->api_name =~ /item_form_map/ );
1825 return $e->retrieve_all_config_item_type_map()
1826 if( $self->api_name =~ /item_type_map/ );
1828 return $e->retrieve_all_config_bib_level_map()
1829 if( $self->api_name =~ /bib_level_map/ );
1831 return $e->retrieve_all_config_audience_map()
1832 if( $self->api_name =~ /audience_map/ );
1839 __PACKAGE__->register_method(
1840 method => 'fetch_slim_record',
1841 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1843 Returns a biblio.record_entry without the attached marcxml
1847 sub fetch_slim_record {
1848 my( $self, $conn, $ids ) = @_;
1850 #my $editor = OpenILS::Utils::Editor->new;
1851 my $editor = new_editor();
1854 return $editor->event unless
1855 my $r = $editor->retrieve_biblio_record_entry($_);
1864 __PACKAGE__->register_method(
1865 method => 'rec_to_mr_rec_descriptors',
1866 api_name => 'open-ils.search.metabib.record_to_descriptors',
1868 specialized method...
1869 Given a biblio record id or a metarecord id,
1870 this returns a list of metabib.record_descriptor
1871 objects that live within the same metarecord
1872 @param args Object of args including:
1876 sub rec_to_mr_rec_descriptors {
1877 my( $self, $conn, $args ) = @_;
1879 my $rec = $$args{record};
1880 my $mrec = $$args{metarecord};
1881 my $item_forms = $$args{item_forms};
1882 my $item_types = $$args{item_types};
1883 my $item_lang = $$args{item_lang};
1885 my $e = new_editor();
1889 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1890 return $e->event unless @$map;
1891 $mrec = $$map[0]->metarecord;
1894 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1895 return $e->event unless @$recs;
1897 my @recs = map { $_->source } @$recs;
1898 my $search = { record => \@recs };
1899 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1900 $search->{item_type} = $item_types if $item_types and @$item_types;
1901 $search->{item_lang} = $item_lang if $item_lang;
1903 my $desc = $e->search_metabib_record_descriptor($search);
1905 return { metarecord => $mrec, descriptors => $desc };
1909 __PACKAGE__->register_method(
1910 method => 'fetch_age_protect',
1911 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1914 sub fetch_age_protect {
1915 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1919 __PACKAGE__->register_method(
1920 method => 'copies_by_cn_label',
1921 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1924 __PACKAGE__->register_method(
1925 method => 'copies_by_cn_label',
1926 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1929 sub copies_by_cn_label {
1930 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1931 my $e = new_editor();
1932 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1933 return [] unless @$cns;
1935 # show all non-deleted copies in the staff client ...
1936 if ($self->api_name =~ /staff$/o) {
1937 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1940 # ... otherwise, grab the copies ...
1941 my $copies = $e->search_asset_copy(
1942 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1943 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1947 # ... and test for location and status visibility
1948 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->opac_visible)) ? ($_->id) : () } @$copies ];