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