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