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;
14 use OpenSRF::Utils::Logger qw/:logger/;
17 use OpenSRF::Utils::JSON;
19 use Time::HiRes qw(time);
20 use OpenSRF::EX qw(:try);
21 use Digest::MD5 qw(md5_hex);
27 $Data::Dumper::Indent = 0;
29 use OpenILS::Const qw/:const/;
31 use OpenILS::Application::AppUtils;
32 my $apputils = "OpenILS::Application::AppUtils";
35 my $pfx = "open-ils.search_";
41 $cache = OpenSRF::Utils::Cache->new('global');
42 my $sclient = OpenSRF::Utils::SettingsClient->new();
43 $cache_timeout = $sclient->config_value(
44 "apps", "open-ils.search", "app_settings", "cache_timeout" ) || 300;
45 $logger->info("Search cache timeout is $cache_timeout");
50 # ---------------------------------------------------------------------------
51 # takes a list of record id's and turns the docs into friendly
52 # mods structures. Creates one MODS structure for each doc id.
53 # ---------------------------------------------------------------------------
54 sub _records_to_mods {
60 my $session = OpenSRF::AppSession->create("open-ils.cstore");
61 my $request = $session->request(
62 "open-ils.cstore.direct.biblio.record_entry.search", { id => \@ids } );
64 while( my $resp = $request->recv ) {
65 my $content = $resp->content;
66 next if $content->id == OILS_PRECAT_RECORD;
67 my $u = OpenILS::Utils::ModsParser->new();
68 $u->start_mods_batch( $content->marc );
69 my $mods = $u->finish_mods_batch();
70 $mods->doc_id($content->id());
71 $mods->tcn($content->tcn_value);
75 $session->disconnect();
79 __PACKAGE__->register_method(
80 method => "record_id_to_mods",
81 api_name => "open-ils.search.biblio.record.mods.retrieve",
83 note => "Provide ID, we provide the mods"
86 # converts a record into a mods object with copy counts attached
87 sub record_id_to_mods {
89 my( $self, $client, $org_id, $id ) = @_;
91 my $mods_list = _records_to_mods( $id );
92 my $mods_obj = $mods_list->[0];
93 my $cmethod = $self->method_lookup(
94 "open-ils.search.biblio.record.copy_count");
95 my ($count) = $cmethod->run($org_id, $id);
96 $mods_obj->copy_count($count);
103 __PACKAGE__->register_method(
104 method => "record_id_to_mods_slim",
105 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
107 note => "Provide ID, we provide the mods"
110 # converts a record into a mods object with NO copy counts attached
111 sub record_id_to_mods_slim {
112 my( $self, $client, $id ) = @_;
113 return undef unless defined $id;
115 if(ref($id) and ref($id) == 'ARRAY') {
116 return _records_to_mods( @$id );
118 my $mods_list = _records_to_mods( $id );
119 my $mods_obj = $mods_list->[0];
120 return OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND') unless $mods_obj;
125 # Returns the number of copies attached to a record based on org location
126 __PACKAGE__->register_method(
127 method => "record_id_to_copy_count",
128 api_name => "open-ils.search.biblio.record.copy_count",
131 __PACKAGE__->register_method(
132 method => "record_id_to_copy_count",
133 api_name => "open-ils.search.biblio.record.copy_count.staff",
136 __PACKAGE__->register_method(
137 method => "record_id_to_copy_count",
138 api_name => "open-ils.search.biblio.metarecord.copy_count",
141 __PACKAGE__->register_method(
142 method => "record_id_to_copy_count",
143 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
145 sub record_id_to_copy_count {
146 my( $self, $client, $org_id, $record_id, $format ) = @_;
148 return [] unless $record_id;
149 $format = undef if (!$format or $format eq 'all');
151 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
154 if($self->api_name =~ /metarecord/) {
155 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
159 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
161 my $count = $U->storagereq( $method,
162 org_unit => $org_id, $key => $record_id, format => $format );
164 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
170 __PACKAGE__->register_method(
171 method => "biblio_search_tcn",
172 api_name => "open-ils.search.biblio.tcn",
174 note => "Retrieve a record by TCN",
177 sub biblio_search_tcn {
179 my( $self, $client, $tcn, $include_deleted ) = @_;
181 $tcn =~ s/.*?(\w+)\s*$/$1/o;
183 my $e = new_editor();
184 my $search = {tcn_value => $tcn};
185 $search->{deleted} = 'f' unless $include_deleted;
186 my $recs = $e->search_biblio_record_entry( $search, {idlist =>1} );
188 return { count => scalar(@$recs), ids => $recs };
192 # --------------------------------------------------------------------------------
194 __PACKAGE__->register_method(
195 method => "biblio_barcode_to_copy",
196 api_name => "open-ils.search.asset.copy.find_by_barcode",);
197 sub biblio_barcode_to_copy {
198 my( $self, $client, $barcode ) = @_;
199 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
204 __PACKAGE__->register_method(
205 method => "biblio_id_to_copy",
206 api_name => "open-ils.search.asset.copy.batch.retrieve",);
207 sub biblio_id_to_copy {
208 my( $self, $client, $ids ) = @_;
209 $logger->info("Fetching copies @$ids");
210 return $U->cstorereq(
211 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
215 __PACKAGE__->register_method(
216 method => "copy_retrieve",
217 api_name => "open-ils.search.asset.copy.retrieve",);
219 my( $self, $client, $cid ) = @_;
220 my( $copy, $evt ) = $U->fetch_copy($cid);
225 __PACKAGE__->register_method(
226 method => "volume_retrieve",
227 api_name => "open-ils.search.asset.call_number.retrieve");
228 sub volume_retrieve {
229 my( $self, $client, $vid ) = @_;
230 my $e = new_editor();
231 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
235 __PACKAGE__->register_method(
236 method => "fleshed_copy_retrieve_batch",
237 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
239 sub fleshed_copy_retrieve_batch {
240 my( $self, $client, $ids ) = @_;
241 $logger->info("Fetching fleshed copies @$ids");
242 return $U->cstorereq(
243 "open-ils.cstore.direct.asset.copy.search.atomic",
246 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
251 __PACKAGE__->register_method(
252 method => "fleshed_copy_retrieve",
253 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
255 sub fleshed_copy_retrieve {
256 my( $self, $client, $id ) = @_;
257 my( $c, $e) = $U->fetch_fleshed_copy($id);
264 __PACKAGE__->register_method(
265 method => 'fleshed_by_barcode',
266 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",);
267 sub fleshed_by_barcode {
268 my( $self, $conn, $barcode ) = @_;
269 my $e = new_editor();
270 my $copyid = $e->search_asset_copy(
271 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
273 return $self->fleshed_copy_retrieve2($conn, $copyid);
277 __PACKAGE__->register_method(
278 method => "fleshed_copy_retrieve2",
279 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
281 sub fleshed_copy_retrieve2 {
282 my( $self, $client, $id ) = @_;
283 my $e = new_editor();
284 my $copy = $e->retrieve_asset_copy(
290 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
291 ascecm => [ qw/ stat_cat stat_cat_entry / ],
295 ) or return $e->event;
297 # For backwards compatibility
298 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
300 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
302 $e->search_action_circulation(
304 { target_copy => $copy->id },
306 order_by => { circ => 'xact_start desc' },
318 __PACKAGE__->register_method(
319 method => 'flesh_copy_custom',
320 api_name => 'open-ils.search.asset.copy.fleshed.custom'
323 sub flesh_copy_custom {
324 my( $self, $conn, $copyid, $fields ) = @_;
325 my $e = new_editor();
326 my $copy = $e->retrieve_asset_copy(
336 ) or return $e->event;
345 __PACKAGE__->register_method(
346 method => "biblio_barcode_to_title",
347 api_name => "open-ils.search.biblio.find_by_barcode",
350 sub biblio_barcode_to_title {
351 my( $self, $client, $barcode ) = @_;
353 my $title = $apputils->simple_scalar_request(
355 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
357 return { ids => [ $title->id ], count => 1 } if $title;
358 return { count => 0 };
361 __PACKAGE__->register_method(
362 method => 'title_id_by_item_barcode',
363 api_name => 'open-ils.search.bib_id.by_barcode'
366 sub title_id_by_item_barcode {
367 my( $self, $conn, $barcode ) = @_;
368 my $e = new_editor();
369 my $copies = $e->search_asset_copy(
371 { deleted => 'f', barcode => $barcode },
375 acp => [ 'call_number' ],
382 return $e->event unless @$copies;
383 return $$copies[0]->call_number->record->id;
387 __PACKAGE__->register_method(
388 method => "biblio_copy_to_mods",
389 api_name => "open-ils.search.biblio.copy.mods.retrieve",
392 # takes a copy object and returns it fleshed mods object
393 sub biblio_copy_to_mods {
394 my( $self, $client, $copy ) = @_;
396 my $volume = $U->cstorereq(
397 "open-ils.cstore.direct.asset.call_number.retrieve",
398 $copy->call_number() );
400 my $mods = _records_to_mods($volume->record());
401 $mods = shift @$mods;
402 $volume->copies([$copy]);
403 push @{$mods->call_numbers()}, $volume;
409 __PACKAGE__->register_method(
410 api_name => 'open-ils.search.biblio.multiclass.query',
411 method => 'multiclass_query',
413 @param arghash @see open-ils.search.biblio.multiclass
414 @param query Raw human-readable query string.
415 Recognized search keys include:
416 keyword/kw - search keyword(s)
417 author/au/name - search author(s)
418 title/ti - search title
419 subject/su - search subject
420 series/se - search series
421 lang - limit by language (specifiy multiple langs with lang:l1 lang:l2 ...)
422 site - search at specified org unit, corresponds to actor.org_unit.shortname
423 sort - sort type (title, author, pubdate)
424 dir - sort direction (asc, desc)
425 available - if set to anything other than "false" or "0", limits to available items
427 keyword, title, author, subject, and series support additional search
428 subclasses, specified with a "|". For example, "title|proper:gone with the wind"
429 For more, see config.metabib_field
431 @param nocache @see open-ils.search.biblio.multiclass
434 __PACKAGE__->register_method(
435 api_name => 'open-ils.search.biblio.multiclass.query.staff',
436 method => 'multiclass_query',
437 signature => '@see open-ils.search.biblio.multiclass.query');
438 __PACKAGE__->register_method(
439 api_name => 'open-ils.search.metabib.multiclass.query',
440 method => 'multiclass_query',
441 signature => '@see open-ils.search.biblio.multiclass.query');
442 __PACKAGE__->register_method(
443 api_name => 'open-ils.search.metabib.multiclass.query.staff',
444 method => 'multiclass_query',
445 signature => '@see open-ils.search.biblio.multiclass.query');
447 sub multiclass_query {
448 my($self, $conn, $arghash, $query, $docache) = @_;
450 $logger->debug("initial search query => $query");
452 $query = decode_utf8($query);
455 $query =~ s/^\s+//go;
457 # convert convenience classes (e.g. kw for keyword) to the full class name
458 $query =~ s/kw(:|\|)/keyword$1/go;
459 $query =~ s/ti(:|\|)/title$1/go;
460 $query =~ s/au(:|\|)/author$1/go;
461 $query =~ s/su(:|\|)/subject$1/go;
462 $query =~ s/se(:|\|)/series$1/go;
463 $query =~ s/name(:|\|)/author$1/og;
465 $logger->debug("cleansed query string => $query");
467 my $search = $arghash->{searches};
468 $arghash->{searches} = $search = {} unless $search;
470 while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):[^:]+)$//so) {
471 my($type, $value) = split(':', $1);
472 next unless $type and $value;
474 $value =~ s/^\s*//og;
475 $value =~ s/\s*$//og;
476 $type = 'sort_dir' if $type eq 'dir';
478 if($type eq 'site') {
479 # 'site' is the org shortname. when using this, we also want
480 # to search at the requested org's depth
481 my $e = new_editor();
482 if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
483 $arghash->{org_unit} = $org->id if $org;
484 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
486 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
489 } elsif($type eq 'available') {
491 $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
493 } elsif($type eq 'lang') {
494 # collect languages into an array of languages
495 $arghash->{language} = [] unless $arghash->{language};
496 push(@{$arghash->{language}}, $value);
499 # append the search term to the term under construction
500 $search->{$type} = {} unless $search->{$type};
501 $search->{$type}->{term} =
502 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
506 # capture the original limit because the search method alters the limit internally
507 my $ol = $arghash->{limit};
509 (my $method = $self->api_name) =~ s/\.query//o;
510 $method = $self->method_lookup($method);
511 my ($data) = $method->run($arghash, $docache);
513 $arghash->{limit} = $ol if $ol;
514 $data->{compiled_search} = $arghash;
515 $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
519 # ----------------------------------------------------------------------------
520 # These are the main OPAC search methods
521 # ----------------------------------------------------------------------------
523 __PACKAGE__->register_method(
524 method => 'the_quest_for_knowledge',
525 api_name => 'open-ils.search.biblio.multiclass',
527 Performs a multi class bilbli or metabib search
528 @param searchhash A search object layed out like so:
529 searches : { "$class" : "$value", ...}
530 org_unit : The org id to focus the search at
531 depth : The org depth
532 limit : The search limit
533 offset : The search offset
534 format : The MARC format
535 sort : What field to sort the results on [ author | title | pubdate ]
536 sort_dir : What direction do we sort? [ asc | desc ]
537 @return An object of the form
538 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
542 __PACKAGE__->register_method(
543 method => 'the_quest_for_knowledge',
544 api_name => 'open-ils.search.biblio.multiclass.staff',
545 signature => q/@see open-ils.search.biblio.multiclass/);
546 __PACKAGE__->register_method(
547 method => 'the_quest_for_knowledge',
548 api_name => 'open-ils.search.metabib.multiclass',
549 signature => q/@see open-ils.search.biblio.multiclass/);
550 __PACKAGE__->register_method(
551 method => 'the_quest_for_knowledge',
552 api_name => 'open-ils.search.metabib.multiclass.staff',
553 signature => q/@see open-ils.search.biblio.multiclass/);
557 sub the_quest_for_knowledge {
558 my( $self, $conn, $searchhash, $docache ) = @_;
560 return { count => 0 } unless $searchhash and
561 ref $searchhash->{searches} eq 'HASH';
563 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
567 if($self->api_name =~ /metabib/) {
569 $method =~ s/biblio/metabib/o;
573 my $offset = $searchhash->{offset} || 0;
574 my $limit = $searchhash->{limit} || 10;
575 my $end = $offset + $limit - 1;
577 # do some simple sanity checking
578 if(!$searchhash->{searches} or
579 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
580 return { count => 0 };
585 $searchhash->{offset} = 0;
586 $searchhash->{limit} = $maxlimit;
588 return { count => 0 } if $offset > $maxlimit;
591 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
592 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
593 my $ckey = $pfx . md5_hex($method . $s);
595 $logger->info("bib search for: $s");
597 $searchhash->{limit} -= $offset;
601 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
605 $method .= ".staff" if($self->api_name =~ /staff/);
606 $method .= ".atomic";
608 for (keys %$searchhash) {
609 delete $$searchhash{$_}
610 unless defined $$searchhash{$_};
613 $result = $U->storagereq( $method, %$searchhash );
620 return {count => 0} unless ($result && $$result[0]);
624 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
627 # If we didn't get this data from the cache, put it into the cache
628 # then return the correct offset of records
629 $logger->debug("putting search cache $ckey\n");
630 put_cache($ckey, $count, \@recs);
634 # if we have the full set of data, trim out
635 # the requested chunk based on limit and offset
637 for ($offset..$end) {
638 last unless $recs[$_];
644 return { ids => \@recs, count => $count };
655 my $end = $offset + $limit - 1;
657 $logger->debug("searching cache for $key : $start..$end\n");
659 return undef unless $cache;
660 my $data = $cache->get_cache($key);
662 return undef unless $data;
664 my $count = $data->[0];
667 return undef unless $offset < $count;
671 for( my $i = $offset; $i <= $end; $i++ ) {
672 last unless my $d = $$data[$i];
676 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
683 my( $key, $count, $data ) = @_;
684 return undef unless $cache;
685 $logger->debug("search_cache putting ".
686 scalar(@$data)." items at key $key with timeout $cache_timeout");
687 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
695 __PACKAGE__->register_method(
696 method => "biblio_mrid_to_modsbatch_batch",
697 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
699 sub biblio_mrid_to_modsbatch_batch {
700 my( $self, $client, $mrids) = @_;
701 warn "Performing mrid_to_modsbatch_batch...";
703 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
704 for my $id (@$mrids) {
705 next unless defined $id;
706 my ($m) = $method->run($id);
713 __PACKAGE__->register_method(
714 method => "biblio_mrid_to_modsbatch",
715 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
716 notes => <<" NOTES");
717 Returns the mvr associated with a given metarecod. If none exists,
721 __PACKAGE__->register_method(
722 method => "biblio_mrid_to_modsbatch",
723 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
724 notes => <<" NOTES");
725 Returns the mvr associated with a given metarecod. If none exists,
729 sub biblio_mrid_to_modsbatch {
730 my( $self, $client, $mrid, $args) = @_;
732 warn "Grabbing mvr for $mrid\n";
734 my ($mr, $evt) = _grab_metarecord($mrid);
735 return $evt unless $mr;
737 my $mvr = $self->biblio_mrid_check_mvr($client, $mr);
738 $mvr = $self->biblio_mrid_make_modsbatch( $client, $mr ) unless $mvr;
740 return $mvr unless ref($args);
742 # Here we find the lead record appropriate for the given filters
743 # and use that for the title and author of the metarecord
744 my $format = $$args{format};
745 my $org = $$args{org};
746 my $depth = $$args{depth};
748 return $mvr unless $format or $org or $depth;
750 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
751 $method = "$method.staff" if $self->api_name =~ /staff/o;
753 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
755 if( my $mods = $U->record_to_mvr($rec) ) {
757 $mvr->title($mods->title);
758 $mvr->title($mods->author);
759 $logger->debug("mods_slim updating title and ".
760 "author in mvr with ".$mods->title." : ".$mods->author);
766 # converts a metarecord to an mvr
769 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
770 return Fieldmapper::metabib::virtual_record->new($perl);
773 # checks to see if a metarecord has mods, if so returns true;
775 __PACKAGE__->register_method(
776 method => "biblio_mrid_check_mvr",
777 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
778 notes => <<" NOTES");
779 Takes a metarecord ID or a metarecord object and returns true
780 if the metarecord already has an mvr associated with it.
783 sub biblio_mrid_check_mvr {
784 my( $self, $client, $mrid ) = @_;
788 if(ref($mrid)) { $mr = $mrid; }
789 else { ($mr, $evt) = _grab_metarecord($mrid); }
792 warn "Checking mvr for mr " . $mr->id . "\n";
794 return _mr_to_mvr($mr) if $mr->mods();
798 sub _grab_metarecord {
800 #my $e = OpenILS::Utils::Editor->new;
801 my $e = new_editor();
802 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
807 __PACKAGE__->register_method(
808 method => "biblio_mrid_make_modsbatch",
809 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
810 notes => <<" NOTES");
811 Takes either a metarecord ID or a metarecord object.
812 Forces the creations of an mvr for the given metarecord.
813 The created mvr is returned.
816 sub biblio_mrid_make_modsbatch {
817 my( $self, $client, $mrid ) = @_;
819 #my $e = OpenILS::Utils::Editor->new;
820 my $e = new_editor();
827 $mr = $e->retrieve_metabib_metarecord($mrid)
831 my $masterid = $mr->master_record;
832 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
834 my $ids = $U->storagereq(
835 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
836 return undef unless @$ids;
838 my $master = $e->retrieve_biblio_record_entry($masterid)
841 # start the mods batch
842 my $u = OpenILS::Utils::ModsParser->new();
843 $u->start_mods_batch( $master->marc );
845 # grab all of the sub-records and shove them into the batch
846 my @ids = grep { $_ ne $masterid } @$ids;
847 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
852 my $r = $e->retrieve_biblio_record_entry($i);
853 push( @$subrecs, $r ) if $r;
858 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
859 $u->push_mods_batch( $_->marc ) if $_->marc;
863 # finish up and send to the client
864 my $mods = $u->finish_mods_batch();
865 $mods->doc_id($mrid);
866 $client->respond_complete($mods);
869 # now update the mods string in the db
870 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
873 #$e = OpenILS::Utils::Editor->new(xact => 1);
874 $e = new_editor(xact => 1);
875 $e->update_metabib_metarecord($mr)
876 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
885 # converts a mr id into a list of record ids
887 __PACKAGE__->register_method(
888 method => "biblio_mrid_to_record_ids",
889 api_name => "open-ils.search.biblio.metarecord_to_records",
892 __PACKAGE__->register_method(
893 method => "biblio_mrid_to_record_ids",
894 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
897 sub biblio_mrid_to_record_ids {
898 my( $self, $client, $mrid, $args ) = @_;
900 my $format = $$args{format};
901 my $org = $$args{org};
902 my $depth = $$args{depth};
904 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
905 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
906 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
908 return { count => scalar(@$recs), ids => $recs };
912 __PACKAGE__->register_method(
913 method => "biblio_record_to_marc_html",
914 api_name => "open-ils.search.biblio.record.html" );
916 my $parser = XML::LibXML->new();
917 my $xslt = XML::LibXSLT->new();
920 my $settings_client = OpenSRF::Utils::SettingsClient->new();
921 sub biblio_record_to_marc_html {
922 my( $self, $client, $recordid ) = @_;
925 my $dir = $settings_client->config_value( "dirs", "xsl" );
926 my $xsl = $settings_client->config_value(
927 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
929 $xsl = $parser->parse_file("$dir/$xsl");
930 $marc_sheet = $xslt->parse_stylesheet( $xsl );
934 my $record = $apputils->simple_scalar_request(
936 "open-ils.cstore.direct.biblio.record_entry.retrieve",
939 my $xmldoc = $parser->parse_string($record->marc);
940 my $html = $marc_sheet->transform($xmldoc);
941 $html = $html->toString();
948 __PACKAGE__->register_method(
949 method => "retrieve_all_copy_locations",
950 api_name => "open-ils.search.config.copy_location.retrieve.all" );
952 my $shelving_locations;
953 sub retrieve_all_copy_locations {
954 my( $self, $client ) = @_;
955 if(!$shelving_locations) {
956 $shelving_locations = $apputils->simple_scalar_request(
958 "open-ils.cstore.direct.asset.copy_location.search.atomic",
959 { id => { "!=" => undef } }
962 return $shelving_locations;
968 __PACKAGE__->register_method(
969 method => "retrieve_all_copy_statuses",
970 api_name => "open-ils.search.config.copy_status.retrieve.all" );
973 sub retrieve_all_copy_statuses {
974 my( $self, $client ) = @_;
975 return $copy_statuses if $copy_statuses;
976 return $copy_statuses =
977 new_editor()->retrieve_all_config_copy_status();
981 __PACKAGE__->register_method(
982 method => "copy_counts_per_org",
983 api_name => "open-ils.search.biblio.copy_counts.retrieve");
985 __PACKAGE__->register_method(
986 method => "copy_counts_per_org",
987 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
989 sub copy_counts_per_org {
990 my( $self, $client, $record_id ) = @_;
992 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
994 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
995 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
997 my $counts = $apputils->simple_scalar_request(
998 "open-ils.storage", $method, $record_id );
1000 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1005 __PACKAGE__->register_method(
1006 method => "copy_count_summary",
1007 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1008 notes => <<" NOTES");
1009 returns an array of these:
1010 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1011 where statusx is a copy status name. the statuses are sorted
1015 sub copy_count_summary {
1016 my( $self, $client, $rid, $org, $depth ) = @_;
1019 my $data = $U->storagereq(
1020 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1022 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1028 __PACKAGE__->register_method(
1029 method => "multiclass_search",
1030 api_name => "open-ils.search.biblio.multiclass",
1031 notes => <<" NOTES");
1032 Performs a multiclass search
1033 PARAMS( searchBlob, org_unit, format, limit )
1034 where searchBlob is defined like this:
1036 "title" : { "term" : "water" },
1037 "author" : { "term" : "smith" },
1042 __PACKAGE__->register_method(
1043 method => "multiclass_search",
1044 api_name => "open-ils.search.biblio.multiclass.staff",
1045 notes => "see open-ils.search.biblio.multiclass" );
1047 sub multiclass_search {
1048 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1050 $logger->debug("Performing multiclass search with org => $orgid, " .
1051 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1053 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1054 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1057 my $records = $apputils->simplereq(
1058 'open-ils.storage', $meth,
1059 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1064 if( ref($records) and $records->[0] and
1065 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1067 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1069 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1070 return { ids => $recs, count => $count };
1076 __PACKAGE__->register_method(
1077 method => "multiclass_search",
1078 api_name => "open-ils.search.biblio.multiclass",
1080 Performs a multiclass search
1081 @param args A names hash of arguments:
1082 org_unit : The org to focus the search on
1083 depth : The search depth
1084 format : Item format
1085 limit : Return limit
1086 offset : Search offset
1087 searches : A named hash of searches which has the following format:
1089 "title" : { "term" : "water" },
1090 "author" : { "term" : "smith" },
1093 @return { ids : <array of ids>, count : hitcount }
1097 __PACKAGE__->register_method(
1098 method => "multiclass_search",
1099 api_name => "open-ils.search.biblio.multiclass.staff",
1100 notes => q/@see open-ils.search.biblio.multiclass/ );
1102 sub multiclass_search {
1103 my( $self, $client, $args ) = @_;
1105 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
1106 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1107 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1109 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1114 if( ref($records) and $records->[0] and
1115 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1117 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1119 return { ids => $recs, count => $count };
1126 __PACKAGE__->register_method(
1127 method => "marc_search",
1128 api_name => "open-ils.search.biblio.marc.staff");
1130 __PACKAGE__->register_method(
1131 method => "marc_search",
1132 api_name => "open-ils.search.biblio.marc",
1133 notes => <<" NOTES");
1135 open-ils.storage.biblio.full_rec.multi_search.atomic
1136 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1137 "limit":5,"sort":"author","item_type":"g"}
1141 my( $self, $conn, $args, $limit, $offset ) = @_;
1143 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1144 $method .= ".staff" if $self->api_name =~ /staff/;
1145 $method .= ".atomic";
1151 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1152 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1154 my $recs = search_cache($ckey, $offset, $limit);
1157 $recs = $U->storagereq($method, %$args) || [];
1159 put_cache($ckey, scalar(@$recs), $recs);
1160 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1167 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1168 my @recs = map { $_->[0] } @$recs;
1170 return { ids => \@recs, count => $count };
1174 __PACKAGE__->register_method(
1175 method => "biblio_search_isbn",
1176 api_name => "open-ils.search.biblio.isbn",
1179 sub biblio_search_isbn {
1180 my( $self, $client, $isbn ) = @_;
1181 $logger->debug("Searching ISBN $isbn");
1182 my $e = new_editor();
1183 my $recs = $U->storagereq(
1184 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1185 return { ids => $recs, count => scalar(@$recs) };
1189 __PACKAGE__->register_method(
1190 method => "biblio_search_issn",
1191 api_name => "open-ils.search.biblio.issn",
1194 sub biblio_search_issn {
1195 my( $self, $client, $issn ) = @_;
1196 $logger->debug("Searching ISSN $issn");
1197 my $e = new_editor();
1198 my $recs = $U->storagereq(
1199 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1200 return { ids => $recs, count => scalar(@$recs) };
1206 __PACKAGE__->register_method(
1207 method => "fetch_mods_by_copy",
1208 api_name => "open-ils.search.biblio.mods_from_copy",
1211 sub fetch_mods_by_copy {
1212 my( $self, $client, $copyid ) = @_;
1213 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1214 return $evt if $evt;
1215 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1216 return $apputils->record_to_mvr($record);
1221 # -------------------------------------------------------------------------------------
1223 __PACKAGE__->register_method(
1224 method => "cn_browse",
1225 api_name => "open-ils.search.callnumber.browse.target",
1226 notes => "Starts a callnumber browse"
1229 __PACKAGE__->register_method(
1230 method => "cn_browse",
1231 api_name => "open-ils.search.callnumber.browse.page_up",
1232 notes => "Returns the previous page of callnumbers",
1235 __PACKAGE__->register_method(
1236 method => "cn_browse",
1237 api_name => "open-ils.search.callnumber.browse.page_down",
1238 notes => "Returns the next page of callnumbers",
1242 # RETURNS array of arrays like so: label, owning_lib, record, id
1244 my( $self, $client, @params ) = @_;
1247 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1248 if( $self->api_name =~ /target/ );
1249 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1250 if( $self->api_name =~ /page_up/ );
1251 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1252 if( $self->api_name =~ /page_down/ );
1254 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1256 # -------------------------------------------------------------------------------------
1258 __PACKAGE__->register_method(
1259 method => "fetch_cn",
1260 api_name => "open-ils.search.callnumber.retrieve",
1261 notes => "retrieves a callnumber based on ID",
1265 my( $self, $client, $id ) = @_;
1266 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1267 return $evt if $evt;
1271 __PACKAGE__->register_method (
1272 method => "fetch_copy_by_cn",
1273 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1275 Returns an array of copy id's by callnumber id
1276 @param cnid The callnumber id
1277 @return An array of copy ids
1281 sub fetch_copy_by_cn {
1282 my( $self, $conn, $cnid ) = @_;
1283 return $U->cstorereq(
1284 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1285 { call_number => $cnid, deleted => 'f' } );
1288 __PACKAGE__->register_method (
1289 method => 'fetch_cn_by_info',
1290 api_name => 'open-ils.search.call_number.retrieve_by_info',
1292 @param label The callnumber label
1293 @param record The record the cn is attached to
1294 @param org The owning library of the cn
1295 @return The callnumber object
1300 sub fetch_cn_by_info {
1301 my( $self, $conn, $label, $record, $org ) = @_;
1302 return $U->cstorereq(
1303 'open-ils.cstore.direct.asset.call_number.search',
1304 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1311 __PACKAGE__->register_method (
1312 method => 'bib_extras',
1313 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1314 __PACKAGE__->register_method (
1315 method => 'bib_extras',
1316 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1317 __PACKAGE__->register_method (
1318 method => 'bib_extras',
1319 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1320 __PACKAGE__->register_method (
1321 method => 'bib_extras',
1322 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1327 my $e = new_editor();
1329 return $e->retrieve_all_config_lit_form_map()
1330 if( $self->api_name =~ /lit_form/ );
1332 return $e->retrieve_all_config_item_form_map()
1333 if( $self->api_name =~ /item_form_map/ );
1335 return $e->retrieve_all_config_item_type_map()
1336 if( $self->api_name =~ /item_type_map/ );
1338 return $e->retrieve_all_config_audience_map()
1339 if( $self->api_name =~ /audience_map/ );
1346 __PACKAGE__->register_method(
1347 method => 'fetch_slim_record',
1348 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1350 Returns a biblio.record_entry without the attached marcxml
1354 sub fetch_slim_record {
1355 my( $self, $conn, $ids ) = @_;
1357 #my $editor = OpenILS::Utils::Editor->new;
1358 my $editor = new_editor();
1361 return $editor->event unless
1362 my $r = $editor->retrieve_biblio_record_entry($_);
1371 __PACKAGE__->register_method(
1372 method => 'rec_to_mr_rec_descriptors',
1373 api_name => 'open-ils.search.metabib.record_to_descriptors',
1375 specialized method...
1376 Given a biblio record id or a metarecord id,
1377 this returns a list of metabib.record_descriptor
1378 objects that live within the same metarecord
1379 @param args Object of args including:
1383 sub rec_to_mr_rec_descriptors {
1384 my( $self, $conn, $args ) = @_;
1386 my $rec = $$args{record};
1387 my $mrec = $$args{metarecord};
1388 my $item_forms = $$args{item_forms};
1389 my $item_types = $$args{item_types};
1390 my $item_lang = $$args{item_lang};
1392 my $e = new_editor();
1396 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1397 return $e->event unless @$map;
1398 $mrec = $$map[0]->metarecord;
1401 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1402 return $e->event unless @$recs;
1404 my @recs = map { $_->source } @$recs;
1405 my $search = { record => \@recs };
1406 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1407 $search->{item_type} = $item_types if $item_types and @$item_types;
1408 $search->{item_lang} = $item_lang if $item_lang;
1410 my $desc = $e->search_metabib_record_descriptor($search);
1412 return { metarecord => $mrec, descriptors => $desc };
1418 __PACKAGE__->register_method(
1419 method => 'copies_created_on',
1423 sub copies_created_on {
1424 my( $self, $conn, $auth, $org, $date ) = @_;
1425 my $e = new_editor(authtoken=>$auth);
1426 return $e->event unless $e->checkauth;
1430 __PACKAGE__->register_method(
1431 method => 'fetch_age_protect',
1432 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1435 sub fetch_age_protect {
1436 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1440 __PACKAGE__->register_method(
1441 method => 'copies_by_cn_label',
1442 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1445 __PACKAGE__->register_method(
1446 method => 'copies_by_cn_label',
1447 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1450 sub copies_by_cn_label {
1451 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1452 my $e = new_editor();
1453 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1454 return [] unless @$cns;
1456 # show all non-deleted copies in the staff client ...
1457 if ($self->api_name =~ /staff$/o) {
1458 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1461 # ... otherwise, grab the copies ...
1462 my $copies = $e->search_asset_copy(
1463 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1464 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1468 # ... and test for location and status visibility
1469 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];