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