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