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