1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Utils::ModsParser;
8 use OpenSRF::Utils::SettingsClient;
10 use OpenILS::Application::AppUtils;
14 use Time::HiRes qw(time);
15 use OpenSRF::EX qw(:try);
16 use Digest::MD5 qw(md5_hex);
21 my $apputils = "OpenILS::Application::AppUtils";
23 # Houses biblio search utilites
25 __PACKAGE__->register_method(
26 method => "biblio_search_marc",
27 api_name => "open-ils.search.biblio.marc",
29 note => "Searches biblio information by marc tag",
32 sub biblio_search_marc {
34 my( $self, $client, $search_hash, $string ) = @_;
36 warn "Building biblio marc session\n";
37 my $session = OpenSRF::AppSession->create("open-ils.storage");
40 warn "Sending biblio marc request. String $string\nSearch hash: " . Dumper($search_hash);
41 my $request = $session->request(
42 "open-ils.storage.direct.metabib.full_rec.search_fts.index_vector.atomic",
43 restrict => $search_hash,
45 my $data = $request->gather(1);
50 $session->disconnect();
58 # ---------------------------------------------------------------------------
59 # takes a list of record id's and turns the docs into friendly
60 # mods structures. Creates one MODS structure for each doc id.
61 # ---------------------------------------------------------------------------
62 sub _records_to_mods {
68 my $session = OpenSRF::AppSession->create("open-ils.storage");
69 my $request = $session->request(
70 "open-ils.storage.direct.biblio.record_entry.batch.retrieve", @ids );
72 my $last_content = undef;
74 while( my $response = $request->recv() ) {
77 my $u = OpenILS::Utils::ModsParser->new();
78 $u->start_mods_batch( $last_content->marc );
79 my $mods = $u->finish_mods_batch();
80 $mods->doc_id($last_content->id());
81 warn "Turning doc " . $mods->doc_id() . " into MODS\n";
82 $last_content = undef;
86 next unless $response;
88 if($response->isa("OpenSRF::EX")) {
89 throw $response ($response->stringify);
92 $last_content = $response->content;
97 my $u = OpenILS::Utils::ModsParser->new();
98 $u->start_mods_batch( $last_content->marc );
99 my $mods = $u->finish_mods_batch();
100 $mods->doc_id($last_content->id());
101 push @results, $mods;
106 $session->disconnect();
112 __PACKAGE__->register_method(
113 method => "record_id_to_mods",
114 api_name => "open-ils.search.biblio.record.mods.retrieve",
116 note => "Provide ID, we provide the mods"
119 # converts a record into a mods object with copy counts attached
120 sub record_id_to_mods {
122 my( $self, $client, $org_id, $id ) = @_;
124 my $mods_list = _records_to_mods( $id );
125 my $mods_obj = $mods_list->[0];
126 my $cmethod = $self->method_lookup(
127 "open-ils.search.biblio.record.copy_count");
128 my ($count) = $cmethod->run($org_id, $id);
129 $mods_obj->copy_count($count);
135 __PACKAGE__->register_method(
136 method => "record_id_to_mods_slim",
137 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
139 note => "Provide ID, we provide the mods"
142 # converts a record into a mods object with NO copy counts attached
143 sub record_id_to_mods_slim {
145 my( $self, $client, $id ) = @_;
146 warn "Retrieving MODS object for record $id\n";
147 return undef unless(defined $id);
149 my $mods_list = _records_to_mods( $id );
150 my $mods_obj = $mods_list->[0];
155 # Returns the number of copies attached to a record based on org location
156 __PACKAGE__->register_method(
157 method => "record_id_to_copy_count",
158 api_name => "open-ils.search.biblio.record.copy_count",
161 __PACKAGE__->register_method(
162 method => "record_id_to_copy_count",
163 api_name => "open-ils.search.biblio.metarecord.copy_count",
165 sub record_id_to_copy_count {
166 my( $self, $client, $org_id, $record_id ) = @_;
168 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
170 if($self->api_name =~ /metarecord/) {
171 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
175 my $session = OpenSRF::AppSession->create("open-ils.storage");
176 warn "copy_count retrieve $record_id\n";
177 return undef unless(defined $record_id);
179 my $request = $session->request(
180 $method, org_unit => $org_id => $key => $record_id );
183 my $count = $request->gather(1);
184 $session->disconnect();
185 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
190 # used for cat search classes
191 my $cat_search_hash = {
194 { tag => "100", subfield => "a"} ,
195 { tag => "700", subfield => "a"},
199 { tag => "245", subfield => "a"},
200 { tag => "242", subfield => "a"},
201 { tag => "240", subfield => "a"},
202 { tag => "210", subfield => "a"},
206 { tag => "650", subfield => "_" },
210 { tag => "035", subfield => "_" },
214 { tag => "020", subfield => "a" },
220 __PACKAGE__->register_method(
221 method => "biblio_search_tcn",
222 api_name => "open-ils.search.biblio.tcn",
224 note => "Retrieve a record by TCN",
227 sub biblio_search_tcn {
229 my( $self, $client, $tcn ) = @_;
231 $tcn =~ s/.*?(\w+)\s*$/$1/o;
232 warn "Searching TCN $tcn\n";
234 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
235 my $request = $session->request(
236 "open-ils.storage.direct.biblio.record_entry.search.tcn_value", $tcn );
237 my $record_entry = $request->gather(1);
240 for my $record (@$record_entry) {
241 push @ids, $record->id;
244 $session->disconnect();
246 warn "received ID's for tcn search @ids\n";
249 return { count => $size, ids => \@ids };
254 # --------------------------------------------------------------------------------
257 __PACKAGE__->register_method(
258 method => "biblio_search_isbn",
259 api_name => "open-ils.search.biblio.isbn",
262 sub biblio_search_isbn {
263 my( $self, $client, $isbn ) = @_;
264 throw OpenSRF::EX::InvalidArg
266 ("biblio_search_isbn needs an ISBN to search")
267 unless defined $isbn;
269 warn "biblio search for ISBN $isbn\n";
270 my $method = $self->method_lookup("open-ils.search.biblio.marc");
271 my ($records) = $method->run( $cat_search_hash->{isbn}, $isbn );
273 my $size = @$records;
274 return { count => $size, ids => $records };
279 # --------------------------------------------------------------------------------
281 __PACKAGE__->register_method(
282 method => "biblio_barcode_to_copy",
283 api_name => "open-ils.search.asset.copy.find_by_barcode",
286 # turns a barcode into a copy object
287 sub biblio_barcode_to_copy {
288 my( $self, $client, $barcode ) = @_;
290 throw OpenSRF::EX::InvalidArg
291 ("search.biblio.barcode needs a barcode to search")
292 unless defined $barcode;
294 warn "copy search for barcode $barcode\n";
295 my $record = OpenILS::Application::AppUtils->simple_scalar_request(
297 "open-ils.storage.direct.asset.copy.search.barcode",
300 return undef unless($record);
305 __PACKAGE__->register_method(
306 method => "biblio_id_to_copy",
307 api_name => "open-ils.search.asset.copy.batch.retrieve",
310 # turns a barcode into a copy object
311 sub biblio_id_to_copy {
312 my( $self, $client, $ids ) = @_;
314 throw OpenSRF::EX::InvalidArg
315 ("search.biblio.batch.retrieve needs a id to search")
318 warn "copy search for ids @$ids\n";
319 my $record = OpenILS::Application::AppUtils->simple_scalar_request(
321 "open-ils.storage.direct.asset.copy.batch.retrieve.atomic",
329 __PACKAGE__->register_method(
330 method => "fleshed_copy_retrieve",
331 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve",
334 # turns a barcode into a copy object
335 sub fleshed_copy_retrieve {
336 my( $self, $client, $ids ) = @_;
338 throw OpenSRF::EX::InvalidArg
339 ("search.biblio.batch.retrieve needs a id to search")
342 warn "fleshed copy search for id @$ids\n";
343 my $copy = OpenILS::Application::AppUtils->simple_scalar_request(
345 "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic",
353 __PACKAGE__->register_method(
354 method => "biblio_barcode_to_title",
355 api_name => "open-ils.search.biblio.find_by_barcode",
358 sub biblio_barcode_to_title {
359 my( $self, $client, $barcode ) = @_;
362 throw OpenSRF::EX::ERROR
363 ("Not enough args to find_by_barcode");
366 my $title = $apputils->simple_scalar_request(
368 "open-ils.storage.biblio.record_entry.retrieve_by_barcode",
371 return { ids => $title->id, count => 1 };
374 my $u = OpenILS::Utils::ModsParser->new();
375 $u->start_mods_batch( $title->marc );
376 my $mods = $u->finish_mods_batch();
377 $mods->doc_id($title->id());
384 __PACKAGE__->register_method(
385 method => "biblio_copy_to_mods",
386 api_name => "open-ils.search.biblio.copy.mods.retrieve",
389 # takes a copy object and returns it fleshed mods object
390 sub biblio_copy_to_mods {
391 my( $self, $client, $copy ) = @_;
393 throw OpenSRF::EX::InvalidArgs
394 ("copy.mods.retrieve needs a copy") unless( $copy );
396 new Fieldmapper::asset::copy($copy);
398 my $volume = OpenILS::Application::AppUtils->simple_scalar_request(
400 "open-ils.storage.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 sub barcode_to_mods {
417 # --------------------------------------------------------------------------------
419 __PACKAGE__->register_method(
420 method => "cat_biblio_search_class",
421 api_name => "open-ils.search.cat.biblio.class",
423 note => "Searches biblio information by search class",
426 sub cat_biblio_search_class {
428 my( $self, $client, $org_id, $class, $sort, $string ) = @_;
430 throw OpenSRF::EX::InvalidArg
431 ("Not enough args to open-ils.search.cat.biblio.class")
432 unless( defined($org_id) and $class and $sort and $string );
437 my $method = $self->method_lookup("open-ils.search.biblio.marc");
439 throw OpenSRF::EX::PANIC
440 ("Can't lookup method 'open-ils.search.biblio.marc'");
443 my ($records) = $method->run( $cat_search_hash->{$class}, $string );
446 for my $i (@$records) { push @ids, $i->[0]; }
448 my $mods_list = _records_to_mods( @ids );
449 return undef unless (ref($mods_list) eq "ARRAY");
451 # ---------------------------------------------------------------
452 # append copy count information to the mods objects
453 my $session = OpenSRF::AppSession->create("open-ils.storage");
455 my $request = $session->request(
456 "open-ils.storage.direct.biblio.record_copy_count.batch", $org_id, @ids );
460 warn "receiving copy counts for doc $id\n";
462 my $response = $request->recv();
463 next unless $response;
465 if( $response and UNIVERSAL::isa($response, "Error")) {
466 throw $response ($response->stringify);
469 my $count = $response->content;
470 my $mods_obj = undef;
471 for my $m (@$mods_list) {
472 $mods_obj = $m if ($m->doc_id() == $id)
475 $mods_obj->copy_count($count);
478 $client->respond( $mods_obj );
484 $session->disconnect();
486 # ---------------------------------------------------------------
493 __PACKAGE__->register_method(
494 method => "cat_biblio_search_class_id",
495 api_name => "open-ils.search.cat.biblio.class.id",
497 note => "Searches biblio information by search class and returns the IDs",
500 sub cat_biblio_search_class_id {
502 my( $self, $client, $org_id, $class, $string, $limit, $offset ) = @_;
509 $string = OpenILS::Application::Search->filter_search($string);
510 if(!$string) { return undef; }
512 warn "Searching cat.biblio.class.id string: $string offset: $offset limit: $limit\n";
514 throw OpenSRF::EX::InvalidArg
515 ("Not enough args to open-ils.search.cat.biblio.class")
516 unless( defined($org_id) and $class and $string );
521 my $cache_key = md5_hex( $org_id . $class . $string );
522 my $id_array = OpenILS::Application::SearchCache->get_cache($cache_key);
525 warn "Returning class search from cache\n";
526 my $size = @$id_array;
527 my @ids = @$id_array[ $offset..($offset+$limit) ];
528 return { count => $size, ids => \@ids };
531 my $method = $self->method_lookup("open-ils.search.biblio.marc");
533 throw OpenSRF::EX::PANIC
534 ("Can't lookup method 'open-ils.search.biblio.marc'");
537 my ($records) = $method->run( $cat_search_hash->{$class}, $string );
541 for my $i (@$records) {
542 if(defined($i->[0])) {
543 push @cache_ids, $i->[0];
547 my @ids = @cache_ids[ $offset..($offset+$limit) ];
548 my $size = @$records;
550 OpenILS::Application::SearchCache->put_cache(
551 $cache_key, \@cache_ids, $size );
553 return { count =>$size, ids => \@ids };
558 __PACKAGE__->register_method(
559 method => "biblio_search_class",
560 api_name => "open-ils.search.biblio.class",
562 note => "Searches biblio information by search class and returns the IDs",
565 sub biblio_search_class {
567 my( $self, $client, $class, $string,
568 $org_id, $org_type, $limit, $offset ) = @_;
570 warn "org: $org_id : depth: $org_type : limit: $limit : offset: $offset\n";
573 $limit = 100 unless defined($limit and $limit > 0 );
574 $org_id = "1" unless defined($org_id); # xxx
575 $org_type = 0 unless defined($org_type);
577 warn "Searching biblio.class.id\n" .
579 "\noffset: $offset\n" .
581 "org_id: $org_id\n" .
582 "depth: $org_type\n" ;
584 $string = OpenILS::Application::Search->filter_search($string);
585 if(!$string) { return undef; }
587 if( !defined($org_id) or !$class or !$string ) {
588 warn "not enbough args to metarecord search\n";
589 throw OpenSRF::EX::InvalidArg
590 ("Not enough args to open-ils.search.cat.biblio.class")
595 if( ($class ne "title") and ($class ne "author") and
596 ($class ne "subject") and ($class ne "keyword") ) {
597 warn "Invalid search class: $class\n";
598 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
601 # grab the mr id's from storage
603 my $method = "open-ils.storage.metabib.$class.search_fts.metarecord_count";
604 warn "Performing count method $method\n";
605 my $session = OpenSRF::AppSession->create('open-ils.storage');
607 my $request = $session->request( $method,
612 my $count = $request->gather(1);
613 warn "Received count $count\n";
614 # XXX check count size and respond accordingly
616 $request = $session->request(
617 "open-ils.storage.metabib.$class.search_fts.metarecord.atomic",
618 #"open-ils.storage.cachable.metabib.$class.search_fts.metarecord.atomic",
626 my $records = $request->gather(1);
630 warn "Received from class search " . Dumper($records);
632 # if we just get one, it won't be wrapped in an array
633 if(!ref($records->[0])) {
634 $records = [$records];
637 for my $i (@$records) {
643 #my @ids = @all_ids[ $offset..($offset+$limit) ];
645 @ids = grep { defined($_->[0]) } @ids;
648 $session->disconnect();
650 return { count =>$count, ids => \@ids };
657 __PACKAGE__->register_method(
658 method => "biblio_mrid_to_modsbatch",
659 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
662 sub biblio_mrid_to_modsbatch {
663 my( $self, $client, $mrid ) = @_;
665 throw OpenSRF::EX::InvalidArg
666 ("search.biblio.metarecord_to_mods requires mr id")
667 unless defined( $mrid );
670 my $metarecord = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage",
671 "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
674 throw OpenSRF::EX::ERROR ("No metarecord exists with the given id: $mrid");
677 my $master_id = $metarecord->master_record();
680 # check for existing mods
681 if($metarecord->mods()){
682 warn "We already have mods for " . $metarecord->id . "\n";
683 my $perl = JSON->JSON2perl($metarecord->mods());
684 return Fieldmapper::metabib::virtual_record->new($perl);
689 warn "Creating mods batch for metarecord $mrid\n";
690 my $id_hash = biblio_mrid_to_record_ids( undef, undef, $mrid );
691 my @ids = @{$id_hash->{ids}};
693 if(@ids < 1) { return undef; }
695 warn "Master ID is $master_id\n";
696 # grab the master record to start the mods batch
698 my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage",
699 "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
702 throw OpenSRF::EX::ERROR
703 ("No record returned with id $master_id");
706 my $u = OpenILS::Utils::ModsParser->new();
708 $u->start_mods_batch( $record->marc );
709 my $main_doc_id = $record->id();
711 @ids = grep { $_ ne $master_id } @ids;
713 # now we have to collect all of the marc objects and push them into a mods batch
714 my $session = OpenSRF::AppSession->create("open-ils.storage");
715 my $request = $session->request(
716 "open-ils.storage.direct.biblio.record_entry.batch.retrieve", @ids );
718 while( my $response = $request->recv() ) {
720 next unless $response;
721 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
722 throw $response ($response->stringify);
725 my $content = $response->content;
728 $u->push_mods_batch( $content->marc );
732 my $mods = $u->finish_mods_batch();
733 $mods->doc_id($mrid);
736 $client->respond_complete($mods);
738 my $mods_string = JSON->perl2JSON($mods->decast);
740 $metarecord->mods($mods_string);
742 my $req = $session->request(
743 "open-ils.storage.direct.metabib.metarecord.update",
750 $session->disconnect();
758 # converts a mr id into a list of record ids
760 __PACKAGE__->register_method(
761 method => "biblio_mrid_to_record_ids",
762 api_name => "open-ils.search.biblio.metarecord_to_records",
765 sub biblio_mrid_to_record_ids {
766 my( $self, $client, $mrid ) = @_;
768 throw OpenSRF::EX::InvalidArg
769 ("search.biblio.metarecord_to_record_ids requires mr id")
770 unless defined( $mrid );
772 warn "Searching for record for MR $mrid\n";
774 my $mrmaps = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage",
775 "open-ils.storage.direct.metabib.metarecord_source_map.search.metarecord", $mrid );
778 for my $map (@$mrmaps) { push @ids, $map->source(); }
780 warn "Recovered id's [@ids] for mr $mrid\n";
784 return { count => $size, ids => \@ids };
790 __PACKAGE__->register_method(
791 method => "biblio_record_to_marc_html",
792 api_name => "open-ils.search.biblio.record.html" );
794 my $parser = XML::LibXML->new();
795 my $xslt = XML::LibXSLT->new();
798 my $settings_client = OpenSRF::Utils::SettingsClient->new();
799 sub biblio_record_to_marc_html {
800 my( $self, $client, $recordid ) = @_;
803 my $dir = $settings_client->config_value( "dirs", "xsl" );
804 my $xsl = $settings_client->config_value(
805 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
807 $xsl = $parser->parse_file("$dir/$xsl");
808 $marc_sheet = $xslt->parse_stylesheet( $xsl );
812 my $record = $apputils->simple_scalar_request(
814 "open-ils.storage.direct.biblio.record_entry.retrieve",
817 my $xmldoc = $parser->parse_string($record->marc);
818 my $html = $marc_sheet->transform($xmldoc);
819 $html = $html->toString();
826 __PACKAGE__->register_method(
827 method => "retrieve_all_copy_locations",
828 api_name => "open-ils.search.config.copy_location.retrieve.all" );
830 my $shelving_locations;
831 sub retrieve_all_copy_locations {
832 my( $self, $client ) = @_;
833 if(!$shelving_locations) {
834 $shelving_locations = $apputils->simple_scalar_request(
836 "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
838 return $shelving_locations;
843 __PACKAGE__->register_method(
844 method => "retrieve_all_copy_statuses",
845 api_name => "open-ils.search.config.copy_status.retrieve.all" );
848 sub retrieve_all_copy_statuses {
849 my( $self, $client ) = @_;
850 if(!$copy_statuses) {
851 $copy_statuses = $apputils->simple_scalar_request(
853 "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
855 return $copy_statuses;