]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/metabib.pm
per jason: snap!
[working/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         my $limit = $args{limit};
149         my $offset = $args{offset};
150
151         my $limit_clause = '';
152         my $offset_clause = '';
153
154         $limit_clause = "LIMIT $limit" if (defined $limit and int($limit) > 0);
155         $offset_clause = "OFFSET $offset" if (defined $offset and int($offset) > 0);
156
157
158         my $descendants = defined($ou_type) ?
159                                 "actor.org_unit_descendants($ou, $ou_type)" :
160                                 "actor.org_unit_descendants($ou)";
161
162         my $class = $self->{cdbi};
163         my $search_table = $class->table;
164
165         my $metabib_metarecord = metabib::metarecord->table;
166         my $metabib_full_rec = metabib::full_rec->table;
167         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
168         my $asset_call_number_table = asset::call_number->table;
169         my $asset_copy_table = asset::copy->table;
170
171         my ($index_col) = $class->columns('FTS');
172         $index_col ||= 'value';
173
174         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'f.value', "f.$index_col");
175
176         my $fts_where = $fts->sql_where_clause;
177         my @fts_ranks = $fts->fts_rank;
178
179         my $rank = join(' + ', @fts_ranks);
180
181         my $has_vols = 'AND cn.owning_lib = d.id';
182         my $has_copies = 'AND cp.call_number = cn.id';
183         my $copies_visible = 'AND cp.opac_visible IS TRUE';
184
185         my $visible_count = ', count(DISTINCT cp.id)';
186         my $visible_count_test = 'HAVING count(DISTINCT cp.id) > 0';
187
188         if ($self->api_name =~ /staff/o) {
189                 $copies_visible = '';
190                 $visible_count_test = '';
191                 $has_copies = '' if ($ou_type == 0);
192                 $has_vols = '' if ($ou_type == 0);
193         }
194
195         my $rank_calc = ", sum($rank)/count(m.source)";
196         my $rank_order = "ORDER BY 2 DESC";
197         $rank_calc = ',1' if ($self->api_name =~ /unordered/o);
198         $rank_order = '' if ($self->api_name =~ /unordered/o);
199
200         my $select = <<"        SQL";
201                 SELECT  m.metarecord $rank_calc $visible_count
202                   FROM  $search_table f,
203                         $metabib_metarecord_source_map_table m,
204                         $asset_call_number_table cn,
205                         $asset_copy_table cp,
206                         $descendants d
207                   WHERE $fts_where
208                         AND m.source = f.source
209                         AND cn.record = m.source
210                         $has_vols
211                         $has_copies
212                         $copies_visible
213                   GROUP BY m.metarecord $visible_count_test
214                   $rank_order
215                   $limit_clause $offset_clause
216         SQL
217
218         $log->debug("Field Search SQL :: [$select]",DEBUG);
219
220         my $recs = $class->db_Main->selectall_arrayref($select);
221         
222         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
223
224         $client->respond($_) for (@$recs);
225         return undef;
226 }
227
228 for my $class ( qw/title author subject keyword series/ ) {
229         __PACKAGE__->register_method(
230                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord",
231                 method          => 'search_class_fts',
232                 api_level       => 1,
233                 stream          => 1,
234                 cdbi            => "metabib::${class}_field_entry",
235                 cachable        => 1,
236         );
237         __PACKAGE__->register_method(
238                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.unordered",
239                 method          => 'search_class_fts',
240                 api_level       => 1,
241                 stream          => 1,
242                 cdbi            => "metabib::${class}_field_entry",
243         );
244         __PACKAGE__->register_method(
245                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.staff",
246                 method          => 'search_class_fts',
247                 api_level       => 1,
248                 stream          => 1,
249                 cdbi            => "metabib::${class}_field_entry",
250                 cachable        => 1,
251         );
252         __PACKAGE__->register_method(
253                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.staff.unordered",
254                 method          => 'search_class_fts',
255                 api_level       => 1,
256                 stream          => 1,
257                 cdbi            => "metabib::${class}_field_entry",
258         );
259 }
260
261 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
262 sub search_class_fts_count {
263         my $self = shift;
264         my $client = shift;
265         my %args = @_;
266         
267         my $term = $args{term};
268         my $ou = $args{org_unit};
269         my $ou_type = $args{depth};
270         my $limit = $args{limit} || 100;
271         my $offset = $args{offset} || 0;
272
273         my $descendants = defined($ou_type) ?
274                                 "actor.org_unit_descendants($ou, $ou_type)" :
275                                 "actor.org_unit_descendants($ou)";
276                 
277
278         (my $search_class = $self->api_name) =~ s/.*metabib.(\w+).search_fts.*/$1/o;
279
280         my $class = $self->{cdbi};
281         my $search_table = $class->table;
282
283         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
284         my $asset_call_number_table = asset::call_number->table;
285         my $asset_copy_table = asset::copy->table;
286
287         my ($index_col) = $class->columns('FTS');
288         $index_col ||= 'value';
289
290         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
291
292         my $fts_where = $fts->sql_where_clause;
293
294         my $has_vols = 'AND cn.owning_lib = d.id';
295         my $has_copies = 'AND cp.call_number = cn.id';
296         my $copies_visible = 'AND cp.opac_visible IS TRUE';
297         if ($self->api_name =~ /staff/o) {
298                 $copies_visible = '';
299                 $has_vols = '' if ($ou_type == 0);
300                 $has_copies = '' if ($ou_type == 0);
301         }
302
303         # XXX test an "EXISTS version of descendant checking...
304         my $select = <<"        SQL";
305                 SELECT  count(distinct  m.metarecord)
306                   FROM  $search_table f,
307                         $metabib_metarecord_source_map_table m,
308                         $asset_call_number_table cn,
309                         $asset_copy_table cp,
310                         $descendants d
311                   WHERE $fts_where
312                         AND m.source = f.source
313                         AND cn.record = m.source
314                         $has_vols
315                         $has_copies
316                         $copies_visible
317         SQL
318
319         $log->debug("Field Search Count SQL :: [$select]",DEBUG);
320
321         my $recs = $class->db_Main->selectrow_arrayref($select)->[0];
322         
323         $log->debug("Count Search yielded $recs results.",DEBUG);
324
325         return $recs;
326
327 }
328 for my $class ( qw/title author subject keyword series/ ) {
329         __PACKAGE__->register_method(
330                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord_count",
331                 method          => 'search_class_fts_count',
332                 api_level       => 1,
333                 stream          => 1,
334                 cdbi            => "metabib::${class}_field_entry",
335                 cachable        => 1,
336         );
337         __PACKAGE__->register_method(
338                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord_count.staff",
339                 method          => 'search_class_fts_count',
340                 api_level       => 1,
341                 stream          => 1,
342                 cdbi            => "metabib::${class}_field_entry",
343                 cachable        => 1,
344         );
345 }
346
347
348 1;