1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
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/;
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 ) = @_;
180 $tcn =~ s/.*?(\w+)\s*$/$1/o;
182 my $e = new_editor();
183 my $recs = $e->search_biblio_record_entry(
184 {deleted => 'f', tcn_value => $tcn}, {idlist =>1});
186 return { count => scalar(@$recs), ids => $recs };
190 # --------------------------------------------------------------------------------
192 __PACKAGE__->register_method(
193 method => "biblio_barcode_to_copy",
194 api_name => "open-ils.search.asset.copy.find_by_barcode",);
195 sub biblio_barcode_to_copy {
196 my( $self, $client, $barcode ) = @_;
197 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
202 __PACKAGE__->register_method(
203 method => "biblio_id_to_copy",
204 api_name => "open-ils.search.asset.copy.batch.retrieve",);
205 sub biblio_id_to_copy {
206 my( $self, $client, $ids ) = @_;
207 $logger->info("Fetching copies @$ids");
208 return $U->cstorereq(
209 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
213 __PACKAGE__->register_method(
214 method => "copy_retrieve",
215 api_name => "open-ils.search.asset.copy.retrieve",);
217 my( $self, $client, $cid ) = @_;
218 my( $copy, $evt ) = $U->fetch_copy($cid);
223 __PACKAGE__->register_method(
224 method => "volume_retrieve",
225 api_name => "open-ils.search.asset.call_number.retrieve");
226 sub volume_retrieve {
227 my( $self, $client, $vid ) = @_;
228 my $e = new_editor();
229 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
233 __PACKAGE__->register_method(
234 method => "fleshed_copy_retrieve_batch",
235 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
237 sub fleshed_copy_retrieve_batch {
238 my( $self, $client, $ids ) = @_;
239 $logger->info("Fetching fleshed copies @$ids");
240 return $U->cstorereq(
241 "open-ils.cstore.direct.asset.copy.search.atomic",
244 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
249 __PACKAGE__->register_method(
250 method => "fleshed_copy_retrieve",
251 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
253 sub fleshed_copy_retrieve {
254 my( $self, $client, $id ) = @_;
255 my( $c, $e) = $U->fetch_fleshed_copy($id);
262 __PACKAGE__->register_method(
263 method => 'fleshed_by_barcode',
264 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",);
265 sub fleshed_by_barcode {
266 my( $self, $conn, $barcode ) = @_;
267 my $e = new_editor();
268 my $copyid = $e->search_asset_copy(
269 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
271 return $self->fleshed_copy_retrieve2($conn, $copyid);
275 __PACKAGE__->register_method(
276 method => "fleshed_copy_retrieve2",
277 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
279 sub fleshed_copy_retrieve2 {
280 my( $self, $client, $id ) = @_;
281 my $e = new_editor();
282 my $copy = $e->retrieve_asset_copy(
288 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
289 ascecm => [ qw/ stat_cat stat_cat_entry / ],
293 ) or return $e->event;
295 # For backwards compatibility
296 $copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
298 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
300 $e->search_action_circulation(
302 { target_copy => $copy->id },
304 order_by => { circ => 'xact_start desc' },
319 __PACKAGE__->register_method(
320 method => "biblio_barcode_to_title",
321 api_name => "open-ils.search.biblio.find_by_barcode",
324 sub biblio_barcode_to_title {
325 my( $self, $client, $barcode ) = @_;
327 my $title = $apputils->simple_scalar_request(
329 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
331 return { ids => [ $title->id ], count => 1 } if $title;
332 return { count => 0 };
336 __PACKAGE__->register_method(
337 method => "biblio_copy_to_mods",
338 api_name => "open-ils.search.biblio.copy.mods.retrieve",
341 # takes a copy object and returns it fleshed mods object
342 sub biblio_copy_to_mods {
343 my( $self, $client, $copy ) = @_;
345 my $volume = $U->cstorereq(
346 "open-ils.cstore.direct.asset.call_number.retrieve",
347 $copy->call_number() );
349 my $mods = _records_to_mods($volume->record());
350 $mods = shift @$mods;
351 $volume->copies([$copy]);
352 push @{$mods->call_numbers()}, $volume;
358 # ----------------------------------------------------------------------------
359 # These are the main OPAC search methods
360 # ----------------------------------------------------------------------------
362 __PACKAGE__->register_method(
363 method => 'the_quest_for_knowledge',
364 api_name => 'open-ils.search.biblio.multiclass',
366 Performs a multi class bilbli or metabib search
367 @param searchhash A search object layed out like so:
368 searches : { "$class" : "$value", ...}
369 org_unit : The org id to focus the search at
370 depth : The org depth
371 limit : The search limit
372 offset : The search offset
373 format : The MARC format
374 sort : What field to sort the results on [ author | title | pubdate ]
375 sort_dir : What direction do we sort? [ asc | desc ]
376 @return An object of the form
377 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
381 __PACKAGE__->register_method(
382 method => 'the_quest_for_knowledge',
383 api_name => 'open-ils.search.biblio.multiclass.staff',
384 signature => q/@see open-ils.search.biblio.multiclass/);
385 __PACKAGE__->register_method(
386 method => 'the_quest_for_knowledge',
387 api_name => 'open-ils.search.metabib.multiclass',
388 signature => q/@see open-ils.search.biblio.multiclass/);
389 __PACKAGE__->register_method(
390 method => 'the_quest_for_knowledge',
391 api_name => 'open-ils.search.metabib.multiclass.staff',
392 signature => q/@see open-ils.search.biblio.multiclass/);
396 sub the_quest_for_knowledge {
397 my( $self, $conn, $searchhash, $docache ) = @_;
399 return { count => 0 } unless $searchhash and
400 ref $searchhash->{searches} eq 'HASH';
402 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
406 if($self->api_name =~ /metabib/) {
408 $method =~ s/biblio/metabib/o;
412 my $offset = $searchhash->{offset} || 0;
413 my $limit = $searchhash->{limit} || 10;
414 my $end = $offset + $limit - 1;
416 # do some simple sanity checking
417 if(!$searchhash->{searches} or
419 !$searchhash->{searches}->{title} and
420 !$searchhash->{searches}->{author} and
421 !$searchhash->{searches}->{subject} and
422 !$searchhash->{searches}->{series} and
423 !$searchhash->{searches}->{keyword} ) ) {
424 return { count => 0 };
429 $searchhash->{offset} = 0;
430 $searchhash->{limit} = $maxlimit;
432 return { count => 0 } if $offset > $maxlimit;
435 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
436 my $s = JSON->perl2JSON(\@search);
437 my $ckey = $pfx . md5_hex($method . $s);
439 $logger->info("bib search for: $s");
441 $searchhash->{limit} -= $offset;
444 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
448 $method .= ".staff" if($self->api_name =~ /staff/);
449 $method .= ".atomic";
451 for (keys %$searchhash) {
452 delete $$searchhash{$_}
453 unless defined $$searchhash{$_};
456 $result = $U->storagereq( $method, %$searchhash );
462 return {count => 0} unless ($result && $$result[0]);
464 #for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
467 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
472 # If we didn't get this data from the cache, put it into the cache
473 # then return the correct offset of records
474 $logger->debug("putting search cache $ckey\n");
475 put_cache($ckey, $count, \@recs);
478 for ($offset..$end) {
479 last unless $recs[$_];
484 #$logger->debug("cache done .. returning $offset..$end : " . JSON->perl2JSON(\@recs));
487 return { ids => \@recs, count => $count };
498 my $end = $offset + $limit - 1;
500 $logger->debug("searching cache for $key : $start..$end\n");
502 return undef unless $cache;
503 my $data = $cache->get_cache($key);
505 return undef unless $data;
507 my $count = $data->[0];
510 return undef unless $offset < $count;
514 for( my $i = $offset; $i <= $end; $i++ ) {
515 last unless my $d = $$data[$i];
519 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
526 my( $key, $count, $data ) = @_;
527 return undef unless $cache;
528 $logger->debug("search_cache putting ".
529 scalar(@$data)." items at key $key with timeout $cache_timeout");
530 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
538 __PACKAGE__->register_method(
539 method => "biblio_mrid_to_modsbatch_batch",
540 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
542 sub biblio_mrid_to_modsbatch_batch {
543 my( $self, $client, $mrids) = @_;
544 warn "Performing mrid_to_modsbatch_batch...";
546 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
547 for my $id (@$mrids) {
548 next unless defined $id;
549 my ($m) = $method->run($id);
556 __PACKAGE__->register_method(
557 method => "biblio_mrid_to_modsbatch",
558 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
559 notes => <<" NOTES");
560 Returns the mvr associated with a given metarecod. If none exists,
564 __PACKAGE__->register_method(
565 method => "biblio_mrid_to_modsbatch",
566 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
567 notes => <<" NOTES");
568 Returns the mvr associated with a given metarecod. If none exists,
572 sub biblio_mrid_to_modsbatch {
573 my( $self, $client, $mrid, $args) = @_;
575 warn "Grabbing mvr for $mrid\n";
577 my ($mr, $evt) = _grab_metarecord($mrid);
578 return $evt unless $mr;
580 my $mvr = $self->biblio_mrid_check_mvr($client, $mr);
581 $mvr = $self->biblio_mrid_make_modsbatch( $client, $mr ) unless $mvr;
583 return $mvr unless ref($args);
585 # Here we find the lead record appropriate for the given filters
586 # and use that for the title and author of the metarecord
587 my $format = $$args{format};
588 my $org = $$args{org};
589 my $depth = $$args{depth};
591 return $mvr unless $format or $org or $depth;
593 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
594 $method = "$method.staff" if $self->api_name =~ /staff/o;
596 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
598 if( my $mods = $U->record_to_mvr($rec) ) {
600 $mvr->title($mods->title);
601 $mvr->title($mods->author);
602 $logger->debug("mods_slim updating title and ".
603 "author in mvr with ".$mods->title." : ".$mods->author);
609 # converts a metarecord to an mvr
612 my $perl = JSON->JSON2perl($mr->mods());
613 return Fieldmapper::metabib::virtual_record->new($perl);
616 # checks to see if a metarecord has mods, if so returns true;
618 __PACKAGE__->register_method(
619 method => "biblio_mrid_check_mvr",
620 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
621 notes => <<" NOTES");
622 Takes a metarecord ID or a metarecord object and returns true
623 if the metarecord already has an mvr associated with it.
626 sub biblio_mrid_check_mvr {
627 my( $self, $client, $mrid ) = @_;
631 if(ref($mrid)) { $mr = $mrid; }
632 else { ($mr, $evt) = _grab_metarecord($mrid); }
635 warn "Checking mvr for mr " . $mr->id . "\n";
637 return _mr_to_mvr($mr) if $mr->mods();
641 sub _grab_metarecord {
643 #my $e = OpenILS::Utils::Editor->new;
644 my $e = new_editor();
645 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
650 __PACKAGE__->register_method(
651 method => "biblio_mrid_make_modsbatch",
652 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
653 notes => <<" NOTES");
654 Takes either a metarecord ID or a metarecord object.
655 Forces the creations of an mvr for the given metarecord.
656 The created mvr is returned.
659 sub biblio_mrid_make_modsbatch {
660 my( $self, $client, $mrid ) = @_;
662 #my $e = OpenILS::Utils::Editor->new;
663 my $e = new_editor();
670 $mr = $e->retrieve_metabib_metarecord($mrid)
674 my $masterid = $mr->master_record;
675 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
677 my $ids = $U->storagereq(
678 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
679 return undef unless @$ids;
681 my $master = $e->retrieve_biblio_record_entry($masterid)
684 # start the mods batch
685 my $u = OpenILS::Utils::ModsParser->new();
686 $u->start_mods_batch( $master->marc );
688 # grab all of the sub-records and shove them into the batch
689 my @ids = grep { $_ ne $masterid } @$ids;
690 my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
693 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
694 $u->push_mods_batch( $_->marc ) if $_->marc;
698 # finish up and send to the client
699 my $mods = $u->finish_mods_batch();
700 $mods->doc_id($mrid);
701 $client->respond_complete($mods);
704 # now update the mods string in the db
705 my $string = JSON->perl2JSON($mods->decast);
708 #$e = OpenILS::Utils::Editor->new(xact => 1);
709 $e = new_editor(xact => 1);
710 $e->update_metabib_metarecord($mr)
711 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
720 # converts a mr id into a list of record ids
722 __PACKAGE__->register_method(
723 method => "biblio_mrid_to_record_ids",
724 api_name => "open-ils.search.biblio.metarecord_to_records",
727 __PACKAGE__->register_method(
728 method => "biblio_mrid_to_record_ids",
729 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
732 sub biblio_mrid_to_record_ids {
733 my( $self, $client, $mrid, $args ) = @_;
735 my $format = $$args{format};
736 my $org = $$args{org};
737 my $depth = $$args{depth};
739 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
740 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
741 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
743 return { count => scalar(@$recs), ids => $recs };
747 __PACKAGE__->register_method(
748 method => "biblio_record_to_marc_html",
749 api_name => "open-ils.search.biblio.record.html" );
751 my $parser = XML::LibXML->new();
752 my $xslt = XML::LibXSLT->new();
755 my $settings_client = OpenSRF::Utils::SettingsClient->new();
756 sub biblio_record_to_marc_html {
757 my( $self, $client, $recordid ) = @_;
760 my $dir = $settings_client->config_value( "dirs", "xsl" );
761 my $xsl = $settings_client->config_value(
762 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
764 $xsl = $parser->parse_file("$dir/$xsl");
765 $marc_sheet = $xslt->parse_stylesheet( $xsl );
769 my $record = $apputils->simple_scalar_request(
771 "open-ils.cstore.direct.biblio.record_entry.retrieve",
774 my $xmldoc = $parser->parse_string($record->marc);
775 my $html = $marc_sheet->transform($xmldoc);
776 $html = $html->toString();
783 __PACKAGE__->register_method(
784 method => "retrieve_all_copy_locations",
785 api_name => "open-ils.search.config.copy_location.retrieve.all" );
787 my $shelving_locations;
788 sub retrieve_all_copy_locations {
789 my( $self, $client ) = @_;
790 if(!$shelving_locations) {
791 $shelving_locations = $apputils->simple_scalar_request(
793 "open-ils.cstore.direct.asset.copy_location.search.atomic",
794 { id => { "!=" => undef } }
797 return $shelving_locations;
803 __PACKAGE__->register_method(
804 method => "retrieve_all_copy_statuses",
805 api_name => "open-ils.search.config.copy_status.retrieve.all" );
808 sub retrieve_all_copy_statuses {
809 my( $self, $client ) = @_;
810 return $copy_statuses if $copy_statuses;
811 return $copy_statuses =
812 new_editor()->retrieve_all_config_copy_status();
816 __PACKAGE__->register_method(
817 method => "copy_counts_per_org",
818 api_name => "open-ils.search.biblio.copy_counts.retrieve");
820 __PACKAGE__->register_method(
821 method => "copy_counts_per_org",
822 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
824 sub copy_counts_per_org {
825 my( $self, $client, $record_id ) = @_;
827 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
829 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
830 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
832 my $counts = $apputils->simple_scalar_request(
833 "open-ils.storage", $method, $record_id );
835 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
840 __PACKAGE__->register_method(
841 method => "copy_count_summary",
842 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
843 notes => <<" NOTES");
844 returns an array of these:
845 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
846 where statusx is a copy status name. the statuses are sorted
850 sub copy_count_summary {
851 my( $self, $client, $rid, $org, $depth ) = @_;
854 return $U->storagereq(
855 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
861 __PACKAGE__->register_method(
862 method => "multiclass_search",
863 api_name => "open-ils.search.biblio.multiclass",
864 notes => <<" NOTES");
865 Performs a multiclass search
866 PARAMS( searchBlob, org_unit, format, limit )
867 where searchBlob is defined like this:
869 "title" : { "term" : "water" },
870 "author" : { "term" : "smith" },
875 __PACKAGE__->register_method(
876 method => "multiclass_search",
877 api_name => "open-ils.search.biblio.multiclass.staff",
878 notes => "see open-ils.search.biblio.multiclass" );
880 sub multiclass_search {
881 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
883 $logger->debug("Performing multiclass search with org => $orgid, " .
884 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
886 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
887 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
890 my $records = $apputils->simplereq(
891 'open-ils.storage', $meth,
892 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
897 if( ref($records) and $records->[0] and
898 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
900 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
902 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
903 return { ids => $recs, count => $count };
909 __PACKAGE__->register_method(
910 method => "multiclass_search",
911 api_name => "open-ils.search.biblio.multiclass",
913 Performs a multiclass search
914 @param args A names hash of arguments:
915 org_unit : The org to focus the search on
916 depth : The search depth
919 offset : Search offset
920 searches : A named hash of searches which has the following format:
922 "title" : { "term" : "water" },
923 "author" : { "term" : "smith" },
926 @return { ids : <array of ids>, count : hitcount }
930 __PACKAGE__->register_method(
931 method => "multiclass_search",
932 api_name => "open-ils.search.biblio.multiclass.staff",
933 notes => q/@see open-ils.search.biblio.multiclass/ );
935 sub multiclass_search {
936 my( $self, $client, $args ) = @_;
938 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
939 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
940 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
942 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
947 if( ref($records) and $records->[0] and
948 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
950 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
952 return { ids => $recs, count => $count };
959 __PACKAGE__->register_method(
960 method => "marc_search",
961 api_name => "open-ils.search.biblio.marc.staff");
963 __PACKAGE__->register_method(
964 method => "marc_search",
965 api_name => "open-ils.search.biblio.marc",
966 notes => <<" NOTES");
968 open-ils.storage.biblio.full_rec.multi_search.atomic
969 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
970 "limit":5,"sort":"author","item_type":"g"}
974 my( $self, $conn, $args, $limit, $offset ) = @_;
976 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
977 $method .= ".staff" if $self->api_name =~ /staff/;
978 $method .= ".atomic";
984 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
985 my $ckey = $pfx . md5_hex($method . JSON->perl2JSON(\@search));
987 my $recs = search_cache($ckey, $offset, $limit);
990 $recs = $U->storagereq($method, %$args);
991 put_cache($ckey, scalar(@$recs), $recs);
992 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
996 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
997 my @recs = map { $_->[0] } @$recs;
999 return { ids => \@recs, count => $count };
1003 __PACKAGE__->register_method(
1004 method => "biblio_search_isbn",
1005 api_name => "open-ils.search.biblio.isbn",
1008 sub biblio_search_isbn {
1009 my( $self, $client, $isbn ) = @_;
1010 $logger->debug("Searching ISBN $isbn");
1011 my $e = new_editor();
1012 my $recs = $U->storagereq(
1013 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1014 return { ids => $recs, count => scalar(@$recs) };
1018 __PACKAGE__->register_method(
1019 method => "biblio_search_issn",
1020 api_name => "open-ils.search.biblio.issn",
1023 sub biblio_search_issn {
1024 my( $self, $client, $issn ) = @_;
1025 $logger->debug("Searching ISSN $issn");
1026 my $e = new_editor();
1027 my $recs = $U->storagereq(
1028 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1029 return { ids => $recs, count => scalar(@$recs) };
1035 __PACKAGE__->register_method(
1036 method => "fetch_mods_by_copy",
1037 api_name => "open-ils.search.biblio.mods_from_copy",
1040 sub fetch_mods_by_copy {
1041 my( $self, $client, $copyid ) = @_;
1042 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1043 return $evt if $evt;
1044 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1045 return $apputils->record_to_mvr($record);
1050 # -------------------------------------------------------------------------------------
1052 __PACKAGE__->register_method(
1053 method => "cn_browse",
1054 api_name => "open-ils.search.callnumber.browse.target",
1055 notes => "Starts a callnumber browse"
1058 __PACKAGE__->register_method(
1059 method => "cn_browse",
1060 api_name => "open-ils.search.callnumber.browse.page_up",
1061 notes => "Returns the previous page of callnumbers",
1064 __PACKAGE__->register_method(
1065 method => "cn_browse",
1066 api_name => "open-ils.search.callnumber.browse.page_down",
1067 notes => "Returns the next page of callnumbers",
1071 # RETURNS array of arrays like so: label, owning_lib, record, id
1073 my( $self, $client, @params ) = @_;
1076 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1077 if( $self->api_name =~ /target/ );
1078 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1079 if( $self->api_name =~ /page_up/ );
1080 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1081 if( $self->api_name =~ /page_down/ );
1083 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1085 # -------------------------------------------------------------------------------------
1087 __PACKAGE__->register_method(
1088 method => "fetch_cn",
1089 api_name => "open-ils.search.callnumber.retrieve",
1090 notes => "retrieves a callnumber based on ID",
1094 my( $self, $client, $id ) = @_;
1095 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1096 return $evt if $evt;
1100 __PACKAGE__->register_method (
1101 method => "fetch_copy_by_cn",
1102 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1104 Returns an array of copy id's by callnumber id
1105 @param cnid The callnumber id
1106 @return An array of copy ids
1110 sub fetch_copy_by_cn {
1111 my( $self, $conn, $cnid ) = @_;
1112 return $U->cstorereq(
1113 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1114 { call_number => $cnid, deleted => 'f' } );
1117 __PACKAGE__->register_method (
1118 method => 'fetch_cn_by_info',
1119 api_name => 'open-ils.search.call_number.retrieve_by_info',
1121 @param label The callnumber label
1122 @param record The record the cn is attached to
1123 @param org The owning library of the cn
1124 @return The callnumber object
1129 sub fetch_cn_by_info {
1130 my( $self, $conn, $label, $record, $org ) = @_;
1131 return $U->cstorereq(
1132 'open-ils.cstore.direct.asset.call_number.search',
1133 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1140 __PACKAGE__->register_method (
1141 method => 'bib_extras',
1142 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1143 __PACKAGE__->register_method (
1144 method => 'bib_extras',
1145 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1146 __PACKAGE__->register_method (
1147 method => 'bib_extras',
1148 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1149 __PACKAGE__->register_method (
1150 method => 'bib_extras',
1151 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1156 my $e = new_editor();
1158 return $e->retrieve_all_config_lit_form_map()
1159 if( $self->api_name =~ /lit_form/ );
1161 return $e->retrieve_all_config_item_form_map()
1162 if( $self->api_name =~ /item_form_map/ );
1164 return $e->retrieve_all_config_item_type_map()
1165 if( $self->api_name =~ /item_type_map/ );
1167 return $e->retrieve_all_config_audience_map()
1168 if( $self->api_name =~ /audience_map/ );
1175 __PACKAGE__->register_method(
1176 method => 'fetch_slim_record',
1177 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1179 Returns a biblio.record_entry without the attached marcxml
1183 sub fetch_slim_record {
1184 my( $self, $conn, $ids ) = @_;
1186 #my $editor = OpenILS::Utils::Editor->new;
1187 my $editor = new_editor();
1190 return $editor->event unless
1191 my $r = $editor->retrieve_biblio_record_entry($_);
1200 __PACKAGE__->register_method(
1201 method => 'rec_to_mr_rec_descriptors',
1202 api_name => 'open-ils.search.metabib.record_to_descriptors',
1204 specialized method...
1205 Given a biblio record id or a metarecord id,
1206 this returns a list of metabib.record_descriptor
1207 objects that live within the same metarecord
1208 @param args Object of args including:
1212 sub rec_to_mr_rec_descriptors {
1213 my( $self, $conn, $args ) = @_;
1215 my $rec = $$args{record};
1216 my $mrec = $$args{metarecord};
1217 my $item_forms = $$args{item_forms};
1218 my $item_types = $$args{item_types};
1219 my $item_lang = $$args{item_lang};
1221 my $e = new_editor();
1225 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1226 return $e->event unless @$map;
1227 $mrec = $$map[0]->metarecord;
1230 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1231 return $e->event unless @$recs;
1233 my @recs = map { $_->source } @$recs;
1234 my $search = { record => \@recs };
1235 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1236 $search->{item_type} = $item_types if $item_types and @$item_types;
1237 $search->{item_lang} = $item_lang if $item_lang;
1239 my $desc = $e->search_metabib_record_descriptor($search);
1241 return { metarecord => $mrec, descriptors => $desc };
1247 __PACKAGE__->register_method(
1248 method => 'copies_created_on',
1252 sub copies_created_on {
1253 my( $self, $conn, $auth, $org, $date ) = @_;
1254 my $e = new_editor(authtoken=>$auth);
1255 return $e->event unless $e->checkauth;
1259 __PACKAGE__->register_method(
1260 method => 'fetch_age_protect',
1261 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1264 sub fetch_age_protect {
1265 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1269 __PACKAGE__->register_method(
1270 method => 'copies_by_cn_label',
1271 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1274 sub copies_by_cn_label {
1275 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1276 my $e = new_editor();
1277 my $cns = $e->search_asset_call_number({record => $record, label => $label}, {idlist=>1});
1278 return [] unless @$cns;
1279 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib}, {idlist=>1});