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