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