]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
started opac details page..
[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         if( !defined($org_id) or !$class or !$string ) {
557                 warn "not enbough args to metarecord search\n";
558                 throw OpenSRF::EX::InvalidArg 
559                         ("Not enough args to open-ils.search.cat.biblio.class")
560         }
561
562         $class =~ s/\s+//g;
563
564         if( ($class ne "title") and ($class ne "author") and 
565                 ($class ne "subject") and ($class ne "keyword") 
566                 and ($class ne "series"  )) {
567                 warn "Invalid search class: $class\n";
568                 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
569         }
570
571         # grab the mr id's from storage
572
573         my $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord_count";
574         if($self->api_name =~ /staff/) { 
575                 $method = "$method.staff"; 
576                 $method =~ s/\.cachable//o;
577         }
578         warn "Performing count method $method\n";
579         warn "API name " . $self->api_name() . "\n";
580
581         my $session = OpenSRF::AppSession->create('open-ils.storage');
582
583         my $request = $session->request( $method, 
584                         term                                    => $string, 
585                         org_unit                                => $org_id, 
586                         cache_page_size => 1,
587                         depth                                   => $org_type,
588                         format                          => $format );
589
590         my $count = $request->gather(1);
591         warn "Received count $count\n";
592
593         return $count;
594 }
595
596
597 __PACKAGE__->register_method(
598         method  => "biblio_search_class",
599         api_name        => "open-ils.search.biblio.class",
600 );
601
602 __PACKAGE__->register_method(
603         method  => "biblio_search_class",
604         api_name        => "open-ils.search.biblio.class.full",
605 );
606
607 __PACKAGE__->register_method(
608         method  => "biblio_search_class",
609         api_name        => "open-ils.search.biblio.class.full.staff",
610 );
611
612 #__PACKAGE__->register_method(
613 #       method  => "biblio_search_class",
614 #       api_name        => "open-ils.search.biblio.class.unordered",
615 #);
616
617 __PACKAGE__->register_method(
618         method  => "biblio_search_class",
619         api_name        => "open-ils.search.biblio.class.staff",
620 );
621
622 #__PACKAGE__->register_method(
623 #       method  => "biblio_search_class",
624 #       api_name        => "open-ils.search.biblio.class.unordered.staff",
625 #);
626
627 sub biblio_search_class {
628
629         my( $self, $client, $class, $string, 
630                         $org_id, $org_type, $limit, $offset, $format ) = @_;
631
632         warn "org: $org_id : depth: $org_type : limit: $limit :  offset: $offset\n";
633
634
635 # new method:  metabib.<class>.new_search_fts.metarecord.<staff>
636
637
638         $offset         = 0     unless (defined($offset) and $offset > 0);
639         $limit          = 100 unless (defined($limit) and $limit > 0);
640         $org_id         = "1" unless (defined($org_id)); # xxx
641         $org_type       = 0     unless (defined($org_type));
642
643         warn "Searching biblio.class.id\n" . 
644                 "string: $string "              . 
645                 "\noffset: $offset\n"   .
646                 "limit: $limit\n"                       .
647                 "org_id: $org_id\n"             .
648                 "depth: $org_type\n"            .
649                 "format: $format\n";
650
651         warn "Search filtering string " . time() . "\n";
652         $string = OpenILS::Application::Search->filter_search($string);
653         if(!$string) { return undef; }
654
655         if( !defined($org_id) or !$class or !$string ) {
656                 warn "not enbough args to metarecord search\n";
657                 throw OpenSRF::EX::InvalidArg 
658                         ("Not enough args to open-ils.search.cat.biblio.class")
659         }
660
661         $class =~ s/\s+//g;
662
663         if( ($class ne "title") and ($class ne "author") and 
664                 ($class ne "subject") and ($class ne "keyword") 
665                 and ($class ne "series") ) {
666                 warn "Invalid search class: $class\n";
667                 throw OpenSRF::EX::InvalidArg ("Not a valid search class: $class")
668         }
669
670         my $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord.atomic";
671
672 #       if($self->api_name =~ /order/) {
673 #               $method = "open-ils.storage.cachable.metabib.$class.search_fts.metarecord.unordered.atomic",
674 #       }
675
676         if($self->api_name =~ /staff/) { 
677                 $method =~ s/atomic/staff\.atomic/og;
678                 $method =~ s/\.cachable//o;
679         }
680
681         if($self->api_name =~ /full/) { 
682                 $method =~ s/search_fts/new_search_fts/o;
683                 $method =~ s/\.cachable//o; #XXX testing..
684         }
685
686         warn "Performing search method $method\n";
687         warn "MR search method is $method\n";
688
689         my $session = OpenSRF::AppSession->create('open-ils.storage');
690
691         warn "Search making request " . time() . "\n";
692         my $request = $session->request(        
693                 $method,
694                 term            => $string, 
695                 org_unit => $org_id, 
696                 depth           => $org_type, 
697                 limit           => $limit,
698                 offset  => $offset,
699                 format  => $format,
700                 cache_page_size => 200,
701                 );
702
703         my $records = $request->gather(1);
704         if(!$records) {return { ids => [] }};
705
706         warn "Search request complete " . time() . "\n";
707
708         my @all_ids;
709
710         use Data::Dumper;
711         warn "Received " . scalar(@$records) . " id's from class search\n";
712
713         # if we just get one, it won't be wrapped in an array
714
715         if(!ref($records->[0])) {
716                 $records = [$records]; } 
717         for my $i (@$records) { 
718                 if(defined($i)) {
719                         push @all_ids, $i; 
720                 }
721         }
722
723         my @ids = @all_ids;
724         @ids = grep { defined($_->[0]) } @ids;
725
726         $session->finish();
727         $session->disconnect();
728
729         my $count = undef;
730         if($self->api_name =~ /full/) { 
731                 if(ref($records) && $records->[0]) {
732                         $count = $records->[0]->[3];
733                 }
734         }
735
736         return { ids => \@ids, count => $count };
737
738 }
739
740
741
742
743 __PACKAGE__->register_method(
744         method  => "biblio_mrid_to_modsbatch_batch",
745         api_name        => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
746
747 sub biblio_mrid_to_modsbatch_batch {
748         my( $self, $client, $mrids) = @_;
749         warn "Performing mrid_to_modsbatch_batch...";
750         my @mods;
751         my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
752         use Data::Dumper;
753         warn "Grabbing mods for " . Dumper($mrids) . "\n";
754
755         for my $id (@$mrids) {
756                 next unless defined $id;
757                 #push @mods, biblio_mrid_to_modsbatch($self, $client, $id);
758                 my ($m) = $method->run($id);
759                 push @mods, $m;
760         }
761         return \@mods;
762 }
763
764
765 __PACKAGE__->register_method(
766         method  => "biblio_mrid_to_modsbatch",
767         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve");
768
769 __PACKAGE__->register_method(
770         method  => "biblio_mrid_to_modsbatch",
771         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff");
772
773 sub biblio_mrid_to_modsbatch {
774         my( $self, $client, $mrid ) = @_;
775
776         throw OpenSRF::EX::InvalidArg 
777                 ("search.biblio.metarecord_to_mods requires mr id")
778                         unless defined( $mrid );
779
780
781         my $metarecord = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
782                         "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
783
784         if(!$metarecord) {
785                 throw OpenSRF::EX::ERROR ("No metarecord exists with the given id: $mrid");
786         }
787
788         my $master_id = $metarecord->master_record();
789
790
791         # check for existing mods
792         if($metarecord->mods()){
793                 warn "We already have mods for " . $metarecord->id . "\n";
794                 my $perl = JSON->JSON2perl($metarecord->mods());
795                 return Fieldmapper::metabib::virtual_record->new($perl);
796         }
797
798
799
800         warn "Creating mods batch for metarecord $mrid\n";
801         my $meth = "open-ils.search.biblio.metarecord_to_records";
802         if( $self->api_name =~ /staff/ ) { $meth .= ".staff";}
803         $meth = $self->method_lookup($meth);
804
805         my ($id_hash) = $meth->run($mrid);
806
807         my @ids = @{$id_hash->{ids}};
808
809         if(@ids < 1) { return undef; }
810
811         warn "Master ID is $master_id\n";
812         # grab the master record to start the mods batch 
813
814         $meth = "open-ils.storage.direct.biblio.record_entry.retrieve";
815
816         my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
817                         "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
818
819         if(!$record) {
820                 throw OpenSRF::EX::ERROR 
821                         ("No record returned with id $master_id");
822         }
823
824         my $u = OpenILS::Utils::ModsParser->new();
825         use Data::Dumper;
826         $u->start_mods_batch( $record->marc );
827         my $main_doc_id = $record->id();
828
829         @ids = grep { $_ ne $master_id } @ids;
830
831         # now we have to collect all of the marc objects and push them into a mods batch
832         my $session = OpenSRF::AppSession->create("open-ils.storage");
833         my $request = $session->request(
834                 "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
835
836         while( my $response = $request->recv() ) {
837
838                 next unless $response;
839                 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
840                         throw $response ($response->stringify);
841                 }
842
843                 my $content = $response->content;
844
845                 if( $content ) {
846                         $u->push_mods_batch( $content->marc );
847                 }
848         }
849
850         my $mods = $u->finish_mods_batch();
851         $mods->doc_id($mrid);
852         $request->finish();
853
854         $client->respond_complete($mods);
855
856         my $mods_string = JSON->perl2JSON($mods->decast);
857
858         $metarecord->mods($mods_string);
859
860         my $req = $session->request( 
861                         "open-ils.storage.direct.metabib.metarecord.update", 
862                         $metarecord );
863
864
865         $req->gather(1);
866
867         $session->finish();
868         $session->disconnect();
869
870         return undef;
871
872 }
873
874
875
876 # converts a mr id into a list of record ids
877
878 __PACKAGE__->register_method(
879         method  => "biblio_mrid_to_record_ids",
880         api_name        => "open-ils.search.biblio.metarecord_to_records",
881 );
882
883 __PACKAGE__->register_method(
884         method  => "biblio_mrid_to_record_ids",
885         api_name        => "open-ils.search.biblio.metarecord_to_records.staff",
886 );
887
888 sub biblio_mrid_to_record_ids {
889         my( $self, $client, $mrid, $format ) = @_;
890
891         throw OpenSRF::EX::InvalidArg 
892                 ("search.biblio.metarecord_to_record_ids requires mr id")
893                         unless defined( $mrid );
894
895         warn "Searching for record for MR $mrid and format $format\n";
896
897         my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
898         if($self and $self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
899         warn "Performing record retrieval with method $method\n";
900
901
902         my $mrmaps = OpenILS::Application::AppUtils->simple_scalar_request( 
903                         "open-ils.storage", $method, $mrid, $format );
904
905         use Data::Dumper;
906         warn Dumper $mrmaps;
907
908         my $size = @$mrmaps;    
909
910         return { count => $size, ids => $mrmaps };
911
912 }
913
914
915 __PACKAGE__->register_method(
916         method  => "biblio_record_to_marc_html",
917         api_name        => "open-ils.search.biblio.record.html" );
918
919 my $parser              = XML::LibXML->new();
920 my $xslt                        = XML::LibXSLT->new();
921 my $marc_sheet;
922
923 my $settings_client = OpenSRF::Utils::SettingsClient->new();
924 sub biblio_record_to_marc_html {
925         my( $self, $client, $recordid ) = @_;
926
927         if( !$marc_sheet ) {
928                 my $dir = $settings_client->config_value( "dirs", "xsl" );
929                 my $xsl = $settings_client->config_value(
930                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
931
932                 $xsl = $parser->parse_file("$dir/$xsl");
933                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
934         }
935
936
937         my $record = $apputils->simple_scalar_request(
938                 "open-ils.storage", 
939                 "open-ils.storage.direct.biblio.record_entry.retrieve",
940                 $recordid );
941
942         my $xmldoc = $parser->parse_string($record->marc);
943         my $html = $marc_sheet->transform($xmldoc);
944         $html = $html->toString();
945         return $html;
946
947 }
948
949
950
951 __PACKAGE__->register_method(
952         method  => "retrieve_all_copy_locations",
953         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
954
955 my $shelving_locations;
956 sub retrieve_all_copy_locations {
957         my( $self, $client ) = @_;
958         if(!$shelving_locations) {
959                 $shelving_locations = $apputils->simple_scalar_request(
960                         "open-ils.storage", 
961                         "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
962         }
963         return $shelving_locations;
964 }
965
966
967
968 __PACKAGE__->register_method(
969         method  => "retrieve_all_copy_statuses",
970         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
971
972 my $copy_statuses;
973 sub retrieve_all_copy_statuses {
974         my( $self, $client ) = @_;
975         if(!$copy_statuses) {
976                 $copy_statuses = $apputils->simple_scalar_request(
977                         "open-ils.storage",
978                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
979         }
980         return $copy_statuses;
981 }
982
983
984 __PACKAGE__->register_method(
985         method  => "copy_counts_per_org",
986         api_name        => "open-ils.search.biblio.copy_counts.retrieve");
987
988 __PACKAGE__->register_method(
989         method  => "copy_counts_per_org",
990         api_name        => "open-ils.search.biblio.copy_counts.retrieve.staff");
991
992 sub copy_counts_per_org {
993         my( $self, $client, $record_id ) = @_;
994
995         warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
996
997         my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
998         if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
999
1000         my $counts = $apputils->simple_scalar_request(
1001                 "open-ils.storage", $method, $record_id );
1002
1003         $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1004         return $counts;
1005 }
1006
1007
1008 __PACKAGE__->register_method(
1009         method          => "copy_count_summary",
1010         api_name        => "open-ils.search.biblio.copy_counts.summary.retrieve",
1011         notes           => <<"  NOTES");
1012         returns an array of these:
1013                 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1014                 where statusx is a copy status name.  the statuses are sorted
1015                 by id.
1016         NOTES
1017
1018 sub copy_count_summary {
1019         my( $self, $client, $rid ) = @_;
1020         my $method = "open-ils.storage.biblio.record_entry.status_copy_count.atomic";
1021         return $apputils->simple_scalar_request( "open-ils.storage", $method, $rid );
1022 }
1023
1024
1025
1026
1027
1028
1029
1030
1031 1;