]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/biblio.pm
protecting against no rec_descriptor (pre-cat)
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / biblio.pm
1 package OpenILS::Application::Storage::Publisher::biblio;
2 use base qw/OpenILS::Application::Storage/;
3 use vars qw/$VERSION/;
4 use OpenSRF::EX qw/:try/;
5 #use OpenILS::Application::Storage::CDBI::biblio;
6 #use OpenILS::Application::Storage::CDBI::asset;
7 use OpenILS::Utils::Fieldmapper;
8
9 $VERSION = 1;
10
11 sub record_copy_count {
12         my $self = shift;
13         my $client = shift;
14
15         my %args = @_;
16
17         my $cn_table = asset::call_number->table;
18         my $cp_table = asset::copy->table;
19         my $out_table = actor::org_unit_type->table;
20         my $descendants = "actor.org_unit_descendants(u.id)";
21         my $ancestors = "actor.org_unit_ancestors(?)";
22
23         my $sql = <<"   SQL";
24                 SELECT  t.depth,
25                         u.id AS org_unit,
26                         sum(
27                                 (SELECT count(cp.id)
28                                   FROM  $cn_table cn
29                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
30                                         JOIN $descendants a ON (cp.circ_lib = a.id)
31                                   WHERE cn.record = ?)
32                         ) AS count,
33                         sum(
34                                 (SELECT count(cp.id)
35                                   FROM  $cn_table cn
36                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
37                                         JOIN $descendants a ON (cp.circ_lib = a.id)
38                                   WHERE cn.record = ?
39                                         AND cp.status = 0)
40                         ) AS available
41                   FROM  $ancestors u
42                         JOIN $out_table t ON (u.ou_type = t.id)
43                   GROUP BY 1,2
44         SQL
45
46         my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
47         $sth->execute(''.$args{record}, ''.$args{record}, ''.$args{org_unit});
48         while ( my $row = $sth->fetchrow_hashref ) {
49                 $client->respond( $row );
50         }
51         return undef;
52 }
53 __PACKAGE__->register_method(
54         api_name        => 'open-ils.storage.biblio.record_entry.copy_count',
55         method          => 'record_copy_count',
56         api_level       => 1,
57         stream          => 1,
58         cachable        => 1,
59 );
60
61 sub record_by_barcode {
62         my $self = shift;
63         my $client = shift;
64
65         my $cn_table = asset::call_number->table;
66         my $cp_table = asset::copy->table;
67
68         my $id = ''.shift;
69         my ($r) = biblio::record_entry->db_Main->selectrow_array( <<"   SQL", {}, $id );
70                 SELECT  cn.record
71                   FROM  $cn_table cn
72                         JOIN $cp_table cp ON (cp.call_number = cn.id)
73                   WHERE cp.barcode = ?
74         SQL
75
76         my $rec = biblio::record_entry->retrieve( $r );
77
78         return $rec->to_fieldmapper if ($rec);
79         return undef;
80 }
81 __PACKAGE__->register_method(
82         api_name        => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
83         method          => 'record_by_barcode',
84         api_level       => 1,
85         cachable        => 1,
86 );
87
88 sub record_by_copy {
89         my $self = shift;
90         my $client = shift;
91
92         my $cn_table = asset::call_number->table;
93         my $cp_table = asset::copy->table;
94
95         my $id = ''.shift;
96         my ($r) = biblio::record_entry->db_Main->selectrow_array( <<"   SQL", {}, $id );
97                 SELECT  cn.record
98                   FROM  $cn_table cn
99                         JOIN $cp_table cp ON (cp.call_number = cn.id)
100                   WHERE cp.id = ?
101         SQL
102
103         my $rec = biblio::record_entry->retrieve( $r );
104         return undef unless ($rec);
105
106         my $r_fm = $rec->to_fieldmapper;
107         my $ff = $rec->record_descriptor->next;
108         $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
109
110         return $r_fm;
111 }
112 __PACKAGE__->register_method(
113         api_name        => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
114         method          => 'record_by_copy',
115         api_level       => 1,
116         cachable        => 1,
117 );
118
119
120 =comment Old version
121
122 my $org_unit_lookup;
123 sub record_copy_count {
124         my $self = shift;
125         my $client = shift;
126         my $oid = shift;
127         my @recs = @_;
128
129         if ($self->api_name !~ /batch/o) {
130                 @recs = ($recs[0]);
131         }
132
133         throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
134                 unless ($oid);
135
136         throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
137                 unless (@recs);
138
139         $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
140         my ($org_unit) = $org_unit_lookup->run($oid);
141
142         # XXX Use descendancy tree here!!!
143         my $short_name_hack = $org_unit->shortname;
144         $short_name_hack = '' if (!$org_unit->parent_ou);
145         $short_name_hack .= '%';
146         # XXX Use descendancy tree here!!!
147
148         my $rec_list = join(',',@recs);
149
150         my $cp_table = asset::copy->table;
151         my $cn_table = asset::call_number->table;
152
153         my $select =<<" SQL";
154                 SELECT  count(cp.*) as copies
155                   FROM  $cn_table cn
156                         JOIN $cp_table cp ON (cp.call_number = cn.id)
157                   WHERE cn.owning_lib LIKE ? AND
158                         cn.record IN ($rec_list)
159         SQL
160
161         my $sth = asset::copy->db_Main->prepare_cached($select);
162         $sth->execute($short_name_hack);
163
164         my $results = $sth->fetchall_hashref('record');
165
166         $client->respond($$results{$_}{copies} || 0) for (@recs);
167
168         return undef;
169 }
170 __PACKAGE__->register_method(
171         method          => 'record_copy_count',
172         api_name        => 'open-ils.storage.direct.biblio.record_copy_count',
173         api_level       => 1,
174         argc            => 1,
175 );
176 __PACKAGE__->register_method(
177         method          => 'record_copy_count',
178         api_name        => 'open-ils.storage.direct.biblio.record_copy_count.batch',
179         api_level       => 1,
180         argc            => 1,
181         stream          => 1,
182 );
183
184 =cut
185
186 sub global_record_copy_count {
187         my $self = shift;
188         my $client = shift;
189
190         my $rec = shift;
191
192         my $cn_table = asset::call_number->table;
193         my $cp_table = asset::copy->table;
194         my $cl_table = asset::copy_location->table;
195         my $cs_table = config::copy_status->table;
196
197         my $copies_visible = 'AND cp.opac_visible IS TRUE AND cs.holdable IS TRUE AND cl.opac_visible IS TRUE';
198         $copies_visible = '' if ($self->api_name =~ /staff/o);
199
200         my $sql = <<"   SQL";
201
202                 SELECT  owning_lib, sum(avail), sum(tot)
203                   FROM  (
204                                 SELECT  cn.owning_lib, count(cp.id) as avail, 0 as tot
205                                   FROM  $cn_table cn
206                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
207                                         JOIN $cs_table cs ON (cs.id = cp.status)
208                                         JOIN $cl_table cl ON (cl.id = cp.location)
209                                   WHERE cn.record = ?
210                                         AND cp.status = 0
211                                         $copies_visible
212                                   GROUP BY 1
213                                                 UNION
214                                 SELECT  cn.owning_lib, 0 as avail, count(cp.id) as tot
215                                   FROM  $cn_table cn
216                                         JOIN $cp_table cp ON (cn.id = cp.call_number)
217                                         JOIN $cs_table cs ON (cs.id = cp.status)
218                                         JOIN $cl_table cl ON (cl.id = cp.location)
219                                   WHERE cn.record = ?
220                                         $copies_visible
221                                   GROUP BY 1
222                         ) x
223                   GROUP BY 1
224         SQL
225
226         my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
227         $sth->execute("$rec", "$rec");
228
229         $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
230         return undef;
231 }
232 __PACKAGE__->register_method(
233         api_name        => 'open-ils.storage.biblio.record_entry.global_copy_count',
234         method          => 'global_record_copy_count',
235         api_level       => 1,
236         stream          => 1,
237         cachable        => 1,
238 );
239 __PACKAGE__->register_method(
240         api_name        => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
241         method          => 'global_record_copy_count',
242         api_level       => 1,
243         stream          => 1,
244         cachable        => 1,
245 );
246
247 sub record_copy_status_count {
248         my $self = shift;
249         my $client = shift;
250
251         my $rec = shift;
252
253         my $cn_table = asset::call_number->table;
254         my $cp_table = asset::copy->table;
255         my $cl_table = asset::copy_location->table;
256         my $cs_table = config::copy_status->table;
257
258         my $sql = <<"   SQL";
259
260                 SELECT  cp.circ_lib, cn.label, cp.status, count(cp.id)
261                   FROM  $cp_table cp,
262                         $cn_table cn,
263                         $cl_table cl,
264                         $cs_table cs
265                   WHERE cn.record = ?
266                         AND cp.call_number = cn.id
267                         AND cp.location = cl.id
268                         AND cp.status = cs.id
269                         AND cl.opac_visible IS TRUE
270                         AND cp.opac_visible IS TRUE
271                         AND cs.holdable
272                   GROUP BY 1,2,3
273                   ORDER BY 1,2,3;
274         SQL
275
276         my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
277         $sth->execute("$rec");
278
279         my ($ou,$cn) = (0,'');
280         my %data = ();
281         for my $row (@{$sth->fetchall_arrayref}) {
282                 if ($ou and $ou ne $$row[0]) {
283                         my $i = 0;
284                         $client->respond( [$ou, $cn, {%data}] );
285                         %data = ();
286                 }
287                 ($ou,$cn) = ($$row[0],$$row[1]);
288                 $data{$$row[2]} = $$row[3];
289         }
290         return [$ou, $cn, {%data}] if ($ou);
291         return undef;
292 }
293 __PACKAGE__->register_method(
294         api_name        => 'open-ils.storage.biblio.record_entry.status_copy_count',
295         method          => 'record_copy_status_count',
296         api_level       => 1,
297         stream          => 1,
298         cachable        => 1,
299 );
300
301 1;