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