1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenILS::Application/;
3 use strict; use warnings;
6 use OpenSRF::Utils::JSON;
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Utils::ModsParser;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Utils::CStoreEditor q/:funcs/;
11 use OpenSRF::Utils::Cache;
14 use OpenSRF::Utils::Logger qw/:logger/;
17 use OpenSRF::Utils::JSON;
19 use Time::HiRes qw(time);
20 use OpenSRF::EX qw(:try);
21 use Digest::MD5 qw(md5_hex);
27 $Data::Dumper::Indent = 0;
29 use OpenILS::Const qw/:const/;
31 use OpenILS::Application::AppUtils;
32 my $apputils = "OpenILS::Application::AppUtils";
35 my $pfx = "open-ils.search_";
43 $cache = OpenSRF::Utils::Cache->new('global');
44 my $sclient = OpenSRF::Utils::SettingsClient->new();
45 $cache_timeout = $sclient->config_value(
46 "apps", "open-ils.search", "app_settings", "cache_timeout" ) || 300;
48 $superpage_size = $sclient->config_value(
49 "apps", "open-ils.search", "app_settings", "superpage_size" ) || 500;
51 $max_superpages = $sclient->config_value(
52 "apps", "open-ils.search", "app_settings", "max_superpages" ) || 20;
54 $logger->info("Search cache timeout is $cache_timeout, ".
55 " superpage_size is $superpage_size, max_superpages is $max_superpages");
60 # ---------------------------------------------------------------------------
61 # takes a list of record id's and turns the docs into friendly
62 # mods structures. Creates one MODS structure for each doc id.
63 # ---------------------------------------------------------------------------
64 sub _records_to_mods {
70 my $session = OpenSRF::AppSession->create("open-ils.cstore");
71 my $request = $session->request(
72 "open-ils.cstore.direct.biblio.record_entry.search", { id => \@ids } );
74 while( my $resp = $request->recv ) {
75 my $content = $resp->content;
76 next if $content->id == OILS_PRECAT_RECORD;
77 my $u = OpenILS::Utils::ModsParser->new();
78 $u->start_mods_batch( $content->marc );
79 my $mods = $u->finish_mods_batch();
80 $mods->doc_id($content->id());
81 $mods->tcn($content->tcn_value);
85 $session->disconnect();
89 __PACKAGE__->register_method(
90 method => "record_id_to_mods",
91 api_name => "open-ils.search.biblio.record.mods.retrieve",
93 note => "Provide ID, we provide the mods"
96 # converts a record into a mods object with copy counts attached
97 sub record_id_to_mods {
99 my( $self, $client, $org_id, $id ) = @_;
101 my $mods_list = _records_to_mods( $id );
102 my $mods_obj = $mods_list->[0];
103 my $cmethod = $self->method_lookup(
104 "open-ils.search.biblio.record.copy_count");
105 my ($count) = $cmethod->run($org_id, $id);
106 $mods_obj->copy_count($count);
113 __PACKAGE__->register_method(
114 method => "record_id_to_mods_slim",
116 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
118 note => "Provide ID, we provide the mods"
121 # converts a record into a mods object with NO copy counts attached
122 sub record_id_to_mods_slim {
123 my( $self, $client, $id ) = @_;
124 return undef unless defined $id;
126 if(ref($id) and ref($id) == 'ARRAY') {
127 return _records_to_mods( @$id );
129 my $mods_list = _records_to_mods( $id );
130 my $mods_obj = $mods_list->[0];
131 return OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND') unless $mods_obj;
137 __PACKAGE__->register_method(
138 method => "record_id_to_mods_slim_batch",
139 api_name => "open-ils.search.biblio.record.mods_slim.batch.retrieve",
142 sub record_id_to_mods_slim_batch {
143 my($self, $conn, $id_list) = @_;
144 $conn->respond(_records_to_mods($_)->[0]) for @$id_list;
149 # Returns the number of copies attached to a record based on org location
150 __PACKAGE__->register_method(
151 method => "record_id_to_copy_count",
152 api_name => "open-ils.search.biblio.record.copy_count",
155 __PACKAGE__->register_method(
156 method => "record_id_to_copy_count",
158 api_name => "open-ils.search.biblio.record.copy_count.staff",
161 __PACKAGE__->register_method(
162 method => "record_id_to_copy_count",
163 api_name => "open-ils.search.biblio.metarecord.copy_count",
166 __PACKAGE__->register_method(
167 method => "record_id_to_copy_count",
168 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
170 sub record_id_to_copy_count {
171 my( $self, $client, $org_id, $record_id, $format ) = @_;
173 return [] unless $record_id;
174 $format = undef if (!$format or $format eq 'all');
176 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
179 if($self->api_name =~ /metarecord/) {
180 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
184 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
186 my $count = $U->storagereq( $method,
187 org_unit => $org_id, $key => $record_id, format => $format );
189 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
195 __PACKAGE__->register_method(
196 method => "biblio_search_tcn",
197 api_name => "open-ils.search.biblio.tcn",
199 note => "Retrieve a record by TCN",
202 sub biblio_search_tcn {
204 my( $self, $client, $tcn, $include_deleted ) = @_;
206 $tcn =~ s/^\s+|\s+$//og;
208 my $e = new_editor();
209 my $search = {tcn_value => $tcn};
210 $search->{deleted} = 'f' unless $include_deleted;
211 my $recs = $e->search_biblio_record_entry( $search, {idlist =>1} );
213 return { count => scalar(@$recs), ids => $recs };
217 # --------------------------------------------------------------------------------
219 __PACKAGE__->register_method(
220 method => "biblio_barcode_to_copy",
221 api_name => "open-ils.search.asset.copy.find_by_barcode",);
222 sub biblio_barcode_to_copy {
223 my( $self, $client, $barcode ) = @_;
224 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
229 __PACKAGE__->register_method(
230 method => "biblio_id_to_copy",
231 api_name => "open-ils.search.asset.copy.batch.retrieve",);
232 sub biblio_id_to_copy {
233 my( $self, $client, $ids ) = @_;
234 $logger->info("Fetching copies @$ids");
235 return $U->cstorereq(
236 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
240 __PACKAGE__->register_method(
241 method => "biblio_id_to_uris",
242 api_name=> "open-ils.search.asset.uri.retrieve_by_bib",
246 @param BibID Which bib record contains the URIs
247 @param OrgID Where to look for URIs
248 @param OrgDepth Range adjustment for OrgID
249 @return A stream or list of 'auri' objects
253 sub biblio_id_to_uris {
254 my( $self, $client, $bib, $org, $depth ) = @_;
255 die "Org ID required" unless defined($org);
256 die "Bib ID required" unless defined($bib);
259 push @params, $depth if (defined $depth);
261 my $ids = $U->cstorereq( "open-ils.cstore.json_query.atomic",
262 { select => { auri => [ 'id' ] },
266 field => 'call_number',
272 filter => { active => 't' }
283 select => { aou => [ { column => 'id', transform => 'actor.org_unit_descendants', params => \@params, result_field => 'id' } ] },
285 where => { id => $org },
295 my $uris = $U->cstorereq(
296 "open-ils.cstore.direct.asset.uri.search.atomic",
297 { id => [ map { (values %$_) } @$ids ] }
300 $client->respond($_) for (@$uris);
306 __PACKAGE__->register_method(
307 method => "copy_retrieve",
308 api_name => "open-ils.search.asset.copy.retrieve",);
310 my( $self, $client, $cid ) = @_;
311 my( $copy, $evt ) = $U->fetch_copy($cid);
316 __PACKAGE__->register_method(
317 method => "volume_retrieve",
318 api_name => "open-ils.search.asset.call_number.retrieve");
319 sub volume_retrieve {
320 my( $self, $client, $vid ) = @_;
321 my $e = new_editor();
322 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
326 __PACKAGE__->register_method(
327 method => "fleshed_copy_retrieve_batch",
329 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
331 sub fleshed_copy_retrieve_batch {
332 my( $self, $client, $ids ) = @_;
333 $logger->info("Fetching fleshed copies @$ids");
334 return $U->cstorereq(
335 "open-ils.cstore.direct.asset.copy.search.atomic",
338 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
343 __PACKAGE__->register_method(
344 method => "fleshed_copy_retrieve",
345 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
347 sub fleshed_copy_retrieve {
348 my( $self, $client, $id ) = @_;
349 my( $c, $e) = $U->fetch_fleshed_copy($id);
356 __PACKAGE__->register_method(
357 method => 'fleshed_by_barcode',
358 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",
361 sub fleshed_by_barcode {
362 my( $self, $conn, $barcode ) = @_;
363 my $e = new_editor();
364 my $copyid = $e->search_asset_copy(
365 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
367 return fleshed_copy_retrieve2( $self, $conn, $copyid);
371 __PACKAGE__->register_method(
372 method => "fleshed_copy_retrieve2",
373 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",
377 sub fleshed_copy_retrieve2 {
378 my( $self, $client, $id ) = @_;
379 my $e = new_editor();
380 my $copy = $e->retrieve_asset_copy(
386 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
387 ascecm => [ qw/ stat_cat stat_cat_entry / ],
391 ) or return $e->event;
393 # For backwards compatibility
394 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
396 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
398 $e->search_action_circulation(
400 { target_copy => $copy->id },
402 order_by => { circ => 'xact_start desc' },
414 __PACKAGE__->register_method(
415 method => 'flesh_copy_custom',
416 api_name => 'open-ils.search.asset.copy.fleshed.custom',
420 sub flesh_copy_custom {
421 my( $self, $conn, $copyid, $fields ) = @_;
422 my $e = new_editor();
423 my $copy = $e->retrieve_asset_copy(
433 ) or return $e->event;
442 __PACKAGE__->register_method(
443 method => "biblio_barcode_to_title",
444 api_name => "open-ils.search.biblio.find_by_barcode",
447 sub biblio_barcode_to_title {
448 my( $self, $client, $barcode ) = @_;
450 my $title = $apputils->simple_scalar_request(
452 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
454 return { ids => [ $title->id ], count => 1 } if $title;
455 return { count => 0 };
458 __PACKAGE__->register_method(
459 method => 'title_id_by_item_barcode',
460 api_name => 'open-ils.search.bib_id.by_barcode',
464 sub title_id_by_item_barcode {
465 my( $self, $conn, $barcode ) = @_;
466 my $e = new_editor();
467 my $copies = $e->search_asset_copy(
469 { deleted => 'f', barcode => $barcode },
473 acp => [ 'call_number' ],
480 return $e->event unless @$copies;
481 return $$copies[0]->call_number->record->id;
485 __PACKAGE__->register_method(
486 method => "biblio_copy_to_mods",
487 api_name => "open-ils.search.biblio.copy.mods.retrieve",
490 # takes a copy object and returns it fleshed mods object
491 sub biblio_copy_to_mods {
492 my( $self, $client, $copy ) = @_;
494 my $volume = $U->cstorereq(
495 "open-ils.cstore.direct.asset.call_number.retrieve",
496 $copy->call_number() );
498 my $mods = _records_to_mods($volume->record());
499 $mods = shift @$mods;
500 $volume->copies([$copy]);
501 push @{$mods->call_numbers()}, $volume;
507 __PACKAGE__->register_method(
508 api_name => 'open-ils.search.biblio.multiclass.query',
509 method => 'multiclass_query',
511 @param arghash @see open-ils.search.biblio.multiclass
512 @param query Raw human-readable query string.
513 Recognized search keys include:
514 keyword/kw - search keyword(s)
515 author/au/name - search author(s)
516 title/ti - search title
517 subject/su - search subject
518 series/se - search series
519 lang - limit by language (specifiy multiple langs with lang:l1 lang:l2 ...)
520 site - search at specified org unit, corresponds to actor.org_unit.shortname
521 sort - sort type (title, author, pubdate)
522 dir - sort direction (asc, desc)
523 available - if set to anything other than "false" or "0", limits to available items
525 keyword, title, author, subject, and series support additional search
526 subclasses, specified with a "|". For example, "title|proper:gone with the wind"
527 For more, see config.metabib_field
529 @param docache @see open-ils.search.biblio.multiclass
532 __PACKAGE__->register_method(
533 api_name => 'open-ils.search.biblio.multiclass.query.staff',
534 method => 'multiclass_query',
535 signature => '@see open-ils.search.biblio.multiclass.query');
536 __PACKAGE__->register_method(
537 api_name => 'open-ils.search.metabib.multiclass.query',
538 method => 'multiclass_query',
539 signature => '@see open-ils.search.biblio.multiclass.query');
540 __PACKAGE__->register_method(
541 api_name => 'open-ils.search.metabib.multiclass.query.staff',
542 method => 'multiclass_query',
543 signature => '@see open-ils.search.biblio.multiclass.query');
545 sub multiclass_query {
546 my($self, $conn, $arghash, $query, $docache) = @_;
548 $logger->debug("initial search query => $query");
549 my $orig_query = $query;
553 $query =~ s/^\s+//go;
555 # convert convenience classes (e.g. kw for keyword) to the full class name
556 $query =~ s/kw(:|\|)/keyword$1/go;
557 $query =~ s/ti(:|\|)/title$1/go;
558 $query =~ s/au(:|\|)/author$1/go;
559 $query =~ s/su(:|\|)/subject$1/go;
560 $query =~ s/se(:|\|)/series$1/go;
561 $query =~ s/name(:|\|)/author$1/og;
563 $logger->debug("cleansed query string => $query");
564 my $search = $arghash->{searches} = {};
566 while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):.+?)$//so) {
568 my $where = index($qpart,':');
569 my $type = substr($qpart, 0, $where++);
570 my $value = substr($qpart, $where);
572 next unless $type and $value;
574 $value =~ s/^\s*//og;
575 $value =~ s/\s*$//og;
576 $type = 'sort_dir' if $type eq 'dir';
578 if($type eq 'site') {
579 # 'site' is the org shortname. when using this, we also want
580 # to search at the requested org's depth
581 my $e = new_editor();
582 if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
583 $arghash->{org_unit} = $org->id if $org;
584 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
586 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
589 } elsif($type eq 'available') {
591 $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
593 } elsif($type eq 'lang') {
594 # collect languages into an array of languages
595 $arghash->{language} = [] unless $arghash->{language};
596 push(@{$arghash->{language}}, $value);
598 } elsif($type =~ /^sort/o) {
599 # sort and sort_dir modifiers
600 $arghash->{$type} = $value;
603 # append the search term to the term under construction
604 $search->{$type} = {} unless $search->{$type};
605 $search->{$type}->{term} =
606 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
611 # This is the front part of the string before any special tokens were parsed.
612 # Add this data to the default search class
613 my $type = $arghash->{default_class} || 'keyword';
614 $type = ($type eq '-') ? 'keyword' : $type;
615 $type = ($type !~ /^(title|author|keyword|subject|series)$/o) ? 'keyword' : $type;
616 $search->{$type} = {} unless $search->{$type};
617 $search->{$type}->{term} =
618 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $query" : $query;
621 # capture the original limit because the search method alters the limit internally
622 my $ol = $arghash->{limit};
624 my $sclient = OpenSRF::Utils::SettingsClient->new;
626 (my $method = $self->api_name) =~ s/\.query//o;
628 $method =~ s/multiclass/multiclass.staged/
629 if $sclient->config_value(apps => 'open-ils.search',
630 app_settings => 'use_staged_search') =~ /true/i;
632 $arghash->{preferred_language} = $U->get_org_locale($arghash->{org_unit})
633 unless $arghash->{preferred_language};
635 $method = $self->method_lookup($method);
636 my ($data) = $method->run($arghash, $docache);
638 $arghash->{limit} = $ol if $ol;
639 $data->{compiled_search} = $arghash;
640 $data->{query} = $orig_query;
642 $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
647 __PACKAGE__->register_method(
648 method => 'cat_search_z_style_wrapper',
649 api_name => 'open-ils.search.biblio.zstyle',
651 signature => q/@see open-ils.search.biblio.multiclass/);
653 __PACKAGE__->register_method(
654 method => 'cat_search_z_style_wrapper',
655 api_name => 'open-ils.search.biblio.zstyle.staff',
657 signature => q/@see open-ils.search.biblio.multiclass/);
659 sub cat_search_z_style_wrapper {
662 my $authtoken = shift;
665 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
667 my $ou = $cstore->request(
668 'open-ils.cstore.direct.actor.org_unit.search',
669 { parent_ou => undef }
672 my $result = { service => 'native-evergreen-catalog', records => [] };
673 my $searchhash = { limit => $$args{limit}, offset => $$args{offset}, org_unit => $ou->id };
675 $$searchhash{searches}{title}{term} = $$args{search}{title} if $$args{search}{title};
676 $$searchhash{searches}{author}{term} = $$args{search}{author} if $$args{search}{author};
677 $$searchhash{searches}{subject}{term} = $$args{search}{subject} if $$args{search}{subject};
678 $$searchhash{searches}{keyword}{term} = $$args{search}{keyword} if $$args{search}{keyword};
680 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{tcn} if $$args{search}{tcn};
681 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{isbn} if $$args{search}{isbn};
682 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{issn} if $$args{search}{issn};
683 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{publisher} if $$args{search}{publisher};
684 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{pubdate} if $$args{search}{pubdate};
685 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{item_type} if $$args{search}{item_type};
687 my $list = the_quest_for_knowledge( $self, $client, $searchhash );
689 if ($list->{count} > 0) {
690 $result->{count} = $list->{count};
692 my $records = $cstore->request(
693 'open-ils.cstore.direct.biblio.record_entry.search.atomic',
694 { id => [ map { ( $_->[0] ) } @{$list->{ids}} ] }
697 for my $rec ( @$records ) {
699 my $u = OpenILS::Utils::ModsParser->new();
700 $u->start_mods_batch( $rec->marc );
701 my $mods = $u->finish_mods_batch();
703 push @{ $result->{records} }, { mvr => $mods, marcxml => $rec->marc, bibid => $rec->id };
709 $cstore->disconnect();
713 # ----------------------------------------------------------------------------
714 # These are the main OPAC search methods
715 # ----------------------------------------------------------------------------
717 __PACKAGE__->register_method(
718 method => 'the_quest_for_knowledge',
719 api_name => 'open-ils.search.biblio.multiclass',
721 Performs a multi class biblio or metabib search
722 @param searchhash A search object layed out like so:
723 searches : { "$class" : "$value", ...}
724 org_unit : The org id to focus the search at
725 depth : The org depth
726 limit : The search limit
727 offset : The search offset
728 format : The MARC format
729 sort : What field to sort the results on [ author | title | pubdate ]
730 sort_dir : What direction do we sort? [ asc | desc ]
731 @return An object of the form
732 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
736 __PACKAGE__->register_method(
737 method => 'the_quest_for_knowledge',
738 api_name => 'open-ils.search.biblio.multiclass.staff',
739 signature => q/@see open-ils.search.biblio.multiclass/);
740 __PACKAGE__->register_method(
741 method => 'the_quest_for_knowledge',
742 api_name => 'open-ils.search.metabib.multiclass',
743 signature => q/@see open-ils.search.biblio.multiclass/);
744 __PACKAGE__->register_method(
745 method => 'the_quest_for_knowledge',
746 api_name => 'open-ils.search.metabib.multiclass.staff',
747 signature => q/@see open-ils.search.biblio.multiclass/);
749 sub the_quest_for_knowledge {
750 my( $self, $conn, $searchhash, $docache ) = @_;
752 return { count => 0 } unless $searchhash and
753 ref $searchhash->{searches} eq 'HASH';
755 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
759 if($self->api_name =~ /metabib/) {
761 $method =~ s/biblio/metabib/o;
765 my $offset = $searchhash->{offset} || 0;
766 my $limit = $searchhash->{limit} || 10;
767 my $end = $offset + $limit - 1;
769 # do some simple sanity checking
770 if(!$searchhash->{searches} or
771 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
772 return { count => 0 };
777 $searchhash->{offset} = 0;
778 $searchhash->{limit} = $maxlimit;
780 return { count => 0 } if $offset > $maxlimit;
783 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
784 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
785 my $ckey = $pfx . md5_hex($method . $s);
787 $logger->info("bib search for: $s");
789 $searchhash->{limit} -= $offset;
793 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
797 $method .= ".staff" if($self->api_name =~ /staff/);
798 $method .= ".atomic";
800 for (keys %$searchhash) {
801 delete $$searchhash{$_}
802 unless defined $$searchhash{$_};
805 $result = $U->storagereq( $method, %$searchhash );
812 return {count => 0} unless ($result && $$result[0]);
816 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
819 # If we didn't get this data from the cache, put it into the cache
820 # then return the correct offset of records
821 $logger->debug("putting search cache $ckey\n");
822 put_cache($ckey, $count, \@recs);
826 # if we have the full set of data, trim out
827 # the requested chunk based on limit and offset
829 for ($offset..$end) {
830 last unless $recs[$_];
836 return { ids => \@recs, count => $count };
840 __PACKAGE__->register_method(
841 method => 'staged_search',
842 api_name => 'open-ils.search.biblio.multiclass.staged');
843 __PACKAGE__->register_method(
844 method => 'staged_search',
845 api_name => 'open-ils.search.biblio.multiclass.staged.staff',
846 signature => q/@see open-ils.search.biblio.multiclass.staged/);
847 __PACKAGE__->register_method(
848 method => 'staged_search',
849 api_name => 'open-ils.search.metabib.multiclass.staged',
850 signature => q/@see open-ils.search.biblio.multiclass.staged/);
851 __PACKAGE__->register_method(
852 method => 'staged_search',
853 api_name => 'open-ils.search.metabib.multiclass.staged.staff',
854 signature => q/@see open-ils.search.biblio.multiclass.staged/);
857 my($self, $conn, $search_hash, $docache) = @_;
859 my $method = ($self->api_name =~ /metabib/) ?
860 'open-ils.storage.metabib.multiclass.staged.search_fts':
861 'open-ils.storage.biblio.multiclass.staged.search_fts';
863 $method .= '.staff' if $self->api_name =~ /staff$/;
864 $method .= '.atomic';
866 return {count => 0} unless (
868 $search_hash->{searches} and
869 scalar( keys %{$search_hash->{searches}} ));
872 my $user_offset = $search_hash->{offset} || 0; # user-specified offset
873 my $user_limit = $search_hash->{limit} || 10;
874 $user_offset = ($user_offset >= 0) ? $user_offset : 0;
875 $user_limit = ($user_limit >= 0) ? $user_limit : 10;
878 # we're grabbing results on a per-superpage basis, which means the
879 # limit and offset should coincide with superpage boundaries
880 $search_hash->{offset} = 0;
881 $search_hash->{limit} = $superpage_size;
883 # force a well-known check_limit
884 $search_hash->{check_limit} = $superpage_size;
885 # restrict total tested to superpage size * number of superpages
886 $search_hash->{core_limit} = $superpage_size * $max_superpages;
888 # Set the configured estimation strategy, defaults to 'inclusion'.
889 my $estimation_strategy = OpenSRF::Utils::SettingsClient
892 apps => 'open-ils.search', app_settings => 'estimation_strategy'
894 $search_hash->{estimation_strategy} = $estimation_strategy;
896 # pull any existing results from the cache
897 my $key = search_cache_key($method, $search_hash);
898 my $cache_data = $cache->get_cache($key) || {};
900 # keep retrieving results until we find enough to
901 # fulfill the user-specified limit and offset
902 my $all_results = [];
903 my $page; # current superpage
904 my $est_hit_count = 0;
905 my $current_page_summary = {};
906 my $global_summary = {checked => 0, visible => 0, excluded => 0, deleted => 0, total => 0};
907 my $is_real_hit_count = 0;
909 for($page = 0; $page < $max_superpages; $page++) {
911 my $data = $cache_data->{$page};
915 $logger->debug("staged search: analyzing superpage $page");
918 # this window of results is already cached
919 $logger->debug("staged search: found cached results");
920 $summary = $data->{summary};
921 $results = $data->{results};
924 # retrieve the window of results from the database
925 $logger->debug("staged search: fetching results from the database");
926 $search_hash->{skip_check} = $page * $superpage_size;
928 $results = $U->storagereq($method, %$search_hash);
929 $search_duration = time - $start;
930 $logger->info("staged search: DB call took $search_duration seconds");
931 $summary = shift(@$results);
934 $logger->info("search timed out: duration=$search_duration: params=".
935 OpenSRF::Utils::JSON->perl2JSON($search_hash));
939 my $hc = $summary->{estimated_hit_count} || $summary->{visible};
941 $logger->info("search returned 0 results: duration=$search_duration: params=".
942 OpenSRF::Utils::JSON->perl2JSON($search_hash));
945 # Create backwards-compatible result structures
946 if($self->api_name =~ /biblio/) {
947 $results = [map {[$_->{id}]} @$results];
949 $results = [map {[$_->{id}, $_->{rel}, $_->{record}]} @$results];
952 $results = [grep {defined $_->[0]} @$results];
953 cache_staged_search_page($key, $page, $summary, $results) if $docache;
956 $current_page_summary = $summary;
958 # add the new set of results to the set under construction
959 push(@$all_results, @$results);
961 my $current_count = scalar(@$all_results);
963 $est_hit_count = $summary->{estimated_hit_count} || $summary->{visible}
966 $logger->debug("staged search: located $current_count, with estimated hits=".
967 $summary->{estimated_hit_count}." : visible=".$summary->{visible}.", checked=".$summary->{checked});
969 if (defined($summary->{estimated_hit_count})) {
970 $global_summary->{checked} += $summary->{checked};
971 $global_summary->{visible} += $summary->{visible};
972 $global_summary->{excluded} += $summary->{excluded};
973 $global_summary->{deleted} += $summary->{deleted};
974 $global_summary->{total} = $summary->{total};
977 # we've found all the possible hits
978 last if $current_count == $summary->{visible}
979 and not defined $summary->{estimated_hit_count};
981 # we've found enough results to satisfy the requested limit/offset
982 last if $current_count >= ($user_limit + $user_offset);
984 # we've scanned all possible hits
985 if($summary->{checked} < $superpage_size) {
986 $est_hit_count = scalar(@$all_results);
987 # we have all possible results in hand, so we know the final hit count
988 $is_real_hit_count = 1;
993 my @results = grep {defined $_} @$all_results[$user_offset..($user_offset + $user_limit - 1)];
995 # refine the estimate if we have more than one superpage
996 if ($page > 0 and not $is_real_hit_count) {
997 if ($global_summary->{checked} >= $global_summary->{total}) {
998 $est_hit_count = $global_summary->{visible};
1000 my $updated_hit_count = $U->storagereq(
1001 'open-ils.storage.fts_paging_estimate',
1002 $global_summary->{checked},
1003 $global_summary->{visible},
1004 $global_summary->{excluded},
1005 $global_summary->{deleted},
1006 $global_summary->{total}
1008 $est_hit_count = $updated_hit_count->{$estimation_strategy};
1013 count => $est_hit_count,
1014 core_limit => $search_hash->{core_limit},
1015 superpage_size => $search_hash->{check_limit},
1016 superpage_summary => $current_page_summary,
1021 # creates a unique token to represent the query in the cache
1022 sub search_cache_key {
1024 my $search_hash = shift;
1026 for my $key (sort keys %$search_hash) {
1027 push(@sorted, ($key => $$search_hash{$key}))
1028 unless $key eq 'limit' or
1030 $key eq 'skip_check';
1032 my $s = OpenSRF::Utils::JSON->perl2JSON(\@sorted);
1033 return $pfx . md5_hex($method . $s);
1036 sub cache_staged_search_page {
1037 # puts this set of results into the cache
1038 my($key, $page, $summary, $results) = @_;
1039 my $data = $cache->get_cache($key);
1042 summary => $summary,
1046 $logger->info("staged search: cached with key=$key, superpage=$page, estimated=".
1047 $summary->{estimated_hit_count}.", visible=".$summary->{visible});
1049 $cache->put_cache($key, $data, $cache_timeout);
1057 my $start = $offset;
1058 my $end = $offset + $limit - 1;
1060 $logger->debug("searching cache for $key : $start..$end\n");
1062 return undef unless $cache;
1063 my $data = $cache->get_cache($key);
1065 return undef unless $data;
1067 my $count = $data->[0];
1070 return undef unless $offset < $count;
1073 for( my $i = $offset; $i <= $end; $i++ ) {
1074 last unless my $d = $$data[$i];
1075 push( @result, $d );
1078 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
1085 my( $key, $count, $data ) = @_;
1086 return undef unless $cache;
1087 $logger->debug("search_cache putting ".
1088 scalar(@$data)." items at key $key with timeout $cache_timeout");
1089 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
1097 __PACKAGE__->register_method(
1098 method => "biblio_mrid_to_modsbatch_batch",
1099 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
1101 sub biblio_mrid_to_modsbatch_batch {
1102 my( $self, $client, $mrids) = @_;
1103 warn "Performing mrid_to_modsbatch_batch...";
1105 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
1106 for my $id (@$mrids) {
1107 next unless defined $id;
1108 my ($m) = $method->run($id);
1115 __PACKAGE__->register_method(
1116 method => "biblio_mrid_to_modsbatch",
1117 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
1118 notes => <<" NOTES");
1119 Returns the mvr associated with a given metarecod. If none exists,
1123 __PACKAGE__->register_method(
1124 method => "biblio_mrid_to_modsbatch",
1125 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
1126 notes => <<" NOTES");
1127 Returns the mvr associated with a given metarecod. If none exists,
1131 sub biblio_mrid_to_modsbatch {
1132 my( $self, $client, $mrid, $args) = @_;
1134 warn "Grabbing mvr for $mrid\n";
1136 my ($mr, $evt) = _grab_metarecord($mrid);
1137 return $evt unless $mr;
1139 my $mvr = biblio_mrid_check_mvr($self, $client, $mr);
1140 $mvr = biblio_mrid_make_modsbatch( $self, $client, $mr ) unless $mvr;
1142 return $mvr unless ref($args);
1144 # Here we find the lead record appropriate for the given filters
1145 # and use that for the title and author of the metarecord
1146 my $format = $$args{format};
1147 my $org = $$args{org};
1148 my $depth = $$args{depth};
1150 return $mvr unless $format or $org or $depth;
1152 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
1153 $method = "$method.staff" if $self->api_name =~ /staff/o;
1155 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
1157 if( my $mods = $U->record_to_mvr($rec) ) {
1159 $mvr->title($mods->title);
1160 $mvr->title($mods->author);
1161 $logger->debug("mods_slim updating title and ".
1162 "author in mvr with ".$mods->title." : ".$mods->author);
1168 # converts a metarecord to an mvr
1171 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
1172 return Fieldmapper::metabib::virtual_record->new($perl);
1175 # checks to see if a metarecord has mods, if so returns true;
1177 __PACKAGE__->register_method(
1178 method => "biblio_mrid_check_mvr",
1179 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
1180 notes => <<" NOTES");
1181 Takes a metarecord ID or a metarecord object and returns true
1182 if the metarecord already has an mvr associated with it.
1185 sub biblio_mrid_check_mvr {
1186 my( $self, $client, $mrid ) = @_;
1190 if(ref($mrid)) { $mr = $mrid; }
1191 else { ($mr, $evt) = _grab_metarecord($mrid); }
1192 return $evt if $evt;
1194 warn "Checking mvr for mr " . $mr->id . "\n";
1196 return _mr_to_mvr($mr) if $mr->mods();
1200 sub _grab_metarecord {
1202 #my $e = OpenILS::Utils::Editor->new;
1203 my $e = new_editor();
1204 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
1209 __PACKAGE__->register_method(
1210 method => "biblio_mrid_make_modsbatch",
1211 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
1212 notes => <<" NOTES");
1213 Takes either a metarecord ID or a metarecord object.
1214 Forces the creations of an mvr for the given metarecord.
1215 The created mvr is returned.
1218 sub biblio_mrid_make_modsbatch {
1219 my( $self, $client, $mrid ) = @_;
1221 #my $e = OpenILS::Utils::Editor->new;
1222 my $e = new_editor();
1229 $mr = $e->retrieve_metabib_metarecord($mrid)
1230 or return $e->event;
1233 my $masterid = $mr->master_record;
1234 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
1236 my $ids = $U->storagereq(
1237 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
1238 return undef unless @$ids;
1240 my $master = $e->retrieve_biblio_record_entry($masterid)
1241 or return $e->event;
1243 # start the mods batch
1244 my $u = OpenILS::Utils::ModsParser->new();
1245 $u->start_mods_batch( $master->marc );
1247 # grab all of the sub-records and shove them into the batch
1248 my @ids = grep { $_ ne $masterid } @$ids;
1249 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
1254 my $r = $e->retrieve_biblio_record_entry($i);
1255 push( @$subrecs, $r ) if $r;
1260 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
1261 $u->push_mods_batch( $_->marc ) if $_->marc;
1265 # finish up and send to the client
1266 my $mods = $u->finish_mods_batch();
1267 $mods->doc_id($mrid);
1268 $client->respond_complete($mods);
1271 # now update the mods string in the db
1272 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
1275 #$e = OpenILS::Utils::Editor->new(xact => 1);
1276 $e = new_editor(xact => 1);
1277 $e->update_metabib_metarecord($mr)
1278 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
1287 # converts a mr id into a list of record ids
1289 __PACKAGE__->register_method(
1290 method => "biblio_mrid_to_record_ids",
1291 api_name => "open-ils.search.biblio.metarecord_to_records",
1294 __PACKAGE__->register_method(
1295 method => "biblio_mrid_to_record_ids",
1296 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
1299 sub biblio_mrid_to_record_ids {
1300 my( $self, $client, $mrid, $args ) = @_;
1302 my $format = $$args{format};
1303 my $org = $$args{org};
1304 my $depth = $$args{depth};
1306 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
1307 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
1308 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
1310 return { count => scalar(@$recs), ids => $recs };
1314 __PACKAGE__->register_method(
1315 method => "biblio_record_to_marc_html",
1316 api_name => "open-ils.search.biblio.record.html" );
1318 __PACKAGE__->register_method(
1319 method => "biblio_record_to_marc_html",
1320 api_name => "open-ils.search.authority.to_html" );
1322 my $parser = XML::LibXML->new();
1323 my $xslt = XML::LibXSLT->new();
1325 my $slim_marc_sheet;
1326 my $settings_client = OpenSRF::Utils::SettingsClient->new();
1328 sub biblio_record_to_marc_html {
1329 my($self, $client, $recordid, $slim, $marcxml) = @_;
1332 my $dir = $settings_client->config_value("dirs", "xsl");
1335 unless($slim_marc_sheet) {
1336 my $xsl = $settings_client->config_value(
1337 "apps", "open-ils.search", "app_settings", 'marc_html_xsl_slim');
1339 $xsl = $parser->parse_file("$dir/$xsl");
1340 $slim_marc_sheet = $xslt->parse_stylesheet($xsl);
1343 $sheet = $slim_marc_sheet;
1347 unless($marc_sheet) {
1348 my $xsl_key = ($slim) ? 'marc_html_xsl_slim' : 'marc_html_xsl';
1349 my $xsl = $settings_client->config_value(
1350 "apps", "open-ils.search", "app_settings", 'marc_html_xsl');
1351 $xsl = $parser->parse_file("$dir/$xsl");
1352 $marc_sheet = $xslt->parse_stylesheet($xsl);
1354 $sheet = $marc_sheet;
1359 my $e = new_editor();
1360 if($self->api_name =~ /authority/) {
1361 $record = $e->retrieve_authority_record_entry($recordid)
1362 or return $e->event;
1364 $record = $e->retrieve_biblio_record_entry($recordid)
1365 or return $e->event;
1367 $marcxml = $record->marc;
1370 my $xmldoc = $parser->parse_string($marcxml);
1371 my $html = $sheet->transform($xmldoc);
1372 return $html->documentElement->toString();
1377 __PACKAGE__->register_method(
1378 method => "retrieve_all_copy_statuses",
1379 api_name => "open-ils.search.config.copy_status.retrieve.all" );
1381 sub retrieve_all_copy_statuses {
1382 my( $self, $client ) = @_;
1383 return new_editor()->retrieve_all_config_copy_status();
1387 __PACKAGE__->register_method(
1388 method => "copy_counts_per_org",
1389 api_name => "open-ils.search.biblio.copy_counts.retrieve");
1391 __PACKAGE__->register_method(
1392 method => "copy_counts_per_org",
1393 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
1395 sub copy_counts_per_org {
1396 my( $self, $client, $record_id ) = @_;
1398 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1400 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1401 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1403 my $counts = $apputils->simple_scalar_request(
1404 "open-ils.storage", $method, $record_id );
1406 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1411 __PACKAGE__->register_method(
1412 method => "copy_count_summary",
1413 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1414 notes => <<" NOTES");
1415 returns an array of these:
1416 [ org_id, callnumber_label, <status1_count>, <status2_count>,...]
1417 where statusx is a copy status name. the statuses are sorted
1421 sub copy_count_summary {
1422 my( $self, $client, $rid, $org, $depth ) = @_;
1425 my $data = $U->storagereq(
1426 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1428 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1431 __PACKAGE__->register_method(
1432 method => "copy_location_count_summary",
1433 api_name => "open-ils.search.biblio.copy_location_counts.summary.retrieve",
1434 notes => <<" NOTES");
1435 returns an array of these:
1436 [ org_id, callnumber_label, copy_location, <status1_count>, <status2_count>,...]
1437 where statusx is a copy status name. the statuses are sorted
1441 sub copy_location_count_summary {
1442 my( $self, $client, $rid, $org, $depth ) = @_;
1445 my $data = $U->storagereq(
1446 'open-ils.storage.biblio.record_entry.status_copy_location_count.atomic', $rid, $org, $depth );
1448 return [ sort { $a->[1] cmp $b->[1] || $a->[2] cmp $b->[2] } @$data ];
1451 __PACKAGE__->register_method(
1452 method => "copy_count_location_summary",
1453 api_name => "open-ils.search.biblio.copy_counts.location.summary.retrieve",
1454 notes => <<" NOTES");
1455 returns an array of these:
1456 [ org_id, callnumber_label, <status1_count>, <status2_count>,...]
1457 where statusx is a copy status name. the statuses are sorted
1461 sub copy_count_location_summary {
1462 my( $self, $client, $rid, $org, $depth ) = @_;
1465 my $data = $U->storagereq(
1466 'open-ils.storage.biblio.record_entry.status_copy_location_count.atomic', $rid, $org, $depth );
1467 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1471 __PACKAGE__->register_method(
1472 method => "marc_search",
1473 api_name => "open-ils.search.biblio.marc.staff");
1475 __PACKAGE__->register_method(
1476 method => "marc_search",
1477 api_name => "open-ils.search.biblio.marc",
1478 notes => <<" NOTES");
1480 open-ils.storage.biblio.full_rec.multi_search.atomic
1481 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1482 "limit":5,"sort":"author","item_type":"g"}
1486 my( $self, $conn, $args, $limit, $offset ) = @_;
1488 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1489 $method .= ".staff" if $self->api_name =~ /staff/;
1490 $method .= ".atomic";
1496 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1497 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1499 my $recs = search_cache($ckey, $offset, $limit);
1502 $recs = $U->storagereq($method, %$args) || [];
1504 put_cache($ckey, scalar(@$recs), $recs);
1505 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1512 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1513 my @recs = map { $_->[0] } @$recs;
1515 return { ids => \@recs, count => $count };
1519 __PACKAGE__->register_method(
1520 method => "biblio_search_isbn",
1521 api_name => "open-ils.search.biblio.isbn",
1524 sub biblio_search_isbn {
1525 my( $self, $client, $isbn ) = @_;
1526 $logger->debug("Searching ISBN $isbn");
1527 my $e = new_editor();
1528 my $recs = $U->storagereq(
1529 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1530 return { ids => $recs, count => scalar(@$recs) };
1534 __PACKAGE__->register_method(
1535 method => "biblio_search_issn",
1536 api_name => "open-ils.search.biblio.issn",
1539 sub biblio_search_issn {
1540 my( $self, $client, $issn ) = @_;
1541 $logger->debug("Searching ISSN $issn");
1542 my $e = new_editor();
1544 my $recs = $U->storagereq(
1545 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1546 return { ids => $recs, count => scalar(@$recs) };
1552 __PACKAGE__->register_method(
1553 method => "fetch_mods_by_copy",
1554 api_name => "open-ils.search.biblio.mods_from_copy",
1557 sub fetch_mods_by_copy {
1558 my( $self, $client, $copyid ) = @_;
1559 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1560 return $evt if $evt;
1561 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1562 return $apputils->record_to_mvr($record);
1567 # -------------------------------------------------------------------------------------
1569 __PACKAGE__->register_method(
1570 method => "cn_browse",
1571 api_name => "open-ils.search.callnumber.browse.target",
1572 notes => "Starts a callnumber browse"
1575 __PACKAGE__->register_method(
1576 method => "cn_browse",
1577 api_name => "open-ils.search.callnumber.browse.page_up",
1578 notes => "Returns the previous page of callnumbers",
1581 __PACKAGE__->register_method(
1582 method => "cn_browse",
1583 api_name => "open-ils.search.callnumber.browse.page_down",
1584 notes => "Returns the next page of callnumbers",
1588 # RETURNS array of arrays like so: label, owning_lib, record, id
1590 my( $self, $client, @params ) = @_;
1593 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1594 if( $self->api_name =~ /target/ );
1595 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1596 if( $self->api_name =~ /page_up/ );
1597 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1598 if( $self->api_name =~ /page_down/ );
1600 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1602 # -------------------------------------------------------------------------------------
1604 __PACKAGE__->register_method(
1605 method => "fetch_cn",
1607 api_name => "open-ils.search.callnumber.retrieve",
1608 notes => "retrieves a callnumber based on ID",
1612 my( $self, $client, $id ) = @_;
1613 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1614 return $evt if $evt;
1618 __PACKAGE__->register_method (
1619 method => "fetch_copy_by_cn",
1620 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1622 Returns an array of copy id's by callnumber id
1623 @param cnid The callnumber id
1624 @return An array of copy ids
1628 sub fetch_copy_by_cn {
1629 my( $self, $conn, $cnid ) = @_;
1630 return $U->cstorereq(
1631 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1632 { call_number => $cnid, deleted => 'f' } );
1635 __PACKAGE__->register_method (
1636 method => 'fetch_cn_by_info',
1637 api_name => 'open-ils.search.call_number.retrieve_by_info',
1639 @param label The callnumber label
1640 @param record The record the cn is attached to
1641 @param org The owning library of the cn
1642 @return The callnumber object
1647 sub fetch_cn_by_info {
1648 my( $self, $conn, $label, $record, $org ) = @_;
1649 return $U->cstorereq(
1650 'open-ils.cstore.direct.asset.call_number.search',
1651 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1658 __PACKAGE__->register_method (
1659 method => 'bib_extras',
1660 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1661 __PACKAGE__->register_method (
1662 method => 'bib_extras',
1663 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1664 __PACKAGE__->register_method (
1665 method => 'bib_extras',
1666 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1667 __PACKAGE__->register_method (
1668 method => 'bib_extras',
1669 api_name => 'open-ils.search.biblio.bib_level_map.retrieve.all');
1670 __PACKAGE__->register_method (
1671 method => 'bib_extras',
1672 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1677 my $e = new_editor();
1679 return $e->retrieve_all_config_lit_form_map()
1680 if( $self->api_name =~ /lit_form/ );
1682 return $e->retrieve_all_config_item_form_map()
1683 if( $self->api_name =~ /item_form_map/ );
1685 return $e->retrieve_all_config_item_type_map()
1686 if( $self->api_name =~ /item_type_map/ );
1688 return $e->retrieve_all_config_bib_level_map()
1689 if( $self->api_name =~ /bib_level_map/ );
1691 return $e->retrieve_all_config_audience_map()
1692 if( $self->api_name =~ /audience_map/ );
1699 __PACKAGE__->register_method(
1700 method => 'fetch_slim_record',
1701 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1703 Returns a biblio.record_entry without the attached marcxml
1707 sub fetch_slim_record {
1708 my( $self, $conn, $ids ) = @_;
1710 #my $editor = OpenILS::Utils::Editor->new;
1711 my $editor = new_editor();
1714 return $editor->event unless
1715 my $r = $editor->retrieve_biblio_record_entry($_);
1724 __PACKAGE__->register_method(
1725 method => 'rec_to_mr_rec_descriptors',
1726 api_name => 'open-ils.search.metabib.record_to_descriptors',
1728 specialized method...
1729 Given a biblio record id or a metarecord id,
1730 this returns a list of metabib.record_descriptor
1731 objects that live within the same metarecord
1732 @param args Object of args including:
1736 sub rec_to_mr_rec_descriptors {
1737 my( $self, $conn, $args ) = @_;
1739 my $rec = $$args{record};
1740 my $mrec = $$args{metarecord};
1741 my $item_forms = $$args{item_forms};
1742 my $item_types = $$args{item_types};
1743 my $item_lang = $$args{item_lang};
1745 my $e = new_editor();
1749 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1750 return $e->event unless @$map;
1751 $mrec = $$map[0]->metarecord;
1754 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1755 return $e->event unless @$recs;
1757 my @recs = map { $_->source } @$recs;
1758 my $search = { record => \@recs };
1759 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1760 $search->{item_type} = $item_types if $item_types and @$item_types;
1761 $search->{item_lang} = $item_lang if $item_lang;
1763 my $desc = $e->search_metabib_record_descriptor($search);
1765 return { metarecord => $mrec, descriptors => $desc };
1771 __PACKAGE__->register_method(
1772 method => 'copies_created_on',
1776 sub copies_created_on {
1777 my( $self, $conn, $auth, $org, $date ) = @_;
1778 my $e = new_editor(authtoken=>$auth);
1779 return $e->event unless $e->checkauth;
1783 __PACKAGE__->register_method(
1784 method => 'fetch_age_protect',
1785 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1788 sub fetch_age_protect {
1789 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1793 __PACKAGE__->register_method(
1794 method => 'copies_by_cn_label',
1795 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1798 __PACKAGE__->register_method(
1799 method => 'copies_by_cn_label',
1800 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1803 sub copies_by_cn_label {
1804 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1805 my $e = new_editor();
1806 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1807 return [] unless @$cns;
1809 # show all non-deleted copies in the staff client ...
1810 if ($self->api_name =~ /staff$/o) {
1811 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1814 # ... otherwise, grab the copies ...
1815 my $copies = $e->search_asset_copy(
1816 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1817 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1821 # ... and test for location and status visibility
1822 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->opac_visible)) ? ($_->id) : () } @$copies ];