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",
106 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
108 note => "Provide ID, we provide the mods"
111 # converts a record into a mods object with NO copy counts attached
112 sub record_id_to_mods_slim {
113 my( $self, $client, $id ) = @_;
114 return undef unless defined $id;
116 if(ref($id) and ref($id) == 'ARRAY') {
117 return _records_to_mods( @$id );
119 my $mods_list = _records_to_mods( $id );
120 my $mods_obj = $mods_list->[0];
121 return OpenILS::Event->new('BIBLIO_RECORD_ENTRY_NOT_FOUND') unless $mods_obj;
126 # Returns the number of copies attached to a record based on org location
127 __PACKAGE__->register_method(
128 method => "record_id_to_copy_count",
129 api_name => "open-ils.search.biblio.record.copy_count",
132 __PACKAGE__->register_method(
133 method => "record_id_to_copy_count",
135 api_name => "open-ils.search.biblio.record.copy_count.staff",
138 __PACKAGE__->register_method(
139 method => "record_id_to_copy_count",
140 api_name => "open-ils.search.biblio.metarecord.copy_count",
143 __PACKAGE__->register_method(
144 method => "record_id_to_copy_count",
145 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
147 sub record_id_to_copy_count {
148 my( $self, $client, $org_id, $record_id, $format ) = @_;
150 return [] unless $record_id;
151 $format = undef if (!$format or $format eq 'all');
153 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
156 if($self->api_name =~ /metarecord/) {
157 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
161 $method =~ s/atomic/staff\.atomic/og if($self->api_name =~ /staff/ );
163 my $count = $U->storagereq( $method,
164 org_unit => $org_id, $key => $record_id, format => $format );
166 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
172 __PACKAGE__->register_method(
173 method => "biblio_search_tcn",
174 api_name => "open-ils.search.biblio.tcn",
176 note => "Retrieve a record by TCN",
179 sub biblio_search_tcn {
181 my( $self, $client, $tcn, $include_deleted ) = @_;
183 $tcn =~ s/.*?(\w+)\s*$/$1/o;
185 my $e = new_editor();
186 my $search = {tcn_value => $tcn};
187 $search->{deleted} = 'f' unless $include_deleted;
188 my $recs = $e->search_biblio_record_entry( $search, {idlist =>1} );
190 return { count => scalar(@$recs), ids => $recs };
194 # --------------------------------------------------------------------------------
196 __PACKAGE__->register_method(
197 method => "biblio_barcode_to_copy",
198 api_name => "open-ils.search.asset.copy.find_by_barcode",);
199 sub biblio_barcode_to_copy {
200 my( $self, $client, $barcode ) = @_;
201 my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
206 __PACKAGE__->register_method(
207 method => "biblio_id_to_copy",
208 api_name => "open-ils.search.asset.copy.batch.retrieve",);
209 sub biblio_id_to_copy {
210 my( $self, $client, $ids ) = @_;
211 $logger->info("Fetching copies @$ids");
212 return $U->cstorereq(
213 "open-ils.cstore.direct.asset.copy.search.atomic", { id => $ids } );
217 __PACKAGE__->register_method(
218 method => "copy_retrieve",
219 api_name => "open-ils.search.asset.copy.retrieve",);
221 my( $self, $client, $cid ) = @_;
222 my( $copy, $evt ) = $U->fetch_copy($cid);
227 __PACKAGE__->register_method(
228 method => "volume_retrieve",
229 api_name => "open-ils.search.asset.call_number.retrieve");
230 sub volume_retrieve {
231 my( $self, $client, $vid ) = @_;
232 my $e = new_editor();
233 my $vol = $e->retrieve_asset_call_number($vid) or return $e->event;
237 __PACKAGE__->register_method(
238 method => "fleshed_copy_retrieve_batch",
240 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve");
242 sub fleshed_copy_retrieve_batch {
243 my( $self, $client, $ids ) = @_;
244 $logger->info("Fetching fleshed copies @$ids");
245 return $U->cstorereq(
246 "open-ils.cstore.direct.asset.copy.search.atomic",
249 flesh_fields => { acp => [ qw/ circ_lib location status stat_cat_entries / ] }
254 __PACKAGE__->register_method(
255 method => "fleshed_copy_retrieve",
256 api_name => "open-ils.search.asset.copy.fleshed.retrieve",);
258 sub fleshed_copy_retrieve {
259 my( $self, $client, $id ) = @_;
260 my( $c, $e) = $U->fetch_fleshed_copy($id);
267 __PACKAGE__->register_method(
268 method => 'fleshed_by_barcode',
269 api_name => "open-ils.search.asset.copy.fleshed2.find_by_barcode",);
270 sub fleshed_by_barcode {
271 my( $self, $conn, $barcode ) = @_;
272 my $e = new_editor();
273 my $copyid = $e->search_asset_copy(
274 {barcode => $barcode, deleted => 'f'}, {idlist=>1})->[0]
276 return fleshed_copy_retrieve2( $self, $conn, $copyid);
280 __PACKAGE__->register_method(
281 method => "fleshed_copy_retrieve2",
282 api_name => "open-ils.search.asset.copy.fleshed2.retrieve",);
284 sub fleshed_copy_retrieve2 {
285 my( $self, $client, $id ) = @_;
286 my $e = new_editor();
287 my $copy = $e->retrieve_asset_copy(
293 acp => [ qw/ location status stat_cat_entry_copy_maps notes age_protect / ],
294 ascecm => [ qw/ stat_cat stat_cat_entry / ],
298 ) or return $e->event;
300 # For backwards compatibility
301 #$copy->stat_cat_entries($copy->stat_cat_entry_copy_maps);
303 if( $copy->status->id == OILS_COPY_STATUS_CHECKED_OUT ) {
305 $e->search_action_circulation(
307 { target_copy => $copy->id },
309 order_by => { circ => 'xact_start desc' },
321 __PACKAGE__->register_method(
322 method => 'flesh_copy_custom',
323 api_name => 'open-ils.search.asset.copy.fleshed.custom'
326 sub flesh_copy_custom {
327 my( $self, $conn, $copyid, $fields ) = @_;
328 my $e = new_editor();
329 my $copy = $e->retrieve_asset_copy(
339 ) or return $e->event;
348 __PACKAGE__->register_method(
349 method => "biblio_barcode_to_title",
350 api_name => "open-ils.search.biblio.find_by_barcode",
353 sub biblio_barcode_to_title {
354 my( $self, $client, $barcode ) = @_;
356 my $title = $apputils->simple_scalar_request(
358 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
360 return { ids => [ $title->id ], count => 1 } if $title;
361 return { count => 0 };
364 __PACKAGE__->register_method(
365 method => 'title_id_by_item_barcode',
366 api_name => 'open-ils.search.bib_id.by_barcode'
369 sub title_id_by_item_barcode {
370 my( $self, $conn, $barcode ) = @_;
371 my $e = new_editor();
372 my $copies = $e->search_asset_copy(
374 { deleted => 'f', barcode => $barcode },
378 acp => [ 'call_number' ],
385 return $e->event unless @$copies;
386 return $$copies[0]->call_number->record->id;
390 __PACKAGE__->register_method(
391 method => "biblio_copy_to_mods",
392 api_name => "open-ils.search.biblio.copy.mods.retrieve",
395 # takes a copy object and returns it fleshed mods object
396 sub biblio_copy_to_mods {
397 my( $self, $client, $copy ) = @_;
399 my $volume = $U->cstorereq(
400 "open-ils.cstore.direct.asset.call_number.retrieve",
401 $copy->call_number() );
403 my $mods = _records_to_mods($volume->record());
404 $mods = shift @$mods;
405 $volume->copies([$copy]);
406 push @{$mods->call_numbers()}, $volume;
412 __PACKAGE__->register_method(
413 api_name => 'open-ils.search.biblio.multiclass.query',
414 method => 'multiclass_query',
416 @param arghash @see open-ils.search.biblio.multiclass
417 @param query Raw human-readable query string.
418 Recognized search keys include:
419 keyword/kw - search keyword(s)
420 author/au/name - search author(s)
421 title/ti - search title
422 subject/su - search subject
423 series/se - search series
424 lang - limit by language (specifiy multiple langs with lang:l1 lang:l2 ...)
425 site - search at specified org unit, corresponds to actor.org_unit.shortname
426 sort - sort type (title, author, pubdate)
427 dir - sort direction (asc, desc)
428 available - if set to anything other than "false" or "0", limits to available items
430 keyword, title, author, subject, and series support additional search
431 subclasses, specified with a "|". For example, "title|proper:gone with the wind"
432 For more, see config.metabib_field
434 @param nocache @see open-ils.search.biblio.multiclass
437 __PACKAGE__->register_method(
438 api_name => 'open-ils.search.biblio.multiclass.query.staff',
439 method => 'multiclass_query',
440 signature => '@see open-ils.search.biblio.multiclass.query');
441 __PACKAGE__->register_method(
442 api_name => 'open-ils.search.metabib.multiclass.query',
443 method => 'multiclass_query',
444 signature => '@see open-ils.search.biblio.multiclass.query');
445 __PACKAGE__->register_method(
446 api_name => 'open-ils.search.metabib.multiclass.query.staff',
447 method => 'multiclass_query',
448 signature => '@see open-ils.search.biblio.multiclass.query');
450 sub multiclass_query {
451 my($self, $conn, $arghash, $query, $docache) = @_;
453 $logger->debug("initial search query => $query");
455 $query = decode_utf8($query);
458 $query =~ s/^\s+//go;
460 # convert convenience classes (e.g. kw for keyword) to the full class name
461 $query =~ s/kw(:|\|)/keyword$1/go;
462 $query =~ s/ti(:|\|)/title$1/go;
463 $query =~ s/au(:|\|)/author$1/go;
464 $query =~ s/su(:|\|)/subject$1/go;
465 $query =~ s/se(:|\|)/series$1/go;
466 $query =~ s/name(:|\|)/author$1/og;
468 $logger->debug("cleansed query string => $query");
469 my $search = $arghash->{searches} = {};
471 while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):[^:]+)$//so) {
472 my($type, $value) = split(':', $1);
473 next unless $type and $value;
475 $value =~ s/^\s*//og;
476 $value =~ s/\s*$//og;
477 $type = 'sort_dir' if $type eq 'dir';
479 if($type eq 'site') {
480 # 'site' is the org shortname. when using this, we also want
481 # to search at the requested org's depth
482 my $e = new_editor();
483 if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
484 $arghash->{org_unit} = $org->id if $org;
485 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
487 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
490 } elsif($type eq 'available') {
492 $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
494 } elsif($type eq 'lang') {
495 # collect languages into an array of languages
496 $arghash->{language} = [] unless $arghash->{language};
497 push(@{$arghash->{language}}, $value);
500 # append the search term to the term under construction
501 $search->{$type} = {} unless $search->{$type};
502 $search->{$type}->{term} =
503 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
508 # This is the front part of the string before any special tokens were parsed.
509 # Add this data to the default search class
510 my $type = $arghash->{default_class} || 'keyword';
511 $search->{$type} = {} unless $search->{$type};
512 $search->{$type}->{term} =
513 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $query" : $query;
516 # capture the original limit because the search method alters the limit internally
517 my $ol = $arghash->{limit};
519 (my $method = $self->api_name) =~ s/\.query//o;
520 $method = $self->method_lookup($method);
521 my ($data) = $method->run($arghash, $docache);
523 $arghash->{limit} = $ol if $ol;
524 $data->{compiled_search} = $arghash;
526 $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
531 __PACKAGE__->register_method(
532 method => 'cat_search_z_style_wrapper',
533 api_name => 'open-ils.search.biblio.zstyle',
535 signature => q/@see open-ils.search.biblio.multiclass/);
537 sub cat_search_z_style_wrapper {
540 my $authtoken = shift;
543 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
545 my $ou = $cstore->request(
546 'open-ils.cstore.direct.actor.org_unit.search',
547 { parent_ou => undef }
550 my $result = { service => 'native-evergreen-catalog', records => [] };
551 my $searchhash = { limit => $$args{limit}, offset => $$args{offset}, org_unit => $ou->id };
553 $$searchhash{searches}{title}{term} = $$args{search}{title} if $$args{search}{title};
554 $$searchhash{searches}{author}{term} = $$args{search}{author} if $$args{search}{author};
555 $$searchhash{searches}{subject}{term} = $$args{search}{subject} if $$args{search}{subject};
556 $$searchhash{searches}{keyword}{term} = $$args{search}{keyword} if $$args{search}{keyword};
558 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{tcn} if $$args{search}{tcn};
559 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{isbn} if $$args{search}{isbn};
560 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{issn} if $$args{search}{issn};
561 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{publisher} if $$args{search}{publisher};
562 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{pubdate} if $$args{search}{pubdate};
563 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{item_type} if $$args{search}{item_type};
565 my $list = the_quest_for_knowledge( $self, $client, $searchhash );
567 if ($list->{count} > 0) {
568 $result->{count} = $list->{count};
570 my $records = $cstore->request(
571 'open-ils.cstore.direct.biblio.record_entry.search.atomic',
572 { id => [ map { ( $_->[0] ) } @{$list->{ids}} ] }
575 for my $rec ( @$records ) {
577 my $u = OpenILS::Utils::ModsParser->new();
578 $u->start_mods_batch( $rec->marc );
579 my $mods = $u->finish_mods_batch();
581 push @{ $result->{records} }, { mvr => $mods, marcxml => $rec->marc, bibid => $rec->id };
587 $cstore->disconnect();
591 # ----------------------------------------------------------------------------
592 # These are the main OPAC search methods
593 # ----------------------------------------------------------------------------
595 __PACKAGE__->register_method(
596 method => 'the_quest_for_knowledge',
597 api_name => 'open-ils.search.biblio.multiclass',
599 Performs a multi class bilbli or metabib search
600 @param searchhash A search object layed out like so:
601 searches : { "$class" : "$value", ...}
602 org_unit : The org id to focus the search at
603 depth : The org depth
604 limit : The search limit
605 offset : The search offset
606 format : The MARC format
607 sort : What field to sort the results on [ author | title | pubdate ]
608 sort_dir : What direction do we sort? [ asc | desc ]
609 @return An object of the form
610 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
614 __PACKAGE__->register_method(
615 method => 'the_quest_for_knowledge',
616 api_name => 'open-ils.search.biblio.multiclass.staff',
617 signature => q/@see open-ils.search.biblio.multiclass/);
618 __PACKAGE__->register_method(
619 method => 'the_quest_for_knowledge',
620 api_name => 'open-ils.search.metabib.multiclass',
621 signature => q/@see open-ils.search.biblio.multiclass/);
622 __PACKAGE__->register_method(
623 method => 'the_quest_for_knowledge',
624 api_name => 'open-ils.search.metabib.multiclass.staff',
625 signature => q/@see open-ils.search.biblio.multiclass/);
627 sub the_quest_for_knowledge {
628 my( $self, $conn, $searchhash, $docache ) = @_;
630 return { count => 0 } unless $searchhash and
631 ref $searchhash->{searches} eq 'HASH';
633 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
637 if($self->api_name =~ /metabib/) {
639 $method =~ s/biblio/metabib/o;
643 my $offset = $searchhash->{offset} || 0;
644 my $limit = $searchhash->{limit} || 10;
645 my $end = $offset + $limit - 1;
647 # do some simple sanity checking
648 if(!$searchhash->{searches} or
649 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
650 return { count => 0 };
655 $searchhash->{offset} = 0;
656 $searchhash->{limit} = $maxlimit;
658 return { count => 0 } if $offset > $maxlimit;
661 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
662 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
663 my $ckey = $pfx . md5_hex($method . $s);
665 $logger->info("bib search for: $s");
667 $searchhash->{limit} -= $offset;
671 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
675 $method .= ".staff" if($self->api_name =~ /staff/);
676 $method .= ".atomic";
678 for (keys %$searchhash) {
679 delete $$searchhash{$_}
680 unless defined $$searchhash{$_};
683 $result = $U->storagereq( $method, %$searchhash );
690 return {count => 0} unless ($result && $$result[0]);
694 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
697 # If we didn't get this data from the cache, put it into the cache
698 # then return the correct offset of records
699 $logger->debug("putting search cache $ckey\n");
700 put_cache($ckey, $count, \@recs);
704 # if we have the full set of data, trim out
705 # the requested chunk based on limit and offset
707 for ($offset..$end) {
708 last unless $recs[$_];
714 return { ids => \@recs, count => $count };
725 my $end = $offset + $limit - 1;
727 $logger->debug("searching cache for $key : $start..$end\n");
729 return undef unless $cache;
730 my $data = $cache->get_cache($key);
732 return undef unless $data;
734 my $count = $data->[0];
737 return undef unless $offset < $count;
741 for( my $i = $offset; $i <= $end; $i++ ) {
742 last unless my $d = $$data[$i];
746 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
753 my( $key, $count, $data ) = @_;
754 return undef unless $cache;
755 $logger->debug("search_cache putting ".
756 scalar(@$data)." items at key $key with timeout $cache_timeout");
757 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
765 __PACKAGE__->register_method(
766 method => "biblio_mrid_to_modsbatch_batch",
767 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
769 sub biblio_mrid_to_modsbatch_batch {
770 my( $self, $client, $mrids) = @_;
771 warn "Performing mrid_to_modsbatch_batch...";
773 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
774 for my $id (@$mrids) {
775 next unless defined $id;
776 my ($m) = $method->run($id);
783 __PACKAGE__->register_method(
784 method => "biblio_mrid_to_modsbatch",
785 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
786 notes => <<" NOTES");
787 Returns the mvr associated with a given metarecod. If none exists,
791 __PACKAGE__->register_method(
792 method => "biblio_mrid_to_modsbatch",
793 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
794 notes => <<" NOTES");
795 Returns the mvr associated with a given metarecod. If none exists,
799 sub biblio_mrid_to_modsbatch {
800 my( $self, $client, $mrid, $args) = @_;
802 warn "Grabbing mvr for $mrid\n";
804 my ($mr, $evt) = _grab_metarecord($mrid);
805 return $evt unless $mr;
807 my $mvr = biblio_mrid_check_mvr($self, $client, $mr);
808 $mvr = biblio_mrid_make_modsbatch( $self, $client, $mr ) unless $mvr;
810 return $mvr unless ref($args);
812 # Here we find the lead record appropriate for the given filters
813 # and use that for the title and author of the metarecord
814 my $format = $$args{format};
815 my $org = $$args{org};
816 my $depth = $$args{depth};
818 return $mvr unless $format or $org or $depth;
820 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
821 $method = "$method.staff" if $self->api_name =~ /staff/o;
823 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
825 if( my $mods = $U->record_to_mvr($rec) ) {
827 $mvr->title($mods->title);
828 $mvr->title($mods->author);
829 $logger->debug("mods_slim updating title and ".
830 "author in mvr with ".$mods->title." : ".$mods->author);
836 # converts a metarecord to an mvr
839 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
840 return Fieldmapper::metabib::virtual_record->new($perl);
843 # checks to see if a metarecord has mods, if so returns true;
845 __PACKAGE__->register_method(
846 method => "biblio_mrid_check_mvr",
847 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
848 notes => <<" NOTES");
849 Takes a metarecord ID or a metarecord object and returns true
850 if the metarecord already has an mvr associated with it.
853 sub biblio_mrid_check_mvr {
854 my( $self, $client, $mrid ) = @_;
858 if(ref($mrid)) { $mr = $mrid; }
859 else { ($mr, $evt) = _grab_metarecord($mrid); }
862 warn "Checking mvr for mr " . $mr->id . "\n";
864 return _mr_to_mvr($mr) if $mr->mods();
868 sub _grab_metarecord {
870 #my $e = OpenILS::Utils::Editor->new;
871 my $e = new_editor();
872 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
877 __PACKAGE__->register_method(
878 method => "biblio_mrid_make_modsbatch",
879 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
880 notes => <<" NOTES");
881 Takes either a metarecord ID or a metarecord object.
882 Forces the creations of an mvr for the given metarecord.
883 The created mvr is returned.
886 sub biblio_mrid_make_modsbatch {
887 my( $self, $client, $mrid ) = @_;
889 #my $e = OpenILS::Utils::Editor->new;
890 my $e = new_editor();
897 $mr = $e->retrieve_metabib_metarecord($mrid)
901 my $masterid = $mr->master_record;
902 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
904 my $ids = $U->storagereq(
905 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
906 return undef unless @$ids;
908 my $master = $e->retrieve_biblio_record_entry($masterid)
911 # start the mods batch
912 my $u = OpenILS::Utils::ModsParser->new();
913 $u->start_mods_batch( $master->marc );
915 # grab all of the sub-records and shove them into the batch
916 my @ids = grep { $_ ne $masterid } @$ids;
917 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
922 my $r = $e->retrieve_biblio_record_entry($i);
923 push( @$subrecs, $r ) if $r;
928 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
929 $u->push_mods_batch( $_->marc ) if $_->marc;
933 # finish up and send to the client
934 my $mods = $u->finish_mods_batch();
935 $mods->doc_id($mrid);
936 $client->respond_complete($mods);
939 # now update the mods string in the db
940 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
943 #$e = OpenILS::Utils::Editor->new(xact => 1);
944 $e = new_editor(xact => 1);
945 $e->update_metabib_metarecord($mr)
946 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
955 # converts a mr id into a list of record ids
957 __PACKAGE__->register_method(
958 method => "biblio_mrid_to_record_ids",
959 api_name => "open-ils.search.biblio.metarecord_to_records",
962 __PACKAGE__->register_method(
963 method => "biblio_mrid_to_record_ids",
964 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
967 sub biblio_mrid_to_record_ids {
968 my( $self, $client, $mrid, $args ) = @_;
970 my $format = $$args{format};
971 my $org = $$args{org};
972 my $depth = $$args{depth};
974 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
975 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
976 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
978 return { count => scalar(@$recs), ids => $recs };
982 __PACKAGE__->register_method(
983 method => "biblio_record_to_marc_html",
984 api_name => "open-ils.search.biblio.record.html" );
986 my $parser = XML::LibXML->new();
987 my $xslt = XML::LibXSLT->new();
990 my $settings_client = OpenSRF::Utils::SettingsClient->new();
991 sub biblio_record_to_marc_html {
992 my( $self, $client, $recordid ) = @_;
995 my $dir = $settings_client->config_value( "dirs", "xsl" );
996 my $xsl = $settings_client->config_value(
997 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
999 $xsl = $parser->parse_file("$dir/$xsl");
1000 $marc_sheet = $xslt->parse_stylesheet( $xsl );
1004 my $record = $apputils->simple_scalar_request(
1006 "open-ils.cstore.direct.biblio.record_entry.retrieve",
1009 my $xmldoc = $parser->parse_string($record->marc);
1010 my $html = $marc_sheet->transform($xmldoc);
1011 $html = $html->toString();
1018 __PACKAGE__->register_method(
1019 method => "retrieve_all_copy_locations",
1020 api_name => "open-ils.search.config.copy_location.retrieve.all" );
1022 my $shelving_locations;
1023 sub retrieve_all_copy_locations {
1024 my( $self, $client ) = @_;
1025 if(!$shelving_locations) {
1026 $shelving_locations = $apputils->simple_scalar_request(
1028 "open-ils.cstore.direct.asset.copy_location.search.atomic",
1029 { id => { "!=" => undef } }
1032 return $shelving_locations;
1038 __PACKAGE__->register_method(
1039 method => "retrieve_all_copy_statuses",
1040 api_name => "open-ils.search.config.copy_status.retrieve.all" );
1043 sub retrieve_all_copy_statuses {
1044 my( $self, $client ) = @_;
1045 return $copy_statuses if $copy_statuses;
1046 return $copy_statuses =
1047 new_editor()->retrieve_all_config_copy_status();
1051 __PACKAGE__->register_method(
1052 method => "copy_counts_per_org",
1053 api_name => "open-ils.search.biblio.copy_counts.retrieve");
1055 __PACKAGE__->register_method(
1056 method => "copy_counts_per_org",
1057 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
1059 sub copy_counts_per_org {
1060 my( $self, $client, $record_id ) = @_;
1062 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1064 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1065 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1067 my $counts = $apputils->simple_scalar_request(
1068 "open-ils.storage", $method, $record_id );
1070 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1075 __PACKAGE__->register_method(
1076 method => "copy_count_summary",
1077 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1078 notes => <<" NOTES");
1079 returns an array of these:
1080 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1081 where statusx is a copy status name. the statuses are sorted
1085 sub copy_count_summary {
1086 my( $self, $client, $rid, $org, $depth ) = @_;
1089 my $data = $U->storagereq(
1090 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1092 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1098 __PACKAGE__->register_method(
1099 method => "multiclass_search",
1100 api_name => "open-ils.search.biblio.multiclass",
1101 notes => <<" NOTES");
1102 Performs a multiclass search
1103 PARAMS( searchBlob, org_unit, format, limit )
1104 where searchBlob is defined like this:
1106 "title" : { "term" : "water" },
1107 "author" : { "term" : "smith" },
1112 __PACKAGE__->register_method(
1113 method => "multiclass_search",
1114 api_name => "open-ils.search.biblio.multiclass.staff",
1115 notes => "see open-ils.search.biblio.multiclass" );
1117 sub multiclass_search {
1118 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1120 $logger->debug("Performing multiclass search with org => $orgid, " .
1121 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1123 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1124 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1127 my $records = $apputils->simplereq(
1128 'open-ils.storage', $meth,
1129 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1134 if( ref($records) and $records->[0] and
1135 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1137 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1139 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1140 return { ids => $recs, count => $count };
1146 __PACKAGE__->register_method(
1147 method => "multiclass_search",
1148 api_name => "open-ils.search.biblio.multiclass",
1150 Performs a multiclass search
1151 @param args A names hash of arguments:
1152 org_unit : The org to focus the search on
1153 depth : The search depth
1154 format : Item format
1155 limit : Return limit
1156 offset : Search offset
1157 searches : A named hash of searches which has the following format:
1159 "title" : { "term" : "water" },
1160 "author" : { "term" : "smith" },
1163 @return { ids : <array of ids>, count : hitcount }
1167 __PACKAGE__->register_method(
1168 method => "multiclass_search",
1169 api_name => "open-ils.search.biblio.multiclass.staff",
1170 notes => q/@see open-ils.search.biblio.multiclass/ );
1172 sub multiclass_search {
1173 my( $self, $client, $args ) = @_;
1175 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
1176 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1177 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1179 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1184 if( ref($records) and $records->[0] and
1185 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1187 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1189 return { ids => $recs, count => $count };
1196 __PACKAGE__->register_method(
1197 method => "marc_search",
1198 api_name => "open-ils.search.biblio.marc.staff");
1200 __PACKAGE__->register_method(
1201 method => "marc_search",
1202 api_name => "open-ils.search.biblio.marc",
1203 notes => <<" NOTES");
1205 open-ils.storage.biblio.full_rec.multi_search.atomic
1206 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1207 "limit":5,"sort":"author","item_type":"g"}
1211 my( $self, $conn, $args, $limit, $offset ) = @_;
1213 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1214 $method .= ".staff" if $self->api_name =~ /staff/;
1215 $method .= ".atomic";
1221 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1222 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1224 my $recs = search_cache($ckey, $offset, $limit);
1227 $recs = $U->storagereq($method, %$args) || [];
1229 put_cache($ckey, scalar(@$recs), $recs);
1230 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1237 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1238 my @recs = map { $_->[0] } @$recs;
1240 return { ids => \@recs, count => $count };
1244 __PACKAGE__->register_method(
1245 method => "biblio_search_isbn",
1246 api_name => "open-ils.search.biblio.isbn",
1249 sub biblio_search_isbn {
1250 my( $self, $client, $isbn ) = @_;
1251 $logger->debug("Searching ISBN $isbn");
1252 my $e = new_editor();
1253 my $recs = $U->storagereq(
1254 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1255 return { ids => $recs, count => scalar(@$recs) };
1259 __PACKAGE__->register_method(
1260 method => "biblio_search_issn",
1261 api_name => "open-ils.search.biblio.issn",
1264 sub biblio_search_issn {
1265 my( $self, $client, $issn ) = @_;
1266 $logger->debug("Searching ISSN $issn");
1267 my $e = new_editor();
1268 my $recs = $U->storagereq(
1269 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1270 return { ids => $recs, count => scalar(@$recs) };
1276 __PACKAGE__->register_method(
1277 method => "fetch_mods_by_copy",
1278 api_name => "open-ils.search.biblio.mods_from_copy",
1281 sub fetch_mods_by_copy {
1282 my( $self, $client, $copyid ) = @_;
1283 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1284 return $evt if $evt;
1285 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1286 return $apputils->record_to_mvr($record);
1291 # -------------------------------------------------------------------------------------
1293 __PACKAGE__->register_method(
1294 method => "cn_browse",
1295 api_name => "open-ils.search.callnumber.browse.target",
1296 notes => "Starts a callnumber browse"
1299 __PACKAGE__->register_method(
1300 method => "cn_browse",
1301 api_name => "open-ils.search.callnumber.browse.page_up",
1302 notes => "Returns the previous page of callnumbers",
1305 __PACKAGE__->register_method(
1306 method => "cn_browse",
1307 api_name => "open-ils.search.callnumber.browse.page_down",
1308 notes => "Returns the next page of callnumbers",
1312 # RETURNS array of arrays like so: label, owning_lib, record, id
1314 my( $self, $client, @params ) = @_;
1317 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1318 if( $self->api_name =~ /target/ );
1319 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1320 if( $self->api_name =~ /page_up/ );
1321 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1322 if( $self->api_name =~ /page_down/ );
1324 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1326 # -------------------------------------------------------------------------------------
1328 __PACKAGE__->register_method(
1329 method => "fetch_cn",
1331 api_name => "open-ils.search.callnumber.retrieve",
1332 notes => "retrieves a callnumber based on ID",
1336 my( $self, $client, $id ) = @_;
1337 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1338 return $evt if $evt;
1342 __PACKAGE__->register_method (
1343 method => "fetch_copy_by_cn",
1344 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1346 Returns an array of copy id's by callnumber id
1347 @param cnid The callnumber id
1348 @return An array of copy ids
1352 sub fetch_copy_by_cn {
1353 my( $self, $conn, $cnid ) = @_;
1354 return $U->cstorereq(
1355 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1356 { call_number => $cnid, deleted => 'f' } );
1359 __PACKAGE__->register_method (
1360 method => 'fetch_cn_by_info',
1361 api_name => 'open-ils.search.call_number.retrieve_by_info',
1363 @param label The callnumber label
1364 @param record The record the cn is attached to
1365 @param org The owning library of the cn
1366 @return The callnumber object
1371 sub fetch_cn_by_info {
1372 my( $self, $conn, $label, $record, $org ) = @_;
1373 return $U->cstorereq(
1374 'open-ils.cstore.direct.asset.call_number.search',
1375 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1382 __PACKAGE__->register_method (
1383 method => 'bib_extras',
1384 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1385 __PACKAGE__->register_method (
1386 method => 'bib_extras',
1387 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1388 __PACKAGE__->register_method (
1389 method => 'bib_extras',
1390 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1391 __PACKAGE__->register_method (
1392 method => 'bib_extras',
1393 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1398 my $e = new_editor();
1400 return $e->retrieve_all_config_lit_form_map()
1401 if( $self->api_name =~ /lit_form/ );
1403 return $e->retrieve_all_config_item_form_map()
1404 if( $self->api_name =~ /item_form_map/ );
1406 return $e->retrieve_all_config_item_type_map()
1407 if( $self->api_name =~ /item_type_map/ );
1409 return $e->retrieve_all_config_audience_map()
1410 if( $self->api_name =~ /audience_map/ );
1417 __PACKAGE__->register_method(
1418 method => 'fetch_slim_record',
1419 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1421 Returns a biblio.record_entry without the attached marcxml
1425 sub fetch_slim_record {
1426 my( $self, $conn, $ids ) = @_;
1428 #my $editor = OpenILS::Utils::Editor->new;
1429 my $editor = new_editor();
1432 return $editor->event unless
1433 my $r = $editor->retrieve_biblio_record_entry($_);
1442 __PACKAGE__->register_method(
1443 method => 'rec_to_mr_rec_descriptors',
1444 api_name => 'open-ils.search.metabib.record_to_descriptors',
1446 specialized method...
1447 Given a biblio record id or a metarecord id,
1448 this returns a list of metabib.record_descriptor
1449 objects that live within the same metarecord
1450 @param args Object of args including:
1454 sub rec_to_mr_rec_descriptors {
1455 my( $self, $conn, $args ) = @_;
1457 my $rec = $$args{record};
1458 my $mrec = $$args{metarecord};
1459 my $item_forms = $$args{item_forms};
1460 my $item_types = $$args{item_types};
1461 my $item_lang = $$args{item_lang};
1463 my $e = new_editor();
1467 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1468 return $e->event unless @$map;
1469 $mrec = $$map[0]->metarecord;
1472 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1473 return $e->event unless @$recs;
1475 my @recs = map { $_->source } @$recs;
1476 my $search = { record => \@recs };
1477 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1478 $search->{item_type} = $item_types if $item_types and @$item_types;
1479 $search->{item_lang} = $item_lang if $item_lang;
1481 my $desc = $e->search_metabib_record_descriptor($search);
1483 return { metarecord => $mrec, descriptors => $desc };
1489 __PACKAGE__->register_method(
1490 method => 'copies_created_on',
1494 sub copies_created_on {
1495 my( $self, $conn, $auth, $org, $date ) = @_;
1496 my $e = new_editor(authtoken=>$auth);
1497 return $e->event unless $e->checkauth;
1501 __PACKAGE__->register_method(
1502 method => 'fetch_age_protect',
1503 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1506 sub fetch_age_protect {
1507 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1511 __PACKAGE__->register_method(
1512 method => 'copies_by_cn_label',
1513 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1516 __PACKAGE__->register_method(
1517 method => 'copies_by_cn_label',
1518 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1521 sub copies_by_cn_label {
1522 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1523 my $e = new_editor();
1524 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1525 return [] unless @$cns;
1527 # show all non-deleted copies in the staff client ...
1528 if ($self->api_name =~ /staff$/o) {
1529 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1532 # ... otherwise, grab the copies ...
1533 my $copies = $e->search_asset_copy(
1534 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1535 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1539 # ... and test for location and status visibility
1540 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];