]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
short circuit retrieval when no records are returned
[working/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         return { count => 0 } unless($records and @$records);
287         my $size = @$records;
288         return { count => $size, ids => $records };
289 }
290
291
292
293 # --------------------------------------------------------------------------------
294
295 __PACKAGE__->register_method(
296         method  => "biblio_barcode_to_copy",
297         api_name        => "open-ils.search.asset.copy.find_by_barcode",
298 );
299
300 # turns a barcode into a copy object
301 sub biblio_barcode_to_copy { 
302         my( $self, $client, $barcode ) = @_;
303
304         throw OpenSRF::EX::InvalidArg 
305                 ("search.biblio.barcode needs a barcode to search")
306                         unless defined $barcode;
307
308         warn "copy search for barcode $barcode\n";
309         my $record = OpenILS::Application::AppUtils->simple_scalar_request(
310                         "open-ils.storage", 
311                         "open-ils.storage.direct.asset.copy.search.barcode",
312                         $barcode );
313
314         return undef unless($record);
315         return $record->[0];
316
317 }
318
319 __PACKAGE__->register_method(
320         method  => "biblio_id_to_copy",
321         api_name        => "open-ils.search.asset.copy.batch.retrieve",
322 );
323
324 # turns a barcode into a copy object
325 sub biblio_id_to_copy { 
326         my( $self, $client, $ids ) = @_;
327
328         throw OpenSRF::EX::InvalidArg 
329                 ("search.biblio.batch.retrieve needs a id to search")
330                         unless defined $ids;
331
332         warn "copy search for ids @$ids\n";
333         my $record = OpenILS::Application::AppUtils->simple_scalar_request(
334                         "open-ils.storage", 
335                         "open-ils.storage.direct.asset.copy.batch.retrieve.atomic",
336                         @$ids );
337
338         return $record;
339
340 }
341
342
343 __PACKAGE__->register_method(
344         method  => "fleshed_copy_retrieve",
345         api_name        => "open-ils.search.asset.copy.fleshed.batch.retrieve",
346 );
347
348 # turns a barcode into a copy object
349 sub fleshed_copy_retrieve { 
350         my( $self, $client, $ids ) = @_;
351
352         throw OpenSRF::EX::InvalidArg 
353                 ("search.biblio.batch.retrieve needs a id to search")
354                         unless defined $ids;
355
356         warn "fleshed copy search for id @$ids\n";
357         my $copy = OpenILS::Application::AppUtils->simple_scalar_request(
358                         "open-ils.storage", 
359                         "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic",
360                         @$ids );
361
362         return $copy;
363 }
364
365
366
367 __PACKAGE__->register_method(
368         method  => "biblio_barcode_to_title",
369         api_name        => "open-ils.search.biblio.find_by_barcode",
370 );
371
372 sub biblio_barcode_to_title {
373         my( $self, $client, $barcode ) = @_;
374
375         if(!$barcode) {
376                 throw OpenSRF::EX::ERROR 
377                         ("Not enough args to find_by_barcode");
378         }
379
380         my $title = $apputils->simple_scalar_request(
381                 "open-ils.storage",
382                 "open-ils.storage.biblio.record_entry.retrieve_by_barcode",
383                 $barcode );
384
385         if($title) {
386                 return { ids => $title->id, count => 1 };
387         } else {
388                 return { count => 0 };
389         }
390
391
392 =head
393         my $u = OpenILS::Utils::ModsParser->new();
394         $u->start_mods_batch( $title->marc );
395         my $mods = $u->finish_mods_batch();
396         $mods->doc_id($title->id());
397         return $mods;
398 =cut
399
400         
401 }
402
403
404 __PACKAGE__->register_method(
405         method  => "biblio_copy_to_mods",
406         api_name        => "open-ils.search.biblio.copy.mods.retrieve",
407 );
408
409 # takes a copy object and returns it fleshed mods object
410 sub biblio_copy_to_mods {
411         my( $self, $client, $copy ) = @_;
412
413         throw OpenSRF::EX::InvalidArgs 
414                 ("copy.mods.retrieve needs a copy") unless( $copy );
415
416         new Fieldmapper::asset::copy($copy);
417
418         my $volume = OpenILS::Application::AppUtils->simple_scalar_request(
419                 "open-ils.storage",
420                 "open-ils.storage.direct.asset.call_number.retrieve",
421                 $copy->call_number() );
422
423         my $mods = _records_to_mods($volume->record());
424         $mods = shift @$mods;
425         $volume->copies([$copy]);
426         push @{$mods->call_numbers()}, $volume;
427
428         return $mods;
429 }
430
431
432 sub barcode_to_mods {
433
434 }
435
436
437 # --------------------------------------------------------------------------------
438
439
440
441 __PACKAGE__->register_method(
442         method  => "cat_biblio_search_class",
443         api_name        => "open-ils.search.cat.biblio.class",
444 );
445
446
447 sub cat_biblio_search_class {
448
449         my( $self, $client, $org_id, $class, $sort, $string ) = @_;
450
451         throw OpenSRF::EX::InvalidArg 
452                 ("Not enough args to open-ils.search.cat.biblio.class")
453                         unless( defined($org_id) and $class and $sort and $string );
454
455
456         my $search_hash;
457
458         my $method = $self->method_lookup("open-ils.search.biblio.marc");
459         if(!$method) {
460                 throw OpenSRF::EX::PANIC 
461                         ("Can't lookup method 'open-ils.search.biblio.marc'");
462         }
463
464         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
465
466         my @ids;
467         for my $i (@$records) { push @ids, $i->[0]; }
468
469         my $mods_list = _records_to_mods( @ids );
470         return undef unless (ref($mods_list) eq "ARRAY");
471
472         # ---------------------------------------------------------------
473         # append copy count information to the mods objects
474         my $session = OpenSRF::AppSession->create("open-ils.storage");
475
476         my $request = $session->request(
477                 "open-ils.storage.direct.biblio.record_copy_count.batch",  $org_id, @ids );
478
479         for my $id (@ids) {
480
481                 warn "receiving copy counts for doc $id\n";
482
483                 my $response = $request->recv();
484                 next unless $response;
485
486                 if( $response and UNIVERSAL::isa($response, "Error")) {
487                         throw $response ($response->stringify);
488                 }
489
490                 my $count = $response->content;
491                 my $mods_obj = undef;
492                 for my $m (@$mods_list) {
493                         $mods_obj = $m if ($m->doc_id() == $id)
494                 }
495                 if($mods_obj) {
496                         $mods_obj->copy_count($count);
497                 }
498
499                 $client->respond( $mods_obj );
500
501         }       
502         $request->finish();
503
504         $session->finish();
505         $session->disconnect();
506         $session->kill_me();
507         # ---------------------------------------------------------------
508
509         return undef;
510 }
511
512
513
514 __PACKAGE__->register_method(
515         method  => "cat_biblio_search_class_id",
516         api_name        => "open-ils.search.cat.biblio.class.id",
517         argc            => 3, 
518         note            => "Searches biblio information by search class and returns the IDs",
519 );
520
521 sub cat_biblio_search_class_id {
522
523         my( $self, $client, $org_id, $class, $string, $limit, $offset ) = @_;
524
525         $offset ||= 0;
526         $limit  ||= 100;
527         $limit -= 1;
528
529
530         my $bool = ($class eq "subject" || $class eq "keyword");
531         $string = OpenILS::Application::Search->filter_search($string, $bool);
532
533         if(!$string) { 
534                 return OpenILS::EX->new("SEARCH_TOO_LARGE")->ex();
535         }
536
537         warn "Searching cat.biblio.class.id string: $string offset: $offset limit: $limit\n";
538
539         throw OpenSRF::EX::InvalidArg 
540                 ("Not enough args to open-ils.search.cat.biblio.class")
541                         unless( defined($org_id) and $class and $string );
542
543
544         my $search_hash;
545
546         my $cache_key = md5_hex( $org_id . $class . $string );
547         my $id_array = OpenILS::Application::SearchCache->get_cache($cache_key);
548
549         if(ref($id_array)) {
550                 warn "Returning class search from cache\n";
551                 my $size = @$id_array;
552                 my @ids = @$id_array[ $offset..($offset+$limit) ];
553                 return { count => $size, ids => \@ids };
554         }
555
556         my $method = $self->method_lookup("open-ils.search.biblio.marc");
557         if(!$method) {
558                 throw OpenSRF::EX::PANIC 
559                         ("Can't lookup method 'open-ils.search.biblio.marc'");
560         }
561
562         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
563
564         my @cache_ids;
565
566         for my $i (@$records) { 
567                 if(defined($i->[0])) {
568                         push @cache_ids, $i->[0]; 
569                 }
570         }
571
572         my @ids = @cache_ids[ $offset..($offset+$limit) ];
573         my $size = @$records;
574
575         OpenILS::Application::SearchCache->put_cache( 
576                         $cache_key, \@cache_ids, $size );
577
578         return { count =>$size, ids => \@ids };
579
580 }
581
582 __PACKAGE__->register_method(
583         method  => "biblio_search_class_count",
584         api_name        => "open-ils.search.biblio.class.count",
585 );
586
587 __PACKAGE__->register_method(
588         method  => "biblio_search_class_count",
589         api_name        => "open-ils.search.biblio.class.count.staff",
590 );
591
592 sub biblio_search_class_count {
593
594         my( $self, $client, $class, $string, $org_id, $org_type ) = @_;
595
596         warn "org: $org_id : depth: $org_type\n";
597
598         $org_id         = "1" unless defined($org_id); # xxx
599         $org_type       = 0     unless defined($org_type);
600
601         warn "Searching biblio.class.id\n" . 
602                 "string: $string "              . 
603                 "org_id: $org_id\n"             .
604                 "depth: $org_type\n" ;
605
606         my $bool = ($class eq "subject" || $class eq "keyword");
607         $string = OpenILS::Application::Search->filter_search($string, $bool);
608
609         if(!$string) { 
610                 return OpenILS::EX->new("SEARCH_TOO_LARGE")->ex;
611         }
612
613                 
614         if( !defined($org_id) or !$class or !$string ) {
615                 warn "not enbough args to metarecord search\n";
616                 throw OpenSRF::EX::InvalidArg 
617                         ("Not enough args to open-ils.search.cat.biblio.class")
618         }
619
620         $class =~ s/\s+//g;
621
622         if( ($class ne "title") and ($class ne "author") and 
623                 ($class ne "subject") and ($class ne "keyword") 
624                 and ($class ne "series"  )) {
625                 warn "Invalid search class: $class\n";
626                 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
627         }
628
629         # grab the mr id's from storage
630
631         my $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord_count";
632         if($self->api_name =~ /staff/) { 
633                 $method = "$method.staff"; 
634                 $method =~ s/\.cachable//o;
635         }
636         warn "Performing count method $method\n";
637         warn "API name " . $self->api_name() . "\n";
638
639         my $session = OpenSRF::AppSession->create('open-ils.storage');
640
641         my $request = $session->request( $method, 
642                         term => $string, 
643                         org_unit => $org_id, 
644                         cache_page_size => 1,
645                         depth =>$org_type );
646
647         my $count = $request->gather(1);
648         warn "Received count $count\n";
649
650         return $count;
651 }
652
653
654 __PACKAGE__->register_method(
655         method  => "biblio_search_class",
656         api_name        => "open-ils.search.biblio.class",
657 );
658
659 __PACKAGE__->register_method(
660         method  => "biblio_search_class",
661         api_name        => "open-ils.search.biblio.class.unordered",
662 );
663
664 __PACKAGE__->register_method(
665         method  => "biblio_search_class",
666         api_name        => "open-ils.search.biblio.class.staff",
667 );
668
669 __PACKAGE__->register_method(
670         method  => "biblio_search_class",
671         api_name        => "open-ils.search.biblio.class.unordered.staff",
672 );
673
674 sub biblio_search_class {
675
676         my( $self, $client, $class, $string, 
677                         $org_id, $org_type, $limit, $offset ) = @_;
678
679         warn "org: $org_id : depth: $org_type : limit: $limit :  offset: $offset\n";
680
681         $offset         ||= 0;
682         $limit          = 100 unless defined($limit and $limit > 0 );
683         $org_id         = "1" unless defined($org_id); # xxx
684         $org_type       = 0     unless defined($org_type);
685
686         warn "Searching biblio.class.id\n" . 
687                 "string: $string "              . 
688                 "\noffset: $offset\n"   .
689                 "limit: $limit\n"                       .
690                 "org_id: $org_id\n"             .
691                 "depth: $org_type\n" ;
692
693         warn "Search filtering string " . time() . "\n";
694         $string = OpenILS::Application::Search->filter_search($string);
695         if(!$string) { return undef; }
696
697         if( !defined($org_id) or !$class or !$string ) {
698                 warn "not enbough args to metarecord search\n";
699                 throw OpenSRF::EX::InvalidArg 
700                         ("Not enough args to open-ils.search.cat.biblio.class")
701         }
702
703         $class =~ s/\s+//g;
704
705         if( ($class ne "title") and ($class ne "author") and 
706                 ($class ne "subject") and ($class ne "keyword") 
707                 and ($class ne "series") ) {
708                 warn "Invalid search class: $class\n";
709                 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
710         }
711
712         #my $method = "open-ils.storage.metabib.$class.search_fts.metarecord.atomic";
713         my $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord.atomic";
714
715         if($self->api_name =~ /order/) {
716                 $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord.unordered.atomic",
717                 #$method = "open-ils.storage.metabib.$class.search_fts.metarecord.unordered.atomic";
718         }
719
720         if($self->api_name =~ /staff/) { 
721                 $method =~ s/atomic/staff\.atomic/og;
722                 $method =~ s/\.cachable//o;
723         }
724
725         warn "Performing search method $method\n";
726         warn "MR search method is $method\n";
727
728         my $session = OpenSRF::AppSession->create('open-ils.storage');
729
730         warn "Search making request " . time() . "\n";
731         my $request = $session->request(        
732                 $method,
733                 term            => $string, 
734                 org_unit => $org_id, 
735                 depth           => $org_type, 
736                 limit           => $limit,
737                 offset  => $offset,
738                 cache_page_size => 200,
739                 );
740
741         my $records = $request->gather(1);
742         warn "Search request complete " . time() . "\n";
743
744         my @all_ids;
745
746         use Data::Dumper;
747         warn "Received " . scalar(@$records) . " id's from class search\n";
748
749         # if we just get one, it won't be wrapped in an array
750         if(!ref($records->[0])) {
751                 $records = [$records]; } 
752         for my $i (@$records) { 
753                 if(defined($i)) {
754                         push @all_ids, $i; 
755                 }
756         }
757
758         my @ids = @all_ids;
759         @ids = grep { defined($_->[0]) } @ids;
760
761         $session->finish();
762         $session->disconnect();
763
764         return { ids => \@ids };
765
766 }
767
768
769
770
771 __PACKAGE__->register_method(
772         method  => "biblio_mrid_to_modsbatch",
773         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
774 );
775
776 sub biblio_mrid_to_modsbatch {
777         my( $self, $client, $mrid ) = @_;
778
779         throw OpenSRF::EX::InvalidArg 
780                 ("search.biblio.metarecord_to_mods requires mr id")
781                         unless defined( $mrid );
782
783
784         my $metarecord = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
785                         "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
786
787         if(!$metarecord) {
788                 throw OpenSRF::EX::ERROR ("No metarecord exists with the given id: $mrid");
789         }
790
791         my $master_id = $metarecord->master_record();
792
793
794         # check for existing mods
795         if($metarecord->mods()){
796                 warn "We already have mods for " . $metarecord->id . "\n";
797                 my $perl = JSON->JSON2perl($metarecord->mods());
798                 return Fieldmapper::metabib::virtual_record->new($perl);
799         }
800
801
802
803         warn "Creating mods batch for metarecord $mrid\n";
804         my $id_hash = biblio_mrid_to_record_ids( undef, undef,  $mrid );
805         my @ids = @{$id_hash->{ids}};
806
807         if(@ids < 1) { return undef; }
808
809         warn "Master ID is $master_id\n";
810         # grab the master record to start the mods batch 
811
812         my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
813                         "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
814
815         if(!$record) {
816                 throw OpenSRF::EX::ERROR 
817                         ("No record returned with id $master_id");
818         }
819
820         my $u = OpenILS::Utils::ModsParser->new();
821         use Data::Dumper;
822         $u->start_mods_batch( $record->marc );
823         my $main_doc_id = $record->id();
824
825         @ids = grep { $_ ne $master_id } @ids;
826
827         # now we have to collect all of the marc objects and push them into a mods batch
828         my $session = OpenSRF::AppSession->create("open-ils.storage");
829         my $request = $session->request(
830                 "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
831
832         while( my $response = $request->recv() ) {
833
834                 next unless $response;
835                 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
836                         throw $response ($response->stringify);
837                 }
838
839                 my $content = $response->content;
840
841                 if( $content ) {
842                         $u->push_mods_batch( $content->marc );
843                 }
844         }
845
846         my $mods = $u->finish_mods_batch();
847         $mods->doc_id($mrid);
848         $request->finish();
849
850         $client->respond_complete($mods);
851
852         my $mods_string = JSON->perl2JSON($mods->decast);
853
854         $metarecord->mods($mods_string);
855
856         my $req = $session->request( 
857                         "open-ils.storage.direct.metabib.metarecord.update", 
858                         $metarecord );
859
860
861         $req->gather(1);
862
863         $session->finish();
864         $session->disconnect();
865
866         return undef;
867
868 }
869
870
871
872 # converts a mr id into a list of record ids
873
874 __PACKAGE__->register_method(
875         method  => "biblio_mrid_to_record_ids",
876         api_name        => "open-ils.search.biblio.metarecord_to_records",
877 );
878
879 sub biblio_mrid_to_record_ids {
880         my( $self, $client, $mrid ) = @_;
881
882         throw OpenSRF::EX::InvalidArg 
883                 ("search.biblio.metarecord_to_record_ids requires mr id")
884                         unless defined( $mrid );
885
886         warn "Searching for record for MR $mrid\n";
887
888         my $mrmaps = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
889                         "open-ils.storage.direct.metabib.metarecord_source_map.search.metarecord", $mrid );
890
891         my @ids;
892         for my $map (@$mrmaps) { push @ids, $map->source(); }
893
894         warn "Recovered id's [@ids] for mr $mrid\n";
895
896         my $size = @ids;
897
898         return { count => $size, ids => \@ids };
899
900 }
901
902
903
904 __PACKAGE__->register_method(
905         method  => "biblio_record_to_marc_html",
906         api_name        => "open-ils.search.biblio.record.html" );
907
908 my $parser              = XML::LibXML->new();
909 my $xslt                        = XML::LibXSLT->new();
910 my $marc_sheet;
911
912 my $settings_client = OpenSRF::Utils::SettingsClient->new();
913 sub biblio_record_to_marc_html {
914         my( $self, $client, $recordid ) = @_;
915
916         if( !$marc_sheet ) {
917                 my $dir = $settings_client->config_value( "dirs", "xsl" );
918                 my $xsl = $settings_client->config_value(
919                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
920
921                 $xsl = $parser->parse_file("$dir/$xsl");
922                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
923         }
924
925
926         my $record = $apputils->simple_scalar_request(
927                 "open-ils.storage", 
928                 "open-ils.storage.direct.biblio.record_entry.retrieve",
929                 $recordid );
930
931         my $xmldoc = $parser->parse_string($record->marc);
932         my $html = $marc_sheet->transform($xmldoc);
933         $html = $html->toString();
934         return $html;
935
936 }
937
938
939
940 __PACKAGE__->register_method(
941         method  => "retrieve_all_copy_locations",
942         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
943
944 my $shelving_locations;
945 sub retrieve_all_copy_locations {
946         my( $self, $client ) = @_;
947         if(!$shelving_locations) {
948                 $shelving_locations = $apputils->simple_scalar_request(
949                         "open-ils.storage", 
950                         "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
951         }
952         return $shelving_locations;
953 }
954
955
956
957 __PACKAGE__->register_method(
958         method  => "retrieve_all_copy_statuses",
959         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
960
961 my $copy_statuses;
962 sub retrieve_all_copy_statuses {
963         my( $self, $client ) = @_;
964         if(!$copy_statuses) {
965                 $copy_statuses = $apputils->simple_scalar_request(
966                         "open-ils.storage",
967                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
968         }
969         return $copy_statuses;
970 }
971
972
973
974
975
976 1;