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