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_";
41 $cache = OpenSRF::Utils::Cache->new('global');
42 my $sclient = OpenSRF::Utils::SettingsClient->new();
43 $cache_timeout = $sclient->config_value(
44 "apps", "open-ils.search", "app_settings", "cache_timeout" ) || 300;
45 $logger->info("Search cache timeout is $cache_timeout");
50 # ---------------------------------------------------------------------------
51 # takes a list of record id's and turns the docs into friendly
52 # mods structures. Creates one MODS structure for each doc id.
53 # ---------------------------------------------------------------------------
54 sub _records_to_mods {
60 my $session = OpenSRF::AppSession->create("open-ils.cstore");
61 my $request = $session->request(
62 "open-ils.cstore.direct.biblio.record_entry.search", { id => \@ids } );
64 while( my $resp = $request->recv ) {
65 my $content = $resp->content;
66 next if $content->id == OILS_PRECAT_RECORD;
67 my $u = OpenILS::Utils::ModsParser->new();
68 $u->start_mods_batch( $content->marc );
69 my $mods = $u->finish_mods_batch();
70 $mods->doc_id($content->id());
71 $mods->tcn($content->tcn_value);
75 $session->disconnect();
79 __PACKAGE__->register_method(
80 method => "record_id_to_mods",
81 api_name => "open-ils.search.biblio.record.mods.retrieve",
83 note => "Provide ID, we provide the mods"
86 # converts a record into a mods object with copy counts attached
87 sub record_id_to_mods {
89 my( $self, $client, $org_id, $id ) = @_;
91 my $mods_list = _records_to_mods( $id );
92 my $mods_obj = $mods_list->[0];
93 my $cmethod = $self->method_lookup(
94 "open-ils.search.biblio.record.copy_count");
95 my ($count) = $cmethod->run($org_id, $id);
96 $mods_obj->copy_count($count);
103 __PACKAGE__->register_method(
104 method => "record_id_to_mods_slim",
106 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
108 note => "Provide ID, we provide the mods"
111 # converts a record into a mods object with NO copy counts attached
112 sub record_id_to_mods_slim {
113 my( $self, $client, $id ) = @_;
114 return undef unless defined $id;
116 if(ref($id) and ref($id) == 'ARRAY') {
117 return _records_to_mods( @$id );
119 my $mods_list = _records_to_mods( $id );
120 my $mods_obj = $mods_list->[0];
121 return OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND') unless $mods_obj;
126 # Returns the number of copies attached to a record based on org location
127 __PACKAGE__->register_method(
128 method => "record_id_to_copy_count",
129 api_name => "open-ils.search.biblio.record.copy_count",
132 __PACKAGE__->register_method(
133 method => "record_id_to_copy_count",
135 api_name => "open-ils.search.biblio.record.copy_count.staff",
138 __PACKAGE__->register_method(
139 method => "record_id_to_copy_count",
140 api_name => "open-ils.search.biblio.metarecord.copy_count",
143 __PACKAGE__->register_method(
144 method => "record_id_to_copy_count",
145 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
147 sub record_id_to_copy_count {
148 my( $self, $client, $org_id, $record_id, $format ) = @_;
150 return [] unless $record_id;
151 $format = undef if (!$format or $format eq 'all');
153 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
156 if($self->api_name =~ /metarecord/) {
157 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
161 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
163 my $count = $U->storagereq( $method,
164 org_unit => $org_id, $key => $record_id, format => $format );
166 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
172 __PACKAGE__->register_method(
173 method => "biblio_search_tcn",
174 api_name => "open-ils.search.biblio.tcn",
176 note => "Retrieve a record by TCN",
179 sub biblio_search_tcn {
181 my( $self, $client, $tcn, $include_deleted ) = @_;
183 $tcn =~ s/.*?(\w+)\s*$/$1/o;
185 my $e = new_editor();
186 my $search = {tcn_value => $tcn};
187 $search->{deleted} = 'f' unless $include_deleted;
188 my $recs = $e->search_biblio_record_entry( $search, {idlist =>1} );
190 return { count => scalar(@$recs), ids => $recs };
194 # --------------------------------------------------------------------------------
196 __PACKAGE__->register_method(
197 method => "biblio_barcode_to_copy",
198 api_name => "open-ils.search.asset.copy.find_by_barcode",);
199 sub biblio_barcode_to_copy {
200 my( $self, $client, $barcode ) = @_;
201 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
206 __PACKAGE__->register_method(
207 method => "biblio_id_to_copy",
208 api_name => "open-ils.search.asset.copy.batch.retrieve",);
209 sub biblio_id_to_copy {
210 my( $self, $client, $ids ) = @_;
211 $logger->info("Fetching copies @$ids");
212 return $U->cstorereq(
213 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
217 __PACKAGE__->register_method(
218 method => "copy_retrieve",
219 api_name => "open-ils.search.asset.copy.retrieve",);
221 my( $self, $client, $cid ) = @_;
222 my( $copy, $evt ) = $U->fetch_copy($cid);
227 __PACKAGE__->register_method(
228 method => "volume_retrieve",
229 api_name => "open-ils.search.asset.call_number.retrieve");
230 sub volume_retrieve {
231 my( $self, $client, $vid ) = @_;
232 my $e = new_editor();
233 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
237 __PACKAGE__->register_method(
238 method => "fleshed_copy_retrieve_batch",
240 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
242 sub fleshed_copy_retrieve_batch {
243 my( $self, $client, $ids ) = @_;
244 $logger->info("Fetching fleshed copies @$ids");
245 return $U->cstorereq(
246 "open-ils.cstore.direct.asset.copy.search.atomic",
249 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
254 __PACKAGE__->register_method(
255 method => "fleshed_copy_retrieve",
256 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
258 sub fleshed_copy_retrieve {
259 my( $self, $client, $id ) = @_;
260 my( $c, $e) = $U->fetch_fleshed_copy($id);
267 __PACKAGE__->register_method(
268 method => 'fleshed_by_barcode',
269 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",);
270 sub fleshed_by_barcode {
271 my( $self, $conn, $barcode ) = @_;
272 my $e = new_editor();
273 my $copyid = $e->search_asset_copy(
274 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
276 return fleshed_copy_retrieve2( $self, $conn, $copyid);
280 __PACKAGE__->register_method(
281 method => "fleshed_copy_retrieve2",
282 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
284 sub fleshed_copy_retrieve2 {
285 my( $self, $client, $id ) = @_;
286 my $e = new_editor();
287 my $copy = $e->retrieve_asset_copy(
293 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
294 ascecm => [ qw/ stat_cat stat_cat_entry / ],
298 ) or return $e->event;
300 # For backwards compatibility
301 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
303 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
305 $e->search_action_circulation(
307 { target_copy => $copy->id },
309 order_by => { circ => 'xact_start desc' },
321 __PACKAGE__->register_method(
322 method => 'flesh_copy_custom',
323 api_name => 'open-ils.search.asset.copy.fleshed.custom'
326 sub flesh_copy_custom {
327 my( $self, $conn, $copyid, $fields ) = @_;
328 my $e = new_editor();
329 my $copy = $e->retrieve_asset_copy(
339 ) or return $e->event;
348 __PACKAGE__->register_method(
349 method => "biblio_barcode_to_title",
350 api_name => "open-ils.search.biblio.find_by_barcode",
353 sub biblio_barcode_to_title {
354 my( $self, $client, $barcode ) = @_;
356 my $title = $apputils->simple_scalar_request(
358 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
360 return { ids => [ $title->id ], count => 1 } if $title;
361 return { count => 0 };
364 __PACKAGE__->register_method(
365 method => 'title_id_by_item_barcode',
366 api_name => 'open-ils.search.bib_id.by_barcode'
369 sub title_id_by_item_barcode {
370 my( $self, $conn, $barcode ) = @_;
371 my $e = new_editor();
372 my $copies = $e->search_asset_copy(
374 { deleted => 'f', barcode => $barcode },
378 acp => [ 'call_number' ],
385 return $e->event unless @$copies;
386 return $$copies[0]->call_number->record->id;
390 __PACKAGE__->register_method(
391 method => "biblio_copy_to_mods",
392 api_name => "open-ils.search.biblio.copy.mods.retrieve",
395 # takes a copy object and returns it fleshed mods object
396 sub biblio_copy_to_mods {
397 my( $self, $client, $copy ) = @_;
399 my $volume = $U->cstorereq(
400 "open-ils.cstore.direct.asset.call_number.retrieve",
401 $copy->call_number() );
403 my $mods = _records_to_mods($volume->record());
404 $mods = shift @$mods;
405 $volume->copies([$copy]);
406 push @{$mods->call_numbers()}, $volume;
412 __PACKAGE__->register_method(
413 api_name => 'open-ils.search.biblio.multiclass.query',
414 method => 'multiclass_query',
416 @param arghash @see open-ils.search.biblio.multiclass
417 @param query Raw human-readable query string.
418 Recognized search keys include:
419 keyword/kw - search keyword(s)
420 author/au/name - search author(s)
421 title/ti - search title
422 subject/su - search subject
423 series/se - search series
424 lang - limit by language (specifiy multiple langs with lang:l1 lang:l2 ...)
425 site - search at specified org unit, corresponds to actor.org_unit.shortname
426 sort - sort type (title, author, pubdate)
427 dir - sort direction (asc, desc)
428 available - if set to anything other than "false" or "0", limits to available items
430 keyword, title, author, subject, and series support additional search
431 subclasses, specified with a "|". For example, "title|proper:gone with the wind"
432 For more, see config.metabib_field
434 @param nocache @see open-ils.search.biblio.multiclass
437 __PACKAGE__->register_method(
438 api_name => 'open-ils.search.biblio.multiclass.query.staff',
439 method => 'multiclass_query',
440 signature => '@see open-ils.search.biblio.multiclass.query');
441 __PACKAGE__->register_method(
442 api_name => 'open-ils.search.metabib.multiclass.query',
443 method => 'multiclass_query',
444 signature => '@see open-ils.search.biblio.multiclass.query');
445 __PACKAGE__->register_method(
446 api_name => 'open-ils.search.metabib.multiclass.query.staff',
447 method => 'multiclass_query',
448 signature => '@see open-ils.search.biblio.multiclass.query');
450 sub multiclass_query {
451 my($self, $conn, $arghash, $query, $docache) = @_;
453 $logger->debug("initial search query => $query");
454 my $orig_query = $query;
456 $query = decode_utf8($query);
459 $query =~ s/^\s+//go;
461 # convert convenience classes (e.g. kw for keyword) to the full class name
462 $query =~ s/kw(:|\|)/keyword$1/go;
463 $query =~ s/ti(:|\|)/title$1/go;
464 $query =~ s/au(:|\|)/author$1/go;
465 $query =~ s/su(:|\|)/subject$1/go;
466 $query =~ s/se(:|\|)/series$1/go;
467 $query =~ s/name(:|\|)/author$1/og;
469 $logger->debug("cleansed query string => $query");
470 my $search = $arghash->{searches} = {};
472 while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):[^:]+)$//so) {
473 my($type, $value) = split(':', $1);
474 next unless $type and $value;
476 $value =~ s/^\s*//og;
477 $value =~ s/\s*$//og;
478 $type = 'sort_dir' if $type eq 'dir';
480 if($type eq 'site') {
481 # 'site' is the org shortname. when using this, we also want
482 # to search at the requested org's depth
483 my $e = new_editor();
484 if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
485 $arghash->{org_unit} = $org->id if $org;
486 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
488 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
491 } elsif($type eq 'available') {
493 $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
495 } elsif($type eq 'lang') {
496 # collect languages into an array of languages
497 $arghash->{language} = [] unless $arghash->{language};
498 push(@{$arghash->{language}}, $value);
500 } elsif($type =~ /^sort/o) {
501 # sort and sort_dir modifiers
502 $arghash->{$type} = $value;
505 # append the search term to the term under construction
506 $search->{$type} = {} unless $search->{$type};
507 $search->{$type}->{term} =
508 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
513 # This is the front part of the string before any special tokens were parsed.
514 # Add this data to the default search class
515 my $type = $arghash->{default_class} || 'keyword';
516 $search->{$type} = {} unless $search->{$type};
517 $search->{$type}->{term} =
518 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $query" : $query;
521 # capture the original limit because the search method alters the limit internally
522 my $ol = $arghash->{limit};
524 (my $method = $self->api_name) =~ s/\.query//o;
525 $method = $self->method_lookup($method);
526 my ($data) = $method->run($arghash, $docache);
528 $arghash->{limit} = $ol if $ol;
529 $data->{compiled_search} = $arghash;
530 $data->{query} = $orig_query;
532 $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
537 __PACKAGE__->register_method(
538 method => 'cat_search_z_style_wrapper',
539 api_name => 'open-ils.search.biblio.zstyle',
541 signature => q/@see open-ils.search.biblio.multiclass/);
543 sub cat_search_z_style_wrapper {
546 my $authtoken = shift;
549 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
551 my $ou = $cstore->request(
552 'open-ils.cstore.direct.actor.org_unit.search',
553 { parent_ou => undef }
556 my $result = { service => 'native-evergreen-catalog', records => [] };
557 my $searchhash = { limit => $$args{limit}, offset => $$args{offset}, org_unit => $ou->id };
559 $$searchhash{searches}{title}{term} = $$args{search}{title} if $$args{search}{title};
560 $$searchhash{searches}{author}{term} = $$args{search}{author} if $$args{search}{author};
561 $$searchhash{searches}{subject}{term} = $$args{search}{subject} if $$args{search}{subject};
562 $$searchhash{searches}{keyword}{term} = $$args{search}{keyword} if $$args{search}{keyword};
564 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{tcn} if $$args{search}{tcn};
565 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{isbn} if $$args{search}{isbn};
566 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{issn} if $$args{search}{issn};
567 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{publisher} if $$args{search}{publisher};
568 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{pubdate} if $$args{search}{pubdate};
569 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{item_type} if $$args{search}{item_type};
571 my $list = the_quest_for_knowledge( $self, $client, $searchhash );
573 if ($list->{count} > 0) {
574 $result->{count} = $list->{count};
576 my $records = $cstore->request(
577 'open-ils.cstore.direct.biblio.record_entry.search.atomic',
578 { id => [ map { ( $_->[0] ) } @{$list->{ids}} ] }
581 for my $rec ( @$records ) {
583 my $u = OpenILS::Utils::ModsParser->new();
584 $u->start_mods_batch( $rec->marc );
585 my $mods = $u->finish_mods_batch();
587 push @{ $result->{records} }, { mvr => $mods, marcxml => $rec->marc, bibid => $rec->id };
593 $cstore->disconnect();
597 # ----------------------------------------------------------------------------
598 # These are the main OPAC search methods
599 # ----------------------------------------------------------------------------
601 __PACKAGE__->register_method(
602 method => 'the_quest_for_knowledge',
603 api_name => 'open-ils.search.biblio.multiclass',
605 Performs a multi class bilbli or metabib search
606 @param searchhash A search object layed out like so:
607 searches : { "$class" : "$value", ...}
608 org_unit : The org id to focus the search at
609 depth : The org depth
610 limit : The search limit
611 offset : The search offset
612 format : The MARC format
613 sort : What field to sort the results on [ author | title | pubdate ]
614 sort_dir : What direction do we sort? [ asc | desc ]
615 @return An object of the form
616 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
620 __PACKAGE__->register_method(
621 method => 'the_quest_for_knowledge',
622 api_name => 'open-ils.search.biblio.multiclass.staff',
623 signature => q/@see open-ils.search.biblio.multiclass/);
624 __PACKAGE__->register_method(
625 method => 'the_quest_for_knowledge',
626 api_name => 'open-ils.search.metabib.multiclass',
627 signature => q/@see open-ils.search.biblio.multiclass/);
628 __PACKAGE__->register_method(
629 method => 'the_quest_for_knowledge',
630 api_name => 'open-ils.search.metabib.multiclass.staff',
631 signature => q/@see open-ils.search.biblio.multiclass/);
633 sub the_quest_for_knowledge {
634 my( $self, $conn, $searchhash, $docache ) = @_;
636 return { count => 0 } unless $searchhash and
637 ref $searchhash->{searches} eq 'HASH';
639 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
643 if($self->api_name =~ /metabib/) {
645 $method =~ s/biblio/metabib/o;
649 my $offset = $searchhash->{offset} || 0;
650 my $limit = $searchhash->{limit} || 10;
651 my $end = $offset + $limit - 1;
653 # do some simple sanity checking
654 if(!$searchhash->{searches} or
655 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
656 return { count => 0 };
661 $searchhash->{offset} = 0;
662 $searchhash->{limit} = $maxlimit;
664 return { count => 0 } if $offset > $maxlimit;
667 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
668 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
669 my $ckey = $pfx . md5_hex($method . $s);
671 $logger->info("bib search for: $s");
673 $searchhash->{limit} -= $offset;
677 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
681 $method .= ".staff" if($self->api_name =~ /staff/);
682 $method .= ".atomic";
684 for (keys %$searchhash) {
685 delete $$searchhash{$_}
686 unless defined $$searchhash{$_};
689 $result = $U->storagereq( $method, %$searchhash );
696 return {count => 0} unless ($result && $$result[0]);
700 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
703 # If we didn't get this data from the cache, put it into the cache
704 # then return the correct offset of records
705 $logger->debug("putting search cache $ckey\n");
706 put_cache($ckey, $count, \@recs);
710 # if we have the full set of data, trim out
711 # the requested chunk based on limit and offset
713 for ($offset..$end) {
714 last unless $recs[$_];
720 return { ids => \@recs, count => $count };
724 __PACKAGE__->register_method(
725 method => 'staged_search',
726 api_name => 'open-ils.search.biblio.multiclass.staged');
727 __PACKAGE__->register_method(
728 method => 'staged_search',
729 api_name => 'open-ils.search.biblio.multiclass.staged.staff',
730 signature => q/@see open-ils.search.biblio.multiclass.staged/);
731 __PACKAGE__->register_method(
732 method => 'staged_search',
733 api_name => 'open-ils.search.metabib.multiclass.staged',
734 signature => q/@see open-ils.search.biblio.multiclass.staged/);
735 __PACKAGE__->register_method(
736 method => 'staged_search',
737 api_name => 'open-ils.search.metabib.multiclass.staged.staff',
738 signature => q/@see open-ils.search.biblio.multiclass.staged/);
740 my $CHECK_SIZE = 1000;
741 my $SEARCH_PAGES = 25;
743 my($self, $conn, $search_hash, $nocache) = @_;
745 my $method = ($self->api_name =~ /metabib/) ?
746 'open-ils.storage.metabib.multiclass.staged.search_fts':
747 'open-ils.storage.biblio.multiclass.staged.search_fts';
749 $method .= '.staff' if $self->api_name =~ /staff$/;
750 $method .= '.atomic';
752 my $user_offset = $search_hash->{offset} || 0; # user-specified offset
753 my $user_limit = $search_hash->{limit} || 10;
755 # we're grabbing results on a per-superpage basis, which means the
756 # limit and offset should coincide with superpage boundaries
757 $search_hash->{offset} = 0;
758 $search_hash->{limit} = $CHECK_SIZE;
760 # pull any existing results from the cache
761 my $key = search_cache_key($method, $search_hash);
762 my $cache_data = $cache->get_cache($key) || {};
764 # keep retrieving results until we find enough to
765 # fulfill the user-specified limit and offset
766 my $all_results = [];
767 my $avg_hit_count = 0;
768 my $page; # current superpage
770 for($page = 0; $page < $SEARCH_PAGES; $page++) {
772 my $data = $cache_data->{$page};
776 $logger->debug("staged search: analyzing superpage $page");
779 # this window of results is already cached
780 $logger->debug("staged search: found cached results");
781 $summary = $data->{summary};
782 $results = $data->{results};
785 # retrieve the window of results from the database
786 $logger->debug("staged search: fetching results from the database");
787 $search_hash->{skip_check} = $page * $CHECK_SIZE;
788 $results = $U->storagereq($method, %$search_hash);
789 $summary = shift(@$results);
791 # Clean up the raw search results
792 if($self->api_name =~ /biblio/) {
793 $results = [map {$_->{id}} @$results];
795 delete $_->{rel} for @$results;
798 cache_staged_search_page($key, $page, $summary, $results) unless $nocache;
801 # add the new set of results to the set under construction
802 push(@$all_results, grep {defined $_} @$results);
804 my $current_count = scalar(@$all_results);
805 $avg_hit_count += $summary->{estimated_hit_count} || $summary->{visible};
807 $logger->debug("staged search: located $current_count, with estimated hits=".
808 $summary->{estimated_hit_count}." : visible=".$summary->{visible});
810 # no results for this search
811 last if $current_count == 0;
813 # we've found all the possible hits
814 last if $current_count == $summary->{visible}
815 and not defined $summary->{estimated_hit_count};
817 # we've found enough results to satisfy the requested limit/offset
818 last if $current_count >= ($user_limit + $user_offset);
823 # calculate the average estimated hit count from the data we've collected thus far
824 $avg_hit_count = int($avg_hit_count / ++$page);
825 $avg_hit_count = scalar(@$all_results) if scalar(@$all_results) > $avg_hit_count;
827 my @results = grep {defined $_} @$all_results[$user_offset..($user_offset + $user_limit - 1)];
830 count => $avg_hit_count,
835 # creates a unique token to represent the query in the cache
836 sub search_cache_key {
838 my $search_hash = shift;
840 for my $key (sort keys %$search_hash) {
841 push(@sorted, ($key => $$search_hash{$key}))
842 unless $key eq 'limit' or
844 $key eq 'skip_check';
846 my $s = OpenSRF::Utils::JSON->perl2JSON(\@sorted);
847 return $pfx . md5_hex($method . $s);
850 sub cache_staged_search_page {
851 # puts this set of results into the cache
852 my($key, $page, $summary, $results) = @_;
853 my $data = $cache->get_cache($key);
860 $logger->info("staged search: cached with key=$key, superpage=$page, estimated=".
861 $summary->{estimated_hit_count}.", visible=".$summary->{visible});
863 $cache->put_cache($key, $data);
872 my $end = $offset + $limit - 1;
874 $logger->debug("searching cache for $key : $start..$end\n");
876 return undef unless $cache;
877 my $data = $cache->get_cache($key);
879 return undef unless $data;
881 my $count = $data->[0];
884 return undef unless $offset < $count;
887 for( my $i = $offset; $i <= $end; $i++ ) {
888 last unless my $d = $$data[$i];
892 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
899 my( $key, $count, $data ) = @_;
900 return undef unless $cache;
901 $logger->debug("search_cache putting ".
902 scalar(@$data)." items at key $key with timeout $cache_timeout");
903 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
911 __PACKAGE__->register_method(
912 method => "biblio_mrid_to_modsbatch_batch",
913 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
915 sub biblio_mrid_to_modsbatch_batch {
916 my( $self, $client, $mrids) = @_;
917 warn "Performing mrid_to_modsbatch_batch...";
919 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
920 for my $id (@$mrids) {
921 next unless defined $id;
922 my ($m) = $method->run($id);
929 __PACKAGE__->register_method(
930 method => "biblio_mrid_to_modsbatch",
931 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
932 notes => <<" NOTES");
933 Returns the mvr associated with a given metarecod. If none exists,
937 __PACKAGE__->register_method(
938 method => "biblio_mrid_to_modsbatch",
939 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
940 notes => <<" NOTES");
941 Returns the mvr associated with a given metarecod. If none exists,
945 sub biblio_mrid_to_modsbatch {
946 my( $self, $client, $mrid, $args) = @_;
948 warn "Grabbing mvr for $mrid\n";
950 my ($mr, $evt) = _grab_metarecord($mrid);
951 return $evt unless $mr;
953 my $mvr = biblio_mrid_check_mvr($self, $client, $mr);
954 $mvr = biblio_mrid_make_modsbatch( $self, $client, $mr ) unless $mvr;
956 return $mvr unless ref($args);
958 # Here we find the lead record appropriate for the given filters
959 # and use that for the title and author of the metarecord
960 my $format = $$args{format};
961 my $org = $$args{org};
962 my $depth = $$args{depth};
964 return $mvr unless $format or $org or $depth;
966 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
967 $method = "$method.staff" if $self->api_name =~ /staff/o;
969 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
971 if( my $mods = $U->record_to_mvr($rec) ) {
973 $mvr->title($mods->title);
974 $mvr->title($mods->author);
975 $logger->debug("mods_slim updating title and ".
976 "author in mvr with ".$mods->title." : ".$mods->author);
982 # converts a metarecord to an mvr
985 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
986 return Fieldmapper::metabib::virtual_record->new($perl);
989 # checks to see if a metarecord has mods, if so returns true;
991 __PACKAGE__->register_method(
992 method => "biblio_mrid_check_mvr",
993 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
994 notes => <<" NOTES");
995 Takes a metarecord ID or a metarecord object and returns true
996 if the metarecord already has an mvr associated with it.
999 sub biblio_mrid_check_mvr {
1000 my( $self, $client, $mrid ) = @_;
1004 if(ref($mrid)) { $mr = $mrid; }
1005 else { ($mr, $evt) = _grab_metarecord($mrid); }
1006 return $evt if $evt;
1008 warn "Checking mvr for mr " . $mr->id . "\n";
1010 return _mr_to_mvr($mr) if $mr->mods();
1014 sub _grab_metarecord {
1016 #my $e = OpenILS::Utils::Editor->new;
1017 my $e = new_editor();
1018 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
1023 __PACKAGE__->register_method(
1024 method => "biblio_mrid_make_modsbatch",
1025 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
1026 notes => <<" NOTES");
1027 Takes either a metarecord ID or a metarecord object.
1028 Forces the creations of an mvr for the given metarecord.
1029 The created mvr is returned.
1032 sub biblio_mrid_make_modsbatch {
1033 my( $self, $client, $mrid ) = @_;
1035 #my $e = OpenILS::Utils::Editor->new;
1036 my $e = new_editor();
1043 $mr = $e->retrieve_metabib_metarecord($mrid)
1044 or return $e->event;
1047 my $masterid = $mr->master_record;
1048 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
1050 my $ids = $U->storagereq(
1051 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
1052 return undef unless @$ids;
1054 my $master = $e->retrieve_biblio_record_entry($masterid)
1055 or return $e->event;
1057 # start the mods batch
1058 my $u = OpenILS::Utils::ModsParser->new();
1059 $u->start_mods_batch( $master->marc );
1061 # grab all of the sub-records and shove them into the batch
1062 my @ids = grep { $_ ne $masterid } @$ids;
1063 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
1068 my $r = $e->retrieve_biblio_record_entry($i);
1069 push( @$subrecs, $r ) if $r;
1074 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
1075 $u->push_mods_batch( $_->marc ) if $_->marc;
1079 # finish up and send to the client
1080 my $mods = $u->finish_mods_batch();
1081 $mods->doc_id($mrid);
1082 $client->respond_complete($mods);
1085 # now update the mods string in the db
1086 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
1089 #$e = OpenILS::Utils::Editor->new(xact => 1);
1090 $e = new_editor(xact => 1);
1091 $e->update_metabib_metarecord($mr)
1092 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
1101 # converts a mr id into a list of record ids
1103 __PACKAGE__->register_method(
1104 method => "biblio_mrid_to_record_ids",
1105 api_name => "open-ils.search.biblio.metarecord_to_records",
1108 __PACKAGE__->register_method(
1109 method => "biblio_mrid_to_record_ids",
1110 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
1113 sub biblio_mrid_to_record_ids {
1114 my( $self, $client, $mrid, $args ) = @_;
1116 my $format = $$args{format};
1117 my $org = $$args{org};
1118 my $depth = $$args{depth};
1120 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
1121 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
1122 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
1124 return { count => scalar(@$recs), ids => $recs };
1128 __PACKAGE__->register_method(
1129 method => "biblio_record_to_marc_html",
1130 api_name => "open-ils.search.biblio.record.html" );
1132 my $parser = XML::LibXML->new();
1133 my $xslt = XML::LibXSLT->new();
1136 my $settings_client = OpenSRF::Utils::SettingsClient->new();
1137 sub biblio_record_to_marc_html {
1138 my( $self, $client, $recordid ) = @_;
1140 if( !$marc_sheet ) {
1141 my $dir = $settings_client->config_value( "dirs", "xsl" );
1142 my $xsl = $settings_client->config_value(
1143 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
1145 $xsl = $parser->parse_file("$dir/$xsl");
1146 $marc_sheet = $xslt->parse_stylesheet( $xsl );
1150 my $record = $apputils->simple_scalar_request(
1152 "open-ils.cstore.direct.biblio.record_entry.retrieve",
1155 my $xmldoc = $parser->parse_string($record->marc);
1156 my $html = $marc_sheet->transform($xmldoc);
1157 $html = $html->toString();
1164 __PACKAGE__->register_method(
1165 method => "retrieve_all_copy_locations",
1166 api_name => "open-ils.search.config.copy_location.retrieve.all" );
1168 my $shelving_locations;
1169 sub retrieve_all_copy_locations {
1170 my( $self, $client ) = @_;
1171 if(!$shelving_locations) {
1172 $shelving_locations = $apputils->simple_scalar_request(
1174 "open-ils.cstore.direct.asset.copy_location.search.atomic",
1175 { id => { "!=" => undef } }
1178 return $shelving_locations;
1184 __PACKAGE__->register_method(
1185 method => "retrieve_all_copy_statuses",
1186 api_name => "open-ils.search.config.copy_status.retrieve.all" );
1189 sub retrieve_all_copy_statuses {
1190 my( $self, $client ) = @_;
1191 return $copy_statuses if $copy_statuses;
1192 return $copy_statuses =
1193 new_editor()->retrieve_all_config_copy_status();
1197 __PACKAGE__->register_method(
1198 method => "copy_counts_per_org",
1199 api_name => "open-ils.search.biblio.copy_counts.retrieve");
1201 __PACKAGE__->register_method(
1202 method => "copy_counts_per_org",
1203 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
1205 sub copy_counts_per_org {
1206 my( $self, $client, $record_id ) = @_;
1208 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1210 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1211 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1213 my $counts = $apputils->simple_scalar_request(
1214 "open-ils.storage", $method, $record_id );
1216 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1221 __PACKAGE__->register_method(
1222 method => "copy_count_summary",
1223 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1224 notes => <<" NOTES");
1225 returns an array of these:
1226 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1227 where statusx is a copy status name. the statuses are sorted
1231 sub copy_count_summary {
1232 my( $self, $client, $rid, $org, $depth ) = @_;
1235 my $data = $U->storagereq(
1236 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1238 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1244 __PACKAGE__->register_method(
1245 method => "multiclass_search",
1246 api_name => "open-ils.search.biblio.multiclass",
1247 notes => <<" NOTES");
1248 Performs a multiclass search
1249 PARAMS( searchBlob, org_unit, format, limit )
1250 where searchBlob is defined like this:
1252 "title" : { "term" : "water" },
1253 "author" : { "term" : "smith" },
1258 __PACKAGE__->register_method(
1259 method => "multiclass_search",
1260 api_name => "open-ils.search.biblio.multiclass.staff",
1261 notes => "see open-ils.search.biblio.multiclass" );
1263 sub multiclass_search {
1264 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1266 $logger->debug("Performing multiclass search with org => $orgid, " .
1267 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1269 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1270 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1273 my $records = $apputils->simplereq(
1274 'open-ils.storage', $meth,
1275 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1280 if( ref($records) and $records->[0] and
1281 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1283 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1285 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1286 return { ids => $recs, count => $count };
1292 __PACKAGE__->register_method(
1293 method => "multiclass_search",
1294 api_name => "open-ils.search.biblio.multiclass",
1296 Performs a multiclass search
1297 @param args A names hash of arguments:
1298 org_unit : The org to focus the search on
1299 depth : The search depth
1300 format : Item format
1301 limit : Return limit
1302 offset : Search offset
1303 searches : A named hash of searches which has the following format:
1305 "title" : { "term" : "water" },
1306 "author" : { "term" : "smith" },
1309 @return { ids : <array of ids>, count : hitcount }
1313 __PACKAGE__->register_method(
1314 method => "multiclass_search",
1315 api_name => "open-ils.search.biblio.multiclass.staff",
1316 notes => q/@see open-ils.search.biblio.multiclass/ );
1318 sub multiclass_search {
1319 my( $self, $client, $args ) = @_;
1321 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
1322 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1323 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1325 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1330 if( ref($records) and $records->[0] and
1331 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1333 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1335 return { ids => $recs, count => $count };
1342 __PACKAGE__->register_method(
1343 method => "marc_search",
1344 api_name => "open-ils.search.biblio.marc.staff");
1346 __PACKAGE__->register_method(
1347 method => "marc_search",
1348 api_name => "open-ils.search.biblio.marc",
1349 notes => <<" NOTES");
1351 open-ils.storage.biblio.full_rec.multi_search.atomic
1352 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1353 "limit":5,"sort":"author","item_type":"g"}
1357 my( $self, $conn, $args, $limit, $offset ) = @_;
1359 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1360 $method .= ".staff" if $self->api_name =~ /staff/;
1361 $method .= ".atomic";
1367 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1368 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1370 my $recs = search_cache($ckey, $offset, $limit);
1373 $recs = $U->storagereq($method, %$args) || [];
1375 put_cache($ckey, scalar(@$recs), $recs);
1376 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1383 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1384 my @recs = map { $_->[0] } @$recs;
1386 return { ids => \@recs, count => $count };
1390 __PACKAGE__->register_method(
1391 method => "biblio_search_isbn",
1392 api_name => "open-ils.search.biblio.isbn",
1395 sub biblio_search_isbn {
1396 my( $self, $client, $isbn ) = @_;
1397 $logger->debug("Searching ISBN $isbn");
1398 my $e = new_editor();
1399 my $recs = $U->storagereq(
1400 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1401 return { ids => $recs, count => scalar(@$recs) };
1405 __PACKAGE__->register_method(
1406 method => "biblio_search_issn",
1407 api_name => "open-ils.search.biblio.issn",
1410 sub biblio_search_issn {
1411 my( $self, $client, $issn ) = @_;
1412 $logger->debug("Searching ISSN $issn");
1413 my $e = new_editor();
1414 my $recs = $U->storagereq(
1415 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1416 return { ids => $recs, count => scalar(@$recs) };
1422 __PACKAGE__->register_method(
1423 method => "fetch_mods_by_copy",
1424 api_name => "open-ils.search.biblio.mods_from_copy",
1427 sub fetch_mods_by_copy {
1428 my( $self, $client, $copyid ) = @_;
1429 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1430 return $evt if $evt;
1431 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1432 return $apputils->record_to_mvr($record);
1437 # -------------------------------------------------------------------------------------
1439 __PACKAGE__->register_method(
1440 method => "cn_browse",
1441 api_name => "open-ils.search.callnumber.browse.target",
1442 notes => "Starts a callnumber browse"
1445 __PACKAGE__->register_method(
1446 method => "cn_browse",
1447 api_name => "open-ils.search.callnumber.browse.page_up",
1448 notes => "Returns the previous page of callnumbers",
1451 __PACKAGE__->register_method(
1452 method => "cn_browse",
1453 api_name => "open-ils.search.callnumber.browse.page_down",
1454 notes => "Returns the next page of callnumbers",
1458 # RETURNS array of arrays like so: label, owning_lib, record, id
1460 my( $self, $client, @params ) = @_;
1463 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1464 if( $self->api_name =~ /target/ );
1465 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1466 if( $self->api_name =~ /page_up/ );
1467 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1468 if( $self->api_name =~ /page_down/ );
1470 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1472 # -------------------------------------------------------------------------------------
1474 __PACKAGE__->register_method(
1475 method => "fetch_cn",
1477 api_name => "open-ils.search.callnumber.retrieve",
1478 notes => "retrieves a callnumber based on ID",
1482 my( $self, $client, $id ) = @_;
1483 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1484 return $evt if $evt;
1488 __PACKAGE__->register_method (
1489 method => "fetch_copy_by_cn",
1490 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1492 Returns an array of copy id's by callnumber id
1493 @param cnid The callnumber id
1494 @return An array of copy ids
1498 sub fetch_copy_by_cn {
1499 my( $self, $conn, $cnid ) = @_;
1500 return $U->cstorereq(
1501 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1502 { call_number => $cnid, deleted => 'f' } );
1505 __PACKAGE__->register_method (
1506 method => 'fetch_cn_by_info',
1507 api_name => 'open-ils.search.call_number.retrieve_by_info',
1509 @param label The callnumber label
1510 @param record The record the cn is attached to
1511 @param org The owning library of the cn
1512 @return The callnumber object
1517 sub fetch_cn_by_info {
1518 my( $self, $conn, $label, $record, $org ) = @_;
1519 return $U->cstorereq(
1520 'open-ils.cstore.direct.asset.call_number.search',
1521 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1528 __PACKAGE__->register_method (
1529 method => 'bib_extras',
1530 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1531 __PACKAGE__->register_method (
1532 method => 'bib_extras',
1533 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1534 __PACKAGE__->register_method (
1535 method => 'bib_extras',
1536 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1537 __PACKAGE__->register_method (
1538 method => 'bib_extras',
1539 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1544 my $e = new_editor();
1546 return $e->retrieve_all_config_lit_form_map()
1547 if( $self->api_name =~ /lit_form/ );
1549 return $e->retrieve_all_config_item_form_map()
1550 if( $self->api_name =~ /item_form_map/ );
1552 return $e->retrieve_all_config_item_type_map()
1553 if( $self->api_name =~ /item_type_map/ );
1555 return $e->retrieve_all_config_audience_map()
1556 if( $self->api_name =~ /audience_map/ );
1563 __PACKAGE__->register_method(
1564 method => 'fetch_slim_record',
1565 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1567 Returns a biblio.record_entry without the attached marcxml
1571 sub fetch_slim_record {
1572 my( $self, $conn, $ids ) = @_;
1574 #my $editor = OpenILS::Utils::Editor->new;
1575 my $editor = new_editor();
1578 return $editor->event unless
1579 my $r = $editor->retrieve_biblio_record_entry($_);
1588 __PACKAGE__->register_method(
1589 method => 'rec_to_mr_rec_descriptors',
1590 api_name => 'open-ils.search.metabib.record_to_descriptors',
1592 specialized method...
1593 Given a biblio record id or a metarecord id,
1594 this returns a list of metabib.record_descriptor
1595 objects that live within the same metarecord
1596 @param args Object of args including:
1600 sub rec_to_mr_rec_descriptors {
1601 my( $self, $conn, $args ) = @_;
1603 my $rec = $$args{record};
1604 my $mrec = $$args{metarecord};
1605 my $item_forms = $$args{item_forms};
1606 my $item_types = $$args{item_types};
1607 my $item_lang = $$args{item_lang};
1609 my $e = new_editor();
1613 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1614 return $e->event unless @$map;
1615 $mrec = $$map[0]->metarecord;
1618 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1619 return $e->event unless @$recs;
1621 my @recs = map { $_->source } @$recs;
1622 my $search = { record => \@recs };
1623 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1624 $search->{item_type} = $item_types if $item_types and @$item_types;
1625 $search->{item_lang} = $item_lang if $item_lang;
1627 my $desc = $e->search_metabib_record_descriptor($search);
1629 return { metarecord => $mrec, descriptors => $desc };
1635 __PACKAGE__->register_method(
1636 method => 'copies_created_on',
1640 sub copies_created_on {
1641 my( $self, $conn, $auth, $org, $date ) = @_;
1642 my $e = new_editor(authtoken=>$auth);
1643 return $e->event unless $e->checkauth;
1647 __PACKAGE__->register_method(
1648 method => 'fetch_age_protect',
1649 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1652 sub fetch_age_protect {
1653 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1657 __PACKAGE__->register_method(
1658 method => 'copies_by_cn_label',
1659 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1662 __PACKAGE__->register_method(
1663 method => 'copies_by_cn_label',
1664 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1667 sub copies_by_cn_label {
1668 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1669 my $e = new_editor();
1670 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1671 return [] unless @$cns;
1673 # show all non-deleted copies in the staff client ...
1674 if ($self->api_name =~ /staff$/o) {
1675 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1678 # ... otherwise, grab the copies ...
1679 my $copies = $e->search_asset_copy(
1680 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1681 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1685 # ... and test for location and status visibility
1686 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];