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