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