]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
cleaned up mods batch creator
[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 use OpenILS::Utils::Editor;
12
13 use OpenSRF::Utils::Logger qw/:logger/;
14
15
16 use JSON;
17
18 use Time::HiRes qw(time);
19 use OpenSRF::EX qw(:try);
20 use Digest::MD5 qw(md5_hex);
21
22 use XML::LibXML;
23 use XML::LibXSLT;
24
25 use Data::Dumper;
26 $Data::Dumper::Indent = 0;
27
28 use OpenILS::Application::AppUtils;
29 my $apputils = "OpenILS::Application::AppUtils";
30 my $U = $apputils;
31
32
33 # useful for MARC based searches
34 my $cat_search_hash =  {
35         isbn    => [ { tag => '020', subfield => 'a' }, ],
36         issn    => [ { tag => '022', subfield => 'a' }, ],
37 };
38
39
40
41
42 # ---------------------------------------------------------------------------
43 # takes a list of record id's and turns the docs into friendly 
44 # mods structures. Creates one MODS structure for each doc id.
45 # ---------------------------------------------------------------------------
46 sub _records_to_mods {
47         my @ids = @_;
48         
49         my @results;
50         my @marcxml_objs;
51
52         my $session = OpenSRF::AppSession->create("open-ils.storage");
53         my $request = $session->request(
54                         "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
55
56         while( my $resp = $request->recv ) {
57                 my $content = $resp->content;
58                 my $u = OpenILS::Utils::ModsParser->new();
59                 $u->start_mods_batch( $content->marc );
60                 my $mods = $u->finish_mods_batch();
61                 $mods->doc_id($content->id());
62                 $mods->tcn($content->tcn_value);
63                 push @results, $mods;
64         }
65
66         $session->disconnect();
67         return \@results;
68 }
69
70 __PACKAGE__->register_method(
71         method  => "record_id_to_mods",
72         api_name        => "open-ils.search.biblio.record.mods.retrieve",
73         argc            => 1, 
74         note            => "Provide ID, we provide the mods"
75 );
76
77 # converts a record into a mods object with copy counts attached
78 sub record_id_to_mods {
79
80         my( $self, $client, $org_id, $id ) = @_;
81
82         my $mods_list = _records_to_mods( $id );
83         my $mods_obj = $mods_list->[0];
84         my $cmethod = $self->method_lookup(
85                         "open-ils.search.biblio.record.copy_count");
86         my ($count) = $cmethod->run($org_id, $id);
87         $mods_obj->copy_count($count);
88
89         return $mods_obj;
90 }
91
92
93
94 __PACKAGE__->register_method(
95         method  => "record_id_to_mods_slim",
96         api_name        => "open-ils.search.biblio.record.mods_slim.retrieve",
97         argc            => 1, 
98         note            => "Provide ID, we provide the mods"
99 );
100
101 # converts a record into a mods object with NO copy counts attached
102 sub record_id_to_mods_slim {
103         my( $self, $client, $id ) = @_;
104         return undef unless defined $id;
105
106         if(ref($id) and ref($id) == 'ARRAY') {
107                 return _records_to_mods( @$id );
108         }
109         my $mods_list = _records_to_mods( $id );
110         my $mods_obj = $mods_list->[0];
111         return $mods_obj;
112 }
113
114
115 # Returns the number of copies attached to a record based on org location
116 __PACKAGE__->register_method(
117         method  => "record_id_to_copy_count",
118         api_name        => "open-ils.search.biblio.record.copy_count",
119 );
120
121 __PACKAGE__->register_method(
122         method  => "record_id_to_copy_count",
123         api_name        => "open-ils.search.biblio.metarecord.copy_count",
124 );
125
126 __PACKAGE__->register_method(
127         method  => "record_id_to_copy_count",
128         api_name        => "open-ils.search.biblio.metarecord.copy_count.staff",
129 );
130 sub record_id_to_copy_count {
131         my( $self, $client, $org_id, $record_id, $format ) = @_;
132
133         $format = undef if (!$format or $format eq 'all');
134
135         my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
136         my $key = "record";
137         if($self->api_name =~ /metarecord/) {
138                 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
139                 $key = "metarecord";
140         }
141
142         if($self->api_name =~ /staff/ ) {
143                 $method =~ s/atomic/staff\.atomic/og;
144                 warn "Doing staff search $method\n";
145         }
146
147
148         my $session = OpenSRF::AppSession->create("open-ils.storage");
149         warn "copy_count retrieve $record_id\n";
150         return undef unless(defined $record_id);
151
152         my $request = $session->request(
153                 $method, org_unit => $org_id => $key => $record_id, format => $format );
154
155
156         my $count = $request->gather(1);
157         $session->disconnect();
158         return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
159
160 }
161
162
163
164
165 __PACKAGE__->register_method(
166         method  => "biblio_search_tcn",
167         api_name        => "open-ils.search.biblio.tcn",
168         argc            => 3, 
169         note            => "Retrieve a record by TCN",
170 );
171
172 sub biblio_search_tcn {
173
174         my( $self, $client, $tcn ) = @_;
175
176         $tcn =~ s/.*?(\w+)\s*$/$1/o;
177         warn "Searching TCN $tcn\n";
178
179         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
180         my $request = $session->request( 
181                         "open-ils.storage.direct.biblio.record_entry.search_where.atomic", 
182                         { tcn_value => $tcn, deleted => 'f' } );
183                         #"open-ils.storage.direct.biblio.record_entry.search.tcn_value.atomic", $tcn );
184         my $record_entry = $request->gather(1);
185
186         my @ids;
187         for my $record (@$record_entry) {
188                 push @ids, $record->id;
189         }
190
191         $session->disconnect();
192
193         my $size = @ids;
194         return { count => $size, ids => \@ids };
195 }
196
197
198 # --------------------------------------------------------------------------------
199
200 __PACKAGE__->register_method(
201         method  => "biblio_barcode_to_copy",
202         api_name        => "open-ils.search.asset.copy.find_by_barcode",);
203 sub biblio_barcode_to_copy { 
204         my( $self, $client, $barcode ) = @_;
205         my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
206         return $evt if $evt;
207         return $copy;
208 }
209
210 __PACKAGE__->register_method(
211         method  => "biblio_id_to_copy",
212         api_name        => "open-ils.search.asset.copy.batch.retrieve",);
213 sub biblio_id_to_copy { 
214         my( $self, $client, $ids ) = @_;
215         $logger->info("Fetching copies @$ids");
216         return $U->storagereq(
217                 "open-ils.storage.direct.asset.copy.batch.retrieve.atomic", @$ids );
218 }
219
220
221 __PACKAGE__->register_method(
222         method  => "copy_retrieve", 
223         api_name        => "open-ils.search.asset.copy.retrieve",);
224 sub copy_retrieve {
225         my( $self, $client, $cid ) = @_;
226         my( $copy, $evt ) = $U->fetch_copy($cid);
227         return $evt if $evt;
228         return $copy;
229 }
230
231
232 __PACKAGE__->register_method(
233         method  => "fleshed_copy_retrieve_batch",
234         api_name        => "open-ils.search.asset.copy.fleshed.batch.retrieve");
235
236 sub fleshed_copy_retrieve_batch { 
237         my( $self, $client, $ids ) = @_;
238         $logger->info("Fetching fleshed copies @$ids");
239         return $U->storagereq(
240                 "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic", @$ids );
241 }
242
243
244 __PACKAGE__->register_method(
245         method  => "fleshed_copy_retrieve",
246         api_name        => "open-ils.search.asset.copy.fleshed.retrieve",);
247
248 sub fleshed_copy_retrieve { 
249         my( $self, $client, $id ) = @_;
250         my( $c, $e) = $U->fetch_fleshed_copy($id);
251         return $e if $e;
252         return $c;
253 }
254
255
256
257 __PACKAGE__->register_method(
258         method  => "biblio_barcode_to_title",
259         api_name        => "open-ils.search.biblio.find_by_barcode",
260 );
261
262 sub biblio_barcode_to_title {
263         my( $self, $client, $barcode ) = @_;
264
265         my $title = $apputils->simple_scalar_request(
266                 "open-ils.storage",
267                 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
268
269         return { ids => [ $title->id ], count => 1 } if $title;
270         return { count => 0 };
271 }
272
273
274 __PACKAGE__->register_method(
275         method  => "biblio_copy_to_mods",
276         api_name        => "open-ils.search.biblio.copy.mods.retrieve",
277 );
278
279 # takes a copy object and returns it fleshed mods object
280 sub biblio_copy_to_mods {
281         my( $self, $client, $copy ) = @_;
282
283         my $volume = $U->storagereq( 
284                 "open-ils.storage.direct.asset.call_number.retrieve",
285                 $copy->call_number() );
286
287         my $mods = _records_to_mods($volume->record());
288         $mods = shift @$mods;
289         $volume->copies([$copy]);
290         push @{$mods->call_numbers()}, $volume;
291
292         return $mods;
293 }
294
295
296 # ----------------------------------------------------------------------------
297 # These are the main OPAC search methods
298 # ----------------------------------------------------------------------------
299
300 __PACKAGE__->register_method(
301         method          => 'the_quest_for_knowledge',
302         api_name                => 'open-ils.search.biblio.multiclass',
303         signature       => q/
304                 Performs a multi class bilbli or metabib search
305                 @param searchhash A search object layed out like so:
306                         searches : { "$class" : "$value", ...}
307                         org_unit : The org id to focus the search at
308                         depth           : The org depth
309                         limit           : The search limit
310                         offset  : The search offset
311                         format  : The MARC format
312                         sort            : What field to sort the results on [ author | title | pubdate ]
313                         sort_dir        : What direction do we sort? [ asc | desc ]
314                 @return An object of the form 
315                         { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
316         /
317 );
318
319 __PACKAGE__->register_method(
320         method          => 'the_quest_for_knowledge',
321         api_name                => 'open-ils.search.record.multiclass.staff',
322         signature       => q/@see open-ils.search.biblio.multiclass/);
323 __PACKAGE__->register_method(
324         method          => 'the_quest_for_knowledge',
325         api_name                => 'open-ils.search.metabib.multiclass',
326         signature       => q/@see open-ils.search.biblio.multiclass/);
327 __PACKAGE__->register_method(
328         method          => 'the_quest_for_knowledge',
329         api_name                => 'open-ils.search.metabib.multiclass.staff',
330         signature       => q/@see open-ils.search.biblio.multiclass/);
331
332
333 sub the_quest_for_knowledge {
334         my( $self, $conn, $searchhash ) = @_;
335
336         my $method = 'open-ils.storage.biblio.multiclass.search_fts';
337         my $ismeta = 0;
338         my @recs;
339
340         if($self->api_name =~ /metabib/) {
341                 $ismeta = 1;
342                 $method =~ s/biblio/metabib/o;
343         }
344
345         $method .= ".staff" if($self->api_name =~ /staff/);
346         $method .= ".atomic";
347
348         for (keys %$searchhash) { 
349                 delete $$searchhash{$_} unless defined $$searchhash{$_}; }
350
351         my $result = $U->storagereq( $method, %$searchhash );
352
353         return {count => 0} unless ($result && $$result[0]);
354
355         for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
356         return { ids => \@recs, 
357                 count => ($ismeta) ? $result->[0]->[3] : $result->[0]->[2] };
358 }
359
360
361
362
363
364
365
366
367 __PACKAGE__->register_method(
368         method  => "biblio_mrid_to_modsbatch_batch",
369         api_name        => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
370
371 sub biblio_mrid_to_modsbatch_batch {
372         my( $self, $client, $mrids) = @_;
373         warn "Performing mrid_to_modsbatch_batch...";
374         my @mods;
375         my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
376         for my $id (@$mrids) {
377                 next unless defined $id;
378                 my ($m) = $method->run($id);
379                 push @mods, $m;
380         }
381         return \@mods;
382 }
383
384
385 __PACKAGE__->register_method(
386         method  => "biblio_mrid_to_modsbatch",
387         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
388         notes           => <<"  NOTES");
389         Returns the mvr associated with a given metarecod. If none exists, 
390         it is created.
391         NOTES
392
393 __PACKAGE__->register_method(
394         method  => "biblio_mrid_to_modsbatch",
395         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
396         notes           => <<"  NOTES");
397         Returns the mvr associated with a given metarecod. If none exists, 
398         it is created.
399         NOTES
400
401 sub biblio_mrid_to_modsbatch {
402         my( $self, $client, $mrid ) = @_;
403
404         warn "Grabbing mvr for $mrid\n";
405
406         my $mr = _grab_metarecord($mrid);
407         return undef unless $mr;
408
409         if( my $m = $self->biblio_mrid_check_mvr($client, $mr)) {
410                 return $m;
411         }
412
413         return $self->biblio_mrid_make_modsbatch( $client, $mr ); 
414 }
415
416 # converts a metarecord to an mvr
417 sub _mr_to_mvr {
418         my $mr = shift;
419         my $perl = JSON->JSON2perl($mr->mods());
420         return Fieldmapper::metabib::virtual_record->new($perl);
421 }
422
423 # checks to see if a metarecord has mods, if so returns true;
424
425 __PACKAGE__->register_method(
426         method  => "biblio_mrid_check_mvr",
427         api_name        => "open-ils.search.biblio.metarecord.mods_slim.check",
428         notes           => <<"  NOTES");
429         Takes a metarecord ID or a metarecord object and returns true
430         if the metarecord already has an mvr associated with it.
431         NOTES
432
433 sub biblio_mrid_check_mvr {
434         my( $self, $client, $mrid ) = @_;
435         my $mr; 
436
437         if(ref($mrid)) { $mr = $mrid; } 
438         else { $mr = _grab_metarecord($mrid); }
439
440         warn "Checking mvr for mr " . $mr->id . "\n";
441
442         return _mr_to_mvr($mr) if $mr->mods();
443         return undef;
444 }
445
446 sub _grab_metarecord {
447
448         my $mrid = shift;
449         warn "Grabbing MR $mrid\n";
450
451         my $mr = OpenILS::Application::AppUtils->simple_scalar_request( 
452                 "open-ils.storage", 
453                 "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
454
455         if(!$mr) {
456                 throw OpenSRF::EX::ERROR 
457                         ("No metarecord exists with the given id: $mrid");
458         }
459
460         return $mr;
461 }
462
463
464 __PACKAGE__->register_method(
465         method  => "biblio_mrid_make_modsbatch",
466         api_name        => "open-ils.search.biblio.metarecord.mods_slim.create",
467         notes           => <<"  NOTES");
468         Takes either a metarecord ID or a metarecord object.
469         Forces the creations of an mvr for the given metarecord.
470         The created mvr is returned.
471         NOTES
472
473 sub biblio_mrid_make_modsbatch {
474         my( $self, $client, $mrid ) = @_;
475
476         my $e = OpenILS::Utils::Editor->new;
477
478         my $mr;
479         if( ref($mrid) ) {
480                 $mr = $mrid;
481                 $mrid = $mr->id;
482         } else {
483                 $mr = $e->retrieve_metabib_metarecord($mrid) 
484                         or return $e->event;
485         }
486
487         my $masterid = $mr->master_record;
488         $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
489
490         my $ids = $e->request(
491                 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
492         return undef unless @$ids;
493
494         my $master = $e->retrieve_biblio_record_entry($masterid)
495                 or return $e->event;
496
497         # start the mods batch
498         my $u = OpenILS::Utils::ModsParser->new();
499         $u->start_mods_batch( $master->marc );
500
501         # grab all of the sub-records and shove them into the batch
502         my @ids = grep { $_ ne $masterid } @$ids;
503         my $subrecs = $e->batch_retrieve_biblio_record_entry(\@ids);
504
505         for(@$subrecs) {
506                 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
507                 $u->push_mods_batch( $_->marc ) if $_->marc;
508         }
509
510
511         # finish up and send to the client
512         my $mods = $u->finish_mods_batch();
513         $mods->doc_id($mrid);
514         $client->respond_complete($mods);
515
516
517         # now update the mods string in the db
518         my $string = JSON->perl2JSON($mods->decast);
519         $mr->mods($string);
520
521         $e = OpenILS::Utils::Editor->new(xact => 1);
522         $e->update_metabib_metarecord($mr) 
523                 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
524         $e->finish;
525
526         return undef;
527 }
528
529
530
531
532
533 =head deprecated
534 sub _biblio_mrid_make_modsbatch {
535
536         my( $self, $client, $mrid ) = @_;
537
538         my $mr; 
539         if(ref($mrid)) { $mr = $mrid; }
540         else { $mr = _grab_metarecord($mrid); }
541         $mrid = $mr->id;
542
543         warn "Forcing mvr creation for mr " . $mr->id . "\n";
544         my $master_id = $mr->master_record;
545
546         my $session = OpenSRF::AppSession->create("open-ils.storage");
547
548         # grab the records attached to this metarecod 
549         warn "Creating mods batch for metarecord $mrid\n";
550         my $meth = "open-ils.search.biblio.metarecord_to_records.staff";
551         $meth = $self->method_lookup($meth);
552         my ($id_hash) = $meth->run($mrid);
553         my @ids = @{$id_hash->{ids}};
554         if(@ids < 1) { return undef; }
555
556         warn "Master ID is $master_id\n";
557         # grab the master record to start the mods batch 
558
559         $meth = "open-ils.storage.direct.biblio.record_entry.retrieve";
560
561         my $record = $session->request(
562                         "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
563         $record = $record->gather(1);
564
565         #my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
566
567         if(!$record) {
568                 warn "No record returned with id $master_id";
569                 throw OpenSRF::EX::ERROR 
570         }
571
572         my $u = OpenILS::Utils::ModsParser->new();
573         $u->start_mods_batch( $record->marc );
574         my $main_doc_id = $record->id();
575
576         @ids = grep { $_ ne $master_id } @ids;
577
578         # now we have to collect all of the marc objects and push them into a mods batch
579         my $request = $session->request(
580                 "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
581
582         while( my $response = $request->recv() ) {
583
584                 next unless $response;
585                 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
586                         throw $response ($response->stringify);
587                 }
588
589                 my $content = $response->content;
590
591                 if( $content ) {
592                         $u->push_mods_batch( $content->marc );
593                 }
594         }
595
596         my $mods = $u->finish_mods_batch();
597         $mods->doc_id($mrid);
598         $request->finish();
599
600         $client->respond_complete($mods);
601
602         my $mods_string = JSON->perl2JSON($mods->decast);
603
604         $mr->mods($mods_string);
605
606         my $req = $session->request( 
607                 "open-ils.storage.direct.metabib.metarecord.update", $mr );
608
609
610         $req->gather(1);
611         $session->finish();
612         $session->disconnect();
613
614         return undef;
615 }
616 =cut
617
618
619
620 # converts a mr id into a list of record ids
621
622 __PACKAGE__->register_method(
623         method  => "biblio_mrid_to_record_ids",
624         api_name        => "open-ils.search.biblio.metarecord_to_records",
625 );
626
627 __PACKAGE__->register_method(
628         method  => "biblio_mrid_to_record_ids",
629         api_name        => "open-ils.search.biblio.metarecord_to_records.staff",
630 );
631
632 sub biblio_mrid_to_record_ids {
633         my( $self, $client, $mrid, $args ) = @_;
634
635         my $format      = $$args{format};
636         my $org         = $$args{org};
637         my $depth       = $$args{depth};
638
639         my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
640         $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o; 
641         my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
642
643         return { count => scalar(@$recs), ids => $recs };
644 }
645
646
647 __PACKAGE__->register_method(
648         method  => "biblio_record_to_marc_html",
649         api_name        => "open-ils.search.biblio.record.html" );
650
651 my $parser              = XML::LibXML->new();
652 my $xslt                        = XML::LibXSLT->new();
653 my $marc_sheet;
654
655 my $settings_client = OpenSRF::Utils::SettingsClient->new();
656 sub biblio_record_to_marc_html {
657         my( $self, $client, $recordid ) = @_;
658
659         if( !$marc_sheet ) {
660                 my $dir = $settings_client->config_value( "dirs", "xsl" );
661                 my $xsl = $settings_client->config_value(
662                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
663
664                 $xsl = $parser->parse_file("$dir/$xsl");
665                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
666         }
667
668
669         my $record = $apputils->simple_scalar_request(
670                 "open-ils.storage", 
671                 "open-ils.storage.direct.biblio.record_entry.retrieve",
672                 $recordid );
673
674         my $xmldoc = $parser->parse_string($record->marc);
675         my $html = $marc_sheet->transform($xmldoc);
676         $html = $html->toString();
677         return $html;
678
679 }
680
681
682 =head duplicate
683 __PACKAGE__->register_method(
684         method  => "retrieve_all_copy_locations",
685         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
686
687 my $shelving_locations;
688 sub retrieve_all_copy_locations {
689         my( $self, $client ) = @_;
690         if(!$shelving_locations) {
691                 $shelving_locations = $apputils->simple_scalar_request(
692                         "open-ils.storage", 
693                         "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
694         }
695         return $shelving_locations;
696 }
697 =cut
698
699
700
701 __PACKAGE__->register_method(
702         method  => "retrieve_all_copy_statuses",
703         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
704
705 my $copy_statuses;
706 sub retrieve_all_copy_statuses {
707         my( $self, $client ) = @_;
708         if(!$copy_statuses) {
709                 $copy_statuses = $apputils->simple_scalar_request(
710                         "open-ils.storage",
711                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
712         }
713         return $copy_statuses;
714 }
715
716
717 __PACKAGE__->register_method(
718         method  => "copy_counts_per_org",
719         api_name        => "open-ils.search.biblio.copy_counts.retrieve");
720
721 __PACKAGE__->register_method(
722         method  => "copy_counts_per_org",
723         api_name        => "open-ils.search.biblio.copy_counts.retrieve.staff");
724
725 sub copy_counts_per_org {
726         my( $self, $client, $record_id ) = @_;
727
728         warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
729
730         my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
731         if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
732
733         my $counts = $apputils->simple_scalar_request(
734                 "open-ils.storage", $method, $record_id );
735
736         $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
737         return $counts;
738 }
739
740
741 __PACKAGE__->register_method(
742         method          => "copy_count_summary",
743         api_name        => "open-ils.search.biblio.copy_counts.summary.retrieve",
744         notes           => <<"  NOTES");
745         returns an array of these:
746                 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
747                 where statusx is a copy status name.  the statuses are sorted
748                 by id.
749         NOTES
750
751 sub copy_count_summary {
752         my( $self, $client, $rid ) = @_;
753         return $U->storagereq(
754                 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid );
755 }
756
757
758
759 =head
760 __PACKAGE__->register_method(
761         method          => "multiclass_search",
762         api_name        => "open-ils.search.biblio.multiclass",
763         notes           => <<"  NOTES");
764                 Performs a multiclass search
765                 PARAMS( searchBlob, org_unit, format, limit ) 
766                 where searchBlob is defined like this:
767                         { 
768                                 "title" : { "term" : "water" }, 
769                                 "author" : { "term" : "smith" }, 
770                                 ... 
771                         }
772         NOTES
773
774 __PACKAGE__->register_method(
775         method          => "multiclass_search",
776         api_name        => "open-ils.search.biblio.multiclass.staff",
777         notes           => "see open-ils.search.biblio.multiclass" );
778
779 sub multiclass_search {
780         my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
781
782         $logger->debug("Performing multiclass search with org => $orgid, " .
783                 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
784
785         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
786         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
787
788
789         my $records = $apputils->simplereq(
790                 'open-ils.storage', $meth, 
791                  org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
792
793         my $count = 0;
794         my $recs = [];
795
796         if( ref($records) and $records->[0] and 
797                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
798
799         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
800
801         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
802         return { ids => $recs, count => $count };
803 }
804 =cut
805
806
807 =head comment-1
808 __PACKAGE__->register_method(
809         method          => "multiclass_search",
810         api_name                => "open-ils.search.biblio.multiclass",
811         signature       => q/
812                 Performs a multiclass search
813                 @param args A names hash of arguments:
814                         org_unit : The org to focus the search on
815                         depth           : The search depth
816                         format  : Item format
817                         limit           : Return limit
818                         offset  : Search offset
819                         searches : A named hash of searches which has the following format:
820                                 { 
821                                         "title" : { "term" : "water" }, 
822                                         "author" : { "term" : "smith" }, 
823                                         ... 
824                                 }
825                 @return { ids : <array of ids>, count : hitcount }
826         /
827 );
828
829 __PACKAGE__->register_method(
830         method          => "multiclass_search",
831         api_name                => "open-ils.search.biblio.multiclass.staff",
832         notes           => q/@see open-ils.search.biblio.multiclass/ );
833
834 sub multiclass_search {
835         my( $self, $client, $args ) = @_;
836
837         $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
838         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
839         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
840
841         my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
842
843         my $count = 0;
844         my $recs = [];
845
846         if( ref($records) and $records->[0] and 
847                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
848
849         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
850
851         return { ids => $recs, count => $count };
852 }
853
854 =cut
855
856
857
858 __PACKAGE__->register_method(
859         method          => "marc_search",
860         api_name        => "open-ils.search.biblio.marc",
861         notes           => <<"  NOTES");
862                 Example:
863                 open-ils.storage.biblio.full_rec.multi_search.atomic 
864                 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
865         "limit":5,"sort":"author","item_type":"g"}
866         NOTES
867
868 sub marc_search {
869         my( $self, $conn, $args ) = @_;
870         my $records = $U->storagereq(
871                 'open-ils.storage.biblio.full_rec.multi_search.atomic', %$args );
872
873         my $count = 0;
874
875         $count = $records->[0]->[2] if( ref($records) and 
876                 $records->[0] and defined($records->[0]->[2]));
877
878         return { ids => $records, count => $count };
879 }
880
881
882
883 __PACKAGE__->register_method(
884         method  => "biblio_search_isbn",
885         api_name        => "open-ils.search.biblio.isbn",
886 );
887
888 sub biblio_search_isbn { 
889         my( $self, $client, $isbn ) = @_;
890         $logger->debug("Searching ISBN $isbn");
891         my $e = OpenILS::Utils::Editor->new;
892         my $recs = $e->request(
893                 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
894         return { ids => $recs, count => scalar(@$recs) };
895 }
896
897
898 __PACKAGE__->register_method(
899         method  => "biblio_search_issn",
900         api_name        => "open-ils.search.biblio.issn",
901 );
902
903 sub biblio_search_issn { 
904         my( $self, $client, $issn ) = @_;
905         $logger->debug("Searching ISSN $issn");
906         my $e = OpenILS::Utils::Editor->new;
907         my $recs = $e->request(
908                 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
909         return { ids => $recs, count => scalar(@$recs) };
910 }
911
912
913
914
915 __PACKAGE__->register_method(
916         method  => "fetch_mods_by_copy",
917         api_name        => "open-ils.search.biblio.mods_from_copy",
918 );
919
920 sub fetch_mods_by_copy {
921         my( $self, $client, $copyid ) = @_;
922         my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
923         return $evt if $evt;
924         return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
925         return $apputils->record_to_mvr($record);
926 }
927
928
929
930
931
932 # -------------------------------------------------------------------------------------
933
934 __PACKAGE__->register_method(
935         method  => "cn_browse",
936         api_name        => "open-ils.search.callnumber.browse.target",
937         notes           => "Starts a callnumber browse"
938         );
939
940 __PACKAGE__->register_method(
941         method  => "cn_browse",
942         api_name        => "open-ils.search.callnumber.browse.page_up",
943         notes           => "Returns the previous page of callnumbers", 
944         );
945
946 __PACKAGE__->register_method(
947         method  => "cn_browse",
948         api_name        => "open-ils.search.callnumber.browse.page_down",
949         notes           => "Returns the next page of callnumbers", 
950         );
951
952
953 # RETURNS array of arrays like so: label, owning_lib, record, id
954 sub cn_browse {
955         my( $self, $client, @params ) = @_;
956         my $method;
957
958         $method = 'open-ils.storage.asset.call_number.browse.target.atomic' 
959                 if( $self->api_name =~ /target/ );
960         $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
961                 if( $self->api_name =~ /page_up/ );
962         $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
963                 if( $self->api_name =~ /page_down/ );
964
965         return $apputils->simplereq( 'open-ils.storage', $method, @params );
966 }
967 # -------------------------------------------------------------------------------------
968
969 __PACKAGE__->register_method(
970         method => "fetch_cn",
971         api_name => "open-ils.search.callnumber.retrieve",
972         notes           => "retrieves a callnumber based on ID",
973         );
974
975 sub fetch_cn {
976         my( $self, $client, $id ) = @_;
977         my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
978         return $evt if $evt;
979         return $cn;
980 }
981
982 __PACKAGE__->register_method (
983         method          => "fetch_copy_by_cn",
984         api_name                => 'open-ils.search.copies_by_call_number.retrieve',
985         signature       => q/
986                 Returns an array of copy id's by callnumber id
987                 @param cnid The callnumber id
988                 @return An array of copy ids
989         /
990 );
991
992 sub fetch_copy_by_cn {
993         my( $self, $conn, $cnid ) = @_;
994         return $U->storagereq(
995                 'open-ils.storage.id_list.asset.copy.search_where.atomic', 
996                 { call_number => $cnid, deleted => 'f' } );
997 }
998
999 __PACKAGE__->register_method (
1000         method          => 'fetch_cn_by_info',
1001         api_name                => 'open-ils.search.call_number.retrieve_by_info',
1002         signature       => q/
1003                 @param label The callnumber label
1004                 @param record The record the cn is attached to
1005                 @param org The owning library of the cn
1006                 @return The callnumber object
1007         /
1008 );
1009
1010
1011 sub fetch_cn_by_info {
1012         my( $self, $conn, $label, $record, $org ) = @_;
1013         return $U->storagereq(
1014                 'open-ils.storage.direct.asset.call_number.search_where',
1015                 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1016 }
1017
1018
1019                 
1020
1021
1022 __PACKAGE__->register_method (
1023         method => 'bib_extras',
1024         api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1025 __PACKAGE__->register_method (
1026         method => 'bib_extras',
1027         api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1028 __PACKAGE__->register_method (
1029         method => 'bib_extras',
1030         api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1031 __PACKAGE__->register_method (
1032         method => 'bib_extras',
1033         api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1034
1035 sub bib_extras {
1036         my $self = shift;
1037         
1038         return $U->storagereq(
1039                 'open-ils.storage.direct.config.lit_form_map.retrieve.all.atomic')
1040                         if( $self->api_name =~ /lit_form/ );
1041
1042         return $U->storagereq(
1043                 'open-ils.storage.direct.config.item_form_map.retrieve.all.atomic')
1044                         if( $self->api_name =~ /item_form_map/ );
1045
1046         return $U->storagereq(
1047                 'open-ils.storage.direct.config.item_type_map.retrieve.all.atomic')
1048                         if( $self->api_name =~ /item_type_map/ );
1049
1050         return $U->storagereq(
1051                 'open-ils.storage.direct.config.audience_map.retrieve.all.atomic')
1052                         if( $self->api_name =~ /audience/ );
1053
1054         return [];
1055 }
1056
1057
1058
1059 __PACKAGE__->register_method(
1060         method  => 'fetch_slim_record',
1061         api_name        => 'open-ils.search.biblio.record_entry.slim.retrieve',
1062         signature=> q/
1063                 Returns a biblio.record_entry without the attached marcxml
1064         /
1065 );
1066
1067 sub fetch_slim_record {
1068         my( $self, $conn, $ids ) = @_;
1069
1070         my $editor = OpenILS::Utils::Editor->new;
1071         my @res;
1072         for( @$ids ) {
1073                 return $editor->event unless
1074                         my $r = $editor->retrieve_biblio_record_entry($_);
1075                 $r->clear_marc;
1076                 push(@res, $r);
1077         }
1078         return \@res;
1079 }
1080
1081
1082
1083
1084
1085
1086
1087 1;
1088
1089