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