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;
136 # Returns the number of copies attached to a record based on org location
137 __PACKAGE__->register_method(
138 method => "record_id_to_copy_count",
139 api_name => "open-ils.search.biblio.record.copy_count",
142 __PACKAGE__->register_method(
143 method => "record_id_to_copy_count",
145 api_name => "open-ils.search.biblio.record.copy_count.staff",
148 __PACKAGE__->register_method(
149 method => "record_id_to_copy_count",
150 api_name => "open-ils.search.biblio.metarecord.copy_count",
153 __PACKAGE__->register_method(
154 method => "record_id_to_copy_count",
155 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
157 sub record_id_to_copy_count {
158 my( $self, $client, $org_id, $record_id, $format ) = @_;
160 return [] unless $record_id;
161 $format = undef if (!$format or $format eq 'all');
163 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
166 if($self->api_name =~ /metarecord/) {
167 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
171 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
173 my $count = $U->storagereq( $method,
174 org_unit => $org_id, $key => $record_id, format => $format );
176 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
182 __PACKAGE__->register_method(
183 method => "biblio_search_tcn",
184 api_name => "open-ils.search.biblio.tcn",
186 note => "Retrieve a record by TCN",
189 sub biblio_search_tcn {
191 my( $self, $client, $tcn, $include_deleted ) = @_;
193 $tcn =~ s/.*?(\w+)\s*$/$1/o;
195 my $e = new_editor();
196 my $search = {tcn_value => $tcn};
197 $search->{deleted} = 'f' unless $include_deleted;
198 my $recs = $e->search_biblio_record_entry( $search, {idlist =>1} );
200 return { count => scalar(@$recs), ids => $recs };
204 # --------------------------------------------------------------------------------
206 __PACKAGE__->register_method(
207 method => "biblio_barcode_to_copy",
208 api_name => "open-ils.search.asset.copy.find_by_barcode",);
209 sub biblio_barcode_to_copy {
210 my( $self, $client, $barcode ) = @_;
211 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
216 __PACKAGE__->register_method(
217 method => "biblio_id_to_copy",
218 api_name => "open-ils.search.asset.copy.batch.retrieve",);
219 sub biblio_id_to_copy {
220 my( $self, $client, $ids ) = @_;
221 $logger->info("Fetching copies @$ids");
222 return $U->cstorereq(
223 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
227 __PACKAGE__->register_method(
228 method => "copy_retrieve",
229 api_name => "open-ils.search.asset.copy.retrieve",);
231 my( $self, $client, $cid ) = @_;
232 my( $copy, $evt ) = $U->fetch_copy($cid);
237 __PACKAGE__->register_method(
238 method => "volume_retrieve",
239 api_name => "open-ils.search.asset.call_number.retrieve");
240 sub volume_retrieve {
241 my( $self, $client, $vid ) = @_;
242 my $e = new_editor();
243 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
247 __PACKAGE__->register_method(
248 method => "fleshed_copy_retrieve_batch",
250 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
252 sub fleshed_copy_retrieve_batch {
253 my( $self, $client, $ids ) = @_;
254 $logger->info("Fetching fleshed copies @$ids");
255 return $U->cstorereq(
256 "open-ils.cstore.direct.asset.copy.search.atomic",
259 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
264 __PACKAGE__->register_method(
265 method => "fleshed_copy_retrieve",
266 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
268 sub fleshed_copy_retrieve {
269 my( $self, $client, $id ) = @_;
270 my( $c, $e) = $U->fetch_fleshed_copy($id);
277 __PACKAGE__->register_method(
278 method => 'fleshed_by_barcode',
279 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",);
280 sub fleshed_by_barcode {
281 my( $self, $conn, $barcode ) = @_;
282 my $e = new_editor();
283 my $copyid = $e->search_asset_copy(
284 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
286 return fleshed_copy_retrieve2( $self, $conn, $copyid);
290 __PACKAGE__->register_method(
291 method => "fleshed_copy_retrieve2",
292 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
294 sub fleshed_copy_retrieve2 {
295 my( $self, $client, $id ) = @_;
296 my $e = new_editor();
297 my $copy = $e->retrieve_asset_copy(
303 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
304 ascecm => [ qw/ stat_cat stat_cat_entry / ],
308 ) or return $e->event;
310 # For backwards compatibility
311 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
313 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
315 $e->search_action_circulation(
317 { target_copy => $copy->id },
319 order_by => { circ => 'xact_start desc' },
331 __PACKAGE__->register_method(
332 method => 'flesh_copy_custom',
333 api_name => 'open-ils.search.asset.copy.fleshed.custom'
336 sub flesh_copy_custom {
337 my( $self, $conn, $copyid, $fields ) = @_;
338 my $e = new_editor();
339 my $copy = $e->retrieve_asset_copy(
349 ) or return $e->event;
358 __PACKAGE__->register_method(
359 method => "biblio_barcode_to_title",
360 api_name => "open-ils.search.biblio.find_by_barcode",
363 sub biblio_barcode_to_title {
364 my( $self, $client, $barcode ) = @_;
366 my $title = $apputils->simple_scalar_request(
368 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
370 return { ids => [ $title->id ], count => 1 } if $title;
371 return { count => 0 };
374 __PACKAGE__->register_method(
375 method => 'title_id_by_item_barcode',
376 api_name => 'open-ils.search.bib_id.by_barcode'
379 sub title_id_by_item_barcode {
380 my( $self, $conn, $barcode ) = @_;
381 my $e = new_editor();
382 my $copies = $e->search_asset_copy(
384 { deleted => 'f', barcode => $barcode },
388 acp => [ 'call_number' ],
395 return $e->event unless @$copies;
396 return $$copies[0]->call_number->record->id;
400 __PACKAGE__->register_method(
401 method => "biblio_copy_to_mods",
402 api_name => "open-ils.search.biblio.copy.mods.retrieve",
405 # takes a copy object and returns it fleshed mods object
406 sub biblio_copy_to_mods {
407 my( $self, $client, $copy ) = @_;
409 my $volume = $U->cstorereq(
410 "open-ils.cstore.direct.asset.call_number.retrieve",
411 $copy->call_number() );
413 my $mods = _records_to_mods($volume->record());
414 $mods = shift @$mods;
415 $volume->copies([$copy]);
416 push @{$mods->call_numbers()}, $volume;
422 __PACKAGE__->register_method(
423 api_name => 'open-ils.search.biblio.multiclass.query',
424 method => 'multiclass_query',
426 @param arghash @see open-ils.search.biblio.multiclass
427 @param query Raw human-readable query string.
428 Recognized search keys include:
429 keyword/kw - search keyword(s)
430 author/au/name - search author(s)
431 title/ti - search title
432 subject/su - search subject
433 series/se - search series
434 lang - limit by language (specifiy multiple langs with lang:l1 lang:l2 ...)
435 site - search at specified org unit, corresponds to actor.org_unit.shortname
436 sort - sort type (title, author, pubdate)
437 dir - sort direction (asc, desc)
438 available - if set to anything other than "false" or "0", limits to available items
440 keyword, title, author, subject, and series support additional search
441 subclasses, specified with a "|". For example, "title|proper:gone with the wind"
442 For more, see config.metabib_field
444 @param docache @see open-ils.search.biblio.multiclass
447 __PACKAGE__->register_method(
448 api_name => 'open-ils.search.biblio.multiclass.query.staff',
449 method => 'multiclass_query',
450 signature => '@see open-ils.search.biblio.multiclass.query');
451 __PACKAGE__->register_method(
452 api_name => 'open-ils.search.metabib.multiclass.query',
453 method => 'multiclass_query',
454 signature => '@see open-ils.search.biblio.multiclass.query');
455 __PACKAGE__->register_method(
456 api_name => 'open-ils.search.metabib.multiclass.query.staff',
457 method => 'multiclass_query',
458 signature => '@see open-ils.search.biblio.multiclass.query');
460 sub multiclass_query {
461 my($self, $conn, $arghash, $query, $docache) = @_;
463 $logger->debug("initial search query => $query");
464 my $orig_query = $query;
468 $query =~ s/^\s+//go;
470 # convert convenience classes (e.g. kw for keyword) to the full class name
471 $query =~ s/kw(:|\|)/keyword$1/go;
472 $query =~ s/ti(:|\|)/title$1/go;
473 $query =~ s/au(:|\|)/author$1/go;
474 $query =~ s/su(:|\|)/subject$1/go;
475 $query =~ s/se(:|\|)/series$1/go;
476 $query =~ s/name(:|\|)/author$1/og;
478 $logger->debug("cleansed query string => $query");
479 my $search = $arghash->{searches} = {};
481 while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):[^:]+)$//so) {
482 my($type, $value) = split(':', $1);
483 next unless $type and $value;
485 $value =~ s/^\s*//og;
486 $value =~ s/\s*$//og;
487 $type = 'sort_dir' if $type eq 'dir';
489 if($type eq 'site') {
490 # 'site' is the org shortname. when using this, we also want
491 # to search at the requested org's depth
492 my $e = new_editor();
493 if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
494 $arghash->{org_unit} = $org->id if $org;
495 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
497 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
500 } elsif($type eq 'available') {
502 $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
504 } elsif($type eq 'lang') {
505 # collect languages into an array of languages
506 $arghash->{language} = [] unless $arghash->{language};
507 push(@{$arghash->{language}}, $value);
509 } elsif($type =~ /^sort/o) {
510 # sort and sort_dir modifiers
511 $arghash->{$type} = $value;
514 # append the search term to the term under construction
515 $search->{$type} = {} unless $search->{$type};
516 $search->{$type}->{term} =
517 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
522 # This is the front part of the string before any special tokens were parsed.
523 # Add this data to the default search class
524 my $type = $arghash->{default_class} || 'keyword';
525 $search->{$type} = {} unless $search->{$type};
526 $search->{$type}->{term} =
527 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $query" : $query;
530 # capture the original limit because the search method alters the limit internally
531 my $ol = $arghash->{limit};
533 my $sclient = OpenSRF::Utils::SettingsClient->new;
535 (my $method = $self->api_name) =~ s/\.query//o;
537 $method =~ s/multiclass/multiclass.staged/
538 if $sclient->config_value(apps => 'open-ils.search',
539 app_settings => 'use_staged_search') =~ /true/i;
542 $method = $self->method_lookup($method);
543 my ($data) = $method->run($arghash, $docache);
545 $arghash->{limit} = $ol if $ol;
546 $data->{compiled_search} = $arghash;
547 $data->{query} = $orig_query;
549 $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
554 __PACKAGE__->register_method(
555 method => 'cat_search_z_style_wrapper',
556 api_name => 'open-ils.search.biblio.zstyle',
558 signature => q/@see open-ils.search.biblio.multiclass/);
560 sub cat_search_z_style_wrapper {
563 my $authtoken = shift;
566 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
568 my $ou = $cstore->request(
569 'open-ils.cstore.direct.actor.org_unit.search',
570 { parent_ou => undef }
573 my $result = { service => 'native-evergreen-catalog', records => [] };
574 my $searchhash = { limit => $$args{limit}, offset => $$args{offset}, org_unit => $ou->id };
576 $$searchhash{searches}{title}{term} = $$args{search}{title} if $$args{search}{title};
577 $$searchhash{searches}{author}{term} = $$args{search}{author} if $$args{search}{author};
578 $$searchhash{searches}{subject}{term} = $$args{search}{subject} if $$args{search}{subject};
579 $$searchhash{searches}{keyword}{term} = $$args{search}{keyword} if $$args{search}{keyword};
581 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{tcn} if $$args{search}{tcn};
582 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{isbn} if $$args{search}{isbn};
583 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{issn} if $$args{search}{issn};
584 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{publisher} if $$args{search}{publisher};
585 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{pubdate} if $$args{search}{pubdate};
586 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{item_type} if $$args{search}{item_type};
588 my $list = the_quest_for_knowledge( $self, $client, $searchhash );
590 if ($list->{count} > 0) {
591 $result->{count} = $list->{count};
593 my $records = $cstore->request(
594 'open-ils.cstore.direct.biblio.record_entry.search.atomic',
595 { id => [ map { ( $_->[0] ) } @{$list->{ids}} ] }
598 for my $rec ( @$records ) {
600 my $u = OpenILS::Utils::ModsParser->new();
601 $u->start_mods_batch( $rec->marc );
602 my $mods = $u->finish_mods_batch();
604 push @{ $result->{records} }, { mvr => $mods, marcxml => $rec->marc, bibid => $rec->id };
610 $cstore->disconnect();
614 # ----------------------------------------------------------------------------
615 # These are the main OPAC search methods
616 # ----------------------------------------------------------------------------
618 __PACKAGE__->register_method(
619 method => 'the_quest_for_knowledge',
620 api_name => 'open-ils.search.biblio.multiclass',
622 Performs a multi class bilbli or metabib search
623 @param searchhash A search object layed out like so:
624 searches : { "$class" : "$value", ...}
625 org_unit : The org id to focus the search at
626 depth : The org depth
627 limit : The search limit
628 offset : The search offset
629 format : The MARC format
630 sort : What field to sort the results on [ author | title | pubdate ]
631 sort_dir : What direction do we sort? [ asc | desc ]
632 @return An object of the form
633 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
637 __PACKAGE__->register_method(
638 method => 'the_quest_for_knowledge',
639 api_name => 'open-ils.search.biblio.multiclass.staff',
640 signature => q/@see open-ils.search.biblio.multiclass/);
641 __PACKAGE__->register_method(
642 method => 'the_quest_for_knowledge',
643 api_name => 'open-ils.search.metabib.multiclass',
644 signature => q/@see open-ils.search.biblio.multiclass/);
645 __PACKAGE__->register_method(
646 method => 'the_quest_for_knowledge',
647 api_name => 'open-ils.search.metabib.multiclass.staff',
648 signature => q/@see open-ils.search.biblio.multiclass/);
650 sub the_quest_for_knowledge {
651 my( $self, $conn, $searchhash, $docache ) = @_;
653 return { count => 0 } unless $searchhash and
654 ref $searchhash->{searches} eq 'HASH';
656 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
660 if($self->api_name =~ /metabib/) {
662 $method =~ s/biblio/metabib/o;
666 my $offset = $searchhash->{offset} || 0;
667 my $limit = $searchhash->{limit} || 10;
668 my $end = $offset + $limit - 1;
670 # do some simple sanity checking
671 if(!$searchhash->{searches} or
672 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
673 return { count => 0 };
678 $searchhash->{offset} = 0;
679 $searchhash->{limit} = $maxlimit;
681 return { count => 0 } if $offset > $maxlimit;
684 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
685 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
686 my $ckey = $pfx . md5_hex($method . $s);
688 $logger->info("bib search for: $s");
690 $searchhash->{limit} -= $offset;
694 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
698 $method .= ".staff" if($self->api_name =~ /staff/);
699 $method .= ".atomic";
701 for (keys %$searchhash) {
702 delete $$searchhash{$_}
703 unless defined $$searchhash{$_};
706 $result = $U->storagereq( $method, %$searchhash );
713 return {count => 0} unless ($result && $$result[0]);
717 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
720 # If we didn't get this data from the cache, put it into the cache
721 # then return the correct offset of records
722 $logger->debug("putting search cache $ckey\n");
723 put_cache($ckey, $count, \@recs);
727 # if we have the full set of data, trim out
728 # the requested chunk based on limit and offset
730 for ($offset..$end) {
731 last unless $recs[$_];
737 return { ids => \@recs, count => $count };
741 __PACKAGE__->register_method(
742 method => 'staged_search',
743 api_name => 'open-ils.search.biblio.multiclass.staged');
744 __PACKAGE__->register_method(
745 method => 'staged_search',
746 api_name => 'open-ils.search.biblio.multiclass.staged.staff',
747 signature => q/@see open-ils.search.biblio.multiclass.staged/);
748 __PACKAGE__->register_method(
749 method => 'staged_search',
750 api_name => 'open-ils.search.metabib.multiclass.staged',
751 signature => q/@see open-ils.search.biblio.multiclass.staged/);
752 __PACKAGE__->register_method(
753 method => 'staged_search',
754 api_name => 'open-ils.search.metabib.multiclass.staged.staff',
755 signature => q/@see open-ils.search.biblio.multiclass.staged/);
758 my($self, $conn, $search_hash, $docache) = @_;
760 my $method = ($self->api_name =~ /metabib/) ?
761 'open-ils.storage.metabib.multiclass.staged.search_fts':
762 'open-ils.storage.biblio.multiclass.staged.search_fts';
764 $method .= '.staff' if $self->api_name =~ /staff$/;
765 $method .= '.atomic';
767 my $user_offset = $search_hash->{offset} || 0; # user-specified offset
768 my $user_limit = $search_hash->{limit} || 10;
769 $user_offset = ($user_offset >= 0) ? $user_offset : 0;
770 $user_limit = ($user_limit >= 0) ? $user_limit : 10;
773 # we're grabbing results on a per-superpage basis, which means the
774 # limit and offset should coincide with superpage boundaries
775 $search_hash->{offset} = 0;
776 $search_hash->{limit} = $superpage_size;
778 # force a well-known check_limit
779 $search_hash->{check_limit} = $superpage_size;
780 # restrict total tested to superpage size * number of superpages
781 $search_hash->{core_limit} = $superpage_size * $max_superpages;
783 # pull any existing results from the cache
784 my $key = search_cache_key($method, $search_hash);
785 my $cache_data = $cache->get_cache($key) || {};
787 # keep retrieving results until we find enough to
788 # fulfill the user-specified limit and offset
789 my $all_results = [];
790 my $page; # current superpage
791 my $est_hit_count = 0;
792 my $current_page_summary = {};
794 for($page = 0; $page < $max_superpages; $page++) {
796 my $data = $cache_data->{$page};
800 $logger->debug("staged search: analyzing superpage $page");
803 # this window of results is already cached
804 $logger->debug("staged search: found cached results");
805 $summary = $data->{summary};
806 $results = $data->{results};
809 # retrieve the window of results from the database
810 $logger->debug("staged search: fetching results from the database");
811 $search_hash->{skip_check} = $page * $superpage_size;
813 $results = $U->storagereq($method, %$search_hash);
814 $logger->info("staged search: DB call took ".(time - $start)." seconds");
815 $summary = shift(@$results);
817 # Create backwards-compatible result structures
818 if($self->api_name =~ /biblio/) {
819 $results = [map {[$_->{id}]} @$results];
821 $results = [map {[$_->{id}, $_->{rel}, $_->{record}]} @$results];
824 $results = [grep {defined $_->[0]} @$results];
825 cache_staged_search_page($key, $page, $summary, $results) if $docache;
828 $current_page_summary = $summary;
830 # add the new set of results to the set under construction
831 push(@$all_results, @$results);
833 my $current_count = scalar(@$all_results);
835 $est_hit_count = $summary->{estimated_hit_count} || $summary->{visible}
838 $logger->debug("staged search: located $current_count, with estimated hits=".
839 $summary->{estimated_hit_count}." : visible=".$summary->{visible}.", checked=".$summary->{checked});
841 # we've found all the possible hits
842 last if $current_count == $summary->{visible}
843 and not defined $summary->{estimated_hit_count};
845 # we've found enough results to satisfy the requested limit/offset
846 last if $current_count >= ($user_limit + $user_offset);
848 # we've scanned all possible hits
849 last if $summary->{checked} < $superpage_size;
852 my @results = grep {defined $_} @$all_results[$user_offset..($user_offset + $user_limit - 1)];
855 count => $est_hit_count,
856 core_limit => $search_hash->{core_limit},
857 superpage_size => $search_hash->{check_limit},
858 superpage_summary => $current_page_summary,
863 # creates a unique token to represent the query in the cache
864 sub search_cache_key {
866 my $search_hash = shift;
868 for my $key (sort keys %$search_hash) {
869 push(@sorted, ($key => $$search_hash{$key}))
870 unless $key eq 'limit' or
872 $key eq 'skip_check';
874 my $s = OpenSRF::Utils::JSON->perl2JSON(\@sorted);
875 return $pfx . md5_hex($method . $s);
878 sub cache_staged_search_page {
879 # puts this set of results into the cache
880 my($key, $page, $summary, $results) = @_;
881 my $data = $cache->get_cache($key);
888 $logger->info("staged search: cached with key=$key, superpage=$page, estimated=".
889 $summary->{estimated_hit_count}.", visible=".$summary->{visible});
891 $cache->put_cache($key, $data);
900 my $end = $offset + $limit - 1;
902 $logger->debug("searching cache for $key : $start..$end\n");
904 return undef unless $cache;
905 my $data = $cache->get_cache($key);
907 return undef unless $data;
909 my $count = $data->[0];
912 return undef unless $offset < $count;
915 for( my $i = $offset; $i <= $end; $i++ ) {
916 last unless my $d = $$data[$i];
920 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
927 my( $key, $count, $data ) = @_;
928 return undef unless $cache;
929 $logger->debug("search_cache putting ".
930 scalar(@$data)." items at key $key with timeout $cache_timeout");
931 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
939 __PACKAGE__->register_method(
940 method => "biblio_mrid_to_modsbatch_batch",
941 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
943 sub biblio_mrid_to_modsbatch_batch {
944 my( $self, $client, $mrids) = @_;
945 warn "Performing mrid_to_modsbatch_batch...";
947 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
948 for my $id (@$mrids) {
949 next unless defined $id;
950 my ($m) = $method->run($id);
957 __PACKAGE__->register_method(
958 method => "biblio_mrid_to_modsbatch",
959 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
960 notes => <<" NOTES");
961 Returns the mvr associated with a given metarecod. If none exists,
965 __PACKAGE__->register_method(
966 method => "biblio_mrid_to_modsbatch",
967 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
968 notes => <<" NOTES");
969 Returns the mvr associated with a given metarecod. If none exists,
973 sub biblio_mrid_to_modsbatch {
974 my( $self, $client, $mrid, $args) = @_;
976 warn "Grabbing mvr for $mrid\n";
978 my ($mr, $evt) = _grab_metarecord($mrid);
979 return $evt unless $mr;
981 my $mvr = biblio_mrid_check_mvr($self, $client, $mr);
982 $mvr = biblio_mrid_make_modsbatch( $self, $client, $mr ) unless $mvr;
984 return $mvr unless ref($args);
986 # Here we find the lead record appropriate for the given filters
987 # and use that for the title and author of the metarecord
988 my $format = $$args{format};
989 my $org = $$args{org};
990 my $depth = $$args{depth};
992 return $mvr unless $format or $org or $depth;
994 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
995 $method = "$method.staff" if $self->api_name =~ /staff/o;
997 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
999 if( my $mods = $U->record_to_mvr($rec) ) {
1001 $mvr->title($mods->title);
1002 $mvr->title($mods->author);
1003 $logger->debug("mods_slim updating title and ".
1004 "author in mvr with ".$mods->title." : ".$mods->author);
1010 # converts a metarecord to an mvr
1013 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
1014 return Fieldmapper::metabib::virtual_record->new($perl);
1017 # checks to see if a metarecord has mods, if so returns true;
1019 __PACKAGE__->register_method(
1020 method => "biblio_mrid_check_mvr",
1021 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
1022 notes => <<" NOTES");
1023 Takes a metarecord ID or a metarecord object and returns true
1024 if the metarecord already has an mvr associated with it.
1027 sub biblio_mrid_check_mvr {
1028 my( $self, $client, $mrid ) = @_;
1032 if(ref($mrid)) { $mr = $mrid; }
1033 else { ($mr, $evt) = _grab_metarecord($mrid); }
1034 return $evt if $evt;
1036 warn "Checking mvr for mr " . $mr->id . "\n";
1038 return _mr_to_mvr($mr) if $mr->mods();
1042 sub _grab_metarecord {
1044 #my $e = OpenILS::Utils::Editor->new;
1045 my $e = new_editor();
1046 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
1051 __PACKAGE__->register_method(
1052 method => "biblio_mrid_make_modsbatch",
1053 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
1054 notes => <<" NOTES");
1055 Takes either a metarecord ID or a metarecord object.
1056 Forces the creations of an mvr for the given metarecord.
1057 The created mvr is returned.
1060 sub biblio_mrid_make_modsbatch {
1061 my( $self, $client, $mrid ) = @_;
1063 #my $e = OpenILS::Utils::Editor->new;
1064 my $e = new_editor();
1071 $mr = $e->retrieve_metabib_metarecord($mrid)
1072 or return $e->event;
1075 my $masterid = $mr->master_record;
1076 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
1078 my $ids = $U->storagereq(
1079 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
1080 return undef unless @$ids;
1082 my $master = $e->retrieve_biblio_record_entry($masterid)
1083 or return $e->event;
1085 # start the mods batch
1086 my $u = OpenILS::Utils::ModsParser->new();
1087 $u->start_mods_batch( $master->marc );
1089 # grab all of the sub-records and shove them into the batch
1090 my @ids = grep { $_ ne $masterid } @$ids;
1091 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
1096 my $r = $e->retrieve_biblio_record_entry($i);
1097 push( @$subrecs, $r ) if $r;
1102 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
1103 $u->push_mods_batch( $_->marc ) if $_->marc;
1107 # finish up and send to the client
1108 my $mods = $u->finish_mods_batch();
1109 $mods->doc_id($mrid);
1110 $client->respond_complete($mods);
1113 # now update the mods string in the db
1114 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
1117 #$e = OpenILS::Utils::Editor->new(xact => 1);
1118 $e = new_editor(xact => 1);
1119 $e->update_metabib_metarecord($mr)
1120 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
1129 # converts a mr id into a list of record ids
1131 __PACKAGE__->register_method(
1132 method => "biblio_mrid_to_record_ids",
1133 api_name => "open-ils.search.biblio.metarecord_to_records",
1136 __PACKAGE__->register_method(
1137 method => "biblio_mrid_to_record_ids",
1138 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
1141 sub biblio_mrid_to_record_ids {
1142 my( $self, $client, $mrid, $args ) = @_;
1144 my $format = $$args{format};
1145 my $org = $$args{org};
1146 my $depth = $$args{depth};
1148 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
1149 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
1150 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
1152 return { count => scalar(@$recs), ids => $recs };
1156 __PACKAGE__->register_method(
1157 method => "biblio_record_to_marc_html",
1158 api_name => "open-ils.search.biblio.record.html" );
1160 my $parser = XML::LibXML->new();
1161 my $xslt = XML::LibXSLT->new();
1164 my $settings_client = OpenSRF::Utils::SettingsClient->new();
1165 sub biblio_record_to_marc_html {
1166 my( $self, $client, $recordid ) = @_;
1168 if( !$marc_sheet ) {
1169 my $dir = $settings_client->config_value( "dirs", "xsl" );
1170 my $xsl = $settings_client->config_value(
1171 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
1173 $xsl = $parser->parse_file("$dir/$xsl");
1174 $marc_sheet = $xslt->parse_stylesheet( $xsl );
1178 my $record = $apputils->simple_scalar_request(
1180 "open-ils.cstore.direct.biblio.record_entry.retrieve",
1183 my $xmldoc = $parser->parse_string($record->marc);
1184 my $html = $marc_sheet->transform($xmldoc);
1185 $html = $html->toString();
1192 __PACKAGE__->register_method(
1193 method => "retrieve_all_copy_locations",
1194 api_name => "open-ils.search.config.copy_location.retrieve.all" );
1196 my $shelving_locations;
1197 sub retrieve_all_copy_locations {
1198 my( $self, $client ) = @_;
1199 if(!$shelving_locations) {
1200 $shelving_locations = $apputils->simple_scalar_request(
1202 "open-ils.cstore.direct.asset.copy_location.search.atomic",
1203 { id => { "!=" => undef } }
1206 return $shelving_locations;
1212 __PACKAGE__->register_method(
1213 method => "retrieve_all_copy_statuses",
1214 api_name => "open-ils.search.config.copy_status.retrieve.all" );
1217 sub retrieve_all_copy_statuses {
1218 my( $self, $client ) = @_;
1219 return $copy_statuses if $copy_statuses;
1220 return $copy_statuses =
1221 new_editor()->retrieve_all_config_copy_status();
1225 __PACKAGE__->register_method(
1226 method => "copy_counts_per_org",
1227 api_name => "open-ils.search.biblio.copy_counts.retrieve");
1229 __PACKAGE__->register_method(
1230 method => "copy_counts_per_org",
1231 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
1233 sub copy_counts_per_org {
1234 my( $self, $client, $record_id ) = @_;
1236 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1238 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1239 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1241 my $counts = $apputils->simple_scalar_request(
1242 "open-ils.storage", $method, $record_id );
1244 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1249 __PACKAGE__->register_method(
1250 method => "copy_count_summary",
1251 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1252 notes => <<" NOTES");
1253 returns an array of these:
1254 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1255 where statusx is a copy status name. the statuses are sorted
1259 sub copy_count_summary {
1260 my( $self, $client, $rid, $org, $depth ) = @_;
1263 my $data = $U->storagereq(
1264 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1266 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1272 __PACKAGE__->register_method(
1273 method => "multiclass_search",
1274 api_name => "open-ils.search.biblio.multiclass",
1275 notes => <<" NOTES");
1276 Performs a multiclass search
1277 PARAMS( searchBlob, org_unit, format, limit )
1278 where searchBlob is defined like this:
1280 "title" : { "term" : "water" },
1281 "author" : { "term" : "smith" },
1286 __PACKAGE__->register_method(
1287 method => "multiclass_search",
1288 api_name => "open-ils.search.biblio.multiclass.staff",
1289 notes => "see open-ils.search.biblio.multiclass" );
1291 sub multiclass_search {
1292 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1294 $logger->debug("Performing multiclass search with org => $orgid, " .
1295 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1297 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1298 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1301 my $records = $apputils->simplereq(
1302 'open-ils.storage', $meth,
1303 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1308 if( ref($records) and $records->[0] and
1309 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1311 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1313 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1314 return { ids => $recs, count => $count };
1320 __PACKAGE__->register_method(
1321 method => "multiclass_search",
1322 api_name => "open-ils.search.biblio.multiclass",
1324 Performs a multiclass search
1325 @param args A names hash of arguments:
1326 org_unit : The org to focus the search on
1327 depth : The search depth
1328 format : Item format
1329 limit : Return limit
1330 offset : Search offset
1331 searches : A named hash of searches which has the following format:
1333 "title" : { "term" : "water" },
1334 "author" : { "term" : "smith" },
1337 @return { ids : <array of ids>, count : hitcount }
1341 __PACKAGE__->register_method(
1342 method => "multiclass_search",
1343 api_name => "open-ils.search.biblio.multiclass.staff",
1344 notes => q/@see open-ils.search.biblio.multiclass/ );
1346 sub multiclass_search {
1347 my( $self, $client, $args ) = @_;
1349 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
1350 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1351 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1353 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1358 if( ref($records) and $records->[0] and
1359 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1361 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1363 return { ids => $recs, count => $count };
1370 __PACKAGE__->register_method(
1371 method => "marc_search",
1372 api_name => "open-ils.search.biblio.marc.staff");
1374 __PACKAGE__->register_method(
1375 method => "marc_search",
1376 api_name => "open-ils.search.biblio.marc",
1377 notes => <<" NOTES");
1379 open-ils.storage.biblio.full_rec.multi_search.atomic
1380 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1381 "limit":5,"sort":"author","item_type":"g"}
1385 my( $self, $conn, $args, $limit, $offset ) = @_;
1387 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1388 $method .= ".staff" if $self->api_name =~ /staff/;
1389 $method .= ".atomic";
1395 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1396 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1398 my $recs = search_cache($ckey, $offset, $limit);
1401 $recs = $U->storagereq($method, %$args) || [];
1403 put_cache($ckey, scalar(@$recs), $recs);
1404 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1411 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1412 my @recs = map { $_->[0] } @$recs;
1414 return { ids => \@recs, count => $count };
1418 __PACKAGE__->register_method(
1419 method => "biblio_search_isbn",
1420 api_name => "open-ils.search.biblio.isbn",
1423 sub biblio_search_isbn {
1424 my( $self, $client, $isbn ) = @_;
1425 $logger->debug("Searching ISBN $isbn");
1426 my $e = new_editor();
1427 my $recs = $U->storagereq(
1428 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1429 return { ids => $recs, count => scalar(@$recs) };
1433 __PACKAGE__->register_method(
1434 method => "biblio_search_issn",
1435 api_name => "open-ils.search.biblio.issn",
1438 sub biblio_search_issn {
1439 my( $self, $client, $issn ) = @_;
1440 $logger->debug("Searching ISSN $issn");
1441 my $e = new_editor();
1442 my $recs = $U->storagereq(
1443 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1444 return { ids => $recs, count => scalar(@$recs) };
1450 __PACKAGE__->register_method(
1451 method => "fetch_mods_by_copy",
1452 api_name => "open-ils.search.biblio.mods_from_copy",
1455 sub fetch_mods_by_copy {
1456 my( $self, $client, $copyid ) = @_;
1457 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1458 return $evt if $evt;
1459 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1460 return $apputils->record_to_mvr($record);
1465 # -------------------------------------------------------------------------------------
1467 __PACKAGE__->register_method(
1468 method => "cn_browse",
1469 api_name => "open-ils.search.callnumber.browse.target",
1470 notes => "Starts a callnumber browse"
1473 __PACKAGE__->register_method(
1474 method => "cn_browse",
1475 api_name => "open-ils.search.callnumber.browse.page_up",
1476 notes => "Returns the previous page of callnumbers",
1479 __PACKAGE__->register_method(
1480 method => "cn_browse",
1481 api_name => "open-ils.search.callnumber.browse.page_down",
1482 notes => "Returns the next page of callnumbers",
1486 # RETURNS array of arrays like so: label, owning_lib, record, id
1488 my( $self, $client, @params ) = @_;
1491 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1492 if( $self->api_name =~ /target/ );
1493 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1494 if( $self->api_name =~ /page_up/ );
1495 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1496 if( $self->api_name =~ /page_down/ );
1498 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1500 # -------------------------------------------------------------------------------------
1502 __PACKAGE__->register_method(
1503 method => "fetch_cn",
1505 api_name => "open-ils.search.callnumber.retrieve",
1506 notes => "retrieves a callnumber based on ID",
1510 my( $self, $client, $id ) = @_;
1511 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1512 return $evt if $evt;
1516 __PACKAGE__->register_method (
1517 method => "fetch_copy_by_cn",
1518 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1520 Returns an array of copy id's by callnumber id
1521 @param cnid The callnumber id
1522 @return An array of copy ids
1526 sub fetch_copy_by_cn {
1527 my( $self, $conn, $cnid ) = @_;
1528 return $U->cstorereq(
1529 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1530 { call_number => $cnid, deleted => 'f' } );
1533 __PACKAGE__->register_method (
1534 method => 'fetch_cn_by_info',
1535 api_name => 'open-ils.search.call_number.retrieve_by_info',
1537 @param label The callnumber label
1538 @param record The record the cn is attached to
1539 @param org The owning library of the cn
1540 @return The callnumber object
1545 sub fetch_cn_by_info {
1546 my( $self, $conn, $label, $record, $org ) = @_;
1547 return $U->cstorereq(
1548 'open-ils.cstore.direct.asset.call_number.search',
1549 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1556 __PACKAGE__->register_method (
1557 method => 'bib_extras',
1558 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1559 __PACKAGE__->register_method (
1560 method => 'bib_extras',
1561 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1562 __PACKAGE__->register_method (
1563 method => 'bib_extras',
1564 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1565 __PACKAGE__->register_method (
1566 method => 'bib_extras',
1567 api_name => 'open-ils.search.biblio.bib_level_map.retrieve.all');
1568 __PACKAGE__->register_method (
1569 method => 'bib_extras',
1570 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1575 my $e = new_editor();
1577 return $e->retrieve_all_config_lit_form_map()
1578 if( $self->api_name =~ /lit_form/ );
1580 return $e->retrieve_all_config_item_form_map()
1581 if( $self->api_name =~ /item_form_map/ );
1583 return $e->retrieve_all_config_item_type_map()
1584 if( $self->api_name =~ /item_type_map/ );
1586 return $e->retrieve_all_config_bib_level_map()
1587 if( $self->api_name =~ /bib_level_map/ );
1589 return $e->retrieve_all_config_audience_map()
1590 if( $self->api_name =~ /audience_map/ );
1597 __PACKAGE__->register_method(
1598 method => 'fetch_slim_record',
1599 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1601 Returns a biblio.record_entry without the attached marcxml
1605 sub fetch_slim_record {
1606 my( $self, $conn, $ids ) = @_;
1608 #my $editor = OpenILS::Utils::Editor->new;
1609 my $editor = new_editor();
1612 return $editor->event unless
1613 my $r = $editor->retrieve_biblio_record_entry($_);
1622 __PACKAGE__->register_method(
1623 method => 'rec_to_mr_rec_descriptors',
1624 api_name => 'open-ils.search.metabib.record_to_descriptors',
1626 specialized method...
1627 Given a biblio record id or a metarecord id,
1628 this returns a list of metabib.record_descriptor
1629 objects that live within the same metarecord
1630 @param args Object of args including:
1634 sub rec_to_mr_rec_descriptors {
1635 my( $self, $conn, $args ) = @_;
1637 my $rec = $$args{record};
1638 my $mrec = $$args{metarecord};
1639 my $item_forms = $$args{item_forms};
1640 my $item_types = $$args{item_types};
1641 my $item_lang = $$args{item_lang};
1643 my $e = new_editor();
1647 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1648 return $e->event unless @$map;
1649 $mrec = $$map[0]->metarecord;
1652 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1653 return $e->event unless @$recs;
1655 my @recs = map { $_->source } @$recs;
1656 my $search = { record => \@recs };
1657 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1658 $search->{item_type} = $item_types if $item_types and @$item_types;
1659 $search->{item_lang} = $item_lang if $item_lang;
1661 my $desc = $e->search_metabib_record_descriptor($search);
1663 return { metarecord => $mrec, descriptors => $desc };
1669 __PACKAGE__->register_method(
1670 method => 'copies_created_on',
1674 sub copies_created_on {
1675 my( $self, $conn, $auth, $org, $date ) = @_;
1676 my $e = new_editor(authtoken=>$auth);
1677 return $e->event unless $e->checkauth;
1681 __PACKAGE__->register_method(
1682 method => 'fetch_age_protect',
1683 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1686 sub fetch_age_protect {
1687 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1691 __PACKAGE__->register_method(
1692 method => 'copies_by_cn_label',
1693 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1696 __PACKAGE__->register_method(
1697 method => 'copies_by_cn_label',
1698 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1701 sub copies_by_cn_label {
1702 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1703 my $e = new_editor();
1704 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1705 return [] unless @$cns;
1707 # show all non-deleted copies in the staff client ...
1708 if ($self->api_name =~ /staff$/o) {
1709 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1712 # ... otherwise, grab the copies ...
1713 my $copies = $e->search_asset_copy(
1714 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1715 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1719 # ... and test for location and status visibility
1720 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->opac_visible)) ? ($_->id) : () } @$copies ];