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