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