]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/metabib.pm
order records of a metarecord by format,copycount
[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 # need to order record IDs by:
17 #  1) format - text, movie, sound, software, images, maps, mixed, music, 3d
18 #  2) proximity --- XXX Can't do it cheap...
19 #  3) count
20 sub ordered_records_from_metarecord {
21         my $self = shift;
22         my $client = shift;
23         my $mr = shift;
24
25         my $copies_visible = 'AND cp.opac_visible IS TRUE';
26         $copies_visible = '' if ($self->api_name =~ /staff/o);
27
28         my $sm_table = metabib::metarecord_source_map->table;
29         my $rd_table = metabib::record_descriptor->table;
30         my $cn_table = asset::call_number->table;
31         my $cp_table = asset::copy->table;
32         my $out_table = actor::org_unit_type->table;
33
34         my $sql = <<"   SQL";
35                 SELECT  cn.record,
36                         rd.item_type,
37                         sum((SELECT     count(cp.id)
38                                FROM     $cp_table cp
39                                WHERE    cn.id = cp.call_number
40                                         $copies_visible
41                           )) AS count
42                   FROM  $cn_table cn,
43                         $sm_table sm,
44                         $rd_table rd
45                   WHERE cn.record = sm.source
46                         AND cn.record = rd.record
47                         AND sm.metarecord = ?
48                   GROUP BY cn.record, rd.item_type
49                   ORDER BY
50                         CASE
51                                 WHEN rd.item_type IS NULL -- default
52                                         THEN 0
53                                 WHEN rd.item_type = '' -- default
54                                         THEN 0
55                                 WHEN rd.item_type IN ('a','t') -- books
56                                         THEN 1
57                                 WHEN rd.item_type = 'g' -- movies
58                                         THEN 2
59                                 WHEN rd.item_type IN ('i','j') -- sound recordings
60                                         THEN 3
61                                 WHEN rd.item_type = 'm' -- software
62                                         THEN 4
63                                 WHEN rd.item_type = 'k' -- images
64                                         THEN 5
65                                 WHEN rd.item_type IN ('e','f') -- maps
66                                         THEN 6
67                                 WHEN rd.item_type IN ('o','p') -- mixed
68                                         THEN 7
69                                 WHEN rd.item_type IN ('c','d') -- music
70                                         THEN 8
71                                 WHEN rd.item_type = 'r' -- 3d
72                                         THEN 9
73                         END,
74                         count DESC
75         SQL
76
77         my $sth = metabib::metarecord_source_map->db_Main->prepare_cached($sql);
78         $sth->execute("$mr");
79         while ( my $row = $sth->fetchrow_arrayref ) {
80                 $client->respond( $$row[0] );
81         }
82         return undef;
83
84 }
85 __PACKAGE__->register_method(
86         api_name        => 'open-ils.storage.ordered.metabib.metarecord.records',
87         method          => 'ordered_records_from_metarecord',
88         api_level       => 1,
89         stream          => 1,
90         cachable        => 1,
91 );
92 __PACKAGE__->register_method(
93         api_name        => 'open-ils.storage.ordered.metabib.metarecord.records.staff',
94         method          => 'ordered_records_from_metarecord',
95         api_level       => 1,
96         stream          => 1,
97         cachable        => 1,
98 );
99
100
101 sub metarecord_copy_count {
102         my $self = shift;
103         my $client = shift;
104
105         my %args = @_;
106
107         my $sm_table = metabib::metarecord_source_map->table;
108         my $cn_table = asset::call_number->table;
109         my $cp_table = asset::copy->table;
110         my $out_table = actor::org_unit_type->table;
111         my $descendants = "actor.org_unit_descendants(u.id)";
112         my $ancestors = "actor.org_unit_ancestors(?)";
113
114         my $copies_visible = 'AND cp.opac_visible IS TRUE';
115         $copies_visible = '' if ($self->api_name =~ /staff/o);
116
117         my $sql = <<"   SQL";
118                 SELECT  t.depth,
119                         u.id AS org_unit,
120                         sum(
121                                 (SELECT count(cp.id)
122                                   FROM  $sm_table r
123                                         JOIN $cn_table cn ON (cn.record = r.source)
124                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
125                                         JOIN $descendants a ON (cp.circ_lib = a.id)
126                                   WHERE r.metarecord = ?
127                                         $copies_visible
128                                 )
129                         ) AS count,
130                         sum(
131                                 (SELECT count(cp.id)
132                                   FROM  $sm_table r
133                                         JOIN $cn_table cn ON (cn.record = r.source)
134                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
135                                         JOIN $descendants a ON (cp.circ_lib = a.id)
136                                   WHERE r.metarecord = ?
137                                         AND cp.status = 0
138                                         $copies_visible
139                                 )
140                         ) AS available
141
142                   FROM  $ancestors u
143                         JOIN $out_table t ON (u.ou_type = t.id)
144                   GROUP BY 1,2
145         SQL
146
147         my $sth = metabib::metarecord_source_map->db_Main->prepare_cached($sql);
148         $sth->execute(''.$args{metarecord}, ''.$args{metarecord}, ''.$args{org_unit});
149         while ( my $row = $sth->fetchrow_hashref ) {
150                 $client->respond( $row );
151         }
152         return undef;
153 }
154 __PACKAGE__->register_method(
155         api_name        => 'open-ils.storage.metabib.metarecord.copy_count',
156         method          => 'metarecord_copy_count',
157         api_level       => 1,
158         stream          => 1,
159         cachable        => 1,
160 );
161 __PACKAGE__->register_method(
162         api_name        => 'open-ils.storage.metabib.metarecord.copy_count.staff',
163         method          => 'metarecord_copy_count',
164         api_level       => 1,
165         stream          => 1,
166         cachable        => 1,
167 );
168
169 sub search_full_rec {
170         my $self = shift;
171         my $client = shift;
172
173         my %args = @_;
174         
175         my $term = $args{term};
176         my $limiters = $args{restrict};
177
178         my ($index_col) = metabib::full_rec->columns('FTS');
179         $index_col ||= 'value';
180         my $search_table = metabib::full_rec->table;
181
182         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
183
184         my $fts_where = $fts->sql_where_clause();
185         my @fts_ranks = $fts->fts_rank;
186
187         my $rank = join(' + ', @fts_ranks);
188
189         my @binds;
190         my @wheres;
191         for my $limit (@$limiters) {
192                 push @wheres, "( tag = ? AND subfield LIKE ? AND $fts_where )";
193                 push @binds, $$limit{tag}, $$limit{subfield};
194                 $log->debug("Limiting query using { tag => $$limit{tag}, subfield => $$limit{subfield} }", DEBUG);
195         }
196         my $where = join(' OR ', @wheres);
197
198         my $select = "SELECT record, sum($rank) FROM $search_table WHERE $where GROUP BY 1 ORDER BY 2 DESC;";
199
200         $log->debug("Search SQL :: [$select]",DEBUG);
201
202         my $recs = metabib::full_rec->db_Main->selectall_arrayref($select, {}, @binds);
203         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
204
205         $client->respond($_) for (@$recs);
206         return undef;
207 }
208 __PACKAGE__->register_method(
209         api_name        => 'open-ils.storage.direct.metabib.full_rec.search_fts.value',
210         method          => 'search_full_rec',
211         api_level       => 1,
212         stream          => 1,
213         cachable        => 1,
214 );
215 __PACKAGE__->register_method(
216         api_name        => 'open-ils.storage.direct.metabib.full_rec.search_fts.index_vector',
217         method          => 'search_full_rec',
218         api_level       => 1,
219         stream          => 1,
220         cachable        => 1,
221 );
222
223
224 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
225 sub search_class_fts {
226         my $self = shift;
227         my $client = shift;
228         my %args = @_;
229         
230         my $term = $args{term};
231         my $ou = $args{org_unit};
232         my $ou_type = $args{depth};
233         my $limit = $args{limit};
234         my $offset = $args{offset};
235
236         my $limit_clause = '';
237         my $offset_clause = '';
238
239         $limit_clause = "LIMIT $limit" if (defined $limit and int($limit) > 0);
240         $offset_clause = "OFFSET $offset" if (defined $offset and int($offset) > 0);
241
242
243         my $descendants = defined($ou_type) ?
244                                 "actor.org_unit_descendants($ou, $ou_type)" :
245                                 "actor.org_unit_descendants($ou)";
246
247         my $class = $self->{cdbi};
248         my $search_table = $class->table;
249
250         my $metabib_metarecord = metabib::metarecord->table;
251         my $metabib_full_rec = metabib::full_rec->table;
252         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
253         my $asset_call_number_table = asset::call_number->table;
254         my $asset_copy_table = asset::copy->table;
255
256         my ($index_col) = $class->columns('FTS');
257         $index_col ||= 'value';
258
259         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'f.value', "f.$index_col");
260
261         my $fts_where = $fts->sql_where_clause;
262         my @fts_ranks = $fts->fts_rank;
263
264         my $rank = join(' + ', @fts_ranks);
265
266         my $has_vols = 'AND cn.owning_lib = d.id';
267         my $has_copies = 'AND cp.call_number = cn.id';
268         my $copies_visible = 'AND cp.opac_visible IS TRUE';
269
270         my $visible_count = ', count(DISTINCT cp.id)';
271         my $visible_count_test = 'HAVING count(DISTINCT cp.id) > 0';
272
273         if ($self->api_name =~ /staff/o) {
274                 $copies_visible = '';
275                 $visible_count_test = '';
276                 $has_copies = '' if ($ou_type == 0);
277                 $has_vols = '' if ($ou_type == 0);
278         }
279
280         my $rank_calc = ", sum($rank + CASE WHEN f.value ILIKE ? THEN 1 ELSE 0 END)/count(m.source)";
281         $rank_calc = ', 1' if ($self->api_name =~ /unordered/o);
282
283         my $select = <<"        SQL";
284                 SELECT  m.metarecord $rank_calc $visible_count
285                   FROM  $search_table f,
286                         $metabib_metarecord_source_map_table m,
287                         $asset_call_number_table cn,
288                         $asset_copy_table cp,
289                         $descendants d
290                   WHERE $fts_where
291                         AND m.source = f.source
292                         AND cn.record = m.source
293                         $has_vols
294                         $has_copies
295                         $copies_visible
296                   GROUP BY m.metarecord $visible_count_test
297                   ORDER BY 2 DESC
298                   $limit_clause $offset_clause
299         SQL
300
301         $log->debug("Field Search SQL :: [$select]",DEBUG);
302
303         my $string = '%'.join('%',$fts->words).'%';
304         my $recs = ($self->api_name =~ /unordered/o) ? 
305                         $class->db_Main->selectall_arrayref($select) :
306                         $class->db_Main->selectall_arrayref($select, {}, lc($string));
307         
308         $log->debug("Search yielded ".scalar(@$recs)." results.",DEBUG);
309
310         $client->respond($_) for (@$recs);
311         return undef;
312 }
313
314 for my $class ( qw/title author subject keyword series/ ) {
315         __PACKAGE__->register_method(
316                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord",
317                 method          => 'search_class_fts',
318                 api_level       => 1,
319                 stream          => 1,
320                 cdbi            => "metabib::${class}_field_entry",
321                 cachable        => 1,
322         );
323         __PACKAGE__->register_method(
324                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.unordered",
325                 method          => 'search_class_fts',
326                 api_level       => 1,
327                 stream          => 1,
328                 cdbi            => "metabib::${class}_field_entry",
329                 cachable        => 1,
330         );
331         __PACKAGE__->register_method(
332                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.staff",
333                 method          => 'search_class_fts',
334                 api_level       => 1,
335                 stream          => 1,
336                 cdbi            => "metabib::${class}_field_entry",
337                 cachable        => 1,
338         );
339         __PACKAGE__->register_method(
340                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord.staff.unordered",
341                 method          => 'search_class_fts',
342                 api_level       => 1,
343                 stream          => 1,
344                 cdbi            => "metabib::${class}_field_entry",
345                 cachable        => 1,
346         );
347 }
348
349 # XXX factored most of the PG dependant stuff out of here... need to find a way to do "dependants".
350 sub search_class_fts_count {
351         my $self = shift;
352         my $client = shift;
353         my %args = @_;
354         
355         my $term = $args{term};
356         my $ou = $args{org_unit};
357         my $ou_type = $args{depth};
358         my $limit = $args{limit} || 100;
359         my $offset = $args{offset} || 0;
360
361         my $descendants = defined($ou_type) ?
362                                 "actor.org_unit_descendants($ou, $ou_type)" :
363                                 "actor.org_unit_descendants($ou)";
364                 
365
366         (my $search_class = $self->api_name) =~ s/.*metabib.(\w+).search_fts.*/$1/o;
367
368         my $class = $self->{cdbi};
369         my $search_table = $class->table;
370
371         my $metabib_metarecord_source_map_table = metabib::metarecord_source_map->table;
372         my $asset_call_number_table = asset::call_number->table;
373         my $asset_copy_table = asset::copy->table;
374
375         my ($index_col) = $class->columns('FTS');
376         $index_col ||= 'value';
377
378         my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value',"$index_col");
379
380         my $fts_where = $fts->sql_where_clause;
381
382         my $has_vols = 'AND cn.owning_lib = d.id';
383         my $has_copies = 'AND cp.call_number = cn.id';
384         my $copies_visible = 'AND cp.opac_visible IS TRUE';
385         if ($self->api_name =~ /staff/o) {
386                 $copies_visible = '';
387                 $has_vols = '' if ($ou_type == 0);
388                 $has_copies = '' if ($ou_type == 0);
389         }
390
391         # XXX test an "EXISTS version of descendant checking...
392         my $select = <<"        SQL";
393                 SELECT  count(distinct  m.metarecord)
394                   FROM  $search_table f,
395                         $metabib_metarecord_source_map_table m,
396                         $asset_call_number_table cn,
397                         $asset_copy_table cp,
398                         $descendants d
399                   WHERE $fts_where
400                         AND m.source = f.source
401                         AND cn.record = m.source
402                         $has_vols
403                         $has_copies
404                         $copies_visible
405         SQL
406
407         $log->debug("Field Search Count SQL :: [$select]",DEBUG);
408
409         my $recs = $class->db_Main->selectrow_arrayref($select)->[0];
410         
411         $log->debug("Count Search yielded $recs results.",DEBUG);
412
413         return $recs;
414
415 }
416 for my $class ( qw/title author subject keyword series/ ) {
417         __PACKAGE__->register_method(
418                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord_count",
419                 method          => 'search_class_fts_count',
420                 api_level       => 1,
421                 stream          => 1,
422                 cdbi            => "metabib::${class}_field_entry",
423                 cachable        => 1,
424         );
425         __PACKAGE__->register_method(
426                 api_name        => "open-ils.storage.metabib.$class.search_fts.metarecord_count.staff",
427                 method          => 'search_class_fts_count',
428                 api_level       => 1,
429                 stream          => 1,
430                 cdbi            => "metabib::${class}_field_entry",
431                 cachable        => 1,
432         );
433 }
434
435
436 1;