]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
Addded record_entry metadata search
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search.pm
1 package OpenILS::Application::Search;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4
5 use OpenILS::Utils::Fieldmapper;
6 use Time::HiRes qw(time);
7 use OpenILS::Application::Cat::Utils;
8
9 use OpenSRF::EX qw(:try);
10
11 # used for cat search classes
12 my $cat_search_hash =  {
13
14         author => [ 
15                 { tag => "100", subfield => "a"} ,
16                 { tag => "700", subfield => "a"}, 
17         ],
18
19         title => [ 
20                 { tag => "245", subfield => "a"},
21                 { tag => "242", subfield => "a"}, 
22                 { tag => "240", subfield => "a"},
23                 { tag => "210", subfield => "a"},
24         ],
25
26         subject => [ 
27                 { tag => "650", subfield => "_" }, 
28         ],
29
30         tcn     => [
31                 { tag => "035", subfield => "_" },
32         ],
33
34 };
35
36
37 __PACKAGE__->register_method(
38         method  => "cat_biblio_search_tcn",
39         api_name        => "open-ils.search.cat.biblio.tcn",
40         argc            => 3, 
41         note            => "Searches biblio information by search class",
42 );
43
44 sub cat_biblio_search_tcn {
45
46         my( $self, $client, $tcn ) = @_;
47
48         $tcn =~ s/.*?(\w+)\s*$/$1/o;
49         warn "Searching TCN $tcn\n";
50
51         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
52         my $request = $session->request( 
53                         "open-ils.storage.biblio.record_entry.search.tcn_value", $tcn );
54         my $response = $request->recv();
55
56
57         unless ($response) { return []; }
58
59         if($response->isa("OpenSRF::EX")) {
60                 throw $response ($response->stringify);
61         }
62
63         my $record_entry = $response->content;
64         my @ids;
65         for my $record (@$record_entry) {
66                 push @ids, $record->id;
67         }
68
69         return _records_to_mods( @ids );
70
71 }
72
73 __PACKAGE__->register_method(
74         method  => "cat_biblio_search_class",
75         api_name        => "open-ils.search.cat.biblio.class",
76         argc            => 3, 
77         note            => "Searches biblio information by search class",
78 );
79
80 sub cat_biblio_search_class {
81         my( $self, $client, $class, $sort, $string ) = @_;
82
83         warn "Starting search " . time() . "\n";
84         
85         my $search_hash;
86
87         warn "Searching $class, $sort, $string\n";
88         
89         warn "Looking up method: "  . time() . "\n";
90
91         my $method = $self->method_lookup("open-ils.search.biblio.marc");
92         if(!$method) {
93                 throw OpenSRF::EX::PANIC 
94                         ("Can't lookup method 'open-ils.search.biblio.marc'");
95         }
96
97         warn "Running: "  . time() . "\n";
98
99         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
100
101         my @ids;
102
103         for my $i (@$records) { push @ids, $i->[0]; }
104
105         warn "Found Id's: @ids " . time() . "\n";
106
107         return _records_to_mods(@ids);
108
109 }
110
111
112
113 __PACKAGE__->register_method(
114         method  => "biblio_search_marc",
115         api_name        => "open-ils.search.biblio.marc",
116         argc            => 1, 
117         note            => "Searches biblio information by marc tag",
118 );
119
120 sub biblio_search_marc {
121
122         my( $self, $client, $search_hash, $string ) = @_;
123
124         my $session = OpenSRF::AppSession->create("open-ils.storage");
125         my $request = $session->request( 
126                         "open-ils.storage.metabib.full_rec.search_fts.index_vector", $search_hash, $string );
127
128         my $response = $request->recv();
129         if($response and $response->isa("OpenSRF::EX")) {
130                 throw $response ($response->stringify);
131         }
132
133         my $data = $response->content;
134
135         $request->finish();
136         $session->finish();
137         $session->disconnect();
138         $session->kill_me();
139
140         return $data;
141
142 }
143
144
145
146
147
148
149 # ---------------------------------------------------------------------------
150 # takes a list of record id's and turns the docs into friendly 
151 # mods structures.
152 # ---------------------------------------------------------------------------
153 sub _records_to_mods {
154         my @ids = @_;
155         
156         my @results;
157         my @marcxml_objs;
158
159         my $session = OpenSRF::AppSession->create("open-ils.storage");
160         my $request = $session->request(
161                         "open-ils.storage.biblio.record_marc.batch.retrieve",  @ids );
162
163         my $last_content = undef;
164
165         while( my $response = $request->recv() ) {
166
167                 if( $last_content ) {
168                         my $u = OpenILS::Application::Cat::Utils->new();
169                         $u->start_mods_batch( $last_content->marc );
170                         my $mods = $u->finish_mods_batch();
171                         $mods->{doc_id} = $last_content->id();
172                         warn "Turning doc " . $mods->{doc_id} . " into MODS\n";
173                         $last_content = undef;
174                         push @results, $mods;
175                 }
176
177                 next unless $response;
178
179                 if($response->isa("OpenSRF::EX")) {
180                         throw $response ($response->stringify);
181                 }
182
183                 $last_content = $response->content;
184
185         }
186
187         if( $last_content ) {
188                 my $u = OpenILS::Application::Cat::Utils->new();
189                 $u->start_mods_batch( $last_content->marc );
190                 my $mods = $u->finish_mods_batch();
191                 $mods->{doc_id} = $last_content->id();
192                 push @results, $mods;
193         }
194
195         $request->finish();
196         $session->finish();
197         $session->disconnect();
198         $session->kill_me();
199
200         return \@results;
201
202 }
203
204
205
206
207 1;