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