]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
now augmenting existing searches instead of replacing them. logging compiled search...
[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 = $arghash->{searches}; 
468     $arghash->{searches} = $search = {} unless $search;
469
470     while ($query =~ s/((?:keyword(?:\|\w+)?|title(?:\|\w+)?|author(?:\|\w+)?|subject(?:\|\w+)?|series(?:\|\w+)?|site|dir|sort|lang|available):[^:]+)$//so) {
471         my($type, $value) = split(':', $1);
472         next unless $type and $value;
473
474         $value =~ s/^\s*//og;
475         $value =~ s/\s*$//og;
476         $type = 'sort_dir' if $type eq 'dir';
477
478         if($type eq 'site') {
479             # 'site' is the org shortname.  when using this, we also want 
480             # to search at the requested org's depth
481             my $e = new_editor();
482             if(my $org = $e->search_actor_org_unit({shortname => $value})->[0]) {
483                 $arghash->{org_unit} = $org->id if $org;
484                 $arghash->{depth} = $e->retrieve_actor_org_unit_type($org->ou_type)->depth;
485             } else {
486                 $logger->warn("'site:' query used on invalid org shortname: $value ... ignoring");
487             }
488
489         } elsif($type eq 'available') {
490             # limit to available
491             $arghash->{available} = 1 unless $value eq 'false' or $value eq '0';
492
493         } elsif($type eq 'lang') {
494             # collect languages into an array of languages
495             $arghash->{language} = [] unless $arghash->{language};
496             push(@{$arghash->{language}}, $value);
497
498         } else {
499             # append the search term to the term under construction
500             $search->{$type} =  {} unless $search->{$type};
501             $search->{$type}->{term} =  
502                 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
503         }
504     }
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     $logger->info("compiled search is " . OpenSRF::Utils::JSON->perl2JSON($arghash));
516     return $data;
517 }
518
519 # ----------------------------------------------------------------------------
520 # These are the main OPAC search methods
521 # ----------------------------------------------------------------------------
522
523 __PACKAGE__->register_method(
524         method          => 'the_quest_for_knowledge',
525         api_name                => 'open-ils.search.biblio.multiclass',
526         signature       => q/
527                 Performs a multi class bilbli or metabib search
528                 @param searchhash A search object layed out like so:
529                         searches : { "$class" : "$value", ...}
530                         org_unit : The org id to focus the search at
531                         depth           : The org depth
532                         limit           : The search limit
533                         offset  : The search offset
534                         format  : The MARC format
535                         sort            : What field to sort the results on [ author | title | pubdate ]
536                         sort_dir        : What direction do we sort? [ asc | desc ]
537                 @return An object of the form 
538                         { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
539         /
540 );
541
542 __PACKAGE__->register_method(
543         method          => 'the_quest_for_knowledge',
544         api_name                => 'open-ils.search.biblio.multiclass.staff',
545         signature       => q/@see open-ils.search.biblio.multiclass/);
546 __PACKAGE__->register_method(
547         method          => 'the_quest_for_knowledge',
548         api_name                => 'open-ils.search.metabib.multiclass',
549         signature       => q/@see open-ils.search.biblio.multiclass/);
550 __PACKAGE__->register_method(
551         method          => 'the_quest_for_knowledge',
552         api_name                => 'open-ils.search.metabib.multiclass.staff',
553         signature       => q/@see open-ils.search.biblio.multiclass/);
554
555
556
557 sub the_quest_for_knowledge {
558         my( $self, $conn, $searchhash, $docache ) = @_;
559
560         return { count => 0 } unless $searchhash and
561                 ref $searchhash->{searches} eq 'HASH';
562
563         my $method = 'open-ils.storage.biblio.multiclass.search_fts';
564         my $ismeta = 0;
565         my @recs;
566
567         if($self->api_name =~ /metabib/) {
568                 $ismeta = 1;
569                 $method =~ s/biblio/metabib/o;
570         }
571
572
573         my $offset      = $searchhash->{offset} || 0;
574         my $limit       = $searchhash->{limit} || 10;
575         my $end         = $offset + $limit - 1;
576
577         # do some simple sanity checking
578         if(!$searchhash->{searches} or
579                 ( !grep { /^(?:title|author|subject|series|keyword)/ } keys %{$searchhash->{searches}} ) ) {
580                 return { count => 0 };
581         }
582
583
584         my $maxlimit = 5000;
585         $searchhash->{offset}   = 0;
586         $searchhash->{limit}            = $maxlimit;
587
588         return { count => 0 } if $offset > $maxlimit;
589
590         my @search;
591         push( @search, ($_ => $$searchhash{$_})) for (sort keys %$searchhash);
592         my $s = OpenSRF::Utils::JSON->perl2JSON(\@search);
593         my $ckey = $pfx . md5_hex($method . $s);
594
595         $logger->info("bib search for: $s");
596
597         $searchhash->{limit} -= $offset;
598
599
600     my $trim = 0;
601         my $result = ($docache) ? search_cache($ckey, $offset, $limit) : undef;
602
603         if(!$result) {
604
605                 $method .= ".staff" if($self->api_name =~ /staff/);
606                 $method .= ".atomic";
607         
608                 for (keys %$searchhash) { 
609                         delete $$searchhash{$_} 
610                                 unless defined $$searchhash{$_}; 
611                 }
612         
613                 $result = $U->storagereq( $method, %$searchhash );
614         $trim = 1;
615
616         } else { 
617                 $docache = 0; 
618         }
619
620         return {count => 0} unless ($result && $$result[0]);
621
622         @recs = @$result;
623
624         my $count = ($ismeta) ? $result->[0]->[3] : $result->[0]->[2];
625
626         if($docache) {
627                 # If we didn't get this data from the cache, put it into the cache
628                 # then return the correct offset of records
629                 $logger->debug("putting search cache $ckey\n");
630                 put_cache($ckey, $count, \@recs);
631         }
632
633     if($trim) {
634         # if we have the full set of data, trim out 
635         # the requested chunk based on limit and offset
636         my @t;
637         for ($offset..$end) {
638             last unless $recs[$_];
639             push(@t, $recs[$_]);
640         }
641         @recs = @t;
642     }
643
644         return { ids => \@recs, count => $count };
645 }
646
647
648
649 sub search_cache {
650
651         my $key         = shift;
652         my $offset      = shift;
653         my $limit       = shift;
654         my $start       = $offset;
655         my $end         = $offset + $limit - 1;
656
657         $logger->debug("searching cache for $key : $start..$end\n");
658
659         return undef unless $cache;
660         my $data = $cache->get_cache($key);
661
662         return undef unless $data;
663
664         my $count = $data->[0];
665         $data = $data->[1];
666
667         return undef unless $offset < $count;
668
669
670         my @result;
671         for( my $i = $offset; $i <= $end; $i++ ) {
672                 last unless my $d = $$data[$i];
673                 push( @result, $d );
674         }
675
676         $logger->debug("search_cache found ".scalar(@result)." items for count=$count, start=$start, end=$end");
677
678         return \@result;
679 }
680
681
682 sub put_cache {
683         my( $key, $count, $data ) = @_;
684         return undef unless $cache;
685         $logger->debug("search_cache putting ".
686                 scalar(@$data)." items at key $key with timeout $cache_timeout");
687         $cache->put_cache($key, [ $count, $data ], $cache_timeout);
688 }
689
690
691
692
693
694
695 __PACKAGE__->register_method(
696         method  => "biblio_mrid_to_modsbatch_batch",
697         api_name        => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
698
699 sub biblio_mrid_to_modsbatch_batch {
700         my( $self, $client, $mrids) = @_;
701         warn "Performing mrid_to_modsbatch_batch...";
702         my @mods;
703         my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
704         for my $id (@$mrids) {
705                 next unless defined $id;
706                 my ($m) = $method->run($id);
707                 push @mods, $m;
708         }
709         return \@mods;
710 }
711
712
713 __PACKAGE__->register_method(
714         method  => "biblio_mrid_to_modsbatch",
715         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
716         notes           => <<"  NOTES");
717         Returns the mvr associated with a given metarecod. If none exists, 
718         it is created.
719         NOTES
720
721 __PACKAGE__->register_method(
722         method  => "biblio_mrid_to_modsbatch",
723         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
724         notes           => <<"  NOTES");
725         Returns the mvr associated with a given metarecod. If none exists, 
726         it is created.
727         NOTES
728
729 sub biblio_mrid_to_modsbatch {
730         my( $self, $client, $mrid, $args) = @_;
731
732         warn "Grabbing mvr for $mrid\n";
733
734         my ($mr, $evt) = _grab_metarecord($mrid);
735         return $evt unless $mr;
736
737         my $mvr = $self->biblio_mrid_check_mvr($client, $mr);
738         $mvr = $self->biblio_mrid_make_modsbatch( $client, $mr ) unless $mvr;
739
740         return $mvr unless ref($args);  
741
742         # Here we find the lead record appropriate for the given filters 
743         # and use that for the title and author of the metarecord
744         my $format      = $$args{format};
745         my $org         = $$args{org};
746         my $depth       = $$args{depth};
747
748         return $mvr unless $format or $org or $depth;
749
750         my $method = "open-ils.storage.ordered.metabib.metarecord.records";
751         $method = "$method.staff" if $self->api_name =~ /staff/o; 
752
753         my $rec = $U->storagereq($method, $format, $org, $depth, 1);
754
755         if( my $mods = $U->record_to_mvr($rec) ) {
756
757                 $mvr->title($mods->title);
758                 $mvr->title($mods->author);
759                 $logger->debug("mods_slim updating title and ".
760                         "author in mvr with ".$mods->title." : ".$mods->author);
761         }
762
763         return $mvr;
764 }
765
766 # converts a metarecord to an mvr
767 sub _mr_to_mvr {
768         my $mr = shift;
769         my $perl = OpenSRF::Utils::JSON->JSON2perl($mr->mods());
770         return Fieldmapper::metabib::virtual_record->new($perl);
771 }
772
773 # checks to see if a metarecord has mods, if so returns true;
774
775 __PACKAGE__->register_method(
776         method  => "biblio_mrid_check_mvr",
777         api_name        => "open-ils.search.biblio.metarecord.mods_slim.check",
778         notes           => <<"  NOTES");
779         Takes a metarecord ID or a metarecord object and returns true
780         if the metarecord already has an mvr associated with it.
781         NOTES
782
783 sub biblio_mrid_check_mvr {
784         my( $self, $client, $mrid ) = @_;
785         my $mr; 
786
787         my $evt;
788         if(ref($mrid)) { $mr = $mrid; } 
789         else { ($mr, $evt) = _grab_metarecord($mrid); }
790         return $evt if $evt;
791
792         warn "Checking mvr for mr " . $mr->id . "\n";
793
794         return _mr_to_mvr($mr) if $mr->mods();
795         return undef;
796 }
797
798 sub _grab_metarecord {
799         my $mrid = shift;
800         #my $e = OpenILS::Utils::Editor->new;
801         my $e = new_editor();
802         my $mr = $e->retrieve_metabib_metarecord($mrid) or return ( undef, $e->event );
803         return ($mr);
804 }
805
806
807 __PACKAGE__->register_method(
808         method  => "biblio_mrid_make_modsbatch",
809         api_name        => "open-ils.search.biblio.metarecord.mods_slim.create",
810         notes           => <<"  NOTES");
811         Takes either a metarecord ID or a metarecord object.
812         Forces the creations of an mvr for the given metarecord.
813         The created mvr is returned.
814         NOTES
815
816 sub biblio_mrid_make_modsbatch {
817         my( $self, $client, $mrid ) = @_;
818
819         #my $e = OpenILS::Utils::Editor->new;
820         my $e = new_editor();
821
822         my $mr;
823         if( ref($mrid) ) {
824                 $mr = $mrid;
825                 $mrid = $mr->id;
826         } else {
827                 $mr = $e->retrieve_metabib_metarecord($mrid) 
828                         or return $e->event;
829         }
830
831         my $masterid = $mr->master_record;
832         $logger->info("creating new mods batch for metarecord=$mrid, master record=$masterid");
833
834         my $ids = $U->storagereq(
835                 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic', $mrid);
836         return undef unless @$ids;
837
838         my $master = $e->retrieve_biblio_record_entry($masterid)
839                 or return $e->event;
840
841         # start the mods batch
842         my $u = OpenILS::Utils::ModsParser->new();
843         $u->start_mods_batch( $master->marc );
844
845         # grab all of the sub-records and shove them into the batch
846         my @ids = grep { $_ ne $masterid } @$ids;
847         #my $subrecs = (@ids) ? $e->batch_retrieve_biblio_record_entry(\@ids) : [];
848
849         my $subrecs = [];
850         if(@$ids) {
851                 for my $i (@$ids) {
852                         my $r = $e->retrieve_biblio_record_entry($i);
853                         push( @$subrecs, $r ) if $r;
854                 }
855         }
856
857         for(@$subrecs) {
858                 $logger->debug("adding record ".$_->id." to mods batch for metarecord=$mrid");
859                 $u->push_mods_batch( $_->marc ) if $_->marc;
860         }
861
862
863         # finish up and send to the client
864         my $mods = $u->finish_mods_batch();
865         $mods->doc_id($mrid);
866         $client->respond_complete($mods);
867
868
869         # now update the mods string in the db
870         my $string = OpenSRF::Utils::JSON->perl2JSON($mods->decast);
871         $mr->mods($string);
872
873         #$e = OpenILS::Utils::Editor->new(xact => 1);
874         $e = new_editor(xact => 1);
875         $e->update_metabib_metarecord($mr) 
876                 or $logger->error("Error setting mods text on metarecord $mrid : " . Dumper($e->event));
877         $e->finish;
878
879         return undef;
880 }
881
882
883
884
885 # converts a mr id into a list of record ids
886
887 __PACKAGE__->register_method(
888         method  => "biblio_mrid_to_record_ids",
889         api_name        => "open-ils.search.biblio.metarecord_to_records",
890 );
891
892 __PACKAGE__->register_method(
893         method  => "biblio_mrid_to_record_ids",
894         api_name        => "open-ils.search.biblio.metarecord_to_records.staff",
895 );
896
897 sub biblio_mrid_to_record_ids {
898         my( $self, $client, $mrid, $args ) = @_;
899
900         my $format      = $$args{format};
901         my $org         = $$args{org};
902         my $depth       = $$args{depth};
903
904         my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
905         $method =~ s/atomic/staff\.atomic/o if $self->api_name =~ /staff/o; 
906         my $recs = $U->storagereq($method, $mrid, $format, $org, $depth);
907
908         return { count => scalar(@$recs), ids => $recs };
909 }
910
911
912 __PACKAGE__->register_method(
913         method  => "biblio_record_to_marc_html",
914         api_name        => "open-ils.search.biblio.record.html" );
915
916 my $parser              = XML::LibXML->new();
917 my $xslt                        = XML::LibXSLT->new();
918 my $marc_sheet;
919
920 my $settings_client = OpenSRF::Utils::SettingsClient->new();
921 sub biblio_record_to_marc_html {
922         my( $self, $client, $recordid ) = @_;
923
924         if( !$marc_sheet ) {
925                 my $dir = $settings_client->config_value( "dirs", "xsl" );
926                 my $xsl = $settings_client->config_value(
927                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
928
929                 $xsl = $parser->parse_file("$dir/$xsl");
930                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
931         }
932
933
934         my $record = $apputils->simple_scalar_request(
935                 "open-ils.cstore", 
936                 "open-ils.cstore.direct.biblio.record_entry.retrieve",
937                 $recordid );
938
939         my $xmldoc = $parser->parse_string($record->marc);
940         my $html = $marc_sheet->transform($xmldoc);
941         $html = $html->toString();
942         return $html;
943
944 }
945
946
947 =head duplicate
948 __PACKAGE__->register_method(
949         method  => "retrieve_all_copy_locations",
950         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
951
952 my $shelving_locations;
953 sub retrieve_all_copy_locations {
954         my( $self, $client ) = @_;
955         if(!$shelving_locations) {
956                 $shelving_locations = $apputils->simple_scalar_request(
957                         "open-ils.cstore", 
958                         "open-ils.cstore.direct.asset.copy_location.search.atomic",
959                         { id => { "!=" => undef } }
960                 );
961         }
962         return $shelving_locations;
963 }
964 =cut
965
966
967
968 __PACKAGE__->register_method(
969         method  => "retrieve_all_copy_statuses",
970         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
971
972 my $copy_statuses;
973 sub retrieve_all_copy_statuses {
974         my( $self, $client ) = @_;
975         return $copy_statuses if $copy_statuses;
976         return $copy_statuses = 
977                 new_editor()->retrieve_all_config_copy_status();
978 }
979
980
981 __PACKAGE__->register_method(
982         method  => "copy_counts_per_org",
983         api_name        => "open-ils.search.biblio.copy_counts.retrieve");
984
985 __PACKAGE__->register_method(
986         method  => "copy_counts_per_org",
987         api_name        => "open-ils.search.biblio.copy_counts.retrieve.staff");
988
989 sub copy_counts_per_org {
990         my( $self, $client, $record_id ) = @_;
991
992         warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
993
994         my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
995         if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
996
997         my $counts = $apputils->simple_scalar_request(
998                 "open-ils.storage", $method, $record_id );
999
1000         $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
1001         return $counts;
1002 }
1003
1004
1005 __PACKAGE__->register_method(
1006         method          => "copy_count_summary",
1007         api_name        => "open-ils.search.biblio.copy_counts.summary.retrieve",
1008         notes           => <<"  NOTES");
1009         returns an array of these:
1010                 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
1011                 where statusx is a copy status name.  the statuses are sorted
1012                 by id.
1013         NOTES
1014
1015 sub copy_count_summary {
1016         my( $self, $client, $rid, $org, $depth ) = @_;
1017         $org ||= 1;
1018         $depth ||= 0;
1019     my $data = $U->storagereq(
1020                 'open-ils.storage.biblio.record_entry.status_copy_count.atomic', $rid, $org, $depth );
1021
1022     return [ sort { $a->[1] cmp $b->[1] } @$data ];
1023 }
1024
1025
1026
1027 =head
1028 __PACKAGE__->register_method(
1029         method          => "multiclass_search",
1030         api_name        => "open-ils.search.biblio.multiclass",
1031         notes           => <<"  NOTES");
1032                 Performs a multiclass search
1033                 PARAMS( searchBlob, org_unit, format, limit ) 
1034                 where searchBlob is defined like this:
1035                         { 
1036                                 "title" : { "term" : "water" }, 
1037                                 "author" : { "term" : "smith" }, 
1038                                 ... 
1039                         }
1040         NOTES
1041
1042 __PACKAGE__->register_method(
1043         method          => "multiclass_search",
1044         api_name        => "open-ils.search.biblio.multiclass.staff",
1045         notes           => "see open-ils.search.biblio.multiclass" );
1046
1047 sub multiclass_search {
1048         my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
1049
1050         $logger->debug("Performing multiclass search with org => $orgid, " .
1051                 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
1052
1053         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1054         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1055
1056
1057         my $records = $apputils->simplereq(
1058                 'open-ils.storage', $meth, 
1059                  org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
1060
1061         my $count = 0;
1062         my $recs = [];
1063
1064         if( ref($records) and $records->[0] and 
1065                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1066
1067         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1068
1069         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
1070         return { ids => $recs, count => $count };
1071 }
1072 =cut
1073
1074
1075 =head comment-1
1076 __PACKAGE__->register_method(
1077         method          => "multiclass_search",
1078         api_name                => "open-ils.search.biblio.multiclass",
1079         signature       => q/
1080                 Performs a multiclass search
1081                 @param args A names hash of arguments:
1082                         org_unit : The org to focus the search on
1083                         depth           : The search depth
1084                         format  : Item format
1085                         limit           : Return limit
1086                         offset  : Search offset
1087                         searches : A named hash of searches which has the following format:
1088                                 { 
1089                                         "title" : { "term" : "water" }, 
1090                                         "author" : { "term" : "smith" }, 
1091                                         ... 
1092                                 }
1093                 @return { ids : <array of ids>, count : hitcount }
1094         /
1095 );
1096
1097 __PACKAGE__->register_method(
1098         method          => "multiclass_search",
1099         api_name                => "open-ils.search.biblio.multiclass.staff",
1100         notes           => q/@see open-ils.search.biblio.multiclass/ );
1101
1102 sub multiclass_search {
1103         my( $self, $client, $args ) = @_;
1104
1105         $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
1106         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
1107         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
1108
1109         my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
1110
1111         my $count = 0;
1112         my $recs = [];
1113
1114         if( ref($records) and $records->[0] and 
1115                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
1116
1117         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
1118
1119         return { ids => $recs, count => $count };
1120 }
1121
1122 =cut
1123
1124
1125
1126 __PACKAGE__->register_method(
1127         method          => "marc_search",
1128         api_name        => "open-ils.search.biblio.marc.staff");
1129
1130 __PACKAGE__->register_method(
1131         method          => "marc_search",
1132         api_name        => "open-ils.search.biblio.marc",
1133         notes           => <<"  NOTES");
1134                 Example:
1135                 open-ils.storage.biblio.full_rec.multi_search.atomic 
1136                 { "searches": [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit": 1,
1137         "limit":5,"sort":"author","item_type":"g"}
1138         NOTES
1139
1140 sub marc_search {
1141         my( $self, $conn, $args, $limit, $offset ) = @_;
1142
1143         my $method = 'open-ils.storage.biblio.full_rec.multi_search';
1144         $method .= ".staff" if $self->api_name =~ /staff/;
1145         $method .= ".atomic";
1146
1147         $limit ||= 10;
1148         $offset ||= 0;
1149
1150         my @search;
1151         push( @search, ($_ => $$args{$_}) ) for (sort keys %$args);
1152         my $ckey = $pfx . md5_hex($method . OpenSRF::Utils::JSON->perl2JSON(\@search));
1153
1154         my $recs = search_cache($ckey, $offset, $limit);
1155
1156         if(!$recs) {
1157                 $recs = $U->storagereq($method, %$args) || [];
1158                 if( $recs ) {
1159                         put_cache($ckey, scalar(@$recs), $recs);
1160                         $recs = [ @$recs[$offset..($offset + ($limit - 1))] ];
1161                 } else {
1162                         $recs = [];
1163                 }
1164         }
1165
1166         my $count = 0;
1167         $count = $recs->[0]->[2] if $recs->[0] and $recs->[0]->[2];
1168         my @recs = map { $_->[0] } @$recs;
1169
1170         return { ids => \@recs, count => $count };
1171 }
1172
1173
1174 __PACKAGE__->register_method(
1175         method  => "biblio_search_isbn",
1176         api_name        => "open-ils.search.biblio.isbn",
1177 );
1178
1179 sub biblio_search_isbn { 
1180         my( $self, $client, $isbn ) = @_;
1181         $logger->debug("Searching ISBN $isbn");
1182         my $e = new_editor();
1183         my $recs = $U->storagereq(
1184                 'open-ils.storage.id_list.biblio.record_entry.search.isbn.atomic', $isbn );
1185         return { ids => $recs, count => scalar(@$recs) };
1186 }
1187
1188
1189 __PACKAGE__->register_method(
1190         method  => "biblio_search_issn",
1191         api_name        => "open-ils.search.biblio.issn",
1192 );
1193
1194 sub biblio_search_issn { 
1195         my( $self, $client, $issn ) = @_;
1196         $logger->debug("Searching ISSN $issn");
1197         my $e = new_editor();
1198         my $recs = $U->storagereq(
1199                 'open-ils.storage.id_list.biblio.record_entry.search.issn.atomic', $issn );
1200         return { ids => $recs, count => scalar(@$recs) };
1201 }
1202
1203
1204
1205
1206 __PACKAGE__->register_method(
1207         method  => "fetch_mods_by_copy",
1208         api_name        => "open-ils.search.biblio.mods_from_copy",
1209 );
1210
1211 sub fetch_mods_by_copy {
1212         my( $self, $client, $copyid ) = @_;
1213         my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
1214         return $evt if $evt;
1215         return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
1216         return $apputils->record_to_mvr($record);
1217 }
1218
1219
1220
1221 # -------------------------------------------------------------------------------------
1222
1223 __PACKAGE__->register_method(
1224         method  => "cn_browse",
1225         api_name        => "open-ils.search.callnumber.browse.target",
1226         notes           => "Starts a callnumber browse"
1227         );
1228
1229 __PACKAGE__->register_method(
1230         method  => "cn_browse",
1231         api_name        => "open-ils.search.callnumber.browse.page_up",
1232         notes           => "Returns the previous page of callnumbers", 
1233         );
1234
1235 __PACKAGE__->register_method(
1236         method  => "cn_browse",
1237         api_name        => "open-ils.search.callnumber.browse.page_down",
1238         notes           => "Returns the next page of callnumbers", 
1239         );
1240
1241
1242 # RETURNS array of arrays like so: label, owning_lib, record, id
1243 sub cn_browse {
1244         my( $self, $client, @params ) = @_;
1245         my $method;
1246
1247         $method = 'open-ils.storage.asset.call_number.browse.target.atomic' 
1248                 if( $self->api_name =~ /target/ );
1249         $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1250                 if( $self->api_name =~ /page_up/ );
1251         $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1252                 if( $self->api_name =~ /page_down/ );
1253
1254         return $apputils->simplereq( 'open-ils.storage', $method, @params );
1255 }
1256 # -------------------------------------------------------------------------------------
1257
1258 __PACKAGE__->register_method(
1259         method => "fetch_cn",
1260         api_name => "open-ils.search.callnumber.retrieve",
1261         notes           => "retrieves a callnumber based on ID",
1262         );
1263
1264 sub fetch_cn {
1265         my( $self, $client, $id ) = @_;
1266         my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1267         return $evt if $evt;
1268         return $cn;
1269 }
1270
1271 __PACKAGE__->register_method (
1272         method          => "fetch_copy_by_cn",
1273         api_name                => 'open-ils.search.copies_by_call_number.retrieve',
1274         signature       => q/
1275                 Returns an array of copy id's by callnumber id
1276                 @param cnid The callnumber id
1277                 @return An array of copy ids
1278         /
1279 );
1280
1281 sub fetch_copy_by_cn {
1282         my( $self, $conn, $cnid ) = @_;
1283         return $U->cstorereq(
1284                 'open-ils.cstore.direct.asset.copy.id_list.atomic', 
1285                 { call_number => $cnid, deleted => 'f' } );
1286 }
1287
1288 __PACKAGE__->register_method (
1289         method          => 'fetch_cn_by_info',
1290         api_name                => 'open-ils.search.call_number.retrieve_by_info',
1291         signature       => q/
1292                 @param label The callnumber label
1293                 @param record The record the cn is attached to
1294                 @param org The owning library of the cn
1295                 @return The callnumber object
1296         /
1297 );
1298
1299
1300 sub fetch_cn_by_info {
1301         my( $self, $conn, $label, $record, $org ) = @_;
1302         return $U->cstorereq(
1303                 'open-ils.cstore.direct.asset.call_number.search',
1304                 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1305 }
1306
1307
1308                 
1309
1310
1311 __PACKAGE__->register_method (
1312         method => 'bib_extras',
1313         api_name => 'open-ils.search.biblio.lit_form_map.retrieve.all');
1314 __PACKAGE__->register_method (
1315         method => 'bib_extras',
1316         api_name => 'open-ils.search.biblio.item_form_map.retrieve.all');
1317 __PACKAGE__->register_method (
1318         method => 'bib_extras',
1319         api_name => 'open-ils.search.biblio.item_type_map.retrieve.all');
1320 __PACKAGE__->register_method (
1321         method => 'bib_extras',
1322         api_name => 'open-ils.search.biblio.audience_map.retrieve.all');
1323
1324 sub bib_extras {
1325         my $self = shift;
1326
1327         my $e = new_editor();
1328
1329         return $e->retrieve_all_config_lit_form_map()
1330                 if( $self->api_name =~ /lit_form/ );
1331
1332         return $e->retrieve_all_config_item_form_map()
1333                 if( $self->api_name =~ /item_form_map/ );
1334
1335         return $e->retrieve_all_config_item_type_map()
1336                 if( $self->api_name =~ /item_type_map/ );
1337
1338         return $e->retrieve_all_config_audience_map()
1339                 if( $self->api_name =~ /audience_map/ );
1340
1341         return [];
1342 }
1343
1344
1345
1346 __PACKAGE__->register_method(
1347         method  => 'fetch_slim_record',
1348         api_name        => 'open-ils.search.biblio.record_entry.slim.retrieve',
1349         signature=> q/
1350                 Returns a biblio.record_entry without the attached marcxml
1351         /
1352 );
1353
1354 sub fetch_slim_record {
1355         my( $self, $conn, $ids ) = @_;
1356
1357         #my $editor = OpenILS::Utils::Editor->new;
1358         my $editor = new_editor();
1359         my @res;
1360         for( @$ids ) {
1361                 return $editor->event unless
1362                         my $r = $editor->retrieve_biblio_record_entry($_);
1363                 $r->clear_marc;
1364                 push(@res, $r);
1365         }
1366         return \@res;
1367 }
1368
1369
1370
1371 __PACKAGE__->register_method(
1372         method => 'rec_to_mr_rec_descriptors',
1373         api_name        => 'open-ils.search.metabib.record_to_descriptors',
1374         signature       => q/
1375                 specialized method...
1376                 Given a biblio record id or a metarecord id, 
1377                 this returns a list of metabib.record_descriptor
1378                 objects that live within the same metarecord
1379                 @param args Object of args including:
1380         /
1381 );
1382
1383 sub rec_to_mr_rec_descriptors {
1384         my( $self, $conn, $args ) = @_;
1385
1386         my $rec = $$args{record};
1387         my $mrec        = $$args{metarecord};
1388         my $item_forms = $$args{item_forms};
1389         my $item_types  = $$args{item_types};
1390         my $item_lang   = $$args{item_lang};
1391
1392         my $e = new_editor();
1393         my $recs;
1394
1395         if( !$mrec ) {
1396                 my $map = $e->search_metabib_metarecord_source_map({source => $rec});
1397                 return $e->event unless @$map;
1398                 $mrec = $$map[0]->metarecord;
1399         }
1400
1401         $recs = $e->search_metabib_metarecord_source_map({metarecord => $mrec});
1402         return $e->event unless @$recs;
1403
1404         my @recs = map { $_->source } @$recs;
1405         my $search = { record => \@recs };
1406         $search->{item_form} = $item_forms if $item_forms and @$item_forms;
1407         $search->{item_type} = $item_types if $item_types and @$item_types;
1408         $search->{item_lang} = $item_lang if $item_lang;
1409
1410         my $desc = $e->search_metabib_record_descriptor($search);
1411
1412         return { metarecord => $mrec, descriptors => $desc };
1413 }
1414
1415
1416
1417
1418 __PACKAGE__->register_method(
1419         method => 'copies_created_on',  
1420 );
1421
1422
1423 sub copies_created_on {
1424         my( $self, $conn, $auth, $org, $date ) = @_;
1425         my $e = new_editor(authtoken=>$auth);
1426         return $e->event unless $e->checkauth;
1427 }
1428
1429
1430 __PACKAGE__->register_method(
1431         method => 'fetch_age_protect',
1432         api_name => 'open-ils.search.copy.age_protect.retrieve.all',
1433 );
1434
1435 sub fetch_age_protect {
1436         return new_editor()->retrieve_all_config_rule_age_hold_protect();
1437 }
1438
1439
1440 __PACKAGE__->register_method(
1441         method => 'copies_by_cn_label',
1442         api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label',
1443 );
1444
1445 __PACKAGE__->register_method(
1446         method => 'copies_by_cn_label',
1447         api_name => 'open-ils.search.asset.copy.retrieve_by_cn_label.staff',
1448 );
1449
1450 sub copies_by_cn_label {
1451         my( $self, $conn, $record, $label, $circ_lib ) = @_;
1452         my $e = new_editor();
1453         my $cns = $e->search_asset_call_number({record => $record, label => $label, deleted => 'f'}, {idlist=>1});
1454         return [] unless @$cns;
1455
1456         # show all non-deleted copies in the staff client ...
1457         if ($self->api_name =~ /staff$/o) {
1458                 return $e->search_asset_copy({call_number => $cns, circ_lib => $circ_lib, deleted => 'f'}, {idlist=>1});
1459         }
1460
1461         # ... otherwise, grab the copies ...
1462         my $copies = $e->search_asset_copy(
1463                 [ {call_number => $cns, circ_lib => $circ_lib, deleted => 'f', opac_visible => 't'},
1464                   {flesh => 1, flesh_fields => { acp => [ qw/location status/] } }
1465                 ]
1466         );
1467
1468         # ... and test for location and status visibility
1469         return [ map { ($U->is_true($_->location->opac_visible) && $U->is_true($_->status->holdable)) ? ($_->id) : () } @$copies ];
1470 }
1471
1472
1473
1474 1;
1475
1476