1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenSRF::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;
13 use OpenSRF::Utils::Logger qw/:logger/;
16 use OpenSRF::Utils::JSON;
18 use Time::HiRes qw(time);
19 use OpenSRF::EX qw(:try);
20 use Digest::MD5 qw(md5_hex);
26 $Data::Dumper::Indent = 0;
28 use OpenILS::Const qw/:const/;
30 use OpenILS::Application::AppUtils;
31 my $apputils = "OpenILS::Application::AppUtils";
34 my $pfx = "open-ils.search_";
40 $cache = OpenSRF::Utils::Cache->new('global');
41 my $sclient = OpenSRF::Utils::SettingsClient->new();
42 $cache_timeout = $sclient->config_value(
43 "apps", "open-ils.search", "app_settings", "cache_timeout" ) || 300;
44 $logger->info("Search cache timeout is $cache_timeout");
49 # ---------------------------------------------------------------------------
50 # takes a list of record id's and turns the docs into friendly
51 # mods structures. Creates one MODS structure for each doc id.
52 # ---------------------------------------------------------------------------
53 sub _records_to_mods {
59 my $session = OpenSRF::AppSession->create("open-ils.cstore");
60 my $request = $session->request(
61 "open-ils.cstore.direct.biblio.record_entry.search", { id => \@ids } );
63 while( my $resp = $request->recv ) {
64 my $content = $resp->content;
65 next if $content->id == OILS_PRECAT_RECORD;
66 my $u = OpenILS::Utils::ModsParser->new();
67 $u->start_mods_batch( $content->marc );
68 my $mods = $u->finish_mods_batch();
69 $mods->doc_id($content->id());
70 $mods->tcn($content->tcn_value);
74 $session->disconnect();
78 __PACKAGE__->register_method(
79 method => "record_id_to_mods",
80 api_name => "open-ils.search.biblio.record.mods.retrieve",
82 note => "Provide ID, we provide the mods"
85 # converts a record into a mods object with copy counts attached
86 sub record_id_to_mods {
88 my( $self, $client, $org_id, $id ) = @_;
90 my $mods_list = _records_to_mods( $id );
91 my $mods_obj = $mods_list->[0];
92 my $cmethod = $self->method_lookup(
93 "open-ils.search.biblio.record.copy_count");
94 my ($count) = $cmethod->run($org_id, $id);
95 $mods_obj->copy_count($count);
102 __PACKAGE__->register_method(
103 method => "record_id_to_mods_slim",
104 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
106 note => "Provide ID, we provide the mods"
109 # converts a record into a mods object with NO copy counts attached
110 sub record_id_to_mods_slim {
111 my( $self, $client, $id ) = @_;
112 return undef unless defined $id;
114 if(ref($id) and ref($id) == 'ARRAY') {
115 return _records_to_mods( @$id );
117 my $mods_list = _records_to_mods( $id );
118 my $mods_obj = $mods_list->[0];
119 return OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND') unless $mods_obj;
124 # Returns the number of copies attached to a record based on org location
125 __PACKAGE__->register_method(
126 method => "record_id_to_copy_count",
127 api_name => "open-ils.search.biblio.record.copy_count",
130 __PACKAGE__->register_method(
131 method => "record_id_to_copy_count",
132 api_name => "open-ils.search.biblio.record.copy_count.staff",
135 __PACKAGE__->register_method(
136 method => "record_id_to_copy_count",
137 api_name => "open-ils.search.biblio.metarecord.copy_count",
140 __PACKAGE__->register_method(
141 method => "record_id_to_copy_count",
142 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
144 sub record_id_to_copy_count {
145 my( $self, $client, $org_id, $record_id, $format ) = @_;
147 return [] unless $record_id;
148 $format = undef if (!$format or $format eq 'all');
150 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
153 if($self->api_name =~ /metarecord/) {
154 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
158 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
160 my $count = $U->storagereq( $method,
161 org_unit => $org_id, $key => $record_id, format => $format );
163 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
169 __PACKAGE__->register_method(
170 method => "biblio_search_tcn",
171 api_name => "open-ils.search.biblio.tcn",
173 note => "Retrieve a record by TCN",
176 sub biblio_search_tcn {
178 my( $self, $client, $tcn, $include_deleted ) = @_;
180 $tcn =~ s/.*?(\w+)\s*$/$1/o;
182 my $e = new_editor();
183 my $search = {tcn_value => $tcn};
184 $search->{deleted} = 'f' unless $include_deleted;
185 my $recs = $e->search_biblio_record_entry( $search, {idlist =>1} );
187 return { count => scalar(@$recs), ids => $recs };
191 # --------------------------------------------------------------------------------
193 __PACKAGE__->register_method(
194 method => "biblio_barcode_to_copy",
195 api_name => "open-ils.search.asset.copy.find_by_barcode",);
196 sub biblio_barcode_to_copy {
197 my( $self, $client, $barcode ) = @_;
198 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
203 __PACKAGE__->register_method(
204 method => "biblio_id_to_copy",
205 api_name => "open-ils.search.asset.copy.batch.retrieve",);
206 sub biblio_id_to_copy {
207 my( $self, $client, $ids ) = @_;
208 $logger->info("Fetching copies @$ids");
209 return $U->cstorereq(
210 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
214 __PACKAGE__->register_method(
215 method => "copy_retrieve",
216 api_name => "open-ils.search.asset.copy.retrieve",);
218 my( $self, $client, $cid ) = @_;
219 my( $copy, $evt ) = $U->fetch_copy($cid);
224 __PACKAGE__->register_method(
225 method => "volume_retrieve",
226 api_name => "open-ils.search.asset.call_number.retrieve");
227 sub volume_retrieve {
228 my( $self, $client, $vid ) = @_;
229 my $e = new_editor();
230 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
234 __PACKAGE__->register_method(
235 method => "fleshed_copy_retrieve_batch",
236 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
238 sub fleshed_copy_retrieve_batch {
239 my( $self, $client, $ids ) = @_;
240 $logger->info("Fetching fleshed copies @$ids");
241 return $U->cstorereq(
242 "open-ils.cstore.direct.asset.copy.search.atomic",
245 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
250 __PACKAGE__->register_method(
251 method => "fleshed_copy_retrieve",
252 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
254 sub fleshed_copy_retrieve {
255 my( $self, $client, $id ) = @_;
256 my( $c, $e) = $U->fetch_fleshed_copy($id);
263 __PACKAGE__->register_method(
264 method => 'fleshed_by_barcode',
265 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",);
266 sub fleshed_by_barcode {
267 my( $self, $conn, $barcode ) = @_;
268 my $e = new_editor();
269 my $copyid = $e->search_asset_copy(
270 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
272 return $self->fleshed_copy_retrieve2($conn, $copyid);
276 __PACKAGE__->register_method(
277 method => "fleshed_copy_retrieve2",
278 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
280 sub fleshed_copy_retrieve2 {
281 my( $self, $client, $id ) = @_;
282 my $e = new_editor();
283 my $copy = $e->retrieve_asset_copy(
289 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
290 ascecm => [ qw/ stat_cat stat_cat_entry / ],
294 ) or return $e->event;
296 # For backwards compatibility
297 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
299 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
301 $e->search_action_circulation(
303 { target_copy => $copy->id },
305 order_by => { circ => 'xact_start desc' },
317 __PACKAGE__->register_method(
318 method => 'flesh_copy_custom',
319 api_name => 'open-ils.search.asset.copy.fleshed.custom'
322 sub flesh_copy_custom {
323 my( $self, $conn, $copyid, $fields ) = @_;
324 my $e = new_editor();
325 my $copy = $e->retrieve_asset_copy(
335 ) or return $e->event;
344 __PACKAGE__->register_method(
345 method => "biblio_barcode_to_title",
346 api_name => "open-ils.search.biblio.find_by_barcode",
349 sub biblio_barcode_to_title {
350 my( $self, $client, $barcode ) = @_;
352 my $title = $apputils->simple_scalar_request(
354 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
356 return { ids => [ $title->id ], count => 1 } if $title;
357 return { count => 0 };
360 __PACKAGE__->register_method(
361 method => 'title_id_by_item_barcode',
362 api_name => 'open-ils.search.bib_id.by_barcode'
365 sub title_id_by_item_barcode {
366 my( $self, $conn, $barcode ) = @_;
367 my $e = new_editor();
368 my $copies = $e->search_asset_copy(
370 { deleted => 'f', barcode => $barcode },
374 acp => [ 'call_number' ],
381 return $e->event unless @$copies;
382 return $$copies[0]->call_number->record->id;
386 __PACKAGE__->register_method(
387 method => "biblio_copy_to_mods",
388 api_name => "open-ils.search.biblio.copy.mods.retrieve",
391 # takes a copy object and returns it fleshed mods object
392 sub biblio_copy_to_mods {
393 my( $self, $client, $copy ) = @_;
395 my $volume = $U->cstorereq(
396 "open-ils.cstore.direct.asset.call_number.retrieve",
397 $copy->call_number() );
399 my $mods = _records_to_mods($volume->record());
400 $mods = shift @$mods;
401 $volume->copies([$copy]);
402 push @{$mods->call_numbers()}, $volume;
408 # ----------------------------------------------------------------------------
409 # These are the main OPAC search methods
410 # ----------------------------------------------------------------------------
412 __PACKAGE__->register_method(
413 method => 'the_quest_for_knowledge',
414 api_name => 'open-ils.search.biblio.multiclass',
416 Performs a multi class bilbli or metabib search
417 @param searchhash A search object layed out like so:
418 searches : { "$class" : "$value", ...}
419 org_unit : The org id to focus the search at
420 depth : The org depth
421 limit : The search limit
422 offset : The search offset
423 format : The MARC format
424 sort : What field to sort the results on [ author | title | pubdate ]
425 sort_dir : What direction do we sort? [ asc | desc ]
426 @return An object of the form
427 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
431 __PACKAGE__->register_method(
432 method => 'the_quest_for_knowledge',
433 api_name => 'open-ils.search.biblio.multiclass.staff',
434 signature => q/@see open-ils.search.biblio.multiclass/);
435 __PACKAGE__->register_method(
436 method => 'the_quest_for_knowledge',
437 api_name => 'open-ils.search.metabib.multiclass',
438 signature => q/@see open-ils.search.biblio.multiclass/);
439 __PACKAGE__->register_method(
440 method => 'the_quest_for_knowledge',
441 api_name => 'open-ils.search.metabib.multiclass.staff',
442 signature => q/@see open-ils.search.biblio.multiclass/);
446 sub the_quest_for_knowledge {
447 my( $self, $conn, $searchhash, $docache ) = @_;
449 return { count => 0 } unless $searchhash and
450 ref $searchhash->{searches} eq 'HASH';
452 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
456 if($self->api_name =~ /metabib/) {
458 $method =~ s/biblio/metabib/o;
462 my $offset = $searchhash->{offset} || 0;
463 my $limit = $searchhash->{limit} || 10;
464 my $end = $offset + $limit - 1;
466 # do some simple sanity checking
467 if(!$searchhash->{searches} or
468 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
469 return { count => 0 };
474 $searchhash->{offset} = 0;
475 $searchhash->{limit} = $maxlimit;
477 return { count => 0 } if $offset > $maxlimit;
480 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
481 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
482 my $ckey = $pfx . md5_hex($method . $s);
484 $logger->info("bib search for: $s");
486 $searchhash->{limit} -= $offset;
489 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
493 $method .= ".staff" if($self->api_name =~ /staff/);
494 $method .= ".atomic";
496 for (keys %$searchhash) {
497 delete $$searchhash{$_}
498 unless defined $$searchhash{$_};
501 $result = $U->storagereq( $method, %$searchhash );
507 return {count => 0} unless ($result && $$result[0]);
509 #for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
512 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
517 # If we didn't get this data from the cache, put it into the cache
518 # then return the correct offset of records
519 $logger->debug("putting search cache $ckey\n");
520 put_cache($ckey, $count, \@recs);
523 for ($offset..$end) {
524 last unless $recs[$_];
529 #$logger->debug("cache done .. returning $offset..$end : " . OpenSRF::Utils::JSON->perl2JSON(\@recs));
532 return { ids => \@recs, count => $count };
543 my $end = $offset + $limit - 1;
545 $logger->debug("searching cache for $key : $start..$end\n");
547 return undef unless $cache;
548 my $data = $cache->get_cache($key);
550 return undef unless $data;
552 my $count = $data->[0];
555 return undef unless $offset < $count;
559 for( my $i = $offset; $i <= $end; $i++ ) {
560 last unless my $d = $$data[$i];
564 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
571 my( $key, $count, $data ) = @_;
572 return undef unless $cache;
573 $logger->debug("search_cache putting ".
574 scalar(@$data)." items at key $key with timeout $cache_timeout");
575 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
583 __PACKAGE__->register_method(
584 method => "biblio_mrid_to_modsbatch_batch",
585 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
587 sub biblio_mrid_to_modsbatch_batch {
588 my( $self, $client, $mrids) = @_;
589 warn "Performing mrid_to_modsbatch_batch...";
591 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
592 for my $id (@$mrids) {
593 next unless defined $id;
594 my ($m) = $method->run($id);
601 __PACKAGE__->register_method(
602 method => "biblio_mrid_to_modsbatch",
603 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
604 notes => <<" NOTES");
605 Returns the mvr associated with a given metarecod. If none exists,
609 __PACKAGE__->register_method(
610 method => "biblio_mrid_to_modsbatch",
611 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
612 notes => <<" NOTES");
613 Returns the mvr associated with a given metarecod. If none exists,
617 sub biblio_mrid_to_modsbatch {
618 my( $self, $client, $mrid, $args) = @_;
620 warn "Grabbing mvr for $mrid\n";
622 my ($mr, $evt) = _grab_metarecord($mrid);
623 return $evt unless $mr;
625 my $mvr = $self->biblio_mrid_check_mvr($client, $mr);
626 $mvr = $self->biblio_mrid_make_modsbatch( $client, $mr ) unless $mvr;
628 return $mvr unless ref($args);
630 # Here we find the lead record appropriate for the given filters
631 # and use that for the title and author of the metarecord
632 my $format = $$args{format};
633 my $org = $$args{org};
634 my $depth = $$args{depth};
636 return $mvr unless $format or $org or $depth;
638 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
639 $method = "$method.staff" if $self->api_name =~ /staff/o;
641 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
643 if( my $mods = $U->record_to_mvr($rec) ) {
645 $mvr->title($mods->title);
646 $mvr->title($mods->author);
647 $logger->debug("mods_slim updating title and ".
648 "author in mvr with ".$mods->title." : ".$mods->author);
654 # converts a metarecord to an mvr
657 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
658 return Fieldmapper::metabib::virtual_record->new($perl);
661 # checks to see if a metarecord has mods, if so returns true;
663 __PACKAGE__->register_method(
664 method => "biblio_mrid_check_mvr",
665 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
666 notes => <<" NOTES");
667 Takes a metarecord ID or a metarecord object and returns true
668 if the metarecord already has an mvr associated with it.
671 sub biblio_mrid_check_mvr {
672 my( $self, $client, $mrid ) = @_;
676 if(ref($mrid)) { $mr = $mrid; }
677 else { ($mr, $evt) = _grab_metarecord($mrid); }
680 warn "Checking mvr for mr " . $mr->id . "\n";
682 return _mr_to_mvr($mr) if $mr->mods();
686 sub _grab_metarecord {
688 #my $e = OpenILS::Utils::Editor->new;
689 my $e = new_editor();
690 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
695 __PACKAGE__->register_method(
696 method => "biblio_mrid_make_modsbatch",
697 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
698 notes => <<" NOTES");
699 Takes either a metarecord ID or a metarecord object.
700 Forces the creations of an mvr for the given metarecord.
701 The created mvr is returned.
704 sub biblio_mrid_make_modsbatch {
705 my( $self, $client, $mrid ) = @_;
707 #my $e = OpenILS::Utils::Editor->new;
708 my $e = new_editor();
715 $mr = $e->retrieve_metabib_metarecord($mrid)
719 my $masterid = $mr->master_record;
720 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
722 my $ids = $U->storagereq(
723 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
724 return undef unless @$ids;
726 my $master = $e->retrieve_biblio_record_entry($masterid)
729 # start the mods batch
730 my $u = OpenILS::Utils::ModsParser->new();
731 $u->start_mods_batch( $master->marc );
733 # grab all of the sub-records and shove them into the batch
734 my @ids = grep { $_ ne $masterid } @$ids;
735 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
740 my $r = $e->retrieve_biblio_record_entry($i);
741 push( @$subrecs, $r ) if $r;
746 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
747 $u->push_mods_batch( $_->marc ) if $_->marc;
751 # finish up and send to the client
752 my $mods = $u->finish_mods_batch();
753 $mods->doc_id($mrid);
754 $client->respond_complete($mods);
757 # now update the mods string in the db
758 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
761 #$e = OpenILS::Utils::Editor->new(xact => 1);
762 $e = new_editor(xact => 1);
763 $e->update_metabib_metarecord($mr)
764 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
773 # converts a mr id into a list of record ids
775 __PACKAGE__->register_method(
776 method => "biblio_mrid_to_record_ids",
777 api_name => "open-ils.search.biblio.metarecord_to_records",
780 __PACKAGE__->register_method(
781 method => "biblio_mrid_to_record_ids",
782 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
785 sub biblio_mrid_to_record_ids {
786 my( $self, $client, $mrid, $args ) = @_;
788 my $format = $$args{format};
789 my $org = $$args{org};
790 my $depth = $$args{depth};
792 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
793 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
794 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
796 return { count => scalar(@$recs), ids => $recs };
800 __PACKAGE__->register_method(
801 method => "biblio_record_to_marc_html",
802 api_name => "open-ils.search.biblio.record.html" );
804 my $parser = XML::LibXML->new();
805 my $xslt = XML::LibXSLT->new();
808 my $settings_client = OpenSRF::Utils::SettingsClient->new();
809 sub biblio_record_to_marc_html {
810 my( $self, $client, $recordid ) = @_;
813 my $dir = $settings_client->config_value( "dirs", "xsl" );
814 my $xsl = $settings_client->config_value(
815 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
817 $xsl = $parser->parse_file("$dir/$xsl");
818 $marc_sheet = $xslt->parse_stylesheet( $xsl );
822 my $record = $apputils->simple_scalar_request(
824 "open-ils.cstore.direct.biblio.record_entry.retrieve",
827 my $xmldoc = $parser->parse_string($record->marc);
828 my $html = $marc_sheet->transform($xmldoc);
829 $html = $html->toString();
836 __PACKAGE__->register_method(
837 method => "retrieve_all_copy_locations",
838 api_name => "open-ils.search.config.copy_location.retrieve.all" );
840 my $shelving_locations;
841 sub retrieve_all_copy_locations {
842 my( $self, $client ) = @_;
843 if(!$shelving_locations) {
844 $shelving_locations = $apputils->simple_scalar_request(
846 "open-ils.cstore.direct.asset.copy_location.search.atomic",
847 { id => { "!=" => undef } }
850 return $shelving_locations;
856 __PACKAGE__->register_method(
857 method => "retrieve_all_copy_statuses",
858 api_name => "open-ils.search.config.copy_status.retrieve.all" );
861 sub retrieve_all_copy_statuses {
862 my( $self, $client ) = @_;
863 return $copy_statuses if $copy_statuses;
864 return $copy_statuses =
865 new_editor()->retrieve_all_config_copy_status();
869 __PACKAGE__->register_method(
870 method => "copy_counts_per_org",
871 api_name => "open-ils.search.biblio.copy_counts.retrieve");
873 __PACKAGE__->register_method(
874 method => "copy_counts_per_org",
875 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
877 sub copy_counts_per_org {
878 my( $self, $client, $record_id ) = @_;
880 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
882 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
883 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
885 my $counts = $apputils->simple_scalar_request(
886 "open-ils.storage", $method, $record_id );
888 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
893 __PACKAGE__->register_method(
894 method => "copy_count_summary",
895 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
896 notes => <<" NOTES");
897 returns an array of these:
898 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
899 where statusx is a copy status name. the statuses are sorted
903 sub copy_count_summary {
904 my( $self, $client, $rid, $org, $depth ) = @_;
907 my $data = $U->storagereq(
908 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
910 return [ sort { $a->[1] cmp $b->[1] } @$data ];
916 __PACKAGE__->register_method(
917 method => "multiclass_search",
918 api_name => "open-ils.search.biblio.multiclass",
919 notes => <<" NOTES");
920 Performs a multiclass search
921 PARAMS( searchBlob, org_unit, format, limit )
922 where searchBlob is defined like this:
924 "title" : { "term" : "water" },
925 "author" : { "term" : "smith" },
930 __PACKAGE__->register_method(
931 method => "multiclass_search",
932 api_name => "open-ils.search.biblio.multiclass.staff",
933 notes => "see open-ils.search.biblio.multiclass" );
935 sub multiclass_search {
936 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
938 $logger->debug("Performing multiclass search with org => $orgid, " .
939 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
941 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
942 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
945 my $records = $apputils->simplereq(
946 'open-ils.storage', $meth,
947 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
952 if( ref($records) and $records->[0] and
953 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
955 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
957 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
958 return { ids => $recs, count => $count };
964 __PACKAGE__->register_method(
965 method => "multiclass_search",
966 api_name => "open-ils.search.biblio.multiclass",
968 Performs a multiclass search
969 @param args A names hash of arguments:
970 org_unit : The org to focus the search on
971 depth : The search depth
974 offset : Search offset
975 searches : A named hash of searches which has the following format:
977 "title" : { "term" : "water" },
978 "author" : { "term" : "smith" },
981 @return { ids : <array of ids>, count : hitcount }
985 __PACKAGE__->register_method(
986 method => "multiclass_search",
987 api_name => "open-ils.search.biblio.multiclass.staff",
988 notes => q/@see open-ils.search.biblio.multiclass/ );
990 sub multiclass_search {
991 my( $self, $client, $args ) = @_;
993 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
994 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
995 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
997 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1002 if( ref($records) and $records->[0] and
1003 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1005 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1007 return { ids => $recs, count => $count };
1014 __PACKAGE__->register_method(
1015 method => "marc_search",
1016 api_name => "open-ils.search.biblio.marc.staff");
1018 __PACKAGE__->register_method(
1019 method => "marc_search",
1020 api_name => "open-ils.search.biblio.marc",
1021 notes => <<" NOTES");
1023 open-ils.storage.biblio.full_rec.multi_search.atomic
1024 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1025 "limit":5,"sort":"author","item_type":"g"}
1029 my( $self, $conn, $args, $limit, $offset ) = @_;
1031 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1032 $method .= ".staff" if $self->api_name =~ /staff/;
1033 $method .= ".atomic";
1039 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1040 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1042 my $recs = search_cache($ckey, $offset, $limit);
1045 $recs = $U->storagereq($method, %$args) || [];
1047 put_cache($ckey, scalar(@$recs), $recs);
1048 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1055 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1056 my @recs = map { $_->[0] } @$recs;
1058 return { ids => \@recs, count => $count };
1062 __PACKAGE__->register_method(
1063 method => "biblio_search_isbn",
1064 api_name => "open-ils.search.biblio.isbn",
1067 sub biblio_search_isbn {
1068 my( $self, $client, $isbn ) = @_;
1069 $logger->debug("Searching ISBN $isbn");
1070 my $e = new_editor();
1071 my $recs = $U->storagereq(
1072 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1073 return { ids => $recs, count => scalar(@$recs) };
1077 __PACKAGE__->register_method(
1078 method => "biblio_search_issn",
1079 api_name => "open-ils.search.biblio.issn",
1082 sub biblio_search_issn {
1083 my( $self, $client, $issn ) = @_;
1084 $logger->debug("Searching ISSN $issn");
1085 my $e = new_editor();
1086 my $recs = $U->storagereq(
1087 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1088 return { ids => $recs, count => scalar(@$recs) };
1094 __PACKAGE__->register_method(
1095 method => "fetch_mods_by_copy",
1096 api_name => "open-ils.search.biblio.mods_from_copy",
1099 sub fetch_mods_by_copy {
1100 my( $self, $client, $copyid ) = @_;
1101 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1102 return $evt if $evt;
1103 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1104 return $apputils->record_to_mvr($record);
1109 # -------------------------------------------------------------------------------------
1111 __PACKAGE__->register_method(
1112 method => "cn_browse",
1113 api_name => "open-ils.search.callnumber.browse.target",
1114 notes => "Starts a callnumber browse"
1117 __PACKAGE__->register_method(
1118 method => "cn_browse",
1119 api_name => "open-ils.search.callnumber.browse.page_up",
1120 notes => "Returns the previous page of callnumbers",
1123 __PACKAGE__->register_method(
1124 method => "cn_browse",
1125 api_name => "open-ils.search.callnumber.browse.page_down",
1126 notes => "Returns the next page of callnumbers",
1130 # RETURNS array of arrays like so: label, owning_lib, record, id
1132 my( $self, $client, @params ) = @_;
1135 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1136 if( $self->api_name =~ /target/ );
1137 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1138 if( $self->api_name =~ /page_up/ );
1139 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1140 if( $self->api_name =~ /page_down/ );
1142 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1144 # -------------------------------------------------------------------------------------
1146 __PACKAGE__->register_method(
1147 method => "fetch_cn",
1148 api_name => "open-ils.search.callnumber.retrieve",
1149 notes => "retrieves a callnumber based on ID",
1153 my( $self, $client, $id ) = @_;
1154 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1155 return $evt if $evt;
1159 __PACKAGE__->register_method (
1160 method => "fetch_copy_by_cn",
1161 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1163 Returns an array of copy id's by callnumber id
1164 @param cnid The callnumber id
1165 @return An array of copy ids
1169 sub fetch_copy_by_cn {
1170 my( $self, $conn, $cnid ) = @_;
1171 return $U->cstorereq(
1172 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1173 { call_number => $cnid, deleted => 'f' } );
1176 __PACKAGE__->register_method (
1177 method => 'fetch_cn_by_info',
1178 api_name => 'open-ils.search.call_number.retrieve_by_info',
1180 @param label The callnumber label
1181 @param record The record the cn is attached to
1182 @param org The owning library of the cn
1183 @return The callnumber object
1188 sub fetch_cn_by_info {
1189 my( $self, $conn, $label, $record, $org ) = @_;
1190 return $U->cstorereq(
1191 'open-ils.cstore.direct.asset.call_number.search',
1192 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1199 __PACKAGE__->register_method (
1200 method => 'bib_extras',
1201 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1202 __PACKAGE__->register_method (
1203 method => 'bib_extras',
1204 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1205 __PACKAGE__->register_method (
1206 method => 'bib_extras',
1207 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1208 __PACKAGE__->register_method (
1209 method => 'bib_extras',
1210 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1215 my $e = new_editor();
1217 return $e->retrieve_all_config_lit_form_map()
1218 if( $self->api_name =~ /lit_form/ );
1220 return $e->retrieve_all_config_item_form_map()
1221 if( $self->api_name =~ /item_form_map/ );
1223 return $e->retrieve_all_config_item_type_map()
1224 if( $self->api_name =~ /item_type_map/ );
1226 return $e->retrieve_all_config_audience_map()
1227 if( $self->api_name =~ /audience_map/ );
1234 __PACKAGE__->register_method(
1235 method => 'fetch_slim_record',
1236 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1238 Returns a biblio.record_entry without the attached marcxml
1242 sub fetch_slim_record {
1243 my( $self, $conn, $ids ) = @_;
1245 #my $editor = OpenILS::Utils::Editor->new;
1246 my $editor = new_editor();
1249 return $editor->event unless
1250 my $r = $editor->retrieve_biblio_record_entry($_);
1259 __PACKAGE__->register_method(
1260 method => 'rec_to_mr_rec_descriptors',
1261 api_name => 'open-ils.search.metabib.record_to_descriptors',
1263 specialized method...
1264 Given a biblio record id or a metarecord id,
1265 this returns a list of metabib.record_descriptor
1266 objects that live within the same metarecord
1267 @param args Object of args including:
1271 sub rec_to_mr_rec_descriptors {
1272 my( $self, $conn, $args ) = @_;
1274 my $rec = $$args{record};
1275 my $mrec = $$args{metarecord};
1276 my $item_forms = $$args{item_forms};
1277 my $item_types = $$args{item_types};
1278 my $item_lang = $$args{item_lang};
1280 my $e = new_editor();
1284 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1285 return $e->event unless @$map;
1286 $mrec = $$map[0]->metarecord;
1289 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1290 return $e->event unless @$recs;
1292 my @recs = map { $_->source } @$recs;
1293 my $search = { record => \@recs };
1294 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1295 $search->{item_type} = $item_types if $item_types and @$item_types;
1296 $search->{item_lang} = $item_lang if $item_lang;
1298 my $desc = $e->search_metabib_record_descriptor($search);
1300 return { metarecord => $mrec, descriptors => $desc };
1306 __PACKAGE__->register_method(
1307 method => 'copies_created_on',
1311 sub copies_created_on {
1312 my( $self, $conn, $auth, $org, $date ) = @_;
1313 my $e = new_editor(authtoken=>$auth);
1314 return $e->event unless $e->checkauth;
1318 __PACKAGE__->register_method(
1319 method => 'fetch_age_protect',
1320 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1323 sub fetch_age_protect {
1324 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1328 __PACKAGE__->register_method(
1329 method => 'copies_by_cn_label',
1330 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1333 __PACKAGE__->register_method(
1334 method => 'copies_by_cn_label',
1335 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1338 sub copies_by_cn_label {
1339 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1340 my $e = new_editor();
1341 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1342 return [] unless @$cns;
1344 # show all non-deleted copies in the staff client ...
1345 if ($self->api_name =~ /staff$/o) {
1346 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1349 # ... otherwise, grab the copies ...
1350 my $copies = $e->search_asset_copy(
1351 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1352 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1356 # ... and test for location and status visibility
1357 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];