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