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