]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
commented out duplicate method to make sure no one is using it
[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 use OpenILS::EX;
6
7 use JSON;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Utils::ModsParser;
10 use OpenSRF::Utils::SettingsClient;
11
12 use OpenSRF::Utils::Logger qw/:logger/;
13
14
15 use JSON;
16
17 use Time::HiRes qw(time);
18 use OpenSRF::EX qw(:try);
19 use Digest::MD5 qw(md5_hex);
20
21 use XML::LibXML;
22 use XML::LibXSLT;
23
24 use Data::Dumper;
25 $Data::Dumper::Indent = 0;
26
27 use OpenILS::Application::AppUtils;
28 my $apputils = "OpenILS::Application::AppUtils";
29 my $U = $apputils;
30
31
32 # useful for MARC based searches
33 my $cat_search_hash =  {
34         isbn    => [ { tag => '020', subfield => 'a' }, ],
35         issn    => [ { tag => '022', subfield => 'a' }, ],
36 };
37
38
39
40
41 # ---------------------------------------------------------------------------
42 # takes a list of record id's and turns the docs into friendly 
43 # mods structures. Creates one MODS structure for each doc id.
44 # ---------------------------------------------------------------------------
45 sub _records_to_mods {
46         my @ids = @_;
47         
48         my @results;
49         my @marcxml_objs;
50
51         my $session = OpenSRF::AppSession->create("open-ils.storage");
52         my $request = $session->request(
53                         "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
54
55         while( my $resp = $request->recv ) {
56                 my $content = $resp->content;
57                 my $u = OpenILS::Utils::ModsParser->new();
58                 $u->start_mods_batch( $content->marc );
59                 my $mods = $u->finish_mods_batch();
60                 $mods->doc_id($content->id());
61                 $mods->tcn($content->tcn_value);
62                 push @results, $mods;
63         }
64
65         $session->disconnect();
66         return \@results;
67 }
68
69 __PACKAGE__->register_method(
70         method  => "record_id_to_mods",
71         api_name        => "open-ils.search.biblio.record.mods.retrieve",
72         argc            => 1, 
73         note            => "Provide ID, we provide the mods"
74 );
75
76 # converts a record into a mods object with copy counts attached
77 sub record_id_to_mods {
78
79         my( $self, $client, $org_id, $id ) = @_;
80
81         my $mods_list = _records_to_mods( $id );
82         my $mods_obj = $mods_list->[0];
83         my $cmethod = $self->method_lookup(
84                         "open-ils.search.biblio.record.copy_count");
85         my ($count) = $cmethod->run($org_id, $id);
86         $mods_obj->copy_count($count);
87
88         return $mods_obj;
89 }
90
91
92
93 __PACKAGE__->register_method(
94         method  => "record_id_to_mods_slim",
95         api_name        => "open-ils.search.biblio.record.mods_slim.retrieve",
96         argc            => 1, 
97         note            => "Provide ID, we provide the mods"
98 );
99
100 # converts a record into a mods object with NO copy counts attached
101 sub record_id_to_mods_slim {
102         my( $self, $client, $id ) = @_;
103         return undef unless defined $id;
104
105         if(ref($id) and ref($id) == 'ARRAY') {
106                 return _records_to_mods( @$id );
107         }
108         my $mods_list = _records_to_mods( $id );
109         my $mods_obj = $mods_list->[0];
110         return $mods_obj;
111 }
112
113
114 # Returns the number of copies attached to a record based on org location
115 __PACKAGE__->register_method(
116         method  => "record_id_to_copy_count",
117         api_name        => "open-ils.search.biblio.record.copy_count",
118 );
119
120 __PACKAGE__->register_method(
121         method  => "record_id_to_copy_count",
122         api_name        => "open-ils.search.biblio.metarecord.copy_count",
123 );
124
125 __PACKAGE__->register_method(
126         method  => "record_id_to_copy_count",
127         api_name        => "open-ils.search.biblio.metarecord.copy_count.staff",
128 );
129 sub record_id_to_copy_count {
130         my( $self, $client, $org_id, $record_id, $format ) = @_;
131
132         $format = undef if ($format eq 'all');
133
134         my $method = "open-ils.storage.biblio.record_entry.copy_count.atomic";
135         my $key = "record";
136         if($self->api_name =~ /metarecord/) {
137                 $method = "open-ils.storage.metabib.metarecord.copy_count.atomic";
138                 $key = "metarecord";
139         }
140
141         if($self->api_name =~ /staff/ ) {
142                 $method =~ s/atomic/staff\.atomic/og;
143                 warn "Doing staff search $method\n";
144         }
145
146
147         my $session = OpenSRF::AppSession->create("open-ils.storage");
148         warn "copy_count retrieve $record_id\n";
149         return undef unless(defined $record_id);
150
151         my $request = $session->request(
152                 $method, org_unit => $org_id => $key => $record_id, format => $format );
153
154
155         my $count = $request->gather(1);
156         $session->disconnect();
157         return [ sort { $a->{depth} <=> $b->{depth} } @$count ];
158
159 }
160
161
162
163
164 __PACKAGE__->register_method(
165         method  => "biblio_search_tcn",
166         api_name        => "open-ils.search.biblio.tcn",
167         argc            => 3, 
168         note            => "Retrieve a record by TCN",
169 );
170
171 sub biblio_search_tcn {
172
173         my( $self, $client, $tcn ) = @_;
174
175         $tcn =~ s/.*?(\w+)\s*$/$1/o;
176         warn "Searching TCN $tcn\n";
177
178         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
179         my $request = $session->request( 
180                         "open-ils.storage.direct.biblio.record_entry.search_where.atomic", 
181                         { tcn_value => $tcn, deleted => 'f' } );
182                         #"open-ils.storage.direct.biblio.record_entry.search.tcn_value.atomic", $tcn );
183         my $record_entry = $request->gather(1);
184
185         my @ids;
186         for my $record (@$record_entry) {
187                 push @ids, $record->id;
188         }
189
190         $session->disconnect();
191
192         my $size = @ids;
193         return { count => $size, ids => \@ids };
194 }
195
196
197 # --------------------------------------------------------------------------------
198
199 __PACKAGE__->register_method(
200         method  => "biblio_barcode_to_copy",
201         api_name        => "open-ils.search.asset.copy.find_by_barcode",);
202 sub biblio_barcode_to_copy { 
203         my( $self, $client, $barcode ) = @_;
204         my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
205         return $evt if $evt;
206         return $copy;
207 }
208
209 __PACKAGE__->register_method(
210         method  => "biblio_id_to_copy",
211         api_name        => "open-ils.search.asset.copy.batch.retrieve",);
212 sub biblio_id_to_copy { 
213         my( $self, $client, $ids ) = @_;
214         $logger->info("Fetching copies @$ids");
215         return $U->storagereq(
216                 "open-ils.storage.direct.asset.copy.batch.retrieve.atomic", @$ids );
217 }
218
219
220 __PACKAGE__->register_method(
221         method  => "copy_retrieve", 
222         api_name        => "open-ils.search.asset.copy.retrieve",);
223 sub copy_retrieve {
224         my( $self, $client, $cid ) = @_;
225         my( $copy, $evt ) = $U->fetch_copy($cid);
226         return $evt if $evt;
227         return $copy;
228 }
229
230
231 __PACKAGE__->register_method(
232         method  => "fleshed_copy_retrieve_batch",
233         api_name        => "open-ils.search.asset.copy.fleshed.batch.retrieve",);
234
235 sub fleshed_copy_retrieve_batch { 
236         my( $self, $client, $ids ) = @_;
237         $logger->info("Fetching fleshed copies @$ids");
238         return $U->storagereq(
239                 "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic", @$ids );
240 }
241
242
243 __PACKAGE__->register_method(
244         method  => "fleshed_copy_retrieve",
245         api_name        => "open-ils.search.asset.copy.fleshed.retrieve",);
246
247 sub fleshed_copy_retrieve { 
248         my( $self, $client, $id ) = @_;
249         my( $c, $e) = $U->fetch_fleshed_copy($id);
250         return $e if $e;
251         return $c;
252 }
253
254
255
256 __PACKAGE__->register_method(
257         method  => "biblio_barcode_to_title",
258         api_name        => "open-ils.search.biblio.find_by_barcode",
259 );
260
261 sub biblio_barcode_to_title {
262         my( $self, $client, $barcode ) = @_;
263
264         my $title = $apputils->simple_scalar_request(
265                 "open-ils.storage",
266                 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
267
268         return { ids => [ $title->id ], count => 1 } if $title;
269         return { count => 0 };
270 }
271
272
273 __PACKAGE__->register_method(
274         method  => "biblio_copy_to_mods",
275         api_name        => "open-ils.search.biblio.copy.mods.retrieve",
276 );
277
278 # takes a copy object and returns it fleshed mods object
279 sub biblio_copy_to_mods {
280         my( $self, $client, $copy ) = @_;
281
282         my $volume = $U->storagereq( 
283                 "open-ils.storage.direct.asset.call_number.retrieve",
284                 $copy->call_number() );
285
286         my $mods = _records_to_mods($volume->record());
287         $mods = shift @$mods;
288         $volume->copies([$copy]);
289         push @{$mods->call_numbers()}, $volume;
290
291         return $mods;
292 }
293
294
295 # ----------------------------------------------------------------------------
296 # These are the main OPAC search methods
297 # ----------------------------------------------------------------------------
298
299 __PACKAGE__->register_method(
300         method          => 'the_quest_for_knowledge',
301         api_name                => 'open-ils.search.biblio.multiclass',
302         signature       => q/
303                 Performs a multi class bilbli or metabib search
304                 @param searchhash A search object layed out like so:
305                         searches : { "$class" : "$value", ...}
306                         org_unit : The org id to focus the search at
307                         depth           : The org depth
308                         limit           : The search limit
309                         offset  : The search offset
310                         format  : The MARC format
311                         sort            : What field to sort the results on [ author | title | pubdate ]
312                         sort_dir        : What direction do we sort? [ asc | desc ]
313                 @return An object of the form 
314                         { "count" : $count, "ids" : [ [ $id, $relevancy, $total ], ...] }
315         /
316 );
317
318 __PACKAGE__->register_method(
319         method          => 'the_quest_for_knowledge',
320         api_name                => 'open-ils.search.record.multiclass.staff',
321         signature       => q/@see open-ils.search.biblio.multiclass/);
322 __PACKAGE__->register_method(
323         method          => 'the_quest_for_knowledge',
324         api_name                => 'open-ils.search.metabib.multiclass',
325         signature       => q/@see open-ils.search.biblio.multiclass/);
326 __PACKAGE__->register_method(
327         method          => 'the_quest_for_knowledge',
328         api_name                => 'open-ils.search.metabib.multiclass.staff',
329         signature       => q/@see open-ils.search.biblio.multiclass/);
330
331
332 sub the_quest_for_knowledge {
333         my( $self, $conn, $searchhash ) = @_;
334
335         my $method = 'open-ils.storage.biblio.multiclass.search_fts';
336         my $ismeta = 0;
337         my @recs;
338
339         if($self->api_name =~ /metabib/) {
340                 $ismeta = 1;
341                 $method =~ s/biblio/metabib/o;
342         }
343
344         $method .= ".staff" if($self->api_name =~ /staff/);
345         $method .= ".atomic";
346
347         for (keys %$searchhash) { 
348                 delete $$searchhash{$_} unless defined $$searchhash{$_}; }
349
350         my $result = $U->storagereq( $method, %$searchhash );
351
352         return {count => 0} unless ($result && $$result[0]);
353
354         for my $r (@$result) { push(@recs, $r) if ($r and $$r[0]); }
355         return { ids => \@recs, 
356                 count => ($ismeta) ? $result->[0]->[3] : $result->[0]->[2] };
357 }
358
359
360
361
362
363 # XXX XXX old class search...
364 =head old search method
365 __PACKAGE__->register_method(
366         method          => "record_search_class",
367         api_name                => "open-ils.search.biblio.record.class.search",
368         signature       => q/
369                 Performs a class search for biblio records (not metarecords)
370                 @param class The search class to use
371                 @param args A hash of named parameters including:
372                         term            : The search string,
373                         org_unit : The org id to focus the search at
374                         depth           : The org depth
375                         limit           : The search limit
376                         offset  : The search offset
377                         format  : The MARC format
378                         sort            : What field to sort the results on [ author | title | pubdate ]
379                         sort_dir        : What direction do we sort? [ asc | desc ]
380                 The only required argument is the term.
381                 @return 
382         /);
383
384 __PACKAGE__->register_method(
385         method          => "record_search_class",
386         api_name                => "open-ils.search.biblio.record.class.search.staff",
387         signature       => q/@see open-ils.search.biblio.record.class.search/);
388 __PACKAGE__->register_method(
389         method  => "record_search_class",
390         api_name        => "open-ils.search.biblio.metabib.class.search",
391         signature       => q/@see open-ils.search.biblio.record.class.search/);
392 __PACKAGE__->register_method(
393         method  => "record_search_class",
394         api_name        => "open-ils.search.biblio.metabib.class.search.staff",
395         signature       => q/@see open-ils.search.biblio.record.class.search/);
396
397 sub record_search_class {
398         my( $self, $client, $class, $args ) = @_;
399
400         my $type                = ($self->api_name =~ /metabib/) ? 1 : 0;
401         my $method      = "open-ils.storage.metabib.$class.post_filter.search_fts.metarecord.atomic";
402         $method         = "open-ils.storage.biblio.$class.search_fts.record.atomic" unless $type;
403
404         if(!$$args{'sort'}) {
405                 delete $$args{'sort'}; 
406                 delete $$args{'sort_dir'};
407         }
408         $$args{limit}   = 200 if (!$$args{limit} || $$args{limit} > 1000);
409         $$args{offset} ||= 0;
410
411         $logger->info("Class search with args: ".Dumper($args));
412
413         $method =~ s/\.atomic/.staff.atomic/o if $self->api_name =~ /staff/o;
414         my $result = $U->storagereq( $method, %$args );
415
416         if($result and $result->[0]) {
417                 my $recs = [];
418                 for my $r (@$result) { push(@$recs, $r) if ($r and $r->[0]); }
419                 return { ids => $recs, 
420                         count => ($type) ? $result->[0]->[3] : $result->[0]->[2] };
421         }
422         return { count => 0 };
423 }
424 =cut
425
426
427
428
429 __PACKAGE__->register_method(
430         method  => "biblio_mrid_to_modsbatch_batch",
431         api_name        => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
432
433 sub biblio_mrid_to_modsbatch_batch {
434         my( $self, $client, $mrids) = @_;
435         warn "Performing mrid_to_modsbatch_batch...";
436         my @mods;
437         my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
438         for my $id (@$mrids) {
439                 next unless defined $id;
440                 my ($m) = $method->run($id);
441                 push @mods, $m;
442         }
443         return \@mods;
444 }
445
446
447 __PACKAGE__->register_method(
448         method  => "biblio_mrid_to_modsbatch",
449         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
450         notes           => <<"  NOTES");
451         Returns the mvr associated with a given metarecod. If none exists, 
452         it is created.
453         NOTES
454
455 __PACKAGE__->register_method(
456         method  => "biblio_mrid_to_modsbatch",
457         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
458         notes           => <<"  NOTES");
459         Returns the mvr associated with a given metarecod. If none exists, 
460         it is created.
461         NOTES
462
463 sub biblio_mrid_to_modsbatch {
464         my( $self, $client, $mrid ) = @_;
465
466         warn "Grabbing mvr for $mrid\n";
467
468         my $mr = _grab_metarecord($mrid);
469         return undef unless $mr;
470
471         if( my $m = $self->biblio_mrid_check_mvr($client, $mr)) {
472                 return $m;
473         }
474
475         return $self->biblio_mrid_make_modsbatch( $client, $mr ); 
476 }
477
478 # converts a metarecord to an mvr
479 sub _mr_to_mvr {
480         my $mr = shift;
481         my $perl = JSON->JSON2perl($mr->mods());
482         return Fieldmapper::metabib::virtual_record->new($perl);
483 }
484
485 # checks to see if a metarecord has mods, if so returns true;
486
487 __PACKAGE__->register_method(
488         method  => "biblio_mrid_check_mvr",
489         api_name        => "open-ils.search.biblio.metarecord.mods_slim.check",
490         notes           => <<"  NOTES");
491         Takes a metarecord ID or a metarecord object and returns true
492         if the metarecord already has an mvr associated with it.
493         NOTES
494
495 sub biblio_mrid_check_mvr {
496         my( $self, $client, $mrid ) = @_;
497         my $mr; 
498
499         if(ref($mrid)) { $mr = $mrid; } 
500         else { $mr = _grab_metarecord($mrid); }
501
502         warn "Checking mvr for mr " . $mr->id . "\n";
503
504         return _mr_to_mvr($mr) if $mr->mods();
505         return undef;
506 }
507
508 sub _grab_metarecord {
509
510         my $mrid = shift;
511         warn "Grabbing MR $mrid\n";
512
513         my $mr = OpenILS::Application::AppUtils->simple_scalar_request( 
514                 "open-ils.storage", 
515                 "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
516
517         if(!$mr) {
518                 throw OpenSRF::EX::ERROR 
519                         ("No metarecord exists with the given id: $mrid");
520         }
521
522         return $mr;
523 }
524
525 __PACKAGE__->register_method(
526         method  => "biblio_mrid_make_modsbatch",
527         api_name        => "open-ils.search.biblio.metarecord.mods_slim.create",
528         notes           => <<"  NOTES");
529         Takes either a metarecord ID or a metarecord object.
530         Forces the creations of an mvr for the given metarecord.
531         The created mvr is returned.
532         NOTES
533
534 sub biblio_mrid_make_modsbatch {
535
536         my( $self, $client, $mrid ) = @_;
537
538         my $mr; 
539         if(ref($mrid)) { $mr = $mrid; }
540         else { $mr = _grab_metarecord($mrid); }
541         $mrid = $mr->id;
542
543         warn "Forcing mvr creation for mr " . $mr->id . "\n";
544         my $master_id = $mr->master_record;
545
546         my $session = OpenSRF::AppSession->create("open-ils.storage");
547
548         # grab the records attached to this metarecod 
549         warn "Creating mods batch for metarecord $mrid\n";
550         my $meth = "open-ils.search.biblio.metarecord_to_records.staff";
551         $meth = $self->method_lookup($meth);
552         my ($id_hash) = $meth->run($mrid);
553         my @ids = @{$id_hash->{ids}};
554         if(@ids < 1) { return undef; }
555
556         warn "Master ID is $master_id\n";
557         # grab the master record to start the mods batch 
558
559         $meth = "open-ils.storage.direct.biblio.record_entry.retrieve";
560
561         my $record = $session->request(
562                         "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
563         $record = $record->gather(1);
564
565         #my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
566
567         if(!$record) {
568                 warn "No record returned with id $master_id";
569                 throw OpenSRF::EX::ERROR 
570         }
571
572         my $u = OpenILS::Utils::ModsParser->new();
573         $u->start_mods_batch( $record->marc );
574         my $main_doc_id = $record->id();
575
576         @ids = grep { $_ ne $master_id } @ids;
577
578         # now we have to collect all of the marc objects and push them into a mods batch
579         my $request = $session->request(
580                 "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
581
582         while( my $response = $request->recv() ) {
583
584                 next unless $response;
585                 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
586                         throw $response ($response->stringify);
587                 }
588
589                 my $content = $response->content;
590
591                 if( $content ) {
592                         $u->push_mods_batch( $content->marc );
593                 }
594         }
595
596         my $mods = $u->finish_mods_batch();
597         $mods->doc_id($mrid);
598         $request->finish();
599
600         $client->respond_complete($mods);
601
602         my $mods_string = JSON->perl2JSON($mods->decast);
603
604         $mr->mods($mods_string);
605
606         my $req = $session->request( 
607                 "open-ils.storage.direct.metabib.metarecord.update", $mr );
608
609
610         $req->gather(1);
611         $session->finish();
612         $session->disconnect();
613
614         return undef;
615 }
616
617
618
619 # converts a mr id into a list of record ids
620
621 __PACKAGE__->register_method(
622         method  => "biblio_mrid_to_record_ids",
623         api_name        => "open-ils.search.biblio.metarecord_to_records",
624 );
625
626 __PACKAGE__->register_method(
627         method  => "biblio_mrid_to_record_ids",
628         api_name        => "open-ils.search.biblio.metarecord_to_records.staff",
629 );
630
631 sub biblio_mrid_to_record_ids {
632         my( $self, $client, $mrid, $format ) = @_;
633
634         throw OpenSRF::EX::InvalidArg 
635                 ("search.biblio.metarecord_to_record_ids requires mr id")
636                         unless defined( $mrid );
637
638         warn "Searching for record for MR $mrid and format $format\n";
639
640         my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
641         if($self and $self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
642         warn "Performing record retrieval with method $method\n";
643
644
645         my $mrmaps = OpenILS::Application::AppUtils->simple_scalar_request( 
646                         "open-ils.storage", $method, $mrid, $format );
647
648         warn Dumper $mrmaps;
649
650         my $size = @$mrmaps;    
651
652         return { count => $size, ids => $mrmaps };
653
654 }
655
656
657 __PACKAGE__->register_method(
658         method  => "biblio_record_to_marc_html",
659         api_name        => "open-ils.search.biblio.record.html" );
660
661 my $parser              = XML::LibXML->new();
662 my $xslt                        = XML::LibXSLT->new();
663 my $marc_sheet;
664
665 my $settings_client = OpenSRF::Utils::SettingsClient->new();
666 sub biblio_record_to_marc_html {
667         my( $self, $client, $recordid ) = @_;
668
669         if( !$marc_sheet ) {
670                 my $dir = $settings_client->config_value( "dirs", "xsl" );
671                 my $xsl = $settings_client->config_value(
672                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
673
674                 $xsl = $parser->parse_file("$dir/$xsl");
675                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
676         }
677
678
679         my $record = $apputils->simple_scalar_request(
680                 "open-ils.storage", 
681                 "open-ils.storage.direct.biblio.record_entry.retrieve",
682                 $recordid );
683
684         my $xmldoc = $parser->parse_string($record->marc);
685         my $html = $marc_sheet->transform($xmldoc);
686         $html = $html->toString();
687         return $html;
688
689 }
690
691
692 =head duplicate
693 __PACKAGE__->register_method(
694         method  => "retrieve_all_copy_locations",
695         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
696
697 my $shelving_locations;
698 sub retrieve_all_copy_locations {
699         my( $self, $client ) = @_;
700         if(!$shelving_locations) {
701                 $shelving_locations = $apputils->simple_scalar_request(
702                         "open-ils.storage", 
703                         "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
704         }
705         return $shelving_locations;
706 }
707 =cut
708
709
710
711 __PACKAGE__->register_method(
712         method  => "retrieve_all_copy_statuses",
713         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
714
715 my $copy_statuses;
716 sub retrieve_all_copy_statuses {
717         my( $self, $client ) = @_;
718         if(!$copy_statuses) {
719                 $copy_statuses = $apputils->simple_scalar_request(
720                         "open-ils.storage",
721                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
722         }
723         return $copy_statuses;
724 }
725
726
727 __PACKAGE__->register_method(
728         method  => "copy_counts_per_org",
729         api_name        => "open-ils.search.biblio.copy_counts.retrieve");
730
731 __PACKAGE__->register_method(
732         method  => "copy_counts_per_org",
733         api_name        => "open-ils.search.biblio.copy_counts.retrieve.staff");
734
735 sub copy_counts_per_org {
736         my( $self, $client, $record_id ) = @_;
737
738         warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
739
740         my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
741         if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
742
743         my $counts = $apputils->simple_scalar_request(
744                 "open-ils.storage", $method, $record_id );
745
746         $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
747         return $counts;
748 }
749
750
751 __PACKAGE__->register_method(
752         method          => "copy_count_summary",
753         api_name        => "open-ils.search.biblio.copy_counts.summary.retrieve",
754         notes           => <<"  NOTES");
755         returns an array of these:
756                 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
757                 where statusx is a copy status name.  the statuses are sorted
758                 by id.
759         NOTES
760
761 sub copy_count_summary {
762         my( $self, $client, $rid ) = @_;
763         my $method = "open-ils.storage.biblio.record_entry.status_copy_count.atomic";
764         return $apputils->simple_scalar_request( "open-ils.storage", $method, $rid );
765 }
766
767
768 =head
769 __PACKAGE__->register_method(
770         method          => "multiclass_search",
771         api_name        => "open-ils.search.biblio.multiclass",
772         notes           => <<"  NOTES");
773                 Performs a multiclass search
774                 PARAMS( searchBlob, org_unit, format, limit ) 
775                 where searchBlob is defined like this:
776                         { 
777                                 "title" : { "term" : "water" }, 
778                                 "author" : { "term" : "smith" }, 
779                                 ... 
780                         }
781         NOTES
782
783 __PACKAGE__->register_method(
784         method          => "multiclass_search",
785         api_name        => "open-ils.search.biblio.multiclass.staff",
786         notes           => "see open-ils.search.biblio.multiclass" );
787
788 sub multiclass_search {
789         my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
790
791         $logger->debug("Performing multiclass search with org => $orgid, " .
792                 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
793
794         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
795         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
796
797
798         my $records = $apputils->simplereq(
799                 'open-ils.storage', $meth, 
800                  org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
801
802         my $count = 0;
803         my $recs = [];
804
805         if( ref($records) and $records->[0] and 
806                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
807
808         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
809
810         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
811         return { ids => $recs, count => $count };
812 }
813 =cut
814
815
816
817 =head comment-1
818 __PACKAGE__->register_method(
819         method          => "multiclass_search",
820         api_name                => "open-ils.search.biblio.multiclass",
821         signature       => q/
822                 Performs a multiclass search
823                 @param args A names hash of arguments:
824                         org_unit : The org to focus the search on
825                         depth           : The search depth
826                         format  : Item format
827                         limit           : Return limit
828                         offset  : Search offset
829                         searches : A named hash of searches which has the following format:
830                                 { 
831                                         "title" : { "term" : "water" }, 
832                                         "author" : { "term" : "smith" }, 
833                                         ... 
834                                 }
835                 @return { ids : <array of ids>, count : hitcount }
836         /
837 );
838
839 __PACKAGE__->register_method(
840         method          => "multiclass_search",
841         api_name                => "open-ils.search.biblio.multiclass.staff",
842         notes           => q/@see open-ils.search.biblio.multiclass/ );
843
844 sub multiclass_search {
845         my( $self, $client, $args ) = @_;
846
847         $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
848         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
849         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
850
851         my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
852
853         my $count = 0;
854         my $recs = [];
855
856         if( ref($records) and $records->[0] and 
857                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
858
859         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
860
861         return { ids => $recs, count => $count };
862 }
863
864 =cut
865
866
867
868 # XXX Fix me.. 
869 # request open-ils.storage
870 #        open-ils.storage.biblio.full_rec.multi_search.atomic "searches",
871 #        [{"term":"harry","restrict": [{"tag":245,"subfield":"a"}]}], "org_unit", 1,
872 #        "limit",5,"sort","author","item_type","g"
873
874
875 __PACKAGE__->register_method(
876         method          => "marc_search",
877         api_name        => "open-ils.search.biblio.marc",
878         notes           => <<"  NOTES");
879                 Performs a multiclass search
880                 PARAMS( searchBlob, org_unit, format ) 
881                 where searchBlob is defined like this:
882                 [ 
883                         {
884                                 "term":"shakespeare",
885                                 "restrict":[{"tag":"245","subfield":"a"}] 
886                         }, 
887                         {
888                                 "term":"bloom",
889                                 "restrict":[{"tag":"100","subfield":"a"}] 
890                         } 
891                 ]
892         NOTES
893
894
895 sub marc_search {
896         my( $self, $client, $searchBlob, $orgid, $format ) = @_;
897
898         $logger->debug("Performing MARC search with org => $orgid, " .
899                 "format => $format and search blob " . Dumper($searchBlob) );
900                 
901         my $records =  $apputils->simplereq(
902                 'open-ils.storage',
903                 'open-ils.storage.metabib.full_rec.multi_search.atomic',
904                 searches => $searchBlob, org_unit => $orgid, format => $format );
905
906         my $count = 0;
907         my $recs = [];
908
909         if( ref($records) and $records->[0] and 
910                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
911
912         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
913
914         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
915         return { ids => $recs, count => $count };
916
917 }
918
919
920
921 __PACKAGE__->register_method(
922         method  => "biblio_search_isbn",
923         api_name        => "open-ils.search.biblio.isbn",
924 );
925
926 sub biblio_search_isbn { 
927         my( $self, $client, $isbn ) = @_;
928
929         $logger->debug("Searching ISBN $isbn");
930
931         my $method = $self->method_lookup("open-ils.search.biblio.marc");
932
933         my ($records) = $method->run(  
934                 [ {     term => $isbn,
935                                 restrict => $cat_search_hash->{isbn} } ], 1);
936
937         return $records;
938 }
939
940
941 __PACKAGE__->register_method(
942         method  => "biblio_search_issn",
943         api_name        => "open-ils.search.biblio.issn",
944 );
945
946 sub biblio_search_issn { 
947         my( $self, $client, $issn ) = @_;
948
949         $logger->debug("Searching ISSN $issn");
950
951         my $method = $self->method_lookup("open-ils.search.biblio.marc");
952
953         my ($records) = $method->run(  
954                 [ {     term => $issn,
955                                 restrict => $cat_search_hash->{issn} } ], 1);
956
957         return $records;
958 }
959
960
961
962
963 __PACKAGE__->register_method(
964         method  => "fetch_mods_by_copy",
965         api_name        => "open-ils.search.biblio.mods_from_copy",
966 );
967
968 sub fetch_mods_by_copy {
969         my( $self, $client, $copyid ) = @_;
970         my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
971         return $evt if $evt;
972         return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
973         return $apputils->record_to_mvr($record);
974 }
975
976
977
978
979
980 # -------------------------------------------------------------------------------------
981
982 __PACKAGE__->register_method(
983         method  => "cn_browse",
984         api_name        => "open-ils.search.callnumber.browse.target",
985         notes           => "Starts a callnumber browse"
986         );
987
988 __PACKAGE__->register_method(
989         method  => "cn_browse",
990         api_name        => "open-ils.search.callnumber.browse.page_up",
991         notes           => "Returns the previous page of callnumbers", 
992         );
993
994 __PACKAGE__->register_method(
995         method  => "cn_browse",
996         api_name        => "open-ils.search.callnumber.browse.page_down",
997         notes           => "Returns the next page of callnumbers", 
998         );
999
1000
1001 # RETURNS array of arrays like so: label, owning_lib, record, id
1002 sub cn_browse {
1003         my( $self, $client, @params ) = @_;
1004         my $method;
1005
1006         $method = 'open-ils.storage.asset.call_number.browse.target.atomic' 
1007                 if( $self->api_name =~ /target/ );
1008         $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
1009                 if( $self->api_name =~ /page_up/ );
1010         $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
1011                 if( $self->api_name =~ /page_down/ );
1012
1013         return $apputils->simplereq( 'open-ils.storage', $method, @params );
1014 }
1015 # -------------------------------------------------------------------------------------
1016
1017 __PACKAGE__->register_method(
1018         method => "fetch_cn",
1019         api_name => "open-ils.search.callnumber.retrieve",
1020         notes           => "retrieves a callnumber based on ID",
1021         );
1022
1023 sub fetch_cn {
1024         my( $self, $client, $id ) = @_;
1025         my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
1026         return $evt if $evt;
1027         return $cn;
1028 }
1029
1030 __PACKAGE__->register_method (
1031         method          => "fetch_copy_by_cn",
1032         api_name                => 'open-ils.search.copies_by_call_number.retrieve',
1033         signature       => q/
1034                 Returns an array of copy id's by callnumber id
1035                 @param cnid The callnumber id
1036                 @return An array of copy ids
1037         /
1038 );
1039
1040 sub fetch_copy_by_cn {
1041         my( $self, $conn, $cnid ) = @_;
1042         return $U->storagereq(
1043                 'open-ils.storage.id_list.asset.copy.search_where.atomic', 
1044                 { call_number => $cnid, deleted => 'f' } );
1045 }
1046
1047 __PACKAGE__->register_method (
1048         method          => 'fetch_cn_by_info',
1049         api_name                => 'open-ils.search.call_number.retrieve_by_info',
1050         signature       => q/
1051                 @param label The callnumber label
1052                 @param record The record the cn is attached to
1053                 @param org The owning library of the cn
1054                 @return The callnumber object
1055         /
1056 );
1057
1058 sub fetch_cn_by_info {
1059         my( $self, $conn, $label, $record, $org ) = @_;
1060         return $U->storagereq(
1061                 'open-ils.storage.direct.asset.call_number.search_where',
1062                 { label => $label, record => $record, owning_lib => $org, deleted => 'f' });
1063 }
1064                 
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075 1;
1076
1077