]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search/Biblio.pm
white space :) added some log lines
[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.tcn_value.atomic", $tcn );
181         my $record_entry = $request->gather(1);
182
183         my @ids;
184         for my $record (@$record_entry) {
185                 push @ids, $record->id;
186         }
187
188         $session->disconnect();
189
190         my $size = @ids;
191         return { count => $size, ids => \@ids };
192 }
193
194
195 # --------------------------------------------------------------------------------
196
197 __PACKAGE__->register_method(
198         method  => "biblio_barcode_to_copy",
199         api_name        => "open-ils.search.asset.copy.find_by_barcode",);
200 sub biblio_barcode_to_copy { 
201         my( $self, $client, $barcode ) = @_;
202         my( $copy, $evt ) = $U->fetch_copy_by_barcode($barcode);
203         return $evt if $evt;
204         return $copy;
205 }
206
207 __PACKAGE__->register_method(
208         method  => "biblio_id_to_copy",
209         api_name        => "open-ils.search.asset.copy.batch.retrieve",);
210 sub biblio_id_to_copy { 
211         my( $self, $client, $ids ) = @_;
212         $logger->info("Fetching copies @$ids");
213         return $U->storagereq(
214                 "open-ils.storage.direct.asset.copy.batch.retrieve.atomic", @$ids );
215 }
216
217
218 __PACKAGE__->register_method(
219         method  => "copy_retrieve", 
220         api_name        => "open-ils.search.asset.copy.retrieve",);
221 sub copy_retrieve {
222         my( $self, $client, $cid ) = @_;
223         my( $copy, $evt ) = $U->fetch_copy($cid);
224         return $evt if $evt;
225         return $copy;
226 }
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->storagereq(
237                 "open-ils.storage.fleshed.asset.copy.batch.retrieve.atomic", @$ids );
238 }
239
240
241 __PACKAGE__->register_method(
242         method  => "fleshed_copy_retrieve",
243         api_name        => "open-ils.search.asset.copy.fleshed.retrieve",);
244
245 sub fleshed_copy_retrieve { 
246         my( $self, $client, $id ) = @_;
247         my( $c, $e) = $U->fetch_fleshed_copy($id);
248         return $e if $e;
249         return $c;
250 }
251
252
253
254 __PACKAGE__->register_method(
255         method  => "biblio_barcode_to_title",
256         api_name        => "open-ils.search.biblio.find_by_barcode",
257 );
258
259 sub biblio_barcode_to_title {
260         my( $self, $client, $barcode ) = @_;
261
262         my $title = $apputils->simple_scalar_request(
263                 "open-ils.storage",
264                 "open-ils.storage.biblio.record_entry.retrieve_by_barcode", $barcode );
265
266         return { ids => [ $title->id ], count => 1 } if $title;
267         return { count => 0 };
268 }
269
270
271 __PACKAGE__->register_method(
272         method  => "biblio_copy_to_mods",
273         api_name        => "open-ils.search.biblio.copy.mods.retrieve",
274 );
275
276 # takes a copy object and returns it fleshed mods object
277 sub biblio_copy_to_mods {
278         my( $self, $client, $copy ) = @_;
279
280         my $volume = $U->storagereq( 
281                 "open-ils.storage.direct.asset.call_number.retrieve",
282                 $copy->call_number() );
283
284         my $mods = _records_to_mods($volume->record());
285         $mods = shift @$mods;
286         $volume->copies([$copy]);
287         push @{$mods->call_numbers()}, $volume;
288
289         return $mods;
290 }
291
292
293 # ----------------------------------------------------------------------------
294 # These are the main OPAC search methods
295 # ----------------------------------------------------------------------------
296
297 __PACKAGE__->register_method(
298         method          => "record_search_class",
299         api_name                => "open-ils.search.biblio.record.class.search",
300         signature       => q/
301                 Performs a class search for biblio records (not metarecords)
302                 @param class The search class to use
303                 @param args A hash of named parameters including:
304                         term            : The search string,
305                         org_unit : The org id to focus the search at
306                         depth           : The org depth
307                         limit           : The search limit
308                         offset  : The search offset
309                         format  : The MARC format
310                         sort            : What field to sort the results on [ author | title | pubdate ]
311                         sort_dir        : What direction do we sort? [ asc | desc ]
312                 The only required argument is the term.
313                 @return 
314         /);
315
316 __PACKAGE__->register_method(
317         method          => "record_search_class",
318         api_name                => "open-ils.search.biblio.record.class.search.staff",
319         signature       => q/@see open-ils.search.biblio.record.class.search/);
320 __PACKAGE__->register_method(
321         method  => "record_search_class",
322         api_name        => "open-ils.search.biblio.metabib.class.search",
323         signature       => q/@see open-ils.search.biblio.record.class.search/);
324 __PACKAGE__->register_method(
325         method  => "record_search_class",
326         api_name        => "open-ils.search.biblio.metabib.class.search.staff",
327         signature       => q/@see open-ils.search.biblio.record.class.search/);
328
329 sub record_search_class {
330         my( $self, $client, $class, $args ) = @_;
331
332         my $type                = ($self->api_name =~ /metabib/) ? 1 : 0;
333         my $method      = "open-ils.storage.metabib.$class.post_filter.search_fts.metarecord.atomic";
334         $method         = "open-ils.storage.biblio.$class.search_fts.record.atomic" unless $type;
335
336         if(!$$args{'sort'}) {
337                 delete $$args{'sort'}; 
338                 delete $$args{'sort_dir'};
339         }
340         $$args{limit}   = 200 if (!$$args{limit} || $$args{limit} > 1000);
341         $$args{offset} ||= 0;
342
343         $logger->info("Class search with args: ".Dumper($args));
344
345         $method =~ s/\.atomic/.staff.atomic/o if $self->api_name =~ /staff/o;
346         my $result = $U->storagereq( $method, %$args );
347
348         if($result and $result->[0]) {
349                 my $recs = [];
350                 for my $r (@$result) { push(@$recs, $r) if ($r and $r->[0]); }
351                 return { ids => $recs, 
352                         count => ($type) ? $result->[0]->[3] : $result->[0]->[2] };
353         }
354         return { count => 0 };
355 }
356
357
358
359
360 __PACKAGE__->register_method(
361         method  => "biblio_mrid_to_modsbatch_batch",
362         api_name        => "open-ils.search.biblio.metarecord.mods_slim.batch.retrieve");
363
364 sub biblio_mrid_to_modsbatch_batch {
365         my( $self, $client, $mrids) = @_;
366         warn "Performing mrid_to_modsbatch_batch...";
367         my @mods;
368         my $method = $self->method_lookup("open-ils.search.biblio.metarecord.mods_slim.retrieve");
369         for my $id (@$mrids) {
370                 next unless defined $id;
371                 my ($m) = $method->run($id);
372                 push @mods, $m;
373         }
374         return \@mods;
375 }
376
377
378 __PACKAGE__->register_method(
379         method  => "biblio_mrid_to_modsbatch",
380         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve",
381         notes           => <<"  NOTES");
382         Returns the mvr associated with a given metarecod. If none exists, 
383         it is created.
384         NOTES
385
386 __PACKAGE__->register_method(
387         method  => "biblio_mrid_to_modsbatch",
388         api_name        => "open-ils.search.biblio.metarecord.mods_slim.retrieve.staff",
389         notes           => <<"  NOTES");
390         Returns the mvr associated with a given metarecod. If none exists, 
391         it is created.
392         NOTES
393
394 sub biblio_mrid_to_modsbatch {
395         my( $self, $client, $mrid ) = @_;
396
397         warn "Grabbing mvr for $mrid\n";
398
399         my $mr = _grab_metarecord($mrid);
400         return undef unless $mr;
401
402         if( my $m = $self->biblio_mrid_check_mvr($client, $mr)) {
403                 return $m;
404         }
405
406         return $self->biblio_mrid_make_modsbatch( $client, $mr ); 
407 }
408
409 # converts a metarecord to an mvr
410 sub _mr_to_mvr {
411         my $mr = shift;
412         my $perl = JSON->JSON2perl($mr->mods());
413         return Fieldmapper::metabib::virtual_record->new($perl);
414 }
415
416 # checks to see if a metarecord has mods, if so returns true;
417
418 __PACKAGE__->register_method(
419         method  => "biblio_mrid_check_mvr",
420         api_name        => "open-ils.search.biblio.metarecord.mods_slim.check",
421         notes           => <<"  NOTES");
422         Takes a metarecord ID or a metarecord object and returns true
423         if the metarecord already has an mvr associated with it.
424         NOTES
425
426 sub biblio_mrid_check_mvr {
427         my( $self, $client, $mrid ) = @_;
428         my $mr; 
429
430         if(ref($mrid)) { $mr = $mrid; } 
431         else { $mr = _grab_metarecord($mrid); }
432
433         warn "Checking mvr for mr " . $mr->id . "\n";
434
435         return _mr_to_mvr($mr) if $mr->mods();
436         return undef;
437 }
438
439 sub _grab_metarecord {
440
441         my $mrid = shift;
442         warn "Grabbing MR $mrid\n";
443
444         my $mr = OpenILS::Application::AppUtils->simple_scalar_request( 
445                 "open-ils.storage", 
446                 "open-ils.storage.direct.metabib.metarecord.retrieve", $mrid );
447
448         if(!$mr) {
449                 throw OpenSRF::EX::ERROR 
450                         ("No metarecord exists with the given id: $mrid");
451         }
452
453         return $mr;
454 }
455
456 __PACKAGE__->register_method(
457         method  => "biblio_mrid_make_modsbatch",
458         api_name        => "open-ils.search.biblio.metarecord.mods_slim.create",
459         notes           => <<"  NOTES");
460         Takes either a metarecord ID or a metarecord object.
461         Forces the creations of an mvr for the given metarecord.
462         The created mvr is returned.
463         NOTES
464
465 sub biblio_mrid_make_modsbatch {
466
467         my( $self, $client, $mrid ) = @_;
468
469         my $mr; 
470         if(ref($mrid)) { $mr = $mrid; }
471         else { $mr = _grab_metarecord($mrid); }
472         $mrid = $mr->id;
473
474         warn "Forcing mvr creation for mr " . $mr->id . "\n";
475         my $master_id = $mr->master_record;
476
477         my $session = OpenSRF::AppSession->create("open-ils.storage");
478
479         # grab the records attached to this metarecod 
480         warn "Creating mods batch for metarecord $mrid\n";
481         my $meth = "open-ils.search.biblio.metarecord_to_records.staff";
482         $meth = $self->method_lookup($meth);
483         my ($id_hash) = $meth->run($mrid);
484         my @ids = @{$id_hash->{ids}};
485         if(@ids < 1) { return undef; }
486
487         warn "Master ID is $master_id\n";
488         # grab the master record to start the mods batch 
489
490         $meth = "open-ils.storage.direct.biblio.record_entry.retrieve";
491
492         my $record = $session->request(
493                         "open-ils.storage.direct.biblio.record_entry.retrieve", $master_id );
494         $record = $record->gather(1);
495
496         #my $record = OpenILS::Application::AppUtils->simple_scalar_request( "open-ils.storage", 
497
498         if(!$record) {
499                 warn "No record returned with id $master_id";
500                 throw OpenSRF::EX::ERROR 
501         }
502
503         my $u = OpenILS::Utils::ModsParser->new();
504         $u->start_mods_batch( $record->marc );
505         my $main_doc_id = $record->id();
506
507         @ids = grep { $_ ne $master_id } @ids;
508
509         # now we have to collect all of the marc objects and push them into a mods batch
510         my $request = $session->request(
511                 "open-ils.storage.direct.biblio.record_entry.batch.retrieve",  @ids );
512
513         while( my $response = $request->recv() ) {
514
515                 next unless $response;
516                 if(UNIVERSAL::isa( $response,"OpenSRF::EX")) {
517                         throw $response ($response->stringify);
518                 }
519
520                 my $content = $response->content;
521
522                 if( $content ) {
523                         $u->push_mods_batch( $content->marc );
524                 }
525         }
526
527         my $mods = $u->finish_mods_batch();
528         $mods->doc_id($mrid);
529         $request->finish();
530
531         $client->respond_complete($mods);
532
533         my $mods_string = JSON->perl2JSON($mods->decast);
534
535         $mr->mods($mods_string);
536
537         my $req = $session->request( 
538                 "open-ils.storage.direct.metabib.metarecord.update", $mr );
539
540
541         $req->gather(1);
542         $session->finish();
543         $session->disconnect();
544
545         return undef;
546 }
547
548
549
550 # converts a mr id into a list of record ids
551
552 __PACKAGE__->register_method(
553         method  => "biblio_mrid_to_record_ids",
554         api_name        => "open-ils.search.biblio.metarecord_to_records",
555 );
556
557 __PACKAGE__->register_method(
558         method  => "biblio_mrid_to_record_ids",
559         api_name        => "open-ils.search.biblio.metarecord_to_records.staff",
560 );
561
562 sub biblio_mrid_to_record_ids {
563         my( $self, $client, $mrid, $format ) = @_;
564
565         throw OpenSRF::EX::InvalidArg 
566                 ("search.biblio.metarecord_to_record_ids requires mr id")
567                         unless defined( $mrid );
568
569         warn "Searching for record for MR $mrid and format $format\n";
570
571         my $method = "open-ils.storage.ordered.metabib.metarecord.records.atomic";
572         if($self and $self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
573         warn "Performing record retrieval with method $method\n";
574
575
576         my $mrmaps = OpenILS::Application::AppUtils->simple_scalar_request( 
577                         "open-ils.storage", $method, $mrid, $format );
578
579         warn Dumper $mrmaps;
580
581         my $size = @$mrmaps;    
582
583         return { count => $size, ids => $mrmaps };
584
585 }
586
587
588 __PACKAGE__->register_method(
589         method  => "biblio_record_to_marc_html",
590         api_name        => "open-ils.search.biblio.record.html" );
591
592 my $parser              = XML::LibXML->new();
593 my $xslt                        = XML::LibXSLT->new();
594 my $marc_sheet;
595
596 my $settings_client = OpenSRF::Utils::SettingsClient->new();
597 sub biblio_record_to_marc_html {
598         my( $self, $client, $recordid ) = @_;
599
600         if( !$marc_sheet ) {
601                 my $dir = $settings_client->config_value( "dirs", "xsl" );
602                 my $xsl = $settings_client->config_value(
603                         "apps", "open-ils.search", "app_settings", "marc_html_xsl" );
604
605                 $xsl = $parser->parse_file("$dir/$xsl");
606                 $marc_sheet = $xslt->parse_stylesheet( $xsl );
607         }
608
609
610         my $record = $apputils->simple_scalar_request(
611                 "open-ils.storage", 
612                 "open-ils.storage.direct.biblio.record_entry.retrieve",
613                 $recordid );
614
615         my $xmldoc = $parser->parse_string($record->marc);
616         my $html = $marc_sheet->transform($xmldoc);
617         $html = $html->toString();
618         return $html;
619
620 }
621
622
623
624 __PACKAGE__->register_method(
625         method  => "retrieve_all_copy_locations",
626         api_name        => "open-ils.search.config.copy_location.retrieve.all" );
627
628 my $shelving_locations;
629 sub retrieve_all_copy_locations {
630         my( $self, $client ) = @_;
631         if(!$shelving_locations) {
632                 $shelving_locations = $apputils->simple_scalar_request(
633                         "open-ils.storage", 
634                         "open-ils.storage.direct.asset.copy_location.retrieve.all.atomic");
635         }
636         return $shelving_locations;
637 }
638
639
640
641 __PACKAGE__->register_method(
642         method  => "retrieve_all_copy_statuses",
643         api_name        => "open-ils.search.config.copy_status.retrieve.all" );
644
645 my $copy_statuses;
646 sub retrieve_all_copy_statuses {
647         my( $self, $client ) = @_;
648         if(!$copy_statuses) {
649                 $copy_statuses = $apputils->simple_scalar_request(
650                         "open-ils.storage",
651                         "open-ils.storage.direct.config.copy_status.retrieve.all.atomic" );
652         }
653         return $copy_statuses;
654 }
655
656
657 __PACKAGE__->register_method(
658         method  => "copy_counts_per_org",
659         api_name        => "open-ils.search.biblio.copy_counts.retrieve");
660
661 __PACKAGE__->register_method(
662         method  => "copy_counts_per_org",
663         api_name        => "open-ils.search.biblio.copy_counts.retrieve.staff");
664
665 sub copy_counts_per_org {
666         my( $self, $client, $record_id ) = @_;
667
668         warn "Retreiveing copy copy counts for record $record_id and method " . $self->api_name . "\n";
669
670         my $method = "open-ils.storage.biblio.record_entry.global_copy_count.atomic";
671         if($self->api_name =~ /staff/) { $method =~ s/atomic/staff\.atomic/; }
672
673         my $counts = $apputils->simple_scalar_request(
674                 "open-ils.storage", $method, $record_id );
675
676         $counts = [ sort {$a->[0] <=> $b->[0]} @$counts ];
677         return $counts;
678 }
679
680
681 __PACKAGE__->register_method(
682         method          => "copy_count_summary",
683         api_name        => "open-ils.search.biblio.copy_counts.summary.retrieve",
684         notes           => <<"  NOTES");
685         returns an array of these:
686                 [ org_id, callnumber_label, <status1_count>, <status2_cout>,...]
687                 where statusx is a copy status name.  the statuses are sorted
688                 by id.
689         NOTES
690
691 sub copy_count_summary {
692         my( $self, $client, $rid ) = @_;
693         my $method = "open-ils.storage.biblio.record_entry.status_copy_count.atomic";
694         return $apputils->simple_scalar_request( "open-ils.storage", $method, $rid );
695 }
696
697
698 =head
699 __PACKAGE__->register_method(
700         method          => "multiclass_search",
701         api_name        => "open-ils.search.biblio.multiclass",
702         notes           => <<"  NOTES");
703                 Performs a multiclass search
704                 PARAMS( searchBlob, org_unit, format, limit ) 
705                 where searchBlob is defined like this:
706                         { 
707                                 "title" : { "term" : "water" }, 
708                                 "author" : { "term" : "smith" }, 
709                                 ... 
710                         }
711         NOTES
712
713 __PACKAGE__->register_method(
714         method          => "multiclass_search",
715         api_name        => "open-ils.search.biblio.multiclass.staff",
716         notes           => "see open-ils.search.biblio.multiclass" );
717
718 sub multiclass_search {
719         my( $self, $client, $searchBlob, $orgid, $format, $limit ) = @_;
720
721         $logger->debug("Performing multiclass search with org => $orgid, " .
722                 "format => $format, limit => $limit, and search blob " . Dumper($searchBlob));
723
724         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
725         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
726
727
728         my $records = $apputils->simplereq(
729                 'open-ils.storage', $meth, 
730                  org_unit => $orgid, searches => $searchBlob, format => $format, limit => $limit );
731
732         my $count = 0;
733         my $recs = [];
734
735         if( ref($records) and $records->[0] and 
736                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
737
738         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
739
740         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
741         return { ids => $recs, count => $count };
742 }
743 =cut
744
745
746
747 __PACKAGE__->register_method(
748         method          => "multiclass_search",
749         api_name                => "open-ils.search.biblio.multiclass",
750         signature       => q/
751                 Performs a multiclass search
752                 @param args A names hash of arguments:
753                         org_unit : The org to focus the search on
754                         depth           : The search depth
755                         format  : Item format
756                         limit           : Return limit
757                         offset  : Search offset
758                         searches : A named hash of searches which has the following format:
759                                 { 
760                                         "title" : { "term" : "water" }, 
761                                         "author" : { "term" : "smith" }, 
762                                         ... 
763                                 }
764                 @return { ids : <array of ids>, count : hitcount }
765         /
766 );
767
768 __PACKAGE__->register_method(
769         method          => "multiclass_search",
770         api_name                => "open-ils.search.biblio.multiclass.staff",
771         notes           => q/@see open-ils.search.biblio.multiclass/ );
772
773 sub multiclass_search {
774         my( $self, $client, $args ) = @_;
775
776         $logger->debug("Performing multiclass search with args:\n" . Dumper($args));
777         my $meth = 'open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.atomic';
778         if($self->api_name =~ /staff/) { $meth =~ s/metarecord\.atomic/metarecord.staff.atomic/; }
779
780         my $records = $apputils->simplereq( 'open-ils.storage', $meth, %$args );
781
782         my $count = 0;
783         my $recs = [];
784
785         if( ref($records) and $records->[0] and 
786                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
787
788         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
789
790         return { ids => $recs, count => $count };
791 }
792
793
794
795
796 __PACKAGE__->register_method(
797         method          => "marc_search",
798         api_name        => "open-ils.search.biblio.marc",
799         notes           => <<"  NOTES");
800                 Performs a multiclass search
801                 PARAMS( searchBlob, org_unit, format ) 
802                 where searchBlob is defined like this:
803                 [ 
804                         {
805                                 "term":"shakespeare",
806                                 "restrict":[{"tag":"245","subfield":"a"}] 
807                         }, 
808                         {
809                                 "term":"bloom",
810                                 "restrict":[{"tag":"100","subfield":"a"}] 
811                         } 
812                 ]
813         NOTES
814
815
816 sub marc_search {
817         my( $self, $client, $searchBlob, $orgid, $format ) = @_;
818
819         $logger->debug("Performing MARC search with org => $orgid, " .
820                 "format => $format and search blob " . Dumper($searchBlob) );
821                 
822         my $records =  $apputils->simplereq(
823                 'open-ils.storage',
824                 'open-ils.storage.metabib.full_rec.multi_search.atomic',
825                 searches => $searchBlob, org_unit => $orgid, format => $format );
826
827         my $count = 0;
828         my $recs = [];
829
830         if( ref($records) and $records->[0] and 
831                 defined($records->[0]->[3])) { $count = $records->[0]->[3];}
832
833         for my $r (@$records) { push( @$recs, $r ) if ($r and $r->[0]); }
834
835         # records has the form: [ mrid, rank, singleRecord / 0, hitCount ];
836         return { ids => $recs, count => $count };
837
838 }
839
840
841
842 __PACKAGE__->register_method(
843         method  => "biblio_search_isbn",
844         api_name        => "open-ils.search.biblio.isbn",
845 );
846
847 sub biblio_search_isbn { 
848         my( $self, $client, $isbn ) = @_;
849
850         $logger->debug("Searching ISBN $isbn");
851
852         my $method = $self->method_lookup("open-ils.search.biblio.marc");
853
854         my ($records) = $method->run(  
855                 [ {     term => $isbn,
856                                 restrict => $cat_search_hash->{isbn} } ], 1);
857
858         return $records;
859 }
860
861
862 __PACKAGE__->register_method(
863         method  => "biblio_search_issn",
864         api_name        => "open-ils.search.biblio.issn",
865 );
866
867 sub biblio_search_issn { 
868         my( $self, $client, $issn ) = @_;
869
870         $logger->debug("Searching ISSN $issn");
871
872         my $method = $self->method_lookup("open-ils.search.biblio.marc");
873
874         my ($records) = $method->run(  
875                 [ {     term => $issn,
876                                 restrict => $cat_search_hash->{issn} } ], 1);
877
878         return $records;
879 }
880
881
882
883
884 __PACKAGE__->register_method(
885         method  => "fetch_mods_by_copy",
886         api_name        => "open-ils.search.biblio.mods_from_copy",
887 );
888
889 sub fetch_mods_by_copy {
890         my( $self, $client, $copyid ) = @_;
891         my ($record, $evt) = $apputils->fetch_record_by_copy( $copyid );
892         return $evt if $evt;
893         return OpenILS::Event->new('ITEM_NOT_CATALOGED') unless $record->marc;
894         return $apputils->record_to_mvr($record);
895 }
896
897
898
899
900
901 # -------------------------------------------------------------------------------------
902
903 __PACKAGE__->register_method(
904         method  => "cn_browse",
905         api_name        => "open-ils.search.callnumber.browse.target",
906         notes           => "Starts a callnumber browse"
907         );
908
909 __PACKAGE__->register_method(
910         method  => "cn_browse",
911         api_name        => "open-ils.search.callnumber.browse.page_up",
912         notes           => "Returns the previous page of callnumbers", 
913         );
914
915 __PACKAGE__->register_method(
916         method  => "cn_browse",
917         api_name        => "open-ils.search.callnumber.browse.page_down",
918         notes           => "Returns the next page of callnumbers", 
919         );
920
921
922 # RETURNS array of arrays like so: label, owning_lib, record, id
923 sub cn_browse {
924         my( $self, $client, @params ) = @_;
925         my $method;
926
927         $method = 'open-ils.storage.asset.call_number.browse.target.atomic' 
928                 if( $self->api_name =~ /target/ );
929         $method = 'open-ils.storage.asset.call_number.browse.page_up.atomic'
930                 if( $self->api_name =~ /page_up/ );
931         $method = 'open-ils.storage.asset.call_number.browse.page_down.atomic'
932                 if( $self->api_name =~ /page_down/ );
933
934         return $apputils->simplereq( 'open-ils.storage', $method, @params );
935 }
936 # -------------------------------------------------------------------------------------
937
938 __PACKAGE__->register_method(
939         method => "fetch_cn",
940         api_name => "open-ils.search.callnumber.retrieve",
941         notes           => "retrieves a callnumber based on ID",
942         );
943
944 sub fetch_cn {
945         my( $self, $client, $id ) = @_;
946         my( $cn, $evt ) = $apputils->fetch_callnumber( $id );
947         return $evt if $evt;
948         return $cn;
949 }
950
951
952
953
954
955
956
957
958
959
960 1;
961
962