1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Utils::ModsParser;
10 use OpenSRF::Utils::SettingsClient;
12 use OpenILS::Application::AppUtils;
13 my $U = "OpenILS::Application::AppUtils";
17 use Time::HiRes qw(time);
18 use OpenSRF::EX qw(:try);
19 use Digest::MD5 qw(md5_hex);
24 $Data::Dumper::Indent = 0;
25 use OpenSRF::Utils::Logger qw/:logger/;
27 my $apputils = "OpenILS::Application::AppUtils";
29 # Houses biblio search utilites
32 __PACKAGE__->register_method(
34 api_name => "open-ils.search.test");
36 sub test { return "test"; }
41 __PACKAGE__->register_method(
42 method => "biblio_search_marc",
43 api_name => "open-ils.search.biblio.marc",
45 note => "Searches biblio information by marc tag",
48 sub biblio_search_marc {
50 my( $self, $client, $search_hash, $string ) = @_;
52 warn "Building biblio marc session\n";
53 my $session = OpenSRF::AppSession->create("open-ils.storage");
55 warn "Sending biblio marc request. String $string\nSearch hash: " . Dumper($search_hash);
56 my $request = $session->request(
57 "open-ils.storage.direct.metabib.full_rec.search_fts.index_vector.atomic",
58 restrict => $search_hash,
60 my $data = $request->gather(1);
65 $session->disconnect();
74 # ---------------------------------------------------------------------------
75 # takes a list of record id's and turns the docs into friendly
76 # mods structures. Creates one MODS structure for each doc id.
77 # ---------------------------------------------------------------------------
78 sub _records_to_mods {
84 my $session = OpenSRF::AppSession->create("open-ils.storage");
85 my $request = $session->request(
86 "open-ils.storage.direct.biblio.record_entry.batch.retrieve", @ids );
88 my $last_content = undef;
90 while( my $response = $request->recv() ) {
93 my $u = OpenILS::Utils::ModsParser->new();
94 $u->start_mods_batch( $last_content->marc );
95 my $mods = $u->finish_mods_batch();
96 $mods->doc_id($last_content->id());
97 $mods->tcn($last_content->tcn_value);
98 warn "Turning doc " . $mods->doc_id() . " into MODS\n";
99 $last_content = undef;
100 push @results, $mods;
103 next unless $response;
105 if($response->isa("OpenSRF::EX")) {
106 throw $response ($response->stringify);
109 $last_content = $response->content;
113 if( $last_content ) {
114 my $u = OpenILS::Utils::ModsParser->new();
115 $u->start_mods_batch( $last_content->marc );
116 my $mods = $u->finish_mods_batch();
117 $mods->doc_id($last_content->id());
118 $mods->tcn($last_content->tcn_value);
119 push @results, $mods;
124 $session->disconnect();
130 __PACKAGE__->register_method(
131 method => "record_id_to_mods",
132 api_name => "open-ils.search.biblio.record.mods.retrieve",
134 note => "Provide ID, we provide the mods"
137 # converts a record into a mods object with copy counts attached
138 sub record_id_to_mods {
140 my( $self, $client, $org_id, $id ) = @_;
142 my $mods_list = _records_to_mods( $id );
143 my $mods_obj = $mods_list->[0];
144 my $cmethod = $self->method_lookup(
145 "open-ils.search.biblio.record.copy_count");
146 my ($count) = $cmethod->run($org_id, $id);
147 $mods_obj->copy_count($count);
154 __PACKAGE__->register_method(
155 method => "record_id_to_mods_slim",
156 api_name => "open-ils.search.biblio.record.mods_slim.retrieve",
158 note => "Provide ID, we provide the mods"
161 # converts a record into a mods object with NO copy counts attached
162 sub record_id_to_mods_slim {
163 my( $self, $client, $id ) = @_;
164 return undef unless defined $id;
166 if(ref($id) and ref($id) == 'ARRAY') {
167 return _records_to_mods( @$id );
169 my $mods_list = _records_to_mods( $id );
170 my $mods_obj = $mods_list->[0];
175 # Returns the number of copies attached to a record based on org location
176 __PACKAGE__->register_method(
177 method => "record_id_to_copy_count",
178 api_name => "open-ils.search.biblio.record.copy_count",
181 __PACKAGE__->register_method(
182 method => "record_id_to_copy_count",
183 api_name => "open-ils.search.biblio.metarecord.copy_count",
186 __PACKAGE__->register_method(
187 method => "record_id_to_copy_count",
188 api_name => "open-ils.search.biblio.metarecord.copy_count.staff",
190 sub record_id_to_copy_count {
191 my( $self, $client, $org_id, $record_id, $format ) = @_;
193 $format = undef if ($format eq 'all');
195 my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
197 if($self->api_name =~ /metarecord/) {
198 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
202 if($self->api_name =~ /staff/ ) {
203 $method =~ s/atomic/staff\.atomic/og;
204 warn "Doing staff search $method\n";
208 my $session = OpenSRF::AppSession->create("open-ils.storage");
209 warn "copy_count retrieve $record_id\n";
210 return undef unless(defined $record_id);
212 my $request = $session->request(
213 $method, org_unit => $org_id => $key => $record_id, format => $format );
216 my $count = $request->gather(1);
217 $session->disconnect();
218 return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
223 # used for cat search classes
224 my $cat_search_hash = {
227 { tag => "100", subfield => "a"} ,
228 { tag => "700", subfield => "a"},
232 { tag => "245", subfield => "a"},
233 { tag => "242", subfield => "a"},
234 { tag => "240", subfield => "a"},
235 { tag => "210", subfield => "a"},
239 { tag => "650", subfield => "_" },
243 { tag => "035", subfield => "_" },
247 { tag => "020", subfield => "a" },
251 { tag => '022', subfield => 'a' },
257 __PACKAGE__->register_method(
258 method => "biblio_search_tcn",
259 api_name => "open-ils.search.biblio.tcn",
261 note => "Retrieve a record by TCN",
264 sub biblio_search_tcn {
266 my( $self, $client, $tcn ) = @_;
268 $tcn =~ s/.*?(\w+)\s*$/$1/o;
269 warn "Searching TCN $tcn\n";
271 my $session = OpenSRF::AppSession->create( "open-ils.storage" );
272 my $request = $session->request(
273 "open-ils.storage.direct.biblio.record_entry.search.tcn_value.atomic", $tcn );
274 my $record_entry = $request->gather(1);
277 for my $record (@$record_entry) {
278 push @ids, $record->id;
281 $session->disconnect();
283 warn "received ID's for tcn search @ids\n";
286 return { count => $size, ids => \@ids };
291 # --------------------------------------------------------------------------------
298 # --------------------------------------------------------------------------------
300 __PACKAGE__->register_method(
301 method => "biblio_barcode_to_copy",
302 api_name => "open-ils.search.asset.copy.find_by_barcode",
305 # turns a barcode into a copy object
306 sub biblio_barcode_to_copy {
307 my( $self, $client, $barcode ) = @_;
309 throw OpenSRF::EX::InvalidArg
310 ("search.biblio.barcode needs a barcode to search")
311 unless defined $barcode;
313 warn "copy search for barcode $barcode\n";
314 my $record = OpenILS::Application::AppUtils->simple_scalar_request(
316 "open-ils.storage.direct.asset.copy.search.barcode.atomic",
319 return undef unless($record);
324 __PACKAGE__->register_method(
325 method => "biblio_id_to_copy",
326 api_name => "open-ils.search.asset.copy.batch.retrieve",
329 # turns a barcode into a copy object
330 sub biblio_id_to_copy {
331 my( $self, $client, $ids ) = @_;
333 throw OpenSRF::EX::InvalidArg
334 ("search.biblio.batch.retrieve needs a id to search")
337 warn "copy search for ids @$ids\n";
338 my $record = OpenILS::Application::AppUtils->simple_scalar_request(
340 "open-ils.storage.direct.asset.copy.batch.retrieve.atomic",
348 __PACKAGE__->register_method(
349 method => "copy_retrieve",
350 api_name => "open-ils.search.asset.copy.retrieve",
354 my( $self, $client, $cid ) = @_;
355 my( $copy, $evt ) = $U->fetch_copy($cid);
361 __PACKAGE__->register_method(
362 method => "fleshed_copy_retrieve_batch",
363 api_name => "open-ils.search.asset.copy.fleshed.batch.retrieve",
366 sub fleshed_copy_retrieve_batch {
367 my( $self, $client, $ids ) = @_;
369 throw OpenSRF::EX::InvalidArg
370 ("search.biblio.batch.retrieve needs a id to search")
373 warn "fleshed copy search for id @$ids\n";
374 my $copy = OpenILS::Application::AppUtils->simple_scalar_request(
376 "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic",
382 __PACKAGE__->register_method(
383 method => "fleshed_copy_retrieve",
384 api_name => "open-ils.search.asset.copy.fleshed.retrieve",
387 sub fleshed_copy_retrieve {
388 my( $self, $client, $id ) = @_;
390 return undef unless defined $id;
391 warn "copy retrieve for id $id\n";
392 return OpenILS::Application::AppUtils->simple_scalar_request(
394 "open-ils.storage.fleshed.asset.copy.retrieve",
400 __PACKAGE__->register_method(
401 method => "biblio_barcode_to_title",
402 api_name => "open-ils.search.biblio.find_by_barcode",
405 sub biblio_barcode_to_title {
406 my( $self, $client, $barcode ) = @_;
409 throw OpenSRF::EX::ERROR
410 ("Not enough args to find_by_barcode");
413 my $title = $apputils->simple_scalar_request(
415 "open-ils.storage.biblio.record_entry.retrieve_by_barcode",
419 return { ids => [ $title->id ], count => 1 };
421 return { count => 0 };
427 __PACKAGE__->register_method(
428 method => "biblio_copy_to_mods",
429 api_name => "open-ils.search.biblio.copy.mods.retrieve",
432 # takes a copy object and returns it fleshed mods object
433 sub biblio_copy_to_mods {
434 my( $self, $client, $copy ) = @_;
436 throw OpenSRF::EX::InvalidArgs
437 ("copy.mods.retrieve needs a copy") unless( $copy );
439 new Fieldmapper::asset::copy($copy);
441 my $volume = OpenILS::Application::AppUtils->simple_scalar_request(
443 "open-ils.storage.direct.asset.call_number.retrieve",
444 $copy->call_number() );
446 my $mods = _records_to_mods($volume->record());
447 $mods = shift @$mods;
448 $volume->copies([$copy]);
449 push @{$mods->call_numbers()}, $volume;
455 sub barcode_to_mods {
460 # --------------------------------------------------------------------------------
465 __PACKAGE__->register_method(
466 method => "biblio_search_class_count",
467 api_name => "open-ils.search.biblio.class.count",
470 __PACKAGE__->register_method(
471 method => "biblio_search_class_count",
472 api_name => "open-ils.search.biblio.class.count.staff",
475 sub biblio_search_class_count {
477 my( $self, $client, $class, $string, $org_id, $org_type, $format ) = @_;
479 warn "org: $org_id : depth: $org_type\n";
481 $org_id = "1" unless defined($org_id); # xxx
482 $org_type = 0 unless defined($org_type);
484 warn "Searching biblio.class.id\n" .
486 "org_id: $org_id\n" .
487 "depth: $org_type\n" .
490 if( !defined($org_id) or !$class or !$string ) {
491 warn "not enbough args to metarecord search\n";
492 throw OpenSRF::EX::InvalidArg
493 ("Not enough args to open-ils.search.cat.biblio.class")
498 if( ($class ne "title") and ($class ne "author") and
499 ($class ne "subject") and ($class ne "keyword")
500 and ($class ne "series" )) {
501 warn "Invalid search class: $class\n";
502 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
505 # grab the mr id's from storage
507 my $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord_count";
508 if($self->api_name =~ /staff/) {
509 $method = "$method.staff";
510 $method =~ s/\.cachable//o;
512 warn "Performing count method $method\n";
513 warn "API name " . $self->api_name() . "\n";
515 my $session = OpenSRF::AppSession->create('open-ils.storage');
517 my $request = $session->request( $method,
520 cache_page_size => 1,
524 my $count = $request->gather(1);
525 warn "Received count $count\n";
533 __PACKAGE__->register_method(
534 method => "record_search_class",
535 api_name => "open-ils.search.biblio.record.class.search",
537 Performs a class search for biblio records (not metarecords)
538 @param class The search class to use
539 @param args A hash of named parameters including:
540 term : The search string,
541 org_unit : The org id to focus the search at
542 depth : The org depth
543 limit : The search limit
544 offset : The search offset
545 format : The MARC format
546 sort : What field to sort the results on [ author | title | pubdate ]
547 sort_dir : What direction do we sort? [ asc | desc ]
548 The only required argument is the term.
552 __PACKAGE__->register_method(
553 method => "record_search_class",
554 api_name => "open-ils.search.biblio.record.class.search.staff",
555 signature => q/@see open-ils.search.biblio.record.class.search/);
556 __PACKAGE__->register_method(
557 method => "record_search_class",
558 api_name => "open-ils.search.biblio.metabib.class.search",
559 signature => q/@see open-ils.search.biblio.record.class.search/);
560 __PACKAGE__->register_method(
561 method => "record_search_class",
562 api_name => "open-ils.search.biblio.metabib.class.search.staff",
563 signature => q/@see open-ils.search.biblio.record.class.search/);
565 sub record_search_class {
566 my( $self, $client, $class, $args ) = @_;
568 my $type = ($self->api_name =~ /metabib/) ? 1 : 0;
569 my $method = "open-ils.storage.metabib.$class.post_filter.search_fts.metarecord.atomic";
570 $method = "open-ils.storage.biblio.$class.search_fts.record.atomic" unless $type;
572 if(!$$args{'sort'}) {
573 delete $$args{'sort'};
574 delete $$args{'sort_dir'};
576 $$args{limit} = 200 if (!$$args{limit} || $$args{limit} > 1000);
577 $$args{offset} ||= 0;
579 $logger->info("Class search with args: ".Dumper($args));
581 $method =~ s/\.atomic/.staff.atomic/o if $self->api_name =~ /staff/o;
582 my $result = $U->storagereq( $method, %$args );
584 if($result and $result->[0]) {
586 for my $r (@$result) { push(@$recs, $r) if ($r and $r->[0]); }
587 return { ids => $recs,
588 count => ($type) ? $result->[0]->[3] : $result->[0]->[2] };
590 return { count => 0 };
596 __PACKAGE__->register_method(
597 method => "biblio_search_class",
598 api_name => "open-ils.search.biblio.class",
601 __PACKAGE__->register_method(
602 method => "biblio_search_class",
603 api_name => "open-ils.search.biblio.class.full",
606 __PACKAGE__->register_method(
607 method => "biblio_search_class",
608 api_name => "open-ils.search.biblio.class.full.staff",
611 __PACKAGE__->register_method(
612 method => "biblio_search_class",
613 api_name => "open-ils.search.biblio.class.staff",
616 sub biblio_search_class {
618 my( $self, $client, $class, $string,
619 $org_id, $org_type, $limit, $offset, $format ) = @_;
621 warn "org: $org_id : depth: $org_type : limit: $limit : offset: $offset\n";
624 $offset = 0 unless (defined($offset) and $offset > 0);
625 $limit = 100 unless (defined($limit) and $limit > 0);
626 $org_id = "1" unless (defined($org_id)); # xxx
627 $org_type = 0 unless (defined($org_type));
629 warn "Searching biblio.class.id\n" .
631 "\noffset: $offset\n" .
633 "org_id: $org_id\n" .
634 "depth: $org_type\n" .
637 warn "Search filtering string " . time() . "\n";
638 $string = OpenILS::Application::Search->filter_search($string);
639 if(!$string) { return undef; }
641 if( !defined($org_id) or !$class or !$string ) {
642 warn "not enbough args to metarecord search\n";
643 throw OpenSRF::EX::InvalidArg
644 ("Not enough args to open-ils.search.biblio.class")
649 if( ($class ne "title") and ($class ne "author") and
650 ($class ne "subject") and ($class ne "keyword")
651 and ($class ne "series") ) {
652 warn "Invalid search class: $class\n";
653 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
656 #my $method = "open-ils.storage.cachable.metabib.$class.post_filter.search_fts.metarecord.atomic";
657 my $method = "open-ils.storage.metabib.$class.post_filter.search_fts.metarecord.atomic";
659 if($self->api_name =~ /staff/) {
660 $method =~ s/atomic/staff\.atomic/og;
661 $method =~ s/\.cachable//o;
664 if($self->api_name =~ /full/) {
665 $method =~ s/\.cachable//o; #XXX testing..
668 warn "Performing search method $method\n";
669 warn "MR search method is $method\n";
671 my $session = OpenSRF::AppSession->create('open-ils.storage');
673 warn "Search making request " . time() . "\n";
674 my $request = $session->request(
682 cache_page_size => 200,
685 my $records = $request->gather(1);
686 if(!$records) {return { ids => [] }};
690 warn "Received " . scalar(@$records) . " id's from class search\n";
692 # for my $i (@$records) { if(defined($i)) { push @all_ids, $i; } }
693 # my @ids = @all_ids;
694 # @ids = grep { defined($_->[0]) } @ids;
697 $session->disconnect();
700 if( $records->[0] && defined($records->[0]->[3])) { $count = $records->[0]->[3];}
702 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
704 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
705 return { ids => $recs, count => $count };
713 __PACKAGE__->register_method(
714 method => "biblio_mrid_to_modsbatch_batch",
715 api_name => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
717 sub biblio_mrid_to_modsbatch_batch {
718 my( $self, $client, $mrids) = @_;
719 warn "Performing mrid_to_modsbatch_batch...";
721 my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
722 warn "Grabbing mods for " . Dumper($mrids) . "\n";
724 for my $id (@$mrids) {
725 next unless defined $id;
726 my ($m) = $method->run($id);
733 __PACKAGE__->register_method(
734 method => "biblio_mrid_to_modsbatch",
735 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
736 notes => <<" NOTES");
737 Returns the mvr associated with a given metarecod. If none exists,
741 __PACKAGE__->register_method(
742 method => "biblio_mrid_to_modsbatch",
743 api_name => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
744 notes => <<" NOTES");
745 Returns the mvr associated with a given metarecod. If none exists,
749 sub biblio_mrid_to_modsbatch {
750 my( $self, $client, $mrid ) = @_;
752 warn "Grabbing mvr for $mrid\n";
754 my $mr = _grab_metarecord($mrid);
755 return undef unless $mr;
757 if( my $m = $self->biblio_mrid_check_mvr($client, $mr)) {
761 return $self->biblio_mrid_make_modsbatch( $client, $mr );
764 # converts a metarecord to an mvr
767 my $perl = JSON->JSON2perl($mr->mods());
768 return Fieldmapper::metabib::virtual_record->new($perl);
771 # checks to see if a metarecord has mods, if so returns true;
773 __PACKAGE__->register_method(
774 method => "biblio_mrid_check_mvr",
775 api_name => "open-ils.search.biblio.metarecord.mods_slim.check",
776 notes => <<" NOTES");
777 Takes a metarecord ID or a metarecord object and returns true
778 if the metarecord already has an mvr associated with it.
781 sub biblio_mrid_check_mvr {
782 my( $self, $client, $mrid ) = @_;
785 if(ref($mrid)) { $mr = $mrid; }
786 else { $mr = _grab_metarecord($mrid); }
788 warn "Checking mvr for mr " . $mr->id . "\n";
790 return _mr_to_mvr($mr) if $mr->mods();
794 sub _grab_metarecord {
797 warn "Grabbing MR $mrid\n";
799 my $mr = OpenILS::Application::AppUtils->simple_scalar_request(
801 "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
804 throw OpenSRF::EX::ERROR
805 ("No metarecord exists with the given id: $mrid");
811 __PACKAGE__->register_method(
812 method => "biblio_mrid_make_modsbatch",
813 api_name => "open-ils.search.biblio.metarecord.mods_slim.create",
814 notes => <<" NOTES");
815 Takes either a metarecord ID or a metarecord object.
816 Forces the creations of an mvr for the given metarecord.
817 The created mvr is returned.
820 sub biblio_mrid_make_modsbatch {
822 my( $self, $client, $mrid ) = @_;
825 if(ref($mrid)) { $mr = $mrid; }
826 else { $mr = _grab_metarecord($mrid); }
829 warn "Forcing mvr creation for mr " . $mr->id . "\n";
830 my $master_id = $mr->master_record;
832 my $session = OpenSRF::AppSession->create("open-ils.storage");
834 # grab the records attached to this metarecod
835 warn "Creating mods batch for metarecord $mrid\n";
836 my $meth = "open-ils.search.biblio.metarecord_to_records.staff";
837 $meth = $self->method_lookup($meth);
838 my ($id_hash) = $meth->run($mrid);
839 my @ids = @{$id_hash->{ids}};
840 if(@ids < 1) { return undef; }
842 warn "Master ID is $master_id\n";
843 # grab the master record to start the mods batch
845 $meth = "open-ils.storage.direct.biblio.record_entry.retrieve";
847 my $record = $session->request(
848 "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
849 $record = $record->gather(1);
851 #my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage",
854 warn "No record returned with id $master_id";
855 throw OpenSRF::EX::ERROR
858 my $u = OpenILS::Utils::ModsParser->new();
859 $u->start_mods_batch( $record->marc );
860 my $main_doc_id = $record->id();
862 @ids = grep { $_ ne $master_id } @ids;
864 # now we have to collect all of the marc objects and push them into a mods batch
865 my $request = $session->request(
866 "open-ils.storage.direct.biblio.record_entry.batch.retrieve", @ids );
868 while( my $response = $request->recv() ) {
870 next unless $response;
871 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
872 throw $response ($response->stringify);
875 my $content = $response->content;
878 $u->push_mods_batch( $content->marc );
882 my $mods = $u->finish_mods_batch();
883 $mods->doc_id($mrid);
886 $client->respond_complete($mods);
888 my $mods_string = JSON->perl2JSON($mods->decast);
890 $mr->mods($mods_string);
892 my $req = $session->request(
893 "open-ils.storage.direct.metabib.metarecord.update", $mr );
898 $session->disconnect();
905 # converts a mr id into a list of record ids
907 __PACKAGE__->register_method(
908 method => "biblio_mrid_to_record_ids",
909 api_name => "open-ils.search.biblio.metarecord_to_records",
912 __PACKAGE__->register_method(
913 method => "biblio_mrid_to_record_ids",
914 api_name => "open-ils.search.biblio.metarecord_to_records.staff",
917 sub biblio_mrid_to_record_ids {
918 my( $self, $client, $mrid, $format ) = @_;
920 throw OpenSRF::EX::InvalidArg
921 ("search.biblio.metarecord_to_record_ids requires mr id")
922 unless defined( $mrid );
924 warn "Searching for record for MR $mrid and format $format\n";
926 my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
927 if($self and $self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
928 warn "Performing record retrieval with method $method\n";
931 my $mrmaps = OpenILS::Application::AppUtils->simple_scalar_request(
932 "open-ils.storage", $method, $mrid, $format );
938 return { count => $size, ids => $mrmaps };
943 __PACKAGE__->register_method(
944 method => "biblio_record_to_marc_html",
945 api_name => "open-ils.search.biblio.record.html" );
947 my $parser = XML::LibXML->new();
948 my $xslt = XML::LibXSLT->new();
951 my $settings_client = OpenSRF::Utils::SettingsClient->new();
952 sub biblio_record_to_marc_html {
953 my( $self, $client, $recordid ) = @_;
956 my $dir = $settings_client->config_value( "dirs", "xsl" );
957 my $xsl = $settings_client->config_value(
958 "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
960 $xsl = $parser->parse_file("$dir/$xsl");
961 $marc_sheet = $xslt->parse_stylesheet( $xsl );
965 my $record = $apputils->simple_scalar_request(
967 "open-ils.storage.direct.biblio.record_entry.retrieve",
970 my $xmldoc = $parser->parse_string($record->marc);
971 my $html = $marc_sheet->transform($xmldoc);
972 $html = $html->toString();
979 __PACKAGE__->register_method(
980 method => "retrieve_all_copy_locations",
981 api_name => "open-ils.search.config.copy_location.retrieve.all" );
983 my $shelving_locations;
984 sub retrieve_all_copy_locations {
985 my( $self, $client ) = @_;
986 if(!$shelving_locations) {
987 $shelving_locations = $apputils->simple_scalar_request(
989 "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
991 return $shelving_locations;
996 __PACKAGE__->register_method(
997 method => "retrieve_all_copy_statuses",
998 api_name => "open-ils.search.config.copy_status.retrieve.all" );
1001 sub retrieve_all_copy_statuses {
1002 my( $self, $client ) = @_;
1003 if(!$copy_statuses) {
1004 $copy_statuses = $apputils->simple_scalar_request(
1006 "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
1008 return $copy_statuses;
1012 __PACKAGE__->register_method(
1013 method => "copy_counts_per_org",
1014 api_name => "open-ils.search.biblio.copy_counts.retrieve");
1016 __PACKAGE__->register_method(
1017 method => "copy_counts_per_org",
1018 api_name => "open-ils.search.biblio.copy_counts.retrieve.staff");
1020 sub copy_counts_per_org {
1021 my( $self, $client, $record_id ) = @_;
1023 warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1025 my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1026 if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1028 my $counts = $apputils->simple_scalar_request(
1029 "open-ils.storage", $method, $record_id );
1031 $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1036 __PACKAGE__->register_method(
1037 method => "copy_count_summary",
1038 api_name => "open-ils.search.biblio.copy_counts.summary.retrieve",
1039 notes => <<" NOTES");
1040 returns an array of these:
1041 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1042 where statusx is a copy status name. the statuses are sorted
1046 sub copy_count_summary {
1047 my( $self, $client, $rid ) = @_;
1048 my $method = "open-ils.storage.biblio.record_entry.status_copy_count.atomic";
1049 return $apputils->simple_scalar_request( "open-ils.storage", $method, $rid );
1053 __PACKAGE__->register_method(
1054 method => "multiclass_search",
1055 api_name => "open-ils.search.biblio.multiclass",
1056 notes => <<" NOTES");
1057 Performs a multiclass search
1058 PARAMS( searchBlob, org_unit, format, limit )
1059 where searchBlob is defined like this:
1061 "title" : { "term" : "water" },
1062 "author" : { "term" : "smith" },
1067 __PACKAGE__->register_method(
1068 method => "multiclass_search",
1069 api_name => "open-ils.search.biblio.multiclass.staff",
1070 notes => "see open-ils.search.biblio.multiclass" );
1072 sub multiclass_search {
1073 my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1075 $logger->debug("Performing multiclass search with org => $orgid, " .
1076 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1078 my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1079 if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1082 my $records = $apputils->simplereq(
1083 'open-ils.storage', $meth,
1084 org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1089 if( ref($records) and $records->[0] and
1090 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1092 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1094 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1095 return { ids => $recs, count => $count };
1100 __PACKAGE__->register_method(
1101 method => "marc_search",
1102 api_name => "open-ils.search.biblio.marc",
1103 notes => <<" NOTES");
1104 Performs a multiclass search
1105 PARAMS( searchBlob, org_unit, format )
1106 where searchBlob is defined like this:
1109 "term":"shakespeare",
1110 "restrict":[{"tag":"245","subfield":"a"}]
1114 "restrict":[{"tag":"100","subfield":"a"}]
1122 my( $self, $client, $searchBlob, $orgid, $format ) = @_;
1124 $logger->debug("Performing MARC search with org => $orgid, " .
1125 "format => $format and search blob " . Dumper($searchBlob) );
1127 my $records = $apputils->simplereq(
1129 'open-ils.storage.metabib.full_rec.multi_search.atomic',
1130 searches => $searchBlob, org_unit => $orgid, format => $format );
1135 if( ref($records) and $records->[0] and
1136 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1138 for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1140 # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1141 return { ids => $recs, count => $count };
1147 __PACKAGE__->register_method(
1148 method => "biblio_search_isbn",
1149 api_name => "open-ils.search.biblio.isbn",
1152 sub biblio_search_isbn {
1153 my( $self, $client, $isbn ) = @_;
1155 $logger->debug("Searching ISBN $isbn");
1157 my $method = $self->method_lookup("open-ils.search.biblio.marc");
1159 my ($records) = $method->run(
1161 restrict => $cat_search_hash->{isbn} } ], 1);
1167 __PACKAGE__->register_method(
1168 method => "biblio_search_issn",
1169 api_name => "open-ils.search.biblio.issn",
1172 sub biblio_search_issn {
1173 my( $self, $client, $issn ) = @_;
1175 $logger->debug("Searching ISSN $issn");
1177 my $method = $self->method_lookup("open-ils.search.biblio.marc");
1179 my ($records) = $method->run(
1181 restrict => $cat_search_hash->{issn} } ], 1);
1189 __PACKAGE__->register_method(
1190 method => "fetch_mods_by_copy",
1191 api_name => "open-ils.search.biblio.mods_from_copy",
1194 sub fetch_mods_by_copy {
1195 my( $self, $client, $copyid ) = @_;
1196 my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1197 return $evt if $evt;
1198 return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1199 return $apputils->record_to_mvr($record);
1206 # -------------------------------------------------------------------------------------
1208 __PACKAGE__->register_method(
1209 method => "cn_browse",
1210 api_name => "open-ils.search.callnumber.browse.target",
1211 notes => "Starts a callnumber browse"
1214 __PACKAGE__->register_method(
1215 method => "cn_browse",
1216 api_name => "open-ils.search.callnumber.browse.page_up",
1217 notes => "Returns the previous page of callnumbers",
1220 __PACKAGE__->register_method(
1221 method => "cn_browse",
1222 api_name => "open-ils.search.callnumber.browse.page_down",
1223 notes => "Returns the next page of callnumbers",
1227 # RETURNS array of arrays like so: label, owning_lib, record, id
1229 my( $self, $client, @params ) = @_;
1232 $method = 'open-ils.storage.asset.call_number.browse.target.atomic'
1233 if( $self->api_name =~ /target/ );
1234 $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1235 if( $self->api_name =~ /page_up/ );
1236 $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1237 if( $self->api_name =~ /page_down/ );
1239 return $apputils->simplereq( 'open-ils.storage', $method, @params );
1241 # -------------------------------------------------------------------------------------
1243 __PACKAGE__->register_method(
1244 method => "fetch_cn",
1245 api_name => "open-ils.search.callnumber.retrieve",
1246 notes => "retrieves a callnumber based on ID",
1250 my( $self, $client, $id ) = @_;
1251 my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1252 return $evt if $evt;