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 docache @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;
458 $query =~ s/^\s+//go;
460 # convert convenience classes (e.g. kw for keyword) to the full class name
461 $query =~ s/kw(:|\|)/keyword$1/go;
462 $query =~ s/ti(:|\|)/title$1/go;
463 $query =~ s/au(:|\|)/author$1/go;
464 $query =~ s/su(:|\|)/subject$1/go;
465 $query =~ s/se(:|\|)/series$1/go;
466 $query =~ s/name(:|\|)/author$1/og;
468 $logger->debug("cleansed query string => $query");
469 my $search = $arghash->{searches} = {};
471 while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):[^:]+)$//so) {
472 my($type, $value) = split(':', $1);
473 next unless $type and $value;
475 $value =~ s/^\s*//og;
476 $value =~ s/\s*$//og;
477 $type = 'sort_dir' if $type eq 'dir';
479 if($type eq 'site') {
480 # 'site' is the org shortname. when using this, we also want
481 # to search at the requested org's depth
482 my $e = new_editor();
483 if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
484 $arghash->{org_unit} = $org->id if $org;
485 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
487 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
490 } elsif($type eq 'available') {
492 $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
494 } elsif($type eq 'lang') {
495 # collect languages into an array of languages
496 $arghash->{language} = [] unless $arghash->{language};
497 push(@{$arghash->{language}}, $value);
499 } elsif($type =~ /^sort/o) {
500 # sort and sort_dir modifiers
501 $arghash->{$type} = $value;
504 # append the search term to the term under construction
505 $search->{$type} = {} unless $search->{$type};
506 $search->{$type}->{term} =
507 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
512 # This is the front part of the string before any special tokens were parsed.
513 # Add this data to the default search class
514 my $type = $arghash->{default_class} || 'keyword';
515 $search->{$type} = {} unless $search->{$type};
516 $search->{$type}->{term} =
517 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $query" : $query;
520 # capture the original limit because the search method alters the limit internally
521 my $ol = $arghash->{limit};
523 my $sclient = OpenSRF::Utils::SettingsClient->new;
525 (my $method = $self->api_name) =~ s/\.query//o;
527 $method =~ s/multiclass/multiclass.staged/
528 if $sclient->config_value(apps => 'open-ils.search',
529 app_settings => 'use_staged_search') =~ /true/i;
532 $method = $self->method_lookup($method);
533 my ($data) = $method->run($arghash, $docache);
535 $arghash->{limit} = $ol if $ol;
536 $data->{compiled_search} = $arghash;
537 $data->{query} = $orig_query;
539 $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
544 __PACKAGE__->register_method(
545 method => 'cat_search_z_style_wrapper',
546 api_name => 'open-ils.search.biblio.zstyle',
548 signature => q/@see open-ils.search.biblio.multiclass/);
550 sub cat_search_z_style_wrapper {
553 my $authtoken = shift;
556 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
558 my $ou = $cstore->request(
559 'open-ils.cstore.direct.actor.org_unit.search',
560 { parent_ou => undef }
563 my $result = { service => 'native-evergreen-catalog', records => [] };
564 my $searchhash = { limit => $$args{limit}, offset => $$args{offset}, org_unit => $ou->id };
566 $$searchhash{searches}{title}{term} = $$args{search}{title} if $$args{search}{title};
567 $$searchhash{searches}{author}{term} = $$args{search}{author} if $$args{search}{author};
568 $$searchhash{searches}{subject}{term} = $$args{search}{subject} if $$args{search}{subject};
569 $$searchhash{searches}{keyword}{term} = $$args{search}{keyword} if $$args{search}{keyword};
571 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{tcn} if $$args{search}{tcn};
572 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{isbn} if $$args{search}{isbn};
573 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{issn} if $$args{search}{issn};
574 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{publisher} if $$args{search}{publisher};
575 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{pubdate} if $$args{search}{pubdate};
576 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{item_type} if $$args{search}{item_type};
578 my $list = the_quest_for_knowledge( $self, $client, $searchhash );
580 if ($list->{count} > 0) {
581 $result->{count} = $list->{count};
583 my $records = $cstore->request(
584 'open-ils.cstore.direct.biblio.record_entry.search.atomic',
585 { id => [ map { ( $_->[0] ) } @{$list->{ids}} ] }
588 for my $rec ( @$records ) {
590 my $u = OpenILS::Utils::ModsParser->new();
591 $u->start_mods_batch( $rec->marc );
592 my $mods = $u->finish_mods_batch();
594 push @{ $result->{records} }, { mvr => $mods, marcxml => $rec->marc, bibid => $rec->id };
600 $cstore->disconnect();
604 # ----------------------------------------------------------------------------
605 # These are the main OPAC search methods
606 # ----------------------------------------------------------------------------
608 __PACKAGE__->register_method(
609 method => 'the_quest_for_knowledge',
610 api_name => 'open-ils.search.biblio.multiclass',
612 Performs a multi class bilbli or metabib search
613 @param searchhash A search object layed out like so:
614 searches : { "$class" : "$value", ...}
615 org_unit : The org id to focus the search at
616 depth : The org depth
617 limit : The search limit
618 offset : The search offset
619 format : The MARC format
620 sort : What field to sort the results on [ author | title | pubdate ]
621 sort_dir : What direction do we sort? [ asc | desc ]
622 @return An object of the form
623 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
627 __PACKAGE__->register_method(
628 method => 'the_quest_for_knowledge',
629 api_name => 'open-ils.search.biblio.multiclass.staff',
630 signature => q/@see open-ils.search.biblio.multiclass/);
631 __PACKAGE__->register_method(
632 method => 'the_quest_for_knowledge',
633 api_name => 'open-ils.search.metabib.multiclass',
634 signature => q/@see open-ils.search.biblio.multiclass/);
635 __PACKAGE__->register_method(
636 method => 'the_quest_for_knowledge',
637 api_name => 'open-ils.search.metabib.multiclass.staff',
638 signature => q/@see open-ils.search.biblio.multiclass/);
640 sub the_quest_for_knowledge {
641 my( $self, $conn, $searchhash, $docache ) = @_;
643 return { count => 0 } unless $searchhash and
644 ref $searchhash->{searches} eq 'HASH';
646 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
650 if($self->api_name =~ /metabib/) {
652 $method =~ s/biblio/metabib/o;
656 my $offset = $searchhash->{offset} || 0;
657 my $limit = $searchhash->{limit} || 10;
658 my $end = $offset + $limit - 1;
660 # do some simple sanity checking
661 if(!$searchhash->{searches} or
662 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
663 return { count => 0 };
668 $searchhash->{offset} = 0;
669 $searchhash->{limit} = $maxlimit;
671 return { count => 0 } if $offset > $maxlimit;
674 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
675 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
676 my $ckey = $pfx . md5_hex($method . $s);
678 $logger->info("bib search for: $s");
680 $searchhash->{limit} -= $offset;
684 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
688 $method .= ".staff" if($self->api_name =~ /staff/);
689 $method .= ".atomic";
691 for (keys %$searchhash) {
692 delete $$searchhash{$_}
693 unless defined $$searchhash{$_};
696 $result = $U->storagereq( $method, %$searchhash );
703 return {count => 0} unless ($result && $$result[0]);
707 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
710 # If we didn't get this data from the cache, put it into the cache
711 # then return the correct offset of records
712 $logger->debug("putting search cache $ckey\n");
713 put_cache($ckey, $count, \@recs);
717 # if we have the full set of data, trim out
718 # the requested chunk based on limit and offset
720 for ($offset..$end) {
721 last unless $recs[$_];
727 return { ids => \@recs, count => $count };
731 __PACKAGE__->register_method(
732 method => 'staged_search',
733 api_name => 'open-ils.search.biblio.multiclass.staged');
734 __PACKAGE__->register_method(
735 method => 'staged_search',
736 api_name => 'open-ils.search.biblio.multiclass.staged.staff',
737 signature => q/@see open-ils.search.biblio.multiclass.staged/);
738 __PACKAGE__->register_method(
739 method => 'staged_search',
740 api_name => 'open-ils.search.metabib.multiclass.staged',
741 signature => q/@see open-ils.search.biblio.multiclass.staged/);
742 __PACKAGE__->register_method(
743 method => 'staged_search',
744 api_name => 'open-ils.search.metabib.multiclass.staged.staff',
745 signature => q/@see open-ils.search.biblio.multiclass.staged/);
747 my $PAGE_SIZE = 1000;
748 my $SEARCH_PAGES = 25;
750 my($self, $conn, $search_hash, $docache) = @_;
752 my $method = ($self->api_name =~ /metabib/) ?
753 'open-ils.storage.metabib.multiclass.staged.search_fts':
754 'open-ils.storage.biblio.multiclass.staged.search_fts';
756 $method .= '.staff' if $self->api_name =~ /staff$/;
757 $method .= '.atomic';
759 my $user_offset = $search_hash->{offset} || 0; # user-specified offset
760 my $user_limit = $search_hash->{limit} || 10;
761 $user_offset = ($user_offset >= 0) ? $user_offset : 0;
762 $user_limit = ($user_limit >= 0) ? $user_limit : 10;
765 # we're grabbing results on a per-superpage basis, which means the
766 # limit and offset should coincide with superpage boundaries
767 $search_hash->{offset} = 0;
768 $search_hash->{limit} = $PAGE_SIZE;
769 $search_hash->{check_limit} = $PAGE_SIZE; # force a well-known check_limit
771 # pull any existing results from the cache
772 my $key = search_cache_key($method, $search_hash);
773 my $cache_data = $cache->get_cache($key) || {};
775 # keep retrieving results until we find enough to
776 # fulfill the user-specified limit and offset
777 my $all_results = [];
778 my $page; # current superpage
779 my $est_hit_count = 0;
781 for($page = 0; $page < $SEARCH_PAGES; $page++) {
783 my $data = $cache_data->{$page};
787 $logger->debug("staged search: analyzing superpage $page");
790 # this window of results is already cached
791 $logger->debug("staged search: found cached results");
792 $summary = $data->{summary};
793 $results = $data->{results};
796 # retrieve the window of results from the database
797 $logger->debug("staged search: fetching results from the database");
798 $search_hash->{skip_check} = $page * $PAGE_SIZE;
800 $results = $U->storagereq($method, %$search_hash);
801 $logger->info("staged search: DB call took ".(time - $start)." seconds");
802 $summary = shift(@$results);
804 # Create backwards-compatible result structures
805 if($self->api_name =~ /biblio/) {
806 $results = [map {[$_->{id}]} @$results];
808 $results = [map {[$_->{id}, $_->{rel}, $_->{record}]} @$results];
811 $results = [grep {defined $_->[0]} @$results];
812 cache_staged_search_page($key, $page, $summary, $results) if $docache;
815 # add the new set of results to the set under construction
816 push(@$all_results, @$results);
818 my $current_count = scalar(@$all_results);
820 $est_hit_count = $summary->{estimated_hit_count} || $summary->{visible}
823 $logger->debug("staged search: located $current_count, with estimated hits=".
824 $summary->{estimated_hit_count}." : visible=".$summary->{visible}.", checked=".$summary->{checked});
826 # we've found all the possible hits
827 last if $current_count == $summary->{visible}
828 and not defined $summary->{estimated_hit_count};
830 # we've found enough results to satisfy the requested limit/offset
831 last if $current_count >= ($user_limit + $user_offset);
833 # we've scanned all possible hits
834 last if $summary->{checked} < $PAGE_SIZE;
837 my @results = grep {defined $_} @$all_results[$user_offset..($user_offset + $user_limit - 1)];
840 count => $est_hit_count,
845 # creates a unique token to represent the query in the cache
846 sub search_cache_key {
848 my $search_hash = shift;
850 for my $key (sort keys %$search_hash) {
851 push(@sorted, ($key => $$search_hash{$key}))
852 unless $key eq 'limit' or
854 $key eq 'skip_check';
856 my $s = OpenSRF::Utils::JSON->perl2JSON(\@sorted);
857 return $pfx . md5_hex($method . $s);
860 sub cache_staged_search_page {
861 # puts this set of results into the cache
862 my($key, $page, $summary, $results) = @_;
863 my $data = $cache->get_cache($key);
870 $logger->info("staged search: cached with key=$key, superpage=$page, estimated=".
871 $summary->{estimated_hit_count}.", visible=".$summary->{visible});
873 $cache->put_cache($key, $data);
882 my $end = $offset + $limit - 1;
884 $logger->debug("searching cache for $key : $start..$end\n");
886 return undef unless $cache;
887 my $data = $cache->get_cache($key);
889 return undef unless $data;
891 my $count = $data->[0];
894 return undef unless $offset < $count;
897 for( my $i = $offset; $i <= $end; $i++ ) {
898 last unless my $d = $$data[$i];
902 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
909 my( $key, $count, $data ) = @_;
910 return undef unless $cache;
911 $logger->debug("search_cache putting ".
912 scalar(@$data)." items at key $key with timeout $cache_timeout");
913 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
921 __PACKAGE__->register_method(
922 method => "biblio_mrid_to_modsbatch_batch",
923 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
925 sub biblio_mrid_to_modsbatch_batch {
926 my( $self, $client, $mrids) = @_;
927 warn "Performing mrid_to_modsbatch_batch...";
929 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
930 for my $id (@$mrids) {
931 next unless defined $id;
932 my ($m) = $method->run($id);
939 __PACKAGE__->register_method(
940 method => "biblio_mrid_to_modsbatch",
941 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
942 notes => <<" NOTES");
943 Returns the mvr associated with a given metarecod. If none exists,
947 __PACKAGE__->register_method(
948 method => "biblio_mrid_to_modsbatch",
949 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
950 notes => <<" NOTES");
951 Returns the mvr associated with a given metarecod. If none exists,
955 sub biblio_mrid_to_modsbatch {
956 my( $self, $client, $mrid, $args) = @_;
958 warn "Grabbing mvr for $mrid\n";
960 my ($mr, $evt) = _grab_metarecord($mrid);
961 return $evt unless $mr;
963 my $mvr = biblio_mrid_check_mvr($self, $client, $mr);
964 $mvr = biblio_mrid_make_modsbatch( $self, $client, $mr ) unless $mvr;
966 return $mvr unless ref($args);
968 # Here we find the lead record appropriate for the given filters
969 # and use that for the title and author of the metarecord
970 my $format = $$args{format};
971 my $org = $$args{org};
972 my $depth = $$args{depth};
974 return $mvr unless $format or $org or $depth;
976 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
977 $method = "$method.staff" if $self->api_name =~ /staff/o;
979 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
981 if( my $mods = $U->record_to_mvr($rec) ) {
983 $mvr->title($mods->title);
984 $mvr->title($mods->author);
985 $logger->debug("mods_slim updating title and ".
986 "author in mvr with ".$mods->title." : ".$mods->author);
992 # converts a metarecord to an mvr
995 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
996 return Fieldmapper::metabib::virtual_record->new($perl);
999 # checks to see if a metarecord has mods, if so returns true;
1001 __PACKAGE__->register_method(
1002 method => "biblio_mrid_check_mvr",
1003 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
1004 notes => <<" NOTES");
1005 Takes a metarecord ID or a metarecord object and returns true
1006 if the metarecord already has an mvr associated with it.
1009 sub biblio_mrid_check_mvr {
1010 my( $self, $client, $mrid ) = @_;
1014 if(ref($mrid)) { $mr = $mrid; }
1015 else { ($mr, $evt) = _grab_metarecord($mrid); }
1016 return $evt if $evt;
1018 warn "Checking mvr for mr " . $mr->id . "\n";
1020 return _mr_to_mvr($mr) if $mr->mods();
1024 sub _grab_metarecord {
1026 #my $e = OpenILS::Utils::Editor->new;
1027 my $e = new_editor();
1028 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
1033 __PACKAGE__->register_method(
1034 method => "biblio_mrid_make_modsbatch",
1035 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
1036 notes => <<" NOTES");
1037 Takes either a metarecord ID or a metarecord object.
1038 Forces the creations of an mvr for the given metarecord.
1039 The created mvr is returned.
1042 sub biblio_mrid_make_modsbatch {
1043 my( $self, $client, $mrid ) = @_;
1045 #my $e = OpenILS::Utils::Editor->new;
1046 my $e = new_editor();
1053 $mr = $e->retrieve_metabib_metarecord($mrid)
1054 or return $e->event;
1057 my $masterid = $mr->master_record;
1058 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
1060 my $ids = $U->storagereq(
1061 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
1062 return undef unless @$ids;
1064 my $master = $e->retrieve_biblio_record_entry($masterid)
1065 or return $e->event;
1067 # start the mods batch
1068 my $u = OpenILS::Utils::ModsParser->new();
1069 $u->start_mods_batch( $master->marc );
1071 # grab all of the sub-records and shove them into the batch
1072 my @ids = grep { $_ ne $masterid } @$ids;
1073 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
1078 my $r = $e->retrieve_biblio_record_entry($i);
1079 push( @$subrecs, $r ) if $r;
1084 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
1085 $u->push_mods_batch( $_->marc ) if $_->marc;
1089 # finish up and send to the client
1090 my $mods = $u->finish_mods_batch();
1091 $mods->doc_id($mrid);
1092 $client->respond_complete($mods);
1095 # now update the mods string in the db
1096 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
1099 #$e = OpenILS::Utils::Editor->new(xact => 1);
1100 $e = new_editor(xact => 1);
1101 $e->update_metabib_metarecord($mr)
1102 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
1111 # converts a mr id into a list of record ids
1113 __PACKAGE__->register_method(
1114 method => "biblio_mrid_to_record_ids",
1115 api_name => "open-ils.search.biblio.metarecord_to_records",
1118 __PACKAGE__->register_method(
1119 method => "biblio_mrid_to_record_ids",
1120 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
1123 sub biblio_mrid_to_record_ids {
1124 my( $self, $client, $mrid, $args ) = @_;
1126 my $format = $$args{format};
1127 my $org = $$args{org};
1128 my $depth = $$args{depth};
1130 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
1131 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
1132 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
1134 return { count => scalar(@$recs), ids => $recs };
1138 __PACKAGE__->register_method(
1139 method => "biblio_record_to_marc_html",
1140 api_name => "open-ils.search.biblio.record.html" );
1142 my $parser = XML::LibXML->new();
1143 my $xslt = XML::LibXSLT->new();
1146 my $settings_client = OpenSRF::Utils::SettingsClient->new();
1147 sub biblio_record_to_marc_html {
1148 my( $self, $client, $recordid ) = @_;
1150 if( !$marc_sheet ) {
1151 my $dir = $settings_client->config_value( "dirs", "xsl" );
1152 my $xsl = $settings_client->config_value(
1153 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
1155 $xsl = $parser->parse_file("$dir/$xsl");
1156 $marc_sheet = $xslt->parse_stylesheet( $xsl );
1160 my $record = $apputils->simple_scalar_request(
1162 "open-ils.cstore.direct.biblio.record_entry.retrieve",
1165 my $xmldoc = $parser->parse_string($record->marc);
1166 my $html = $marc_sheet->transform($xmldoc);
1167 $html = $html->toString();
1174 __PACKAGE__->register_method(
1175 method => "retrieve_all_copy_locations",
1176 api_name => "open-ils.search.config.copy_location.retrieve.all" );
1178 my $shelving_locations;
1179 sub retrieve_all_copy_locations {
1180 my( $self, $client ) = @_;
1181 if(!$shelving_locations) {
1182 $shelving_locations = $apputils->simple_scalar_request(
1184 "open-ils.cstore.direct.asset.copy_location.search.atomic",
1185 { id => { "!=" => undef } }
1188 return $shelving_locations;
1194 __PACKAGE__->register_method(
1195 method => "retrieve_all_copy_statuses",
1196 api_name => "open-ils.search.config.copy_status.retrieve.all" );
1199 sub retrieve_all_copy_statuses {
1200 my( $self, $client ) = @_;
1201 return $copy_statuses if $copy_statuses;
1202 return $copy_statuses =
1203 new_editor()->retrieve_all_config_copy_status();
1207 __PACKAGE__->register_method(
1208 method => "copy_counts_per_org",
1209 api_name => "open-ils.search.biblio.copy_counts.retrieve");
1211 __PACKAGE__->register_method(
1212 method => "copy_counts_per_org",
1213 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
1215 sub copy_counts_per_org {
1216 my( $self, $client, $record_id ) = @_;
1218 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1220 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1221 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1223 my $counts = $apputils->simple_scalar_request(
1224 "open-ils.storage", $method, $record_id );
1226 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1231 __PACKAGE__->register_method(
1232 method => "copy_count_summary",
1233 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1234 notes => <<" NOTES");
1235 returns an array of these:
1236 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1237 where statusx is a copy status name. the statuses are sorted
1241 sub copy_count_summary {
1242 my( $self, $client, $rid, $org, $depth ) = @_;
1245 my $data = $U->storagereq(
1246 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1248 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1254 __PACKAGE__->register_method(
1255 method => "multiclass_search",
1256 api_name => "open-ils.search.biblio.multiclass",
1257 notes => <<" NOTES");
1258 Performs a multiclass search
1259 PARAMS( searchBlob, org_unit, format, limit )
1260 where searchBlob is defined like this:
1262 "title" : { "term" : "water" },
1263 "author" : { "term" : "smith" },
1268 __PACKAGE__->register_method(
1269 method => "multiclass_search",
1270 api_name => "open-ils.search.biblio.multiclass.staff",
1271 notes => "see open-ils.search.biblio.multiclass" );
1273 sub multiclass_search {
1274 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1276 $logger->debug("Performing multiclass search with org => $orgid, " .
1277 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1279 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1280 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1283 my $records = $apputils->simplereq(
1284 'open-ils.storage', $meth,
1285 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1290 if( ref($records) and $records->[0] and
1291 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1293 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1295 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1296 return { ids => $recs, count => $count };
1302 __PACKAGE__->register_method(
1303 method => "multiclass_search",
1304 api_name => "open-ils.search.biblio.multiclass",
1306 Performs a multiclass search
1307 @param args A names hash of arguments:
1308 org_unit : The org to focus the search on
1309 depth : The search depth
1310 format : Item format
1311 limit : Return limit
1312 offset : Search offset
1313 searches : A named hash of searches which has the following format:
1315 "title" : { "term" : "water" },
1316 "author" : { "term" : "smith" },
1319 @return { ids : <array of ids>, count : hitcount }
1323 __PACKAGE__->register_method(
1324 method => "multiclass_search",
1325 api_name => "open-ils.search.biblio.multiclass.staff",
1326 notes => q/@see open-ils.search.biblio.multiclass/ );
1328 sub multiclass_search {
1329 my( $self, $client, $args ) = @_;
1331 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
1332 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1333 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1335 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1340 if( ref($records) and $records->[0] and
1341 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1343 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1345 return { ids => $recs, count => $count };
1352 __PACKAGE__->register_method(
1353 method => "marc_search",
1354 api_name => "open-ils.search.biblio.marc.staff");
1356 __PACKAGE__->register_method(
1357 method => "marc_search",
1358 api_name => "open-ils.search.biblio.marc",
1359 notes => <<" NOTES");
1361 open-ils.storage.biblio.full_rec.multi_search.atomic
1362 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1363 "limit":5,"sort":"author","item_type":"g"}
1367 my( $self, $conn, $args, $limit, $offset ) = @_;
1369 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1370 $method .= ".staff" if $self->api_name =~ /staff/;
1371 $method .= ".atomic";
1377 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1378 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1380 my $recs = search_cache($ckey, $offset, $limit);
1383 $recs = $U->storagereq($method, %$args) || [];
1385 put_cache($ckey, scalar(@$recs), $recs);
1386 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1393 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1394 my @recs = map { $_->[0] } @$recs;
1396 return { ids => \@recs, count => $count };
1400 __PACKAGE__->register_method(
1401 method => "biblio_search_isbn",
1402 api_name => "open-ils.search.biblio.isbn",
1405 sub biblio_search_isbn {
1406 my( $self, $client, $isbn ) = @_;
1407 $logger->debug("Searching ISBN $isbn");
1408 my $e = new_editor();
1409 my $recs = $U->storagereq(
1410 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1411 return { ids => $recs, count => scalar(@$recs) };
1415 __PACKAGE__->register_method(
1416 method => "biblio_search_issn",
1417 api_name => "open-ils.search.biblio.issn",
1420 sub biblio_search_issn {
1421 my( $self, $client, $issn ) = @_;
1422 $logger->debug("Searching ISSN $issn");
1423 my $e = new_editor();
1424 my $recs = $U->storagereq(
1425 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1426 return { ids => $recs, count => scalar(@$recs) };
1432 __PACKAGE__->register_method(
1433 method => "fetch_mods_by_copy",
1434 api_name => "open-ils.search.biblio.mods_from_copy",
1437 sub fetch_mods_by_copy {
1438 my( $self, $client, $copyid ) = @_;
1439 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1440 return $evt if $evt;
1441 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1442 return $apputils->record_to_mvr($record);
1447 # -------------------------------------------------------------------------------------
1449 __PACKAGE__->register_method(
1450 method => "cn_browse",
1451 api_name => "open-ils.search.callnumber.browse.target",
1452 notes => "Starts a callnumber browse"
1455 __PACKAGE__->register_method(
1456 method => "cn_browse",
1457 api_name => "open-ils.search.callnumber.browse.page_up",
1458 notes => "Returns the previous page of callnumbers",
1461 __PACKAGE__->register_method(
1462 method => "cn_browse",
1463 api_name => "open-ils.search.callnumber.browse.page_down",
1464 notes => "Returns the next page of callnumbers",
1468 # RETURNS array of arrays like so: label, owning_lib, record, id
1470 my( $self, $client, @params ) = @_;
1473 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1474 if( $self->api_name =~ /target/ );
1475 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1476 if( $self->api_name =~ /page_up/ );
1477 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1478 if( $self->api_name =~ /page_down/ );
1480 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1482 # -------------------------------------------------------------------------------------
1484 __PACKAGE__->register_method(
1485 method => "fetch_cn",
1487 api_name => "open-ils.search.callnumber.retrieve",
1488 notes => "retrieves a callnumber based on ID",
1492 my( $self, $client, $id ) = @_;
1493 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1494 return $evt if $evt;
1498 __PACKAGE__->register_method (
1499 method => "fetch_copy_by_cn",
1500 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1502 Returns an array of copy id's by callnumber id
1503 @param cnid The callnumber id
1504 @return An array of copy ids
1508 sub fetch_copy_by_cn {
1509 my( $self, $conn, $cnid ) = @_;
1510 return $U->cstorereq(
1511 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1512 { call_number => $cnid, deleted => 'f' } );
1515 __PACKAGE__->register_method (
1516 method => 'fetch_cn_by_info',
1517 api_name => 'open-ils.search.call_number.retrieve_by_info',
1519 @param label The callnumber label
1520 @param record The record the cn is attached to
1521 @param org The owning library of the cn
1522 @return The callnumber object
1527 sub fetch_cn_by_info {
1528 my( $self, $conn, $label, $record, $org ) = @_;
1529 return $U->cstorereq(
1530 'open-ils.cstore.direct.asset.call_number.search',
1531 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1538 __PACKAGE__->register_method (
1539 method => 'bib_extras',
1540 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1541 __PACKAGE__->register_method (
1542 method => 'bib_extras',
1543 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1544 __PACKAGE__->register_method (
1545 method => 'bib_extras',
1546 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1547 __PACKAGE__->register_method (
1548 method => 'bib_extras',
1549 api_name => 'open-ils.search.biblio.bib_level_map.retrieve.all');
1550 __PACKAGE__->register_method (
1551 method => 'bib_extras',
1552 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1557 my $e = new_editor();
1559 return $e->retrieve_all_config_lit_form_map()
1560 if( $self->api_name =~ /lit_form/ );
1562 return $e->retrieve_all_config_item_form_map()
1563 if( $self->api_name =~ /item_form_map/ );
1565 return $e->retrieve_all_config_item_type_map()
1566 if( $self->api_name =~ /item_type_map/ );
1568 return $e->retrieve_all_config_bib_level_map()
1569 if( $self->api_name =~ /bib_level_map/ );
1571 return $e->retrieve_all_config_audience_map()
1572 if( $self->api_name =~ /audience_map/ );
1579 __PACKAGE__->register_method(
1580 method => 'fetch_slim_record',
1581 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1583 Returns a biblio.record_entry without the attached marcxml
1587 sub fetch_slim_record {
1588 my( $self, $conn, $ids ) = @_;
1590 #my $editor = OpenILS::Utils::Editor->new;
1591 my $editor = new_editor();
1594 return $editor->event unless
1595 my $r = $editor->retrieve_biblio_record_entry($_);
1604 __PACKAGE__->register_method(
1605 method => 'rec_to_mr_rec_descriptors',
1606 api_name => 'open-ils.search.metabib.record_to_descriptors',
1608 specialized method...
1609 Given a biblio record id or a metarecord id,
1610 this returns a list of metabib.record_descriptor
1611 objects that live within the same metarecord
1612 @param args Object of args including:
1616 sub rec_to_mr_rec_descriptors {
1617 my( $self, $conn, $args ) = @_;
1619 my $rec = $$args{record};
1620 my $mrec = $$args{metarecord};
1621 my $item_forms = $$args{item_forms};
1622 my $item_types = $$args{item_types};
1623 my $item_lang = $$args{item_lang};
1625 my $e = new_editor();
1629 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1630 return $e->event unless @$map;
1631 $mrec = $$map[0]->metarecord;
1634 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1635 return $e->event unless @$recs;
1637 my @recs = map { $_->source } @$recs;
1638 my $search = { record => \@recs };
1639 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1640 $search->{item_type} = $item_types if $item_types and @$item_types;
1641 $search->{item_lang} = $item_lang if $item_lang;
1643 my $desc = $e->search_metabib_record_descriptor($search);
1645 return { metarecord => $mrec, descriptors => $desc };
1651 __PACKAGE__->register_method(
1652 method => 'copies_created_on',
1656 sub copies_created_on {
1657 my( $self, $conn, $auth, $org, $date ) = @_;
1658 my $e = new_editor(authtoken=>$auth);
1659 return $e->event unless $e->checkauth;
1663 __PACKAGE__->register_method(
1664 method => 'fetch_age_protect',
1665 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1668 sub fetch_age_protect {
1669 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1673 __PACKAGE__->register_method(
1674 method => 'copies_by_cn_label',
1675 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1678 __PACKAGE__->register_method(
1679 method => 'copies_by_cn_label',
1680 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1683 sub copies_by_cn_label {
1684 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1685 my $e = new_editor();
1686 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1687 return [] unless @$cns;
1689 # show all non-deleted copies in the staff client ...
1690 if ($self->api_name =~ /staff$/o) {
1691 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1694 # ... otherwise, grab the copies ...
1695 my $copies = $e->search_asset_copy(
1696 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1697 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1701 # ... and test for location and status visibility
1702 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];