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