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