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