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 fleshed_copy_retrieve2( $self, $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");
466 my $search = $arghash->{searches} = {};
468 while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):[^:]+)$//so) {
469 my($type, $value) = split(':', $1);
470 next unless $type and $value;
472 $value =~ s/^\s*//og;
473 $value =~ s/\s*$//og;
474 $type = 'sort_dir' if $type eq 'dir';
476 if($type eq 'site') {
477 # 'site' is the org shortname. when using this, we also want
478 # to search at the requested org's depth
479 my $e = new_editor();
480 if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
481 $arghash->{org_unit} = $org->id if $org;
482 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
484 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
487 } elsif($type eq 'available') {
489 $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
491 } elsif($type eq 'lang') {
492 # collect languages into an array of languages
493 $arghash->{language} = [] unless $arghash->{language};
494 push(@{$arghash->{language}}, $value);
497 # append the search term to the term under construction
498 $search->{$type} = {} unless $search->{$type};
499 $search->{$type}->{term} =
500 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
505 # This is the front part of the string before any special tokens were parsed.
506 # Add this data to the default search class
507 my $type = $arghash->{default_class} || 'keyword';
508 $search->{$type} = {} unless $search->{$type};
509 $search->{$type}->{term} =
510 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $query" : $query;
513 # capture the original limit because the search method alters the limit internally
514 my $ol = $arghash->{limit};
516 (my $method = $self->api_name) =~ s/\.query//o;
517 $method = $self->method_lookup($method);
518 my ($data) = $method->run($arghash, $docache);
520 $arghash->{limit} = $ol if $ol;
521 $data->{compiled_search} = $arghash;
523 $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
528 __PACKAGE__->register_method(
529 method => 'cat_search_z_style_wrapper',
530 api_name => 'open-ils.search.biblio.zstyle',
532 signature => q/@see open-ils.search.biblio.multiclass/);
534 sub cat_search_z_style_wrapper {
537 my $authtoken = shift;
540 my $cstore = OpenSRF::AppSession->connect('open-ils.cstore');
542 my $ou = $cstore->request(
543 'open-ils.cstore.direct.actor.org_unit.search',
544 { parent_ou => undef }
547 my $result = { service => 'native-evergreen-catalog', records => [] };
548 my $searchhash = { limit => $$args{limit}, offset => $$args{offset}, org_unit => $ou->id };
550 $$searchhash{searches}{title}{term} = $$args{search}{title} if $$args{search}{title};
551 $$searchhash{searches}{author}{term} = $$args{search}{author} if $$args{search}{author};
552 $$searchhash{searches}{subject}{term} = $$args{search}{subject} if $$args{search}{subject};
553 $$searchhash{searches}{keyword}{term} = $$args{search}{keyword} if $$args{search}{keyword};
555 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{tcn} if $$args{search}{tcn};
556 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{isbn} if $$args{search}{isbn};
557 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{issn} if $$args{search}{issn};
558 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{publisher} if $$args{search}{publisher};
559 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{pubdate} if $$args{search}{pubdate};
560 $$searchhash{searches}{keyword}{term} .= join ' ', $$searchhash{searches}{keyword}{term}, $$args{search}{item_type} if $$args{search}{item_type};
562 my $list = the_quest_for_knowledge( $self, $client, $searchhash );
564 if ($list->{count} > 0) {
565 $result->{count} = $list->{count};
567 my $records = $cstore->request(
568 'open-ils.cstore.direct.biblio.record_entry.search.atomic',
569 { id => [ map { ( $_->[0] ) } @{$list->{ids}} ] }
572 for my $rec ( @$records ) {
574 my $u = OpenILS::Utils::ModsParser->new();
575 $u->start_mods_batch( $rec->marc );
576 my $mods = $u->finish_mods_batch();
578 push @{ $result->{records} }, { mvr => $mods, marcxml => $rec->marc, bibid => $rec->id };
584 $cstore->disconnect();
588 # ----------------------------------------------------------------------------
589 # These are the main OPAC search methods
590 # ----------------------------------------------------------------------------
592 __PACKAGE__->register_method(
593 method => 'the_quest_for_knowledge',
594 api_name => 'open-ils.search.biblio.multiclass',
596 Performs a multi class bilbli or metabib search
597 @param searchhash A search object layed out like so:
598 searches : { "$class" : "$value", ...}
599 org_unit : The org id to focus the search at
600 depth : The org depth
601 limit : The search limit
602 offset : The search offset
603 format : The MARC format
604 sort : What field to sort the results on [ author | title | pubdate ]
605 sort_dir : What direction do we sort? [ asc | desc ]
606 @return An object of the form
607 { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
611 __PACKAGE__->register_method(
612 method => 'the_quest_for_knowledge',
613 api_name => 'open-ils.search.biblio.multiclass.staff',
614 signature => q/@see open-ils.search.biblio.multiclass/);
615 __PACKAGE__->register_method(
616 method => 'the_quest_for_knowledge',
617 api_name => 'open-ils.search.metabib.multiclass',
618 signature => q/@see open-ils.search.biblio.multiclass/);
619 __PACKAGE__->register_method(
620 method => 'the_quest_for_knowledge',
621 api_name => 'open-ils.search.metabib.multiclass.staff',
622 signature => q/@see open-ils.search.biblio.multiclass/);
624 sub the_quest_for_knowledge {
625 my( $self, $conn, $searchhash, $docache ) = @_;
627 return { count => 0 } unless $searchhash and
628 ref $searchhash->{searches} eq 'HASH';
630 my $method = 'open-ils.storage.biblio.multiclass.search_fts';
634 if($self->api_name =~ /metabib/) {
636 $method =~ s/biblio/metabib/o;
640 my $offset = $searchhash->{offset} || 0;
641 my $limit = $searchhash->{limit} || 10;
642 my $end = $offset + $limit - 1;
644 # do some simple sanity checking
645 if(!$searchhash->{searches} or
646 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
647 return { count => 0 };
652 $searchhash->{offset} = 0;
653 $searchhash->{limit} = $maxlimit;
655 return { count => 0 } if $offset > $maxlimit;
658 push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
659 my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
660 my $ckey = $pfx . md5_hex($method . $s);
662 $logger->info("bib search for: $s");
664 $searchhash->{limit} -= $offset;
668 my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
672 $method .= ".staff" if($self->api_name =~ /staff/);
673 $method .= ".atomic";
675 for (keys %$searchhash) {
676 delete $$searchhash{$_}
677 unless defined $$searchhash{$_};
680 $result = $U->storagereq( $method, %$searchhash );
687 return {count => 0} unless ($result && $$result[0]);
691 my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
694 # If we didn't get this data from the cache, put it into the cache
695 # then return the correct offset of records
696 $logger->debug("putting search cache $ckey\n");
697 put_cache($ckey, $count, \@recs);
701 # if we have the full set of data, trim out
702 # the requested chunk based on limit and offset
704 for ($offset..$end) {
705 last unless $recs[$_];
711 return { ids => \@recs, count => $count };
722 my $end = $offset + $limit - 1;
724 $logger->debug("searching cache for $key : $start..$end\n");
726 return undef unless $cache;
727 my $data = $cache->get_cache($key);
729 return undef unless $data;
731 my $count = $data->[0];
734 return undef unless $offset < $count;
738 for( my $i = $offset; $i <= $end; $i++ ) {
739 last unless my $d = $$data[$i];
743 $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
750 my( $key, $count, $data ) = @_;
751 return undef unless $cache;
752 $logger->debug("search_cache putting ".
753 scalar(@$data)." items at key $key with timeout $cache_timeout");
754 $cache->put_cache($key, [ $count, $data ], $cache_timeout);
762 __PACKAGE__->register_method(
763 method => "biblio_mrid_to_modsbatch_batch",
764 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
766 sub biblio_mrid_to_modsbatch_batch {
767 my( $self, $client, $mrids) = @_;
768 warn "Performing mrid_to_modsbatch_batch...";
770 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
771 for my $id (@$mrids) {
772 next unless defined $id;
773 my ($m) = $method->run($id);
780 __PACKAGE__->register_method(
781 method => "biblio_mrid_to_modsbatch",
782 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
783 notes => <<" NOTES");
784 Returns the mvr associated with a given metarecod. If none exists,
788 __PACKAGE__->register_method(
789 method => "biblio_mrid_to_modsbatch",
790 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
791 notes => <<" NOTES");
792 Returns the mvr associated with a given metarecod. If none exists,
796 sub biblio_mrid_to_modsbatch {
797 my( $self, $client, $mrid, $args) = @_;
799 warn "Grabbing mvr for $mrid\n";
801 my ($mr, $evt) = _grab_metarecord($mrid);
802 return $evt unless $mr;
804 my $mvr = biblio_mrid_check_mvr($self, $client, $mr);
805 $mvr = biblio_mrid_make_modsbatch( $self, $client, $mr ) unless $mvr;
807 return $mvr unless ref($args);
809 # Here we find the lead record appropriate for the given filters
810 # and use that for the title and author of the metarecord
811 my $format = $$args{format};
812 my $org = $$args{org};
813 my $depth = $$args{depth};
815 return $mvr unless $format or $org or $depth;
817 my $method = "open-ils.storage.ordered.metabib.metarecord.records";
818 $method = "$method.staff" if $self->api_name =~ /staff/o;
820 my $rec = $U->storagereq($method, $format, $org, $depth, 1);
822 if( my $mods = $U->record_to_mvr($rec) ) {
824 $mvr->title($mods->title);
825 $mvr->title($mods->author);
826 $logger->debug("mods_slim updating title and ".
827 "author in mvr with ".$mods->title." : ".$mods->author);
833 # converts a metarecord to an mvr
836 my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
837 return Fieldmapper::metabib::virtual_record->new($perl);
840 # checks to see if a metarecord has mods, if so returns true;
842 __PACKAGE__->register_method(
843 method => "biblio_mrid_check_mvr",
844 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
845 notes => <<" NOTES");
846 Takes a metarecord ID or a metarecord object and returns true
847 if the metarecord already has an mvr associated with it.
850 sub biblio_mrid_check_mvr {
851 my( $self, $client, $mrid ) = @_;
855 if(ref($mrid)) { $mr = $mrid; }
856 else { ($mr, $evt) = _grab_metarecord($mrid); }
859 warn "Checking mvr for mr " . $mr->id . "\n";
861 return _mr_to_mvr($mr) if $mr->mods();
865 sub _grab_metarecord {
867 #my $e = OpenILS::Utils::Editor->new;
868 my $e = new_editor();
869 my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
874 __PACKAGE__->register_method(
875 method => "biblio_mrid_make_modsbatch",
876 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
877 notes => <<" NOTES");
878 Takes either a metarecord ID or a metarecord object.
879 Forces the creations of an mvr for the given metarecord.
880 The created mvr is returned.
883 sub biblio_mrid_make_modsbatch {
884 my( $self, $client, $mrid ) = @_;
886 #my $e = OpenILS::Utils::Editor->new;
887 my $e = new_editor();
894 $mr = $e->retrieve_metabib_metarecord($mrid)
898 my $masterid = $mr->master_record;
899 $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
901 my $ids = $U->storagereq(
902 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
903 return undef unless @$ids;
905 my $master = $e->retrieve_biblio_record_entry($masterid)
908 # start the mods batch
909 my $u = OpenILS::Utils::ModsParser->new();
910 $u->start_mods_batch( $master->marc );
912 # grab all of the sub-records and shove them into the batch
913 my @ids = grep { $_ ne $masterid } @$ids;
914 #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
919 my $r = $e->retrieve_biblio_record_entry($i);
920 push( @$subrecs, $r ) if $r;
925 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
926 $u->push_mods_batch( $_->marc ) if $_->marc;
930 # finish up and send to the client
931 my $mods = $u->finish_mods_batch();
932 $mods->doc_id($mrid);
933 $client->respond_complete($mods);
936 # now update the mods string in the db
937 my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
940 #$e = OpenILS::Utils::Editor->new(xact => 1);
941 $e = new_editor(xact => 1);
942 $e->update_metabib_metarecord($mr)
943 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
952 # converts a mr id into a list of record ids
954 __PACKAGE__->register_method(
955 method => "biblio_mrid_to_record_ids",
956 api_name => "open-ils.search.biblio.metarecord_to_records",
959 __PACKAGE__->register_method(
960 method => "biblio_mrid_to_record_ids",
961 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
964 sub biblio_mrid_to_record_ids {
965 my( $self, $client, $mrid, $args ) = @_;
967 my $format = $$args{format};
968 my $org = $$args{org};
969 my $depth = $$args{depth};
971 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
972 $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o;
973 my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
975 return { count => scalar(@$recs), ids => $recs };
979 __PACKAGE__->register_method(
980 method => "biblio_record_to_marc_html",
981 api_name => "open-ils.search.biblio.record.html" );
983 my $parser = XML::LibXML->new();
984 my $xslt = XML::LibXSLT->new();
987 my $settings_client = OpenSRF::Utils::SettingsClient->new();
988 sub biblio_record_to_marc_html {
989 my( $self, $client, $recordid ) = @_;
992 my $dir = $settings_client->config_value( "dirs", "xsl" );
993 my $xsl = $settings_client->config_value(
994 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
996 $xsl = $parser->parse_file("$dir/$xsl");
997 $marc_sheet = $xslt->parse_stylesheet( $xsl );
1001 my $record = $apputils->simple_scalar_request(
1003 "open-ils.cstore.direct.biblio.record_entry.retrieve",
1006 my $xmldoc = $parser->parse_string($record->marc);
1007 my $html = $marc_sheet->transform($xmldoc);
1008 $html = $html->toString();
1015 __PACKAGE__->register_method(
1016 method => "retrieve_all_copy_locations",
1017 api_name => "open-ils.search.config.copy_location.retrieve.all" );
1019 my $shelving_locations;
1020 sub retrieve_all_copy_locations {
1021 my( $self, $client ) = @_;
1022 if(!$shelving_locations) {
1023 $shelving_locations = $apputils->simple_scalar_request(
1025 "open-ils.cstore.direct.asset.copy_location.search.atomic",
1026 { id => { "!=" => undef } }
1029 return $shelving_locations;
1035 __PACKAGE__->register_method(
1036 method => "retrieve_all_copy_statuses",
1037 api_name => "open-ils.search.config.copy_status.retrieve.all" );
1040 sub retrieve_all_copy_statuses {
1041 my( $self, $client ) = @_;
1042 return $copy_statuses if $copy_statuses;
1043 return $copy_statuses =
1044 new_editor()->retrieve_all_config_copy_status();
1048 __PACKAGE__->register_method(
1049 method => "copy_counts_per_org",
1050 api_name => "open-ils.search.biblio.copy_counts.retrieve");
1052 __PACKAGE__->register_method(
1053 method => "copy_counts_per_org",
1054 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
1056 sub copy_counts_per_org {
1057 my( $self, $client, $record_id ) = @_;
1059 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1061 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1062 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1064 my $counts = $apputils->simple_scalar_request(
1065 "open-ils.storage", $method, $record_id );
1067 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1072 __PACKAGE__->register_method(
1073 method => "copy_count_summary",
1074 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1075 notes => <<" NOTES");
1076 returns an array of these:
1077 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1078 where statusx is a copy status name. the statuses are sorted
1082 sub copy_count_summary {
1083 my( $self, $client, $rid, $org, $depth ) = @_;
1086 my $data = $U->storagereq(
1087 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1089 return [ sort { $a->[1] cmp $b->[1] } @$data ];
1095 __PACKAGE__->register_method(
1096 method => "multiclass_search",
1097 api_name => "open-ils.search.biblio.multiclass",
1098 notes => <<" NOTES");
1099 Performs a multiclass search
1100 PARAMS( searchBlob, org_unit, format, limit )
1101 where searchBlob is defined like this:
1103 "title" : { "term" : "water" },
1104 "author" : { "term" : "smith" },
1109 __PACKAGE__->register_method(
1110 method => "multiclass_search",
1111 api_name => "open-ils.search.biblio.multiclass.staff",
1112 notes => "see open-ils.search.biblio.multiclass" );
1114 sub multiclass_search {
1115 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1117 $logger->debug("Performing multiclass search with org => $orgid, " .
1118 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1120 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1121 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1124 my $records = $apputils->simplereq(
1125 'open-ils.storage', $meth,
1126 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1131 if( ref($records) and $records->[0] and
1132 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1134 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1136 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1137 return { ids => $recs, count => $count };
1143 __PACKAGE__->register_method(
1144 method => "multiclass_search",
1145 api_name => "open-ils.search.biblio.multiclass",
1147 Performs a multiclass search
1148 @param args A names hash of arguments:
1149 org_unit : The org to focus the search on
1150 depth : The search depth
1151 format : Item format
1152 limit : Return limit
1153 offset : Search offset
1154 searches : A named hash of searches which has the following format:
1156 "title" : { "term" : "water" },
1157 "author" : { "term" : "smith" },
1160 @return { ids : <array of ids>, count : hitcount }
1164 __PACKAGE__->register_method(
1165 method => "multiclass_search",
1166 api_name => "open-ils.search.biblio.multiclass.staff",
1167 notes => q/@see open-ils.search.biblio.multiclass/ );
1169 sub multiclass_search {
1170 my( $self, $client, $args ) = @_;
1172 $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
1173 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1174 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1176 my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1181 if( ref($records) and $records->[0] and
1182 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1184 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1186 return { ids => $recs, count => $count };
1193 __PACKAGE__->register_method(
1194 method => "marc_search",
1195 api_name => "open-ils.search.biblio.marc.staff");
1197 __PACKAGE__->register_method(
1198 method => "marc_search",
1199 api_name => "open-ils.search.biblio.marc",
1200 notes => <<" NOTES");
1202 open-ils.storage.biblio.full_rec.multi_search.atomic
1203 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1204 "limit":5,"sort":"author","item_type":"g"}
1208 my( $self, $conn, $args, $limit, $offset ) = @_;
1210 my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1211 $method .= ".staff" if $self->api_name =~ /staff/;
1212 $method .= ".atomic";
1218 push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1219 my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1221 my $recs = search_cache($ckey, $offset, $limit);
1224 $recs = $U->storagereq($method, %$args) || [];
1226 put_cache($ckey, scalar(@$recs), $recs);
1227 $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1234 $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1235 my @recs = map { $_->[0] } @$recs;
1237 return { ids => \@recs, count => $count };
1241 __PACKAGE__->register_method(
1242 method => "biblio_search_isbn",
1243 api_name => "open-ils.search.biblio.isbn",
1246 sub biblio_search_isbn {
1247 my( $self, $client, $isbn ) = @_;
1248 $logger->debug("Searching ISBN $isbn");
1249 my $e = new_editor();
1250 my $recs = $U->storagereq(
1251 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1252 return { ids => $recs, count => scalar(@$recs) };
1256 __PACKAGE__->register_method(
1257 method => "biblio_search_issn",
1258 api_name => "open-ils.search.biblio.issn",
1261 sub biblio_search_issn {
1262 my( $self, $client, $issn ) = @_;
1263 $logger->debug("Searching ISSN $issn");
1264 my $e = new_editor();
1265 my $recs = $U->storagereq(
1266 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1267 return { ids => $recs, count => scalar(@$recs) };
1273 __PACKAGE__->register_method(
1274 method => "fetch_mods_by_copy",
1275 api_name => "open-ils.search.biblio.mods_from_copy",
1278 sub fetch_mods_by_copy {
1279 my( $self, $client, $copyid ) = @_;
1280 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1281 return $evt if $evt;
1282 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1283 return $apputils->record_to_mvr($record);
1288 # -------------------------------------------------------------------------------------
1290 __PACKAGE__->register_method(
1291 method => "cn_browse",
1292 api_name => "open-ils.search.callnumber.browse.target",
1293 notes => "Starts a callnumber browse"
1296 __PACKAGE__->register_method(
1297 method => "cn_browse",
1298 api_name => "open-ils.search.callnumber.browse.page_up",
1299 notes => "Returns the previous page of callnumbers",
1302 __PACKAGE__->register_method(
1303 method => "cn_browse",
1304 api_name => "open-ils.search.callnumber.browse.page_down",
1305 notes => "Returns the next page of callnumbers",
1309 # RETURNS array of arrays like so: label, owning_lib, record, id
1311 my( $self, $client, @params ) = @_;
1314 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1315 if( $self->api_name =~ /target/ );
1316 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1317 if( $self->api_name =~ /page_up/ );
1318 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1319 if( $self->api_name =~ /page_down/ );
1321 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1323 # -------------------------------------------------------------------------------------
1325 __PACKAGE__->register_method(
1326 method => "fetch_cn",
1327 api_name => "open-ils.search.callnumber.retrieve",
1328 notes => "retrieves a callnumber based on ID",
1332 my( $self, $client, $id ) = @_;
1333 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1334 return $evt if $evt;
1338 __PACKAGE__->register_method (
1339 method => "fetch_copy_by_cn",
1340 api_name => 'open-ils.search.copies_by_call_number.retrieve',
1342 Returns an array of copy id's by callnumber id
1343 @param cnid The callnumber id
1344 @return An array of copy ids
1348 sub fetch_copy_by_cn {
1349 my( $self, $conn, $cnid ) = @_;
1350 return $U->cstorereq(
1351 'open-ils.cstore.direct.asset.copy.id_list.atomic',
1352 { call_number => $cnid, deleted => 'f' } );
1355 __PACKAGE__->register_method (
1356 method => 'fetch_cn_by_info',
1357 api_name => 'open-ils.search.call_number.retrieve_by_info',
1359 @param label The callnumber label
1360 @param record The record the cn is attached to
1361 @param org The owning library of the cn
1362 @return The callnumber object
1367 sub fetch_cn_by_info {
1368 my( $self, $conn, $label, $record, $org ) = @_;
1369 return $U->cstorereq(
1370 'open-ils.cstore.direct.asset.call_number.search',
1371 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1378 __PACKAGE__->register_method (
1379 method => 'bib_extras',
1380 api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1381 __PACKAGE__->register_method (
1382 method => 'bib_extras',
1383 api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1384 __PACKAGE__->register_method (
1385 method => 'bib_extras',
1386 api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1387 __PACKAGE__->register_method (
1388 method => 'bib_extras',
1389 api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1394 my $e = new_editor();
1396 return $e->retrieve_all_config_lit_form_map()
1397 if( $self->api_name =~ /lit_form/ );
1399 return $e->retrieve_all_config_item_form_map()
1400 if( $self->api_name =~ /item_form_map/ );
1402 return $e->retrieve_all_config_item_type_map()
1403 if( $self->api_name =~ /item_type_map/ );
1405 return $e->retrieve_all_config_audience_map()
1406 if( $self->api_name =~ /audience_map/ );
1413 __PACKAGE__->register_method(
1414 method => 'fetch_slim_record',
1415 api_name => 'open-ils.search.biblio.record_entry.slim.retrieve',
1417 Returns a biblio.record_entry without the attached marcxml
1421 sub fetch_slim_record {
1422 my( $self, $conn, $ids ) = @_;
1424 #my $editor = OpenILS::Utils::Editor->new;
1425 my $editor = new_editor();
1428 return $editor->event unless
1429 my $r = $editor->retrieve_biblio_record_entry($_);
1438 __PACKAGE__->register_method(
1439 method => 'rec_to_mr_rec_descriptors',
1440 api_name => 'open-ils.search.metabib.record_to_descriptors',
1442 specialized method...
1443 Given a biblio record id or a metarecord id,
1444 this returns a list of metabib.record_descriptor
1445 objects that live within the same metarecord
1446 @param args Object of args including:
1450 sub rec_to_mr_rec_descriptors {
1451 my( $self, $conn, $args ) = @_;
1453 my $rec = $$args{record};
1454 my $mrec = $$args{metarecord};
1455 my $item_forms = $$args{item_forms};
1456 my $item_types = $$args{item_types};
1457 my $item_lang = $$args{item_lang};
1459 my $e = new_editor();
1463 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1464 return $e->event unless @$map;
1465 $mrec = $$map[0]->metarecord;
1468 $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1469 return $e->event unless @$recs;
1471 my @recs = map { $_->source } @$recs;
1472 my $search = { record => \@recs };
1473 $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1474 $search->{item_type} = $item_types if $item_types and @$item_types;
1475 $search->{item_lang} = $item_lang if $item_lang;
1477 my $desc = $e->search_metabib_record_descriptor($search);
1479 return { metarecord => $mrec, descriptors => $desc };
1485 __PACKAGE__->register_method(
1486 method => 'copies_created_on',
1490 sub copies_created_on {
1491 my( $self, $conn, $auth, $org, $date ) = @_;
1492 my $e = new_editor(authtoken=>$auth);
1493 return $e->event unless $e->checkauth;
1497 __PACKAGE__->register_method(
1498 method => 'fetch_age_protect',
1499 api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1502 sub fetch_age_protect {
1503 return new_editor()->retrieve_all_config_rule_age_hold_protect();
1507 __PACKAGE__->register_method(
1508 method => 'copies_by_cn_label',
1509 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1512 __PACKAGE__->register_method(
1513 method => 'copies_by_cn_label',
1514 api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1517 sub copies_by_cn_label {
1518 my( $self, $conn, $record, $label, $circ_lib ) = @_;
1519 my $e = new_editor();
1520 my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1521 return [] unless @$cns;
1523 # show all non-deleted copies in the staff client ...
1524 if ($self->api_name =~ /staff$/o) {
1525 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1528 # ... otherwise, grab the copies ...
1529 my $copies = $e->search_asset_copy(
1530 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1531 {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1535 # ... and test for location and status visibility
1536 return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];