]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
4deef5230d4bc201f2674ee7577c0edc73c2ed66
[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
14 use JSON;
15
16 use Time::HiRes qw(time);
17 use OpenSRF::EX qw(:try);
18 use Digest::MD5 qw(md5_hex);
19
20 use XML::LibXML;
21 use XML::LibXSLT;
22
23 my $apputils = "OpenILS::Application::AppUtils";
24
25 # Houses biblio search utilites 
26
27 __PACKAGE__->register_method(
28         method  => "biblio_search_marc",
29         api_name        => "open-ils.search.biblio.marc",
30         argc            => 1, 
31         note            => "Searches biblio information by marc tag",
32 );
33
34 sub biblio_search_marc {
35
36         my( $self, $client, $search_hash, $string ) = @_;
37
38         warn "Building biblio marc session\n";
39         my $session = OpenSRF::AppSession->create("open-ils.storage");
40
41         use Data::Dumper;
42         warn "Sending biblio marc request. String $string\nSearch hash: " . Dumper($search_hash);
43         my $request = $session->request( 
44                         "open-ils.storage.direct.metabib.full_rec.search_fts.index_vector.atomic", 
45                         restrict => $search_hash, 
46                         term            => $string );
47         my $data = $request->gather(1);
48
49         warn Dumper $data;
50
51         $session->finish();
52         $session->disconnect();
53
54         return $data;
55
56 }
57
58
59
60 # ---------------------------------------------------------------------------
61 # takes a list of record id's and turns the docs into friendly 
62 # mods structures. Creates one MODS structure for each doc id.
63 # ---------------------------------------------------------------------------
64 sub _records_to_mods {
65         my @ids = @_;
66         
67         my @results;
68         my @marcxml_objs;
69
70         my $session = OpenSRF::AppSession->create("open-ils.storage");
71         my $request = $session->request(
72                         "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
73
74         my $last_content = undef;
75
76         while( my $response = $request->recv() ) {
77
78                 if( $last_content ) {
79                         my $u = OpenILS::Utils::ModsParser->new();
80                         $u->start_mods_batch( $last_content->marc );
81                         my $mods = $u->finish_mods_batch();
82                         $mods->doc_id($last_content->id());
83                         warn "Turning doc " . $mods->doc_id() . " into MODS\n";
84                         $last_content = undef;
85                         push @results, $mods;
86                 }
87
88                 next unless $response;
89
90                 if($response->isa("OpenSRF::EX")) {
91                         throw $response ($response->stringify);
92                 }
93
94                 $last_content = $response->content;
95
96         }
97
98         if( $last_content ) {
99                 my $u = OpenILS::Utils::ModsParser->new();
100                 $u->start_mods_batch( $last_content->marc );
101                 my $mods = $u->finish_mods_batch();
102                 $mods->doc_id($last_content->id());
103                 push @results, $mods;
104         }
105
106         $request->finish();
107         $session->finish();
108         $session->disconnect();
109
110         return \@results;
111
112 }
113
114 __PACKAGE__->register_method(
115         method  => "record_id_to_mods",
116         api_name        => "open-ils.search.biblio.record.mods.retrieve",
117         argc            => 1, 
118         note            => "Provide ID, we provide the mods"
119 );
120
121 # converts a record into a mods object with copy counts attached
122 sub record_id_to_mods {
123
124         my( $self, $client, $org_id, $id ) = @_;
125
126         my $mods_list = _records_to_mods( $id );
127         my $mods_obj = $mods_list->[0];
128         my $cmethod = $self->method_lookup(
129                         "open-ils.search.biblio.record.copy_count");
130         my ($count) = $cmethod->run($org_id, $id);
131         $mods_obj->copy_count($count);
132
133         return $mods_obj;
134 }
135
136
137 __PACKAGE__->register_method(
138         method  => "record_id_to_mods_slim",
139         api_name        => "open-ils.search.biblio.record.mods_slim.retrieve",
140         argc            => 1, 
141         note            => "Provide ID, we provide the mods"
142 );
143
144 # converts a record into a mods object with NO copy counts attached
145 sub record_id_to_mods_slim {
146
147         my( $self, $client, $id ) = @_;
148         warn "Retrieving MODS object for record $id\n";
149         return undef unless(defined $id);
150
151         my $mods_list = _records_to_mods( $id );
152         my $mods_obj = $mods_list->[0];
153         return $mods_obj;
154 }
155
156
157 # Returns the number of copies attached to a record based on org location
158 __PACKAGE__->register_method(
159         method  => "record_id_to_copy_count",
160         api_name        => "open-ils.search.biblio.record.copy_count",
161 );
162
163 __PACKAGE__->register_method(
164         method  => "record_id_to_copy_count",
165         api_name        => "open-ils.search.biblio.metarecord.copy_count",
166 );
167
168 __PACKAGE__->register_method(
169         method  => "record_id_to_copy_count",
170         api_name        => "open-ils.search.biblio.metarecord.copy_count.staff",
171 );
172 sub record_id_to_copy_count {
173         my( $self, $client, $org_id, $record_id ) = @_;
174
175         my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
176         my $key = "record";
177         if($self->api_name =~ /metarecord/) {
178                 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
179                 $key = "metarecord";
180         }
181
182         if($self->api_name =~ /staff/ ) {
183                 $method =~ s/atomic/staff\.atomic/og;
184                 warn "Doing staff search $method\n";
185         }
186
187
188         my $session = OpenSRF::AppSession->create("open-ils.storage");
189         warn "copy_count retrieve $record_id\n";
190         return undef unless(defined $record_id);
191
192         my $request = $session->request(
193                 $method, org_unit => $org_id => $key => $record_id );
194
195
196         my $count = $request->gather(1);
197         $session->disconnect();
198         return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
199
200 }
201
202
203 # used for cat search classes
204 my $cat_search_hash =  {
205
206         author => [ 
207                 { tag => "100", subfield => "a"} ,
208                 { tag => "700", subfield => "a"}, 
209         ],
210
211         title => [ 
212                 { tag => "245", subfield => "a"},
213                 { tag => "242", subfield => "a"}, 
214                 { tag => "240", subfield => "a"},
215                 { tag => "210", subfield => "a"},
216         ],
217
218         subject => [ 
219                 { tag => "650", subfield => "_" }, 
220         ],
221
222         tcn     => [
223                 { tag => "035", subfield => "_" },
224         ],
225
226         isbn    => [
227                 { tag => "020", subfield => "a" },
228         ],
229
230 };
231
232
233 __PACKAGE__->register_method(
234         method  => "biblio_search_tcn",
235         api_name        => "open-ils.search.biblio.tcn",
236         argc            => 3, 
237         note            => "Retrieve a record by TCN",
238 );
239
240 sub biblio_search_tcn {
241
242         my( $self, $client, $tcn ) = @_;
243
244         $tcn =~ s/.*?(\w+)\s*$/$1/o;
245         warn "Searching TCN $tcn\n";
246
247         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
248         my $request = $session->request( 
249                         "open-ils.storage.direct.biblio.record_entry.search.tcn_value", $tcn );
250         my $record_entry = $request->gather(1);
251
252         my @ids;
253         for my $record (@$record_entry) {
254                 push @ids, $record->id;
255         }
256
257         $session->disconnect();
258
259         warn "received ID's for tcn search @ids\n";
260         my $size = @ids;
261
262         return { count => $size, ids => \@ids };
263
264 }
265
266
267 # --------------------------------------------------------------------------------
268 # ISBN
269
270 __PACKAGE__->register_method(
271         method  => "biblio_search_isbn",
272         api_name        => "open-ils.search.biblio.isbn",
273 );
274
275 sub biblio_search_isbn { 
276         my( $self, $client, $isbn ) = @_;
277         throw OpenSRF::EX::InvalidArg 
278
279                 ("biblio_search_isbn needs an ISBN to search")
280                         unless defined $isbn;
281
282         warn "biblio search for ISBN $isbn\n";
283         my $method = $self->method_lookup("open-ils.search.biblio.marc");
284         my ($records) = $method->run( $cat_search_hash->{isbn}, $isbn );
285
286         my $size = @$records;
287         return { count => $size, ids => $records };
288 }
289
290
291
292 # --------------------------------------------------------------------------------
293
294 __PACKAGE__->register_method(
295         method  => "biblio_barcode_to_copy",
296         api_name        => "open-ils.search.asset.copy.find_by_barcode",
297 );
298
299 # turns a barcode into a copy object
300 sub biblio_barcode_to_copy { 
301         my( $self, $client, $barcode ) = @_;
302
303         throw OpenSRF::EX::InvalidArg 
304                 ("search.biblio.barcode needs a barcode to search")
305                         unless defined $barcode;
306
307         warn "copy search for barcode $barcode\n";
308         my $record = OpenILS::Application::AppUtils->simple_scalar_request(
309                         "open-ils.storage", 
310                         "open-ils.storage.direct.asset.copy.search.barcode",
311                         $barcode );
312
313         return undef unless($record);
314         return $record->[0];
315
316 }
317
318 __PACKAGE__->register_method(
319         method  => "biblio_id_to_copy",
320         api_name        => "open-ils.search.asset.copy.batch.retrieve",
321 );
322
323 # turns a barcode into a copy object
324 sub biblio_id_to_copy { 
325         my( $self, $client, $ids ) = @_;
326
327         throw OpenSRF::EX::InvalidArg 
328                 ("search.biblio.batch.retrieve needs a id to search")
329                         unless defined $ids;
330
331         warn "copy search for ids @$ids\n";
332         my $record = OpenILS::Application::AppUtils->simple_scalar_request(
333                         "open-ils.storage", 
334                         "open-ils.storage.direct.asset.copy.batch.retrieve.atomic",
335                         @$ids );
336
337         return $record;
338
339 }
340
341
342 __PACKAGE__->register_method(
343         method  => "fleshed_copy_retrieve",
344         api_name        => "open-ils.search.asset.copy.fleshed.batch.retrieve",
345 );
346
347 # turns a barcode into a copy object
348 sub fleshed_copy_retrieve { 
349         my( $self, $client, $ids ) = @_;
350
351         throw OpenSRF::EX::InvalidArg 
352                 ("search.biblio.batch.retrieve needs a id to search")
353                         unless defined $ids;
354
355         warn "fleshed copy search for id @$ids\n";
356         my $copy = OpenILS::Application::AppUtils->simple_scalar_request(
357                         "open-ils.storage", 
358                         "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic",
359                         @$ids );
360
361         return $copy;
362 }
363
364
365
366 __PACKAGE__->register_method(
367         method  => "biblio_barcode_to_title",
368         api_name        => "open-ils.search.biblio.find_by_barcode",
369 );
370
371 sub biblio_barcode_to_title {
372         my( $self, $client, $barcode ) = @_;
373
374         if(!$barcode) {
375                 throw OpenSRF::EX::ERROR 
376                         ("Not enough args to find_by_barcode");
377         }
378
379         my $title = $apputils->simple_scalar_request(
380                 "open-ils.storage",
381                 "open-ils.storage.biblio.record_entry.retrieve_by_barcode",
382                 $barcode );
383
384         if($title) {
385                 return { ids => $title->id, count => 1 };
386         } else {
387                 return { count => 0 };
388         }
389
390
391 =head
392         my $u = OpenILS::Utils::ModsParser->new();
393         $u->start_mods_batch( $title->marc );
394         my $mods = $u->finish_mods_batch();
395         $mods->doc_id($title->id());
396         return $mods;
397 =cut
398
399         
400 }
401
402
403 __PACKAGE__->register_method(
404         method  => "biblio_copy_to_mods",
405         api_name        => "open-ils.search.biblio.copy.mods.retrieve",
406 );
407
408 # takes a copy object and returns it fleshed mods object
409 sub biblio_copy_to_mods {
410         my( $self, $client, $copy ) = @_;
411
412         throw OpenSRF::EX::InvalidArgs 
413                 ("copy.mods.retrieve needs a copy") unless( $copy );
414
415         new Fieldmapper::asset::copy($copy);
416
417         my $volume = OpenILS::Application::AppUtils->simple_scalar_request(
418                 "open-ils.storage",
419                 "open-ils.storage.direct.asset.call_number.retrieve",
420                 $copy->call_number() );
421
422         my $mods = _records_to_mods($volume->record());
423         $mods = shift @$mods;
424         $volume->copies([$copy]);
425         push @{$mods->call_numbers()}, $volume;
426
427         return $mods;
428 }
429
430
431 sub barcode_to_mods {
432
433 }
434
435
436 # --------------------------------------------------------------------------------
437
438
439
440 __PACKAGE__->register_method(
441         method  => "cat_biblio_search_class",
442         api_name        => "open-ils.search.cat.biblio.class",
443 );
444
445
446 sub cat_biblio_search_class {
447
448         my( $self, $client, $org_id, $class, $sort, $string ) = @_;
449
450         throw OpenSRF::EX::InvalidArg 
451                 ("Not enough args to open-ils.search.cat.biblio.class")
452                         unless( defined($org_id) and $class and $sort and $string );
453
454
455         my $search_hash;
456
457         my $method = $self->method_lookup("open-ils.search.biblio.marc");
458         if(!$method) {
459                 throw OpenSRF::EX::PANIC 
460                         ("Can't lookup method 'open-ils.search.biblio.marc'");
461         }
462
463         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
464
465         my @ids;
466         for my $i (@$records) { push @ids, $i->[0]; }
467
468         my $mods_list = _records_to_mods( @ids );
469         return undef unless (ref($mods_list) eq "ARRAY");
470
471         # ---------------------------------------------------------------
472         # append copy count information to the mods objects
473         my $session = OpenSRF::AppSession->create("open-ils.storage");
474
475         my $request = $session->request(
476                 "open-ils.storage.direct.biblio.record_copy_count.batch",  $org_id, @ids );
477
478         for my $id (@ids) {
479
480                 warn "receiving copy counts for doc $id\n";
481
482                 my $response = $request->recv();
483                 next unless $response;
484
485                 if( $response and UNIVERSAL::isa($response, "Error")) {
486                         throw $response ($response->stringify);
487                 }
488
489                 my $count = $response->content;
490                 my $mods_obj = undef;
491                 for my $m (@$mods_list) {
492                         $mods_obj = $m if ($m->doc_id() == $id)
493                 }
494                 if($mods_obj) {
495                         $mods_obj->copy_count($count);
496                 }
497
498                 $client->respond( $mods_obj );
499
500         }       
501         $request->finish();
502
503         $session->finish();
504         $session->disconnect();
505         $session->kill_me();
506         # ---------------------------------------------------------------
507
508         return undef;
509 }
510
511
512
513 __PACKAGE__->register_method(
514         method  => "cat_biblio_search_class_id",
515         api_name        => "open-ils.search.cat.biblio.class.id",
516         argc            => 3, 
517         note            => "Searches biblio information by search class and returns the IDs",
518 );
519
520 sub cat_biblio_search_class_id {
521
522         my( $self, $client, $org_id, $class, $string, $limit, $offset ) = @_;
523
524         $offset ||= 0;
525         $limit  ||= 100;
526         $limit -= 1;
527
528
529         my $bool = ($class eq "subject" || $class eq "keyword");
530         $string = OpenILS::Application::Search->filter_search($string, $bool);
531
532         if(!$string) { 
533                 return OpenILS::EX->new("SEARCH_TOO_LARGE")->ex();
534         }
535
536         warn "Searching cat.biblio.class.id string: $string offset: $offset limit: $limit\n";
537
538         throw OpenSRF::EX::InvalidArg 
539                 ("Not enough args to open-ils.search.cat.biblio.class")
540                         unless( defined($org_id) and $class and $string );
541
542
543         my $search_hash;
544
545         my $cache_key = md5_hex( $org_id . $class . $string );
546         my $id_array = OpenILS::Application::SearchCache->get_cache($cache_key);
547
548         if(ref($id_array)) {
549                 warn "Returning class search from cache\n";
550                 my $size = @$id_array;
551                 my @ids = @$id_array[ $offset..($offset+$limit) ];
552                 return { count => $size, ids => \@ids };
553         }
554
555         my $method = $self->method_lookup("open-ils.search.biblio.marc");
556         if(!$method) {
557                 throw OpenSRF::EX::PANIC 
558                         ("Can't lookup method 'open-ils.search.biblio.marc'");
559         }
560
561         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
562
563         my @cache_ids;
564
565         for my $i (@$records) { 
566                 if(defined($i->[0])) {
567                         push @cache_ids, $i->[0]; 
568                 }
569         }
570
571         my @ids = @cache_ids[ $offset..($offset+$limit) ];
572         my $size = @$records;
573
574         OpenILS::Application::SearchCache->put_cache( 
575                         $cache_key, \@cache_ids, $size );
576
577         return { count =>$size, ids => \@ids };
578
579 }
580
581 __PACKAGE__->register_method(
582         method  => "biblio_search_class_count",
583         api_name        => "open-ils.search.biblio.class.count",
584 );
585
586 __PACKAGE__->register_method(
587         method  => "biblio_search_class_count",
588         api_name        => "open-ils.search.biblio.class.count.staff",
589 );
590
591 sub biblio_search_class_count {
592
593         my( $self, $client, $class, $string, $org_id, $org_type ) = @_;
594
595         warn "org: $org_id : depth: $org_type\n";
596
597         $org_id         = "1" unless defined($org_id); # xxx
598         $org_type       = 0     unless defined($org_type);
599
600         warn "Searching biblio.class.id\n" . 
601                 "string: $string "              . 
602                 "org_id: $org_id\n"             .
603                 "depth: $org_type\n" ;
604
605         my $bool = ($class eq "subject" || $class eq "keyword");
606         $string = OpenILS::Application::Search->filter_search($string, $bool);
607
608         if(!$string) { 
609                 return OpenILS::EX->new("SEARCH_TOO_LARGE")->ex;
610         }
611
612                 
613         if( !defined($org_id) or !$class or !$string ) {
614                 warn "not enbough args to metarecord search\n";
615                 throw OpenSRF::EX::InvalidArg 
616                         ("Not enough args to open-ils.search.cat.biblio.class")
617         }
618
619         $class =~ s/\s+//g;
620
621         if( ($class ne "title") and ($class ne "author") and 
622                 ($class ne "subject") and ($class ne "keyword") 
623                 and ($class ne "series"  )) {
624                 warn "Invalid search class: $class\n";
625                 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
626         }
627
628         # grab the mr id's from storage
629
630         my $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord_count";
631         if($self->api_name =~ /staff/) { 
632                 $method = "$method.staff"; 
633                 $method =~ s/\.cachable//o;
634         }
635         warn "Performing count method $method\n";
636         warn "API name " . $self->api_name() . "\n";
637
638         my $session = OpenSRF::AppSession->create('open-ils.storage');
639
640         my $request = $session->request( $method, 
641                         term => $string, 
642                         org_unit => $org_id, 
643                         cache_page_size => 1,
644                         depth =>$org_type );
645
646         my $count = $request->gather(1);
647         warn "Received count $count\n";
648
649         return $count;
650 }
651
652
653 __PACKAGE__->register_method(
654         method  => "biblio_search_class",
655         api_name        => "open-ils.search.biblio.class",
656 );
657
658 __PACKAGE__->register_method(
659         method  => "biblio_search_class",
660         api_name        => "open-ils.search.biblio.class.unordered",
661 );
662
663 __PACKAGE__->register_method(
664         method  => "biblio_search_class",
665         api_name        => "open-ils.search.biblio.class.staff",
666 );
667
668 __PACKAGE__->register_method(
669         method  => "biblio_search_class",
670         api_name        => "open-ils.search.biblio.class.unordered.staff",
671 );
672
673 sub biblio_search_class {
674
675         my( $self, $client, $class, $string, 
676                         $org_id, $org_type, $limit, $offset ) = @_;
677
678         warn "org: $org_id : depth: $org_type : limit: $limit :  offset: $offset\n";
679
680         $offset         ||= 0;
681         $limit          = 100 unless defined($limit and $limit > 0 );
682         $org_id         = "1" unless defined($org_id); # xxx
683         $org_type       = 0     unless defined($org_type);
684
685         warn "Searching biblio.class.id\n" . 
686                 "string: $string "              . 
687                 "\noffset: $offset\n"   .
688                 "limit: $limit\n"                       .
689                 "org_id: $org_id\n"             .
690                 "depth: $org_type\n" ;
691
692         warn "Search filtering string " . time() . "\n";
693         $string = OpenILS::Application::Search->filter_search($string);
694         if(!$string) { return undef; }
695
696         if( !defined($org_id) or !$class or !$string ) {
697                 warn "not enbough args to metarecord search\n";
698                 throw OpenSRF::EX::InvalidArg 
699                         ("Not enough args to open-ils.search.cat.biblio.class")
700         }
701
702         $class =~ s/\s+//g;
703
704         if( ($class ne "title") and ($class ne "author") and 
705                 ($class ne "subject") and ($class ne "keyword") 
706                 and ($class ne "series") ) {
707                 warn "Invalid search class: $class\n";
708                 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
709         }
710
711         #my $method = "open-ils.storage.metabib.$class.search_fts.metarecord.atomic";
712         my $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord.atomic";
713
714         if($self->api_name =~ /order/) {
715                 $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord.unordered.atomic",
716                 #$method = "open-ils.storage.metabib.$class.search_fts.metarecord.unordered.atomic";
717         }
718
719         if($self->api_name =~ /staff/) { 
720                 $method =~ s/atomic/staff\.atomic/og;
721                 $method =~ s/\.cachable//o;
722         }
723
724         warn "Performing search method $method\n";
725         warn "MR search method is $method\n";
726
727         my $session = OpenSRF::AppSession->create('open-ils.storage');
728
729         warn "Search making request " . time() . "\n";
730         my $request = $session->request(        
731                 $method,
732                 term            => $string, 
733                 org_unit => $org_id, 
734                 depth           => $org_type, 
735                 limit           => $limit,
736                 offset  => $offset,
737                 cache_page_size => 200,
738                 );
739
740         my $records = $request->gather(1);
741         warn "Search request complete " . time() . "\n";
742
743         my @all_ids;
744
745         use Data::Dumper;
746         warn "Received " . scalar(@$records) . " id's from class search\n";
747
748         # if we just get one, it won't be wrapped in an array
749         if(!ref($records->[0])) {
750                 $records = [$records]; } 
751         for my $i (@$records) { 
752                 if(defined($i)) {
753                         push @all_ids, $i; 
754                 }
755         }
756
757         my @ids = @all_ids;
758         @ids = grep { defined($_->[0]) } @ids;
759
760         $session->finish();
761         $session->disconnect();
762
763         return { ids => \@ids };
764
765 }
766
767
768
769
770 __PACKAGE__->register_method(
771         method  => "biblio_mrid_to_modsbatch",
772         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
773 );
774
775 sub biblio_mrid_to_modsbatch {
776         my( $self, $client, $mrid ) = @_;
777
778         throw OpenSRF::EX::InvalidArg 
779                 ("search.biblio.metarecord_to_mods requires mr id")
780                         unless defined( $mrid );
781
782
783         my $metarecord = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
784                         "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
785
786         if(!$metarecord) {
787                 throw OpenSRF::EX::ERROR ("No metarecord exists with the given id: $mrid");
788         }
789
790         my $master_id = $metarecord->master_record();
791
792
793         # check for existing mods
794         if($metarecord->mods()){
795                 warn "We already have mods for " . $metarecord->id . "\n";
796                 my $perl = JSON->JSON2perl($metarecord->mods());
797                 return Fieldmapper::metabib::virtual_record->new($perl);
798         }
799
800
801
802         warn "Creating mods batch for metarecord $mrid\n";
803         my $id_hash = biblio_mrid_to_record_ids( undef, undef,  $mrid );
804         my @ids = @{$id_hash->{ids}};
805
806         if(@ids < 1) { return undef; }
807
808         warn "Master ID is $master_id\n";
809         # grab the master record to start the mods batch 
810
811         my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
812                         "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
813
814         if(!$record) {
815                 throw OpenSRF::EX::ERROR 
816                         ("No record returned with id $master_id");
817         }
818
819         my $u = OpenILS::Utils::ModsParser->new();
820         use Data::Dumper;
821         $u->start_mods_batch( $record->marc );
822         my $main_doc_id = $record->id();
823
824         @ids = grep { $_ ne $master_id } @ids;
825
826         # now we have to collect all of the marc objects and push them into a mods batch
827         my $session = OpenSRF::AppSession->create("open-ils.storage");
828         my $request = $session->request(
829                 "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
830
831         while( my $response = $request->recv() ) {
832
833                 next unless $response;
834                 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
835                         throw $response ($response->stringify);
836                 }
837
838                 my $content = $response->content;
839
840                 if( $content ) {
841                         $u->push_mods_batch( $content->marc );
842                 }
843         }
844
845         my $mods = $u->finish_mods_batch();
846         $mods->doc_id($mrid);
847         $request->finish();
848
849         $client->respond_complete($mods);
850
851         my $mods_string = JSON->perl2JSON($mods->decast);
852
853         $metarecord->mods($mods_string);
854
855         my $req = $session->request( 
856                         "open-ils.storage.direct.metabib.metarecord.update", 
857                         $metarecord );
858
859
860         $req->gather(1);
861
862         $session->finish();
863         $session->disconnect();
864
865         return undef;
866
867 }
868
869
870
871 # converts a mr id into a list of record ids
872
873 __PACKAGE__->register_method(
874         method  => "biblio_mrid_to_record_ids",
875         api_name        => "open-ils.search.biblio.metarecord_to_records",
876 );
877
878 sub biblio_mrid_to_record_ids {
879         my( $self, $client, $mrid ) = @_;
880
881         throw OpenSRF::EX::InvalidArg 
882                 ("search.biblio.metarecord_to_record_ids requires mr id")
883                         unless defined( $mrid );
884
885         warn "Searching for record for MR $mrid\n";
886
887         my $mrmaps = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
888                         "open-ils.storage.direct.metabib.metarecord_source_map.search.metarecord", $mrid );
889
890         my @ids;
891         for my $map (@$mrmaps) { push @ids, $map->source(); }
892
893         warn "Recovered id's [@ids] for mr $mrid\n";
894
895         my $size = @ids;
896
897         return { count => $size, ids => \@ids };
898
899 }
900
901
902
903 __PACKAGE__->register_method(
904         method  => "biblio_record_to_marc_html",
905         api_name        => "open-ils.search.biblio.record.html" );
906
907 my $parser              = XML::LibXML->new();
908 my $xslt                        = XML::LibXSLT->new();
909 my $marc_sheet;
910
911 my $settings_client = OpenSRF::Utils::SettingsClient->new();
912 sub biblio_record_to_marc_html {
913         my( $self, $client, $recordid ) = @_;
914
915         if( !$marc_sheet ) {
916                 my $dir = $settings_client->config_value( "dirs", "xsl" );
917                 my $xsl = $settings_client->config_value(
918                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
919
920                 $xsl = $parser->parse_file("$dir/$xsl");
921                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
922         }
923
924
925         my $record = $apputils->simple_scalar_request(
926                 "open-ils.storage", 
927                 "open-ils.storage.direct.biblio.record_entry.retrieve",
928                 $recordid );
929
930         my $xmldoc = $parser->parse_string($record->marc);
931         my $html = $marc_sheet->transform($xmldoc);
932         $html = $html->toString();
933         return $html;
934
935 }
936
937
938
939 __PACKAGE__->register_method(
940         method  => "retrieve_all_copy_locations",
941         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
942
943 my $shelving_locations;
944 sub retrieve_all_copy_locations {
945         my( $self, $client ) = @_;
946         if(!$shelving_locations) {
947                 $shelving_locations = $apputils->simple_scalar_request(
948                         "open-ils.storage", 
949                         "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
950         }
951         return $shelving_locations;
952 }
953
954
955
956 __PACKAGE__->register_method(
957         method  => "retrieve_all_copy_statuses",
958         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
959
960 my $copy_statuses;
961 sub retrieve_all_copy_statuses {
962         my( $self, $client ) = @_;
963         if(!$copy_statuses) {
964                 $copy_statuses = $apputils->simple_scalar_request(
965                         "open-ils.storage",
966                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
967         }
968         return $copy_statuses;
969 }
970
971
972
973
974
975 1;