]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
added regex to clean up TCN 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 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         $tcn =~ s/.*?(\w+)\s*$/$1/o;
60         warn "Searching TCN $tcn\n";
61
62         my $session = OpenSRF::AppSession->create( "open-ils.storage" );
63         my $request = $session->request( 
64                         "open-ils.storage.biblio.record_entry.search.tcn_value", $tcn );
65         my $response = $request->recv();
66
67
68         unless ($response) { return undef; }
69
70         if($response->isa("OpenSRF::EX")) {
71                 warn $response->stringify();
72                 throw $response ($response->stringify);
73         }
74
75
76         my $record_entry = $response->content;
77         $record_entry = $record_entry->[0];
78         $request->finish();
79
80         $request = $session->request(
81                 "open-ils.storage.biblio.record_marc.retrieve",  $record_entry->id() );
82         $response = $request->recv();
83
84         unless ($response) { return undef; }
85
86         if($response->isa("OpenSRF::EX")) {
87                 warn $response->stringify();
88                 throw $response ($response->stringify);
89         }
90
91         my $marcxml = $response->content;
92
93         my $u = OpenILS::Application::Cat::Utils->new();
94         $u->start_mods_batch( $marcxml->marc );
95         my $mods = $u->finish_mods_batch();
96         $mods->{doc_id} = $marcxml->id();
97
98
99         $request->finish();
100         $session->disconnect;
101         $session->kill_me;
102
103         return $mods;
104 }
105
106
107 __PACKAGE__->register_method(
108         method  => "cat_biblio_search_class",
109         api_name        => "open-ils.search.cat.biblio.class",
110         argc            => 3, 
111         note            => "Searches biblio information by search class",
112 );
113
114 sub cat_biblio_search_class {
115         my( $self, $client, $class, $sort, $string ) = @_;
116
117         # sort = title, author, pubdate
118
119         warn "Starting search " . time() . "\n";
120         
121         my $search_hash;
122
123         warn "Searching $class, $sort, $string\n";
124         
125         warn "Looking up method: "  . time() . "\n";
126
127         my $method = $self->method_lookup("open-ils.search.biblio.marc");
128         if(!$method) {
129                 throw OpenSRF::EX::PANIC 
130                         ("Can't lookup method 'open-ils.search.biblio.marc'");
131         }
132
133         warn "Running: "  . time() . "\n";
134
135         my ($records) = $method->run( $cat_search_hash->{$class}, $string );
136
137         my @ids;
138
139         for my $i (@$records) {
140                 push @ids, $i->[0];
141         }
142
143         warn "Found Id's: @ids\n";
144         warn "Search For Id's complete, fixing: "  . time() . "\n";
145
146         my @results = ();
147
148         my @marcxml_objs;
149         my $session = OpenSRF::AppSession->create("open-ils.storage");
150         my $request = $session->request(
151                         "open-ils.storage.biblio.record_marc.batch.retrieve",  @ids );
152
153
154         my $last_content = undef;
155
156         while( my $response = $request->recv() ) {
157
158                 warn "Received record from storage " . time() . "\n";
159
160                 if( $last_content ) {
161                         my $u = OpenILS::Application::Cat::Utils->new();
162                         $u->start_mods_batch( $last_content->marc );
163                         my $mods = $u->finish_mods_batch();
164                         $mods->{doc_id} = $last_content->id();
165                         warn "Processed mods " . time() . "\n";
166                         #$client->respond( $mods );
167                         $last_content = undef;
168                         push @results, $mods;
169                 }
170
171                 next unless $response;
172
173                 if($response->isa("OpenSRF::EX")) {
174                         throw $response ($response->stringify);
175                 }
176
177                 $last_content = $response->content;
178
179         }
180
181         if( $last_content ) {
182                 my $u = OpenILS::Application::Cat::Utils->new();
183                 $u->start_mods_batch( $last_content->marc );
184                 my $mods = $u->finish_mods_batch();
185                 $mods->{doc_id} = $last_content->id();
186                 warn "Processed mods " . time() . "\n";
187                 #$client->respond( $mods );
188                 push @results, $mods;
189         }
190
191
192         $client->respond( \@results );
193         
194         $request->finish();
195         $session->finish();
196         $session->disconnect();
197         $session->kill_me();
198         
199         return undef;
200
201 }
202
203
204
205 __PACKAGE__->register_method(
206         method  => "biblio_search_marc",
207         api_name        => "open-ils.search.biblio.marc",
208         argc            => 1, 
209         note            => "Searches biblio information by marc tag",
210 );
211
212 sub biblio_search_marc {
213
214         my( $self, $client, $search_hash, $string ) = @_;
215
216         my $session = OpenSRF::AppSession->create("open-ils.storage");
217         my $request = $session->request( 
218                         "open-ils.storage.metabib.full_rec.search_fts.index_vector", $search_hash, $string );
219
220         my $response = $request->recv();
221         if($response and $response->isa("OpenSRF::EX")) {
222                 throw $response ($response->stringify);
223         }
224
225         my $data = $response->content;
226
227         $request->finish();
228         $session->finish();
229         $session->disconnect();
230         $session->kill_me();
231
232         return $data;
233
234 }
235
236
237
238
239 1;