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