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