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