]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/metabib.pm
field name adjustments
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / metabib.pm
1 package OpenILS::Application::Storage::Publisher::metabib;
2 use base qw/OpenILS::Application::Storage::Publisher/;
3 use vars qw/$VERSION/;
4 use OpenSRF::EX qw/:try/;
5 use OpenILS::Application::Storage::FTS;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenSRF::Utils::Logger qw/:level/;
8 use OpenSRF::Utils::Cache;
9 use Data::Dumper;
10 use Digest::MD5 qw/md5_hex/;
11
12 my $log = 'OpenSRF::Utils::Logger';
13
14 $VERSION = 1;
15
16 sub metarecord_copy_count {
17         my $self = shift;
18         my $client = shift;
19
20         my %args = @_;
21
22         my $sm_table = metabib::metarecord_source_map->table;
23         my $cn_table = asset::call_number->table;
24         my $cp_table = asset::copy->table;
25         my $out_table = actor::org_unit_type->table;
26         my $descendants = "actor.org_unit_descendants(u.id)";
27         my $ancestors = "actor.org_unit_ancestors(?)";
28
29         my $copies_visible = 'AND cp.opac_visible IS TRUE';
30         $copies_visible = '' if ($self->api_name =~ /staff/o);
31
32         my $sql = <<"   SQL";
33                 SELECT  t.depth,
34                         u.id AS org_unit,
35                         sum(
36                                 (SELECT count(cp.id)
37                                   FROM  $sm_table r
38                                         JOIN $cn_table cn ON (cn.record = r.source)
39                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
40                                         JOIN $descendants a ON (cp.circ_lib = a.id)
41                                   WHERE r.metarecord = ?
42                                         $copies_visible
43                                 )
44                         ) AS count,
45                         sum(
46                                 (SELECT count(cp.id)
47                                   FROM  $sm_table r
48                                         JOIN $cn_table cn ON (cn.record = r.source)
49                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
50                                         JOIN $descendants a ON (cp.circ_lib = a.id)
51                                   WHERE r.metarecord = ?
52                                         AND cp.status = 0
53                                         $copies_visible
54                                 )
55                         ) AS available
56
57                   FROM  $ancestors u
58                         JOIN $out_table t ON (u.ou_type = t.id)
59                   GROUP BY 1,2
60         SQL
61
62         my $sth = metabib::metarecord_source_map->db_Main->prepare_cached($sql);
63         $sth->execute(''.$args{metarecord}, ''.$args{metarecord}, ''.$args{org_unit});
64         while ( my $row = $sth->fetchrow_hashref ) {
65                 $client->respond( $row );
66         }
67         return undef;
68 }
69 __PACKAGE__->register_method(
70         api_name        => 'open-ils.storage.metabib.metarecord.copy_count',
71         method          => 'metarecord_copy_count',
72         api_level       => 1,
73         stream          => 1,
74         cachable        => 1,
75 );
76 __PACKAGE__->register_method(
77         api_name        => 'open-ils.storage.metabib.metarecord.copy_count.staff',
78         method          => 'metarecord_copy_count',
79         api_level       => 1,
80         stream          => 1,
81         cachable        => 1,
82 );
83
84 sub search_full_rec {
85         my $self = shift;
86         my $client = shift;
87
88         my %args = @_;
89         
90         my $term = $args{term};
91         my $limiters = $args{restrict};
92
93         my ($index_col) = metabib::full_rec->columns('FTS');
94         $index_col ||= 'value';
95         my $search_table = metabib::full_rec->table;
96
97         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
98
99         my $fts_where = $fts->sql_where_clause();
100         my @fts_ranks = $fts->fts_rank;
101
102         my $rank = join(' + ', @fts_ranks);
103
104         my @binds;
105         my @wheres;
106         for my $limit (@$limiters) {
107                 push @wheres, "( tag = ? AND subfield LIKE ? AND $fts_where )";
108                 push @binds, $$limit{tag}, $$limit{subfield};
109                 $log->debug("Limiting query using { tag => $$limit{tag}, subfield => $$limit{subfield} }", DEBUG);
110         }
111         my $where = join(' OR ', @wheres);
112
113         my $select = "SELECT record, sum($rank) FROM $search_table WHERE $where GROUP BY 1 ORDER BY 2 DESC;";
114
115         $log->debug("Search SQL :: [$select]",DEBUG);
116
117         my $recs = metabib::full_rec->db_Main->selectall_arrayref($select, {}, @binds);
118         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
119
120         $client->respond($_) for (@$recs);
121         return undef;
122 }
123 __PACKAGE__->register_method(
124         api_name        => 'open-ils.storage.direct.metabib.full_rec.search_fts.value',
125         method          => 'search_full_rec',
126         api_level       => 1,
127         stream          => 1,
128         cachable        => 1,
129 );
130 __PACKAGE__->register_method(
131         api_name        => 'open-ils.storage.direct.metabib.full_rec.search_fts.index_vector',
132         method          => 'search_full_rec',
133         api_level       => 1,
134         stream          => 1,
135         cachable        => 1,
136 );
137
138
139 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
140 sub search_class_fts {
141         my $self = shift;
142         my $client = shift;
143         my %args = @_;
144         
145         my $term = $args{term};
146         my $ou = $args{org_unit};
147         my $ou_type = $args{depth};
148
149
150         my $descendants = defined($ou_type) ?
151                                 "actor.org_unit_descendants($ou, $ou_type)" :
152                                 "actor.org_unit_descendants($ou)";
153
154         my $class = $self->{cdbi};
155         my $search_table = $class->table;
156
157         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
158         my $asset_call_number_table = asset::call_number->table;
159         my $asset_copy_table = asset::copy->table;
160
161         my ($index_col) = $class->columns('FTS');
162         $index_col ||= 'value';
163
164         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value', "$index_col");
165
166         my $fts_where = $fts->sql_where_clause;
167         my @fts_ranks = $fts->fts_rank;
168
169         my $rank = join(' + ', @fts_ranks);
170
171         my $has_vols = 'AND cn.owning_lib = d.id';
172         my $has_copies = 'AND cp.call_number = cn.id';
173         my $copies_visible = 'AND cp.opac_visible IS TRUE';
174
175         my $visible_count = ', count(DISTINCT cp.id)';
176         my $visible_count_test = 'HAVING count(DISTINCT cp.id) > 0';
177
178         if ($self->api_name =~ /staff/o) {
179                 $copies_visible = '';
180                 $visible_count_test = '';
181                 $has_copies = '' if ($ou_type == 0);
182                 $has_vols = '' if ($ou_type == 0);
183         }
184
185         my $rank_calc = ", sum($rank)/count(m.source)";
186         my $rank_order = "ORDER BY 2 DESC";
187         $rank_calc = ',1' if ($self->api_name =~ /unordered/o);
188         $rank_order = '' if ($self->api_name =~ /unordered/o);
189
190         my $select = <<"        SQL";
191                 SELECT  m.metarecord $rank_calc $visible_count
192                   FROM  $search_table f,
193                         $metabib_metarecord_source_map_table m,
194                         $asset_call_number_table cn,
195                         $asset_copy_table cp,
196                         $descendants d
197                   WHERE $fts_where
198                         AND m.source = f.source
199                         AND cn.record = m.source
200                         $has_vols
201                         $has_copies
202                         $copies_visible
203                   GROUP BY 1
204                   $visible_count_test
205                   $rank_order
206         SQL
207
208         $log->debug("Field Search SQL :: [$select]",DEBUG);
209
210         my $recs = $class->db_Main->selectall_arrayref($select);
211         
212         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
213
214         $client->respond($_) for (@$recs);
215         return undef;
216 }
217
218 for my $class ( qw/title author subject keyword/ ) {
219         __PACKAGE__->register_method(
220                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord",
221                 method          => 'search_class_fts',
222                 api_level       => 1,
223                 stream          => 1,
224                 cdbi            => "metabib::${class}_field_entry",
225                 cachable        => 1,
226         );
227         __PACKAGE__->register_method(
228                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.unordered",
229                 method          => 'search_class_fts',
230                 api_level       => 1,
231                 stream          => 1,
232                 cdbi            => "metabib::${class}_field_entry",
233                 cachable        => 1,
234         );
235         __PACKAGE__->register_method(
236                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.staff",
237                 method          => 'search_class_fts',
238                 api_level       => 1,
239                 stream          => 1,
240                 cdbi            => "metabib::${class}_field_entry",
241                 cachable        => 1,
242         );
243         __PACKAGE__->register_method(
244                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.staff.unordered",
245                 method          => 'search_class_fts',
246                 api_level       => 1,
247                 stream          => 1,
248                 cdbi            => "metabib::${class}_field_entry",
249                 cachable        => 1,
250         );
251 }
252
253 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
254 sub search_class_fts_count {
255         my $self = shift;
256         my $client = shift;
257         my %args = @_;
258         
259         my $term = $args{term};
260         my $ou = $args{org_unit};
261         my $ou_type = $args{depth};
262         my $limit = $args{limit} || 100;
263         my $offset = $args{offset} || 0;
264
265         my $descendants = defined($ou_type) ?
266                                 "actor.org_unit_descendants($ou, $ou_type)" :
267                                 "actor.org_unit_descendants($ou)";
268                 
269
270         (my $search_class = $self->api_name) =~ s/.*metabib.(\w+).search_fts.*/$1/o;
271
272         my $class = $self->{cdbi};
273         my $search_table = $class->table;
274
275         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
276         my $asset_call_number_table = asset::call_number->table;
277         my $asset_copy_table = asset::copy->table;
278
279         my ($index_col) = $class->columns('FTS');
280         $index_col ||= 'value';
281
282         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
283
284         my $fts_where = $fts->sql_where_clause;
285
286         my $has_vols = 'AND cn.owning_lib = d.id';
287         my $has_copies = 'AND cp.call_number = cn.id';
288         my $copies_visible = 'AND cp.opac_visible IS TRUE';
289         if ($self->api_name =~ /staff/o) {
290                 $copies_visible = '';
291                 $has_vols = '' if ($ou_type == 0);
292                 $has_copies = '' if ($ou_type == 0);
293         }
294
295         # XXX test an "EXISTS version of descendant checking...
296         my $select = <<"        SQL";
297                 SELECT  count(distinct  m.metarecord)
298                   FROM  $search_table f,
299                         $metabib_metarecord_source_map_table m,
300                         $asset_call_number_table cn,
301                         $asset_copy_table cp,
302                         $descendants d
303                   WHERE $fts_where
304                         AND m.source = f.source
305                         AND cn.record = m.source
306                         $has_vols
307                         $has_copies
308                         $copies_visible
309         SQL
310
311         $log->debug("Field Search Count SQL :: [$select]",DEBUG);
312
313         my $recs = $class->db_Main->selectrow_arrayref($select)->[0];
314         
315         $log->debug("Count Search yielded $recs results.",DEBUG);
316
317         return $recs;
318
319 }
320 for my $class ( qw/title author subject keyword/ ) {
321         __PACKAGE__->register_method(
322                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord_count",
323                 method          => 'search_class_fts_count',
324                 api_level       => 1,
325                 stream          => 1,
326                 cdbi            => "metabib::${class}_field_entry",
327                 cachable        => 1,
328         );
329         __PACKAGE__->register_method(
330                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord_count.staff",
331                 method          => 'search_class_fts_count',
332                 api_level       => 1,
333                 stream          => 1,
334                 cdbi            => "metabib::${class}_field_entry",
335                 cachable        => 1,
336         );
337 }
338
339
340 1;