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