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