]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
fixing OPAC copy count display bug
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search / Biblio.pm
1 package OpenILS::Application::Search::Biblio;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5 use OpenILS::EX;
6
7 use JSON;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Utils::ModsParser;
10 use OpenSRF::Utils::SettingsClient;
11
12 use OpenILS::Application::AppUtils;
13 my $U = "OpenILS::Application::AppUtils";
14
15 use JSON;
16
17 use Time::HiRes qw(time);
18 use OpenSRF::EX qw(:try);
19 use Digest::MD5 qw(md5_hex);
20
21 use XML::LibXML;
22 use XML::LibXSLT;
23 use Data::Dumper;
24 $Data::Dumper::Indent = 0;
25 use OpenSRF::Utils::Logger qw/:logger/;
26
27 my $apputils = "OpenILS::Application::AppUtils";
28
29 # Houses biblio search utilites 
30
31
32 __PACKAGE__->register_method(
33         method  => "test",
34         api_name        => "open-ils.search.test");
35
36 sub test { return "test"; }
37
38
39
40 =head comment
41 __PACKAGE__->register_method(
42         method  => "biblio_search_marc",
43         api_name        => "open-ils.search.biblio.marc",
44         argc            => 1, 
45         note            => "Searches biblio information by marc tag",
46 );
47
48 sub biblio_search_marc {
49
50         my( $self, $client, $search_hash, $string ) = @_;
51
52         warn "Building biblio marc session\n";
53         my $session = OpenSRF::AppSession->create("open-ils.storage");
54
55         use Data::Dumper;
56         warn "Sending biblio marc request. String $string\nSearch hash: " . Dumper($search_hash);
57         my $request = $session->request( 
58                         "open-ils.storage.direct.metabib.full_rec.search_fts.index_vector.atomic", 
59                         restrict => $search_hash, 
60                         term            => $string );
61         my $data = $request->gather(1);
62
63         warn Dumper $data;
64
65         $session->finish();
66         $session->disconnect();
67
68         return $data;
69
70 }
71 =cut
72
73
74
75 # ---------------------------------------------------------------------------
76 # takes a list of record id's and turns the docs into friendly 
77 # mods structures. Creates one MODS structure for each doc id.
78 # ---------------------------------------------------------------------------
79 sub _records_to_mods {
80         my @ids = @_;
81         
82         my @results;
83         my @marcxml_objs;
84
85         my $session = OpenSRF::AppSession->create("open-ils.storage");
86         my $request = $session->request(
87                         "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
88
89         my $last_content = undef;
90
91         while( my $response = $request->recv() ) {
92
93                 if( $last_content ) {
94                         my $u = OpenILS::Utils::ModsParser->new();
95                         $u->start_mods_batch( $last_content->marc );
96                         my $mods = $u->finish_mods_batch();
97                         $mods->doc_id($last_content->id());
98                         $mods->tcn($last_content->tcn_value);
99                         warn "Turning doc " . $mods->doc_id() . " into MODS\n";
100                         $last_content = undef;
101                         push @results, $mods;
102                 }
103
104                 next unless $response;
105
106                 if($response->isa("OpenSRF::EX")) {
107                         throw $response ($response->stringify);
108                 }
109
110                 $last_content = $response->content;
111
112         }
113
114         if( $last_content ) {
115                 my $u = OpenILS::Utils::ModsParser->new();
116                 $u->start_mods_batch( $last_content->marc );
117                 my $mods = $u->finish_mods_batch();
118                 $mods->doc_id($last_content->id());
119                 $mods->tcn($last_content->tcn_value);
120                 push @results, $mods;
121         }
122
123         $request->finish();
124         $session->finish();
125         $session->disconnect();
126
127         return \@results;
128
129 }
130
131 __PACKAGE__->register_method(
132         method  => "record_id_to_mods",
133         api_name        => "open-ils.search.biblio.record.mods.retrieve",
134         argc            => 1, 
135         note            => "Provide ID, we provide the mods"
136 );
137
138 # converts a record into a mods object with copy counts attached
139 sub record_id_to_mods {
140
141         my( $self, $client, $org_id, $id ) = @_;
142
143         my $mods_list = _records_to_mods( $id );
144         my $mods_obj = $mods_list->[0];
145         my $cmethod = $self->method_lookup(
146                         "open-ils.search.biblio.record.copy_count");
147         my ($count) = $cmethod->run($org_id, $id);
148         $mods_obj->copy_count($count);
149
150         return $mods_obj;
151 }
152
153
154
155 __PACKAGE__->register_method(
156         method  => "record_id_to_mods_slim",
157         api_name        => "open-ils.search.biblio.record.mods_slim.retrieve",
158         argc            => 1, 
159         note            => "Provide ID, we provide the mods"
160 );
161
162 # converts a record into a mods object with NO copy counts attached
163 sub record_id_to_mods_slim {
164         my( $self, $client, $id ) = @_;
165         return undef unless defined $id;
166
167         if(ref($id) and ref($id) == 'ARRAY') {
168                 return _records_to_mods( @$id );
169         }
170         my $mods_list = _records_to_mods( $id );
171         my $mods_obj = $mods_list->[0];
172         return $mods_obj;
173 }
174
175
176 # Returns the number of copies attached to a record based on org location
177 __PACKAGE__->register_method(
178         method  => "record_id_to_copy_count",
179         api_name        => "open-ils.search.biblio.record.copy_count",
180 );
181
182 __PACKAGE__->register_method(
183         method  => "record_id_to_copy_count",
184         api_name        => "open-ils.search.biblio.metarecord.copy_count",
185 );
186
187 __PACKAGE__->register_method(
188         method  => "record_id_to_copy_count",
189         api_name        => "open-ils.search.biblio.metarecord.copy_count.staff",
190 );
191 sub record_id_to_copy_count {
192         my( $self, $client, $org_id, $record_id, $format ) = @_;
193
194         $format = undef if ($format eq 'all');
195
196         my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
197         my $key = "record";
198         if($self->api_name =~ /metarecord/) {
199                 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
200                 $key = "metarecord";
201         }
202
203         if($self->api_name =~ /staff/ ) {
204                 $method =~ s/atomic/staff\.atomic/og;
205                 warn "Doing staff search $method\n";
206         }
207
208
209         my $session = OpenSRF::AppSession->create("open-ils.storage");
210         warn "copy_count retrieve $record_id\n";
211         return undef unless(defined $record_id);
212
213         my $request = $session->request(
214                 $method, org_unit => $org_id => $key => $record_id, format => $format );
215
216
217         my $count = $request->gather(1);
218         $session->disconnect();
219         return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
220
221 }
222
223
224 # used for cat search classes
225 my $cat_search_hash =  {
226
227         author => [ 
228                 { tag => "100", subfield => "a"} ,
229                 { tag => "700", subfield => "a"}, 
230         ],
231
232         title => [ 
233                 { tag => "245", subfield => "a"},
234                 { tag => "242", subfield => "a"}, 
235                 { tag => "240", subfield => "a"},
236                 { tag => "210", subfield => "a"},
237         ],
238
239         subject => [ 
240                 { tag => "650", subfield => "_" }, 
241         ],
242
243         tcn     => [
244                 { tag => "035", subfield => "_" },
245         ],
246
247         isbn    => [
248                 { tag => "020", subfield => "a" },
249         ],
250
251         issn    => [
252                 { tag => '022', subfield => 'a' },
253         ],
254
255 };
256
257
258 __PACKAGE__->register_method(
259         method  => "biblio_search_tcn",
260         api_name        => "open-ils.search.biblio.tcn",
261         argc            => 3, 
262         note            => "Retrieve a record by TCN",
263 );
264
265 sub biblio_search_tcn {
266
267         my( $self, $client, $tcn ) = @_;
268
269         $tcn =~ s/.*?(\w+)\s*$/$1/o;
270         warn "Searching TCN $tcn\n";
271
272         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
273         my $request = $session->request( 
274                         "open-ils.storage.direct.biblio.record_entry.search.tcn_value.atomic", $tcn );
275         my $record_entry = $request->gather(1);
276
277         my @ids;
278         for my $record (@$record_entry) {
279                 push @ids, $record->id;
280         }
281
282         $session->disconnect();
283
284         warn "received ID's for tcn search @ids\n";
285         my $size = @ids;
286
287         return { count => $size, ids => \@ids };
288
289 }
290
291
292 # --------------------------------------------------------------------------------
293 # ISBN
294
295
296
297
298
299 # --------------------------------------------------------------------------------
300
301 __PACKAGE__->register_method(
302         method  => "biblio_barcode_to_copy",
303         api_name        => "open-ils.search.asset.copy.find_by_barcode",
304 );
305
306 # turns a barcode into a copy object
307 sub biblio_barcode_to_copy { 
308         my( $self, $client, $barcode ) = @_;
309
310         throw OpenSRF::EX::InvalidArg 
311                 ("search.biblio.barcode needs a barcode to search")
312                         unless defined $barcode;
313
314         warn "copy search for barcode $barcode\n";
315         my $record = OpenILS::Application::AppUtils->simple_scalar_request(
316                         "open-ils.storage", 
317                         "open-ils.storage.direct.asset.copy.search.barcode.atomic",
318                         $barcode );
319
320         return undef unless($record);
321         return $record->[0];
322
323 }
324
325 __PACKAGE__->register_method(
326         method  => "biblio_id_to_copy",
327         api_name        => "open-ils.search.asset.copy.batch.retrieve",
328 );
329
330 # turns a barcode into a copy object
331 sub biblio_id_to_copy { 
332         my( $self, $client, $ids ) = @_;
333
334         throw OpenSRF::EX::InvalidArg 
335                 ("search.biblio.batch.retrieve needs a id to search")
336                         unless defined $ids;
337
338         warn "copy search for ids @$ids\n";
339         my $record = OpenILS::Application::AppUtils->simple_scalar_request(
340                         "open-ils.storage", 
341                         "open-ils.storage.direct.asset.copy.batch.retrieve.atomic",
342                         @$ids );
343
344         return $record;
345
346 }
347
348
349 __PACKAGE__->register_method(
350         method  => "copy_retrieve", 
351         api_name        => "open-ils.search.asset.copy.retrieve",
352 );
353
354 sub copy_retrieve {
355         my( $self, $client, $cid ) = @_;
356         my( $copy, $evt ) = $U->fetch_copy($cid);
357         return $evt if $evt;
358         return $copy;
359 }
360
361
362 __PACKAGE__->register_method(
363         method  => "fleshed_copy_retrieve_batch",
364         api_name        => "open-ils.search.asset.copy.fleshed.batch.retrieve",
365 );
366
367 sub fleshed_copy_retrieve_batch { 
368         my( $self, $client, $ids ) = @_;
369
370         throw OpenSRF::EX::InvalidArg 
371                 ("search.biblio.batch.retrieve needs a id to search")
372                         unless defined $ids;
373
374         warn "fleshed copy search for id @$ids\n";
375         my $copy = OpenILS::Application::AppUtils->simple_scalar_request(
376                         "open-ils.storage", 
377                         "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic",
378                         @$ids );
379
380         return $copy;
381 }
382
383 __PACKAGE__->register_method(
384         method  => "fleshed_copy_retrieve",
385         api_name        => "open-ils.search.asset.copy.fleshed.retrieve",
386 );
387
388 sub fleshed_copy_retrieve { 
389         my( $self, $client, $id ) = @_;
390
391         return undef unless defined $id;
392         warn "copy retrieve for id $id\n";
393         return OpenILS::Application::AppUtils->simple_scalar_request(
394                         "open-ils.storage", 
395                         "open-ils.storage.fleshed.asset.copy.retrieve",
396                         $id );
397 }
398
399
400
401 __PACKAGE__->register_method(
402         method  => "biblio_barcode_to_title",
403         api_name        => "open-ils.search.biblio.find_by_barcode",
404 );
405
406 sub biblio_barcode_to_title {
407         my( $self, $client, $barcode ) = @_;
408
409         if(!$barcode) {
410                 throw OpenSRF::EX::ERROR 
411                         ("Not enough args to find_by_barcode");
412         }
413
414         my $title = $apputils->simple_scalar_request(
415                 "open-ils.storage",
416                 "open-ils.storage.biblio.record_entry.retrieve_by_barcode",
417                 $barcode );
418
419         if($title) {
420                 return { ids => [ $title->id ], count => 1 };
421         } else {
422                 return { count => 0 };
423         }
424
425 }
426
427
428 __PACKAGE__->register_method(
429         method  => "biblio_copy_to_mods",
430         api_name        => "open-ils.search.biblio.copy.mods.retrieve",
431 );
432
433 # takes a copy object and returns it fleshed mods object
434 sub biblio_copy_to_mods {
435         my( $self, $client, $copy ) = @_;
436
437         throw OpenSRF::EX::InvalidArgs 
438                 ("copy.mods.retrieve needs a copy") unless( $copy );
439
440         new Fieldmapper::asset::copy($copy);
441
442         my $volume = OpenILS::Application::AppUtils->simple_scalar_request(
443                 "open-ils.storage",
444                 "open-ils.storage.direct.asset.call_number.retrieve",
445                 $copy->call_number() );
446
447         my $mods = _records_to_mods($volume->record());
448         $mods = shift @$mods;
449         $volume->copies([$copy]);
450         push @{$mods->call_numbers()}, $volume;
451
452         return $mods;
453 }
454
455
456 sub barcode_to_mods {
457
458 }
459
460
461 # --------------------------------------------------------------------------------
462
463
464
465 =head comment
466 __PACKAGE__->register_method(
467         method  => "cat_biblio_search_class",
468         api_name        => "open-ils.search.cat.biblio.class",
469 );
470
471
472 sub cat_biblio_search_class {
473
474         my( $self, $client, $org_id, $class, $sort, $string ) = @_;
475
476         throw OpenSRF::EX::InvalidArg 
477                 ("Not enough args to open-ils.search.cat.biblio.class")
478                         unless( defined($org_id) and $class and $sort and $string );
479
480
481         my $search_hash;
482
483         my $method = $self->method_lookup("open-ils.search.biblio.marc");
484         if(!$method) {
485                 throw OpenSRF::EX::PANIC 
486                         ("Can't lookup method 'open-ils.search.biblio.marc'");
487         }
488
489         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
490
491         my @ids;
492         for my $i (@$records) { push @ids, $i->[0]; }
493
494         my $mods_list = _records_to_mods( @ids );
495         return undef unless (ref($mods_list) eq "ARRAY");
496
497         # ---------------------------------------------------------------
498         # append copy count information to the mods objects
499         my $session = OpenSRF::AppSession->create("open-ils.storage");
500
501         my $request = $session->request(
502                 "open-ils.storage.direct.biblio.record_copy_count.batch",  $org_id, @ids );
503
504         for my $id (@ids) {
505
506                 warn "receiving copy counts for doc $id\n";
507
508                 my $response = $request->recv();
509                 next unless $response;
510
511                 if( $response and UNIVERSAL::isa($response, "Error")) {
512                         throw $response ($response->stringify);
513                 }
514
515                 my $count = $response->content;
516                 my $mods_obj = undef;
517                 for my $m (@$mods_list) {
518                         $mods_obj = $m if ($m->doc_id() == $id)
519                 }
520                 if($mods_obj) {
521                         $mods_obj->copy_count($count);
522                 }
523
524                 $client->respond( $mods_obj );
525
526         }       
527         $request->finish();
528
529         $session->finish();
530         $session->disconnect();
531         $session->kill_me();
532         # ---------------------------------------------------------------
533
534         return undef;
535 }
536
537 =cut
538
539
540
541
542 __PACKAGE__->register_method(
543         method  => "biblio_search_class_count",
544         api_name        => "open-ils.search.biblio.class.count",
545 );
546
547 __PACKAGE__->register_method(
548         method  => "biblio_search_class_count",
549         api_name        => "open-ils.search.biblio.class.count.staff",
550 );
551
552 sub biblio_search_class_count {
553
554         my( $self, $client, $class, $string, $org_id, $org_type, $format ) = @_;
555
556         warn "org: $org_id : depth: $org_type\n";
557
558         $org_id         = "1" unless defined($org_id); # xxx
559         $org_type       = 0     unless defined($org_type);
560
561         warn "Searching biblio.class.id\n" . 
562                 "string: $string "              . 
563                 "org_id: $org_id\n"             .
564                 "depth: $org_type\n"            .
565                 "format: $format\n";
566
567         if( !defined($org_id) or !$class or !$string ) {
568                 warn "not enbough args to metarecord search\n";
569                 throw OpenSRF::EX::InvalidArg 
570                         ("Not enough args to open-ils.search.cat.biblio.class")
571         }
572
573         $class =~ s/\s+//g;
574
575         if( ($class ne "title") and ($class ne "author") and 
576                 ($class ne "subject") and ($class ne "keyword") 
577                 and ($class ne "series"  )) {
578                 warn "Invalid search class: $class\n";
579                 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
580         }
581
582         # grab the mr id's from storage
583
584         my $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord_count";
585         if($self->api_name =~ /staff/) { 
586                 $method = "$method.staff"; 
587                 $method =~ s/\.cachable//o;
588         }
589         warn "Performing count method $method\n";
590         warn "API name " . $self->api_name() . "\n";
591
592         my $session = OpenSRF::AppSession->create('open-ils.storage');
593
594         my $request = $session->request( $method, 
595                         term                                    => $string, 
596                         org_unit                                => $org_id, 
597                         cache_page_size => 1,
598                         depth                                   => $org_type,
599                         format                          => $format );
600
601         my $count = $request->gather(1);
602         warn "Received count $count\n";
603
604         return $count;
605 }
606
607
608 __PACKAGE__->register_method(
609         method  => "biblio_search_class",
610         api_name        => "open-ils.search.biblio.class",
611 );
612
613 __PACKAGE__->register_method(
614         method  => "biblio_search_class",
615         api_name        => "open-ils.search.biblio.class.full",
616 );
617
618 __PACKAGE__->register_method(
619         method  => "biblio_search_class",
620         api_name        => "open-ils.search.biblio.class.full.staff",
621 );
622
623 __PACKAGE__->register_method(
624         method  => "biblio_search_class",
625         api_name        => "open-ils.search.biblio.class.staff",
626 );
627
628 sub biblio_search_class {
629
630         my( $self, $client, $class, $string, 
631                         $org_id, $org_type, $limit, $offset, $format ) = @_;
632
633         warn "org: $org_id : depth: $org_type : limit: $limit :  offset: $offset\n";
634
635
636         $offset         = 0     unless (defined($offset) and $offset > 0);
637         $limit          = 100 unless (defined($limit) and $limit > 0);
638         $org_id         = "1" unless (defined($org_id)); # xxx
639         $org_type       = 0     unless (defined($org_type));
640
641         warn "Searching biblio.class.id\n" . 
642                 "string: $string "              . 
643                 "\noffset: $offset\n"   .
644                 "limit: $limit\n"                       .
645                 "org_id: $org_id\n"             .
646                 "depth: $org_type\n"            .
647                 "format: $format\n";
648
649         warn "Search filtering string " . time() . "\n";
650         $string = OpenILS::Application::Search->filter_search($string);
651         if(!$string) { return undef; }
652
653         if( !defined($org_id) or !$class or !$string ) {
654                 warn "not enbough args to metarecord search\n";
655                 throw OpenSRF::EX::InvalidArg 
656                         ("Not enough args to open-ils.search.biblio.class")
657         }
658
659         $class =~ s/\s+//g;
660
661         if( ($class ne "title") and ($class ne "author") and 
662                 ($class ne "subject") and ($class ne "keyword") 
663                 and ($class ne "series") ) {
664                 warn "Invalid search class: $class\n";
665                 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
666         }
667
668         #my $method = "open-ils.storage.cachable.metabib.$class.post_filter.search_fts.metarecord.atomic";
669         my $method = "open-ils.storage.metabib.$class.post_filter.search_fts.metarecord.atomic";
670
671         if($self->api_name =~ /staff/) { 
672                 $method =~ s/atomic/staff\.atomic/og;
673                 $method =~ s/\.cachable//o;
674         }
675
676         if($self->api_name =~ /full/) { 
677                 $method =~ s/\.cachable//o; #XXX testing..
678         }
679
680         warn "Performing search method $method\n";
681         warn "MR search method is $method\n";
682
683         my $session = OpenSRF::AppSession->create('open-ils.storage');
684
685         warn "Search making request " . time() . "\n";
686         my $request = $session->request(        
687                 $method,
688                 term            => $string, 
689                 org_unit => $org_id, 
690                 depth           => $org_type, 
691                 limit           => $limit,
692                 offset  => $offset,
693                 format  => $format,
694                 cache_page_size => 200,
695                 );
696
697         my $records = $request->gather(1);
698         if(!$records) {return { ids => [] }};
699
700         #my @all_ids;
701
702         warn "Received " . scalar(@$records) . " id's from class search\n";
703
704 #       for my $i (@$records) { if(defined($i)) { push @all_ids, $i; } }
705 #       my @ids = @all_ids;
706 #       @ids = grep { defined($_->[0]) } @ids;
707
708         $session->finish();
709         $session->disconnect();
710
711         my $count = undef;
712         if( $records->[0] && defined($records->[0]->[3])) { $count = $records->[0]->[3];}
713         my $recs = [];
714         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
715
716         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
717         return { ids => $recs, count => $count };
718
719 }
720
721
722
723
724 __PACKAGE__->register_method(
725         method  => "biblio_mrid_to_modsbatch_batch",
726         api_name        => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
727
728 sub biblio_mrid_to_modsbatch_batch {
729         my( $self, $client, $mrids) = @_;
730         warn "Performing mrid_to_modsbatch_batch...";
731         my @mods;
732         my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
733         use Data::Dumper;
734         warn "Grabbing mods for " . Dumper($mrids) . "\n";
735
736         for my $id (@$mrids) {
737                 next unless defined $id;
738                 #push @mods, biblio_mrid_to_modsbatch($self, $client, $id);
739                 my ($m) = $method->run($id);
740                 push @mods, $m;
741         }
742         return \@mods;
743 }
744
745
746 __PACKAGE__->register_method(
747         method  => "biblio_mrid_to_modsbatch",
748         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
749         notes           => <<"  NOTES");
750         Returns the mvr associated with a given metarecod. If none exists, 
751         it is created.
752         NOTES
753
754 __PACKAGE__->register_method(
755         method  => "biblio_mrid_to_modsbatch",
756         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
757         notes           => <<"  NOTES");
758         Returns the mvr associated with a given metarecod. If none exists, 
759         it is created.
760         NOTES
761
762 sub biblio_mrid_to_modsbatch {
763         my( $self, $client, $mrid ) = @_;
764
765         warn "Grabbing mvr for $mrid\n";
766
767         my $mr = _grab_metarecord($mrid);
768         return undef unless $mr;
769
770         if( my $m = $self->biblio_mrid_check_mvr($client, $mr)) {
771                 return $m;
772         }
773
774         return $self->biblio_mrid_make_modsbatch( $client, $mr ); 
775 }
776
777 # converts a metarecord to an mvr
778 sub _mr_to_mvr {
779         my $mr = shift;
780         my $perl = JSON->JSON2perl($mr->mods());
781         return Fieldmapper::metabib::virtual_record->new($perl);
782 }
783
784 # checks to see if a metarecord has mods, if so returns true;
785
786 __PACKAGE__->register_method(
787         method  => "biblio_mrid_check_mvr",
788         api_name        => "open-ils.search.biblio.metarecord.mods_slim.check",
789         notes           => <<"  NOTES");
790         Takes a metarecord ID or a metarecord object and returns true
791         if the metarecord already has an mvr associated with it.
792         NOTES
793
794 sub biblio_mrid_check_mvr {
795         my( $self, $client, $mrid ) = @_;
796         my $mr; 
797
798         if(ref($mrid)) { $mr = $mrid; } 
799         else { $mr = _grab_metarecord($mrid); }
800
801         warn "Checking mvr for mr " . $mr->id . "\n";
802
803         return _mr_to_mvr($mr) if $mr->mods();
804         return undef;
805 }
806
807 sub _grab_metarecord {
808
809         my $mrid = shift;
810         warn "Grabbing MR $mrid\n";
811
812         my $mr = OpenILS::Application::AppUtils->simple_scalar_request( 
813                 "open-ils.storage", 
814                 "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
815
816         if(!$mr) {
817                 throw OpenSRF::EX::ERROR 
818                         ("No metarecord exists with the given id: $mrid");
819         }
820
821         return $mr;
822 }
823
824 __PACKAGE__->register_method(
825         method  => "biblio_mrid_make_modsbatch",
826         api_name        => "open-ils.search.biblio.metarecord.mods_slim.create",
827         notes           => <<"  NOTES");
828         Takes either a metarecord ID or a metarecord object.
829         Forces the creations of an mvr for the given metarecord.
830         The created mvr is returned.
831         NOTES
832
833 sub biblio_mrid_make_modsbatch {
834
835         my( $self, $client, $mrid ) = @_;
836
837         my $mr; 
838         if(ref($mrid)) { $mr = $mrid; }
839         else { $mr = _grab_metarecord($mrid); }
840         $mrid = $mr->id;
841
842         warn "Forcing mvr creation for mr " . $mr->id . "\n";
843         my $master_id = $mr->master_record;
844
845         my $session = OpenSRF::AppSession->create("open-ils.storage");
846
847         # grab the records attached to this metarecod 
848         warn "Creating mods batch for metarecord $mrid\n";
849         my $meth = "open-ils.search.biblio.metarecord_to_records.staff";
850         $meth = $self->method_lookup($meth);
851         my ($id_hash) = $meth->run($mrid);
852         my @ids = @{$id_hash->{ids}};
853         if(@ids < 1) { return undef; }
854
855         warn "Master ID is $master_id\n";
856         # grab the master record to start the mods batch 
857
858         $meth = "open-ils.storage.direct.biblio.record_entry.retrieve";
859
860         my $record = $session->request(
861                         "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
862         $record = $record->gather(1);
863
864         #my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
865
866         if(!$record) {
867                 warn "No record returned with id $master_id";
868                 throw OpenSRF::EX::ERROR 
869         }
870
871         my $u = OpenILS::Utils::ModsParser->new();
872         use Data::Dumper;
873         $u->start_mods_batch( $record->marc );
874         my $main_doc_id = $record->id();
875
876         @ids = grep { $_ ne $master_id } @ids;
877
878         # now we have to collect all of the marc objects and push them into a mods batch
879         my $request = $session->request(
880                 "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
881
882         while( my $response = $request->recv() ) {
883
884                 next unless $response;
885                 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
886                         throw $response ($response->stringify);
887                 }
888
889                 my $content = $response->content;
890
891                 if( $content ) {
892                         $u->push_mods_batch( $content->marc );
893                 }
894         }
895
896         my $mods = $u->finish_mods_batch();
897         $mods->doc_id($mrid);
898         $request->finish();
899
900         $client->respond_complete($mods);
901
902         my $mods_string = JSON->perl2JSON($mods->decast);
903
904         $mr->mods($mods_string);
905
906         my $req = $session->request( 
907                 "open-ils.storage.direct.metabib.metarecord.update", $mr );
908
909
910         $req->gather(1);
911         $session->finish();
912         $session->disconnect();
913
914         return undef;
915 }
916
917
918
919 # converts a mr id into a list of record ids
920
921 __PACKAGE__->register_method(
922         method  => "biblio_mrid_to_record_ids",
923         api_name        => "open-ils.search.biblio.metarecord_to_records",
924 );
925
926 __PACKAGE__->register_method(
927         method  => "biblio_mrid_to_record_ids",
928         api_name        => "open-ils.search.biblio.metarecord_to_records.staff",
929 );
930
931 sub biblio_mrid_to_record_ids {
932         my( $self, $client, $mrid, $format ) = @_;
933
934         throw OpenSRF::EX::InvalidArg 
935                 ("search.biblio.metarecord_to_record_ids requires mr id")
936                         unless defined( $mrid );
937
938         warn "Searching for record for MR $mrid and format $format\n";
939
940         my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
941         if($self and $self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
942         warn "Performing record retrieval with method $method\n";
943
944
945         my $mrmaps = OpenILS::Application::AppUtils->simple_scalar_request( 
946                         "open-ils.storage", $method, $mrid, $format );
947
948         use Data::Dumper;
949         warn Dumper $mrmaps;
950
951         my $size = @$mrmaps;    
952
953         return { count => $size, ids => $mrmaps };
954
955 }
956
957
958 __PACKAGE__->register_method(
959         method  => "biblio_record_to_marc_html",
960         api_name        => "open-ils.search.biblio.record.html" );
961
962 my $parser              = XML::LibXML->new();
963 my $xslt                        = XML::LibXSLT->new();
964 my $marc_sheet;
965
966 my $settings_client = OpenSRF::Utils::SettingsClient->new();
967 sub biblio_record_to_marc_html {
968         my( $self, $client, $recordid ) = @_;
969
970         if( !$marc_sheet ) {
971                 my $dir = $settings_client->config_value( "dirs", "xsl" );
972                 my $xsl = $settings_client->config_value(
973                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
974
975                 $xsl = $parser->parse_file("$dir/$xsl");
976                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
977         }
978
979
980         my $record = $apputils->simple_scalar_request(
981                 "open-ils.storage", 
982                 "open-ils.storage.direct.biblio.record_entry.retrieve",
983                 $recordid );
984
985         my $xmldoc = $parser->parse_string($record->marc);
986         my $html = $marc_sheet->transform($xmldoc);
987         $html = $html->toString();
988         return $html;
989
990 }
991
992
993
994 __PACKAGE__->register_method(
995         method  => "retrieve_all_copy_locations",
996         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
997
998 my $shelving_locations;
999 sub retrieve_all_copy_locations {
1000         my( $self, $client ) = @_;
1001         if(!$shelving_locations) {
1002                 $shelving_locations = $apputils->simple_scalar_request(
1003                         "open-ils.storage", 
1004                         "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
1005         }
1006         return $shelving_locations;
1007 }
1008
1009
1010
1011 __PACKAGE__->register_method(
1012         method  => "retrieve_all_copy_statuses",
1013         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
1014
1015 my $copy_statuses;
1016 sub retrieve_all_copy_statuses {
1017         my( $self, $client ) = @_;
1018         if(!$copy_statuses) {
1019                 $copy_statuses = $apputils->simple_scalar_request(
1020                         "open-ils.storage",
1021                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
1022         }
1023         return $copy_statuses;
1024 }
1025
1026
1027 __PACKAGE__->register_method(
1028         method  => "copy_counts_per_org",
1029         api_name        => "open-ils.search.biblio.copy_counts.retrieve");
1030
1031 __PACKAGE__->register_method(
1032         method  => "copy_counts_per_org",
1033         api_name        => "open-ils.search.biblio.copy_counts.retrieve.staff");
1034
1035 sub copy_counts_per_org {
1036         my( $self, $client, $record_id ) = @_;
1037
1038         warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
1039
1040         my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
1041         if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
1042
1043         my $counts = $apputils->simple_scalar_request(
1044                 "open-ils.storage", $method, $record_id );
1045
1046         $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1047         return $counts;
1048 }
1049
1050
1051 __PACKAGE__->register_method(
1052         method          => "copy_count_summary",
1053         api_name        => "open-ils.search.biblio.copy_counts.summary.retrieve",
1054         notes           => <<"  NOTES");
1055         returns an array of these:
1056                 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1057                 where statusx is a copy status name.  the statuses are sorted
1058                 by id.
1059         NOTES
1060
1061 sub copy_count_summary {
1062         my( $self, $client, $rid ) = @_;
1063         my $method = "open-ils.storage.biblio.record_entry.status_copy_count.atomic";
1064         return $apputils->simple_scalar_request( "open-ils.storage", $method, $rid );
1065 }
1066
1067
1068 __PACKAGE__->register_method(
1069         method          => "multiclass_search",
1070         api_name        => "open-ils.search.biblio.multiclass",
1071         notes           => <<"  NOTES");
1072                 Performs a multiclass search
1073                 PARAMS( searchBlob, org_unit, format, limit ) 
1074                 where searchBlob is defined like this:
1075                         { 
1076                                 "title" : { "term" : "water" }, 
1077                                 "author" : { "term" : "smith" }, 
1078                                 ... 
1079                         }
1080         NOTES
1081
1082 __PACKAGE__->register_method(
1083         method          => "multiclass_search",
1084         api_name        => "open-ils.search.biblio.multiclass.staff",
1085         notes           => "see open-ils.search.biblio.multiclass" );
1086
1087 sub multiclass_search {
1088         my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1089
1090         $logger->debug("Performing multiclass search with org => $orgid, " .
1091                 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1092
1093         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1094         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1095
1096
1097         my $records = $apputils->simplereq(
1098                 'open-ils.storage', $meth, 
1099                  org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1100
1101         my $count = 0;
1102         my $recs = [];
1103
1104         if( ref($records) and $records->[0] and 
1105                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1106
1107         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1108
1109         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1110         return { ids => $recs, count => $count };
1111
1112
1113 }
1114
1115 __PACKAGE__->register_method(
1116         method          => "marc_search",
1117         api_name        => "open-ils.search.biblio.marc",
1118         notes           => <<"  NOTES");
1119                 Performs a multiclass search
1120                 PARAMS( searchBlob, org_unit, format ) 
1121                 where searchBlob is defined like this:
1122                 [ 
1123                         {
1124                                 "term":"shakespeare",
1125                                 "restrict":[{"tag":"245","subfield":"a"}] 
1126                         }, 
1127                         {
1128                                 "term":"bloom",
1129                                 "restrict":[{"tag":"100","subfield":"a"}] 
1130                         } 
1131
1132                 ]
1133         NOTES
1134
1135
1136 sub marc_search {
1137         my( $self, $client, $searchBlob, $orgid, $format ) = @_;
1138
1139         $logger->debug("Performing MARC search with org => $orgid, " .
1140                 "format => $format and search blob " . Dumper($searchBlob) );
1141                 
1142         my $records =  $apputils->simplereq(
1143                 'open-ils.storage',
1144                 'open-ils.storage.metabib.full_rec.multi_search.atomic',
1145                 searches => $searchBlob, org_unit => $orgid, format => $format );
1146
1147         my $count = 0;
1148         my $recs = [];
1149
1150         if( ref($records) and $records->[0] and 
1151                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1152
1153         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1154
1155         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1156         return { ids => $recs, count => $count };
1157
1158 }
1159
1160
1161
1162 __PACKAGE__->register_method(
1163         method  => "biblio_search_isbn",
1164         api_name        => "open-ils.search.biblio.isbn",
1165 );
1166
1167 sub biblio_search_isbn { 
1168         my( $self, $client, $isbn ) = @_;
1169
1170         $logger->debug("Searching ISBN $isbn");
1171
1172         my $method = $self->method_lookup("open-ils.search.biblio.marc");
1173
1174         my ($records) = $method->run(  
1175                 [ {     term => $isbn,
1176                                 restrict => $cat_search_hash->{isbn} } ], 1);
1177
1178         return $records;
1179 }
1180
1181
1182 __PACKAGE__->register_method(
1183         method  => "biblio_search_issn",
1184         api_name        => "open-ils.search.biblio.issn",
1185 );
1186
1187 sub biblio_search_issn { 
1188         my( $self, $client, $issn ) = @_;
1189
1190         $logger->debug("Searching ISSN $issn");
1191
1192         my $method = $self->method_lookup("open-ils.search.biblio.marc");
1193
1194         my ($records) = $method->run(  
1195                 [ {     term => $issn,
1196                                 restrict => $cat_search_hash->{issn} } ], 1);
1197
1198         return $records;
1199 }
1200
1201
1202
1203
1204 __PACKAGE__->register_method(
1205         method  => "fetch_mods_by_copy",
1206         api_name        => "open-ils.search.biblio.mods_from_copy",
1207 );
1208
1209 sub fetch_mods_by_copy {
1210         my( $self, $client, $copyid ) = @_;
1211         my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1212         return $evt if $evt;
1213         return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1214         return $apputils->record_to_mvr($record);
1215 }
1216
1217
1218
1219
1220
1221 # -------------------------------------------------------------------------------------
1222
1223 __PACKAGE__->register_method(
1224         method  => "cn_browse",
1225         api_name        => "open-ils.search.callnumber.browse.target",
1226         notes           => "Starts a callnumber browse"
1227         );
1228
1229 __PACKAGE__->register_method(
1230         method  => "cn_browse",
1231         api_name        => "open-ils.search.callnumber.browse.page_up",
1232         notes           => "Returns the previous page of callnumbers", 
1233         );
1234
1235 __PACKAGE__->register_method(
1236         method  => "cn_browse",
1237         api_name        => "open-ils.search.callnumber.browse.page_down",
1238         notes           => "Returns the next page of callnumbers", 
1239         );
1240
1241
1242 # RETURNS array of arrays like so: label, owning_lib, record, id
1243 sub cn_browse {
1244         my( $self, $client, @params ) = @_;
1245         my $method;
1246
1247         $method = 'open-ils.storage.asset.call_number.browse.target.atomic' 
1248                 if( $self->api_name =~ /target/ );
1249         $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1250                 if( $self->api_name =~ /page_up/ );
1251         $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1252                 if( $self->api_name =~ /page_down/ );
1253
1254         return $apputils->simplereq( 'open-ils.storage', $method, @params );
1255 }
1256 # -------------------------------------------------------------------------------------
1257
1258 __PACKAGE__->register_method(
1259         method => "fetch_cn",
1260         api_name => "open-ils.search.callnumber.retrieve",
1261         notes           => "retrieves a callnumber based on ID",
1262         );
1263
1264 sub fetch_cn {
1265         my( $self, $client, $id ) = @_;
1266         my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1267         return $evt if $evt;
1268         return $cn;
1269 }
1270
1271
1272
1273
1274
1275
1276
1277
1278 1;