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