1 package OpenILS::Application::Storage::Publisher::biblio;
2 use base qw/OpenILS::Application::Storage/;
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;
11 sub record_copy_count {
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(?)";
29 JOIN $cp_table cp ON (cn.id = cp.call_number)
30 JOIN $descendants a ON (cp.circ_lib = a.id)
36 JOIN $cp_table cp ON (cn.id = cp.call_number)
37 JOIN $descendants a ON (cp.circ_lib = a.id)
42 JOIN $out_table t ON (u.ou_type = t.id)
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 );
53 __PACKAGE__->register_method(
54 api_name => 'open-ils.storage.biblio.record_entry.copy_count',
55 method => 'record_copy_count',
61 sub record_ranged_tree {
66 my $depth = shift || 0;
72 'SELECT id FROM actor.org_unit_descendants(?,?)',
78 return undef unless ($ou_list and @$ou_list);
80 $r = biblio::record_entry->retrieve( $r );
81 return undef unless ($r);
83 my $rec = $r->to_fieldmapper;
84 $rec->call_numbers([]);
86 $rec->fixed_fields( $r->record_descriptor->next->to_fieldmapper );
88 for my $cn ( $r->call_numbers ) {
89 my $call_number = $cn->to_fieldmapper;
90 $call_number->copies([]);
93 for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
94 my $copy = $cp->to_fieldmapper;
95 $copy->status( $cp->status->to_fieldmapper );
96 $copy->location( $cp->status->to_fieldmapper );
98 push @{ $call_number->copies }, $copy;
101 push @{ $rec->call_numbers }, $call_number if (@{ $call_number->copies });
106 __PACKAGE__->register_method(
107 api_name => 'open-ils.storage.biblio.record_entry.ranged_tree',
108 method => 'record_ranged_tree',
113 sub record_by_barcode {
117 my $cn_table = asset::call_number->table;
118 my $cp_table = asset::copy->table;
121 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
124 JOIN $cp_table cp ON (cp.call_number = cn.id)
128 my $rec = biblio::record_entry->retrieve( $r );
130 return $rec->to_fieldmapper if ($rec);
133 __PACKAGE__->register_method(
134 api_name => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
135 method => 'record_by_barcode',
144 my $cn_table = asset::call_number->table;
145 my $cp_table = asset::copy->table;
148 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
151 JOIN $cp_table cp ON (cp.call_number = cn.id)
155 my $rec = biblio::record_entry->retrieve( $r );
156 return undef unless ($rec);
158 my $r_fm = $rec->to_fieldmapper;
159 my $ff = $rec->record_descriptor->next;
160 $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
164 __PACKAGE__->register_method(
165 api_name => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
166 method => 'record_by_copy',
175 sub record_copy_count {
181 if ($self->api_name !~ /batch/o) {
185 throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
188 throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
191 $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
192 my ($org_unit) = $org_unit_lookup->run($oid);
194 # XXX Use descendancy tree here!!!
195 my $short_name_hack = $org_unit->shortname;
196 $short_name_hack = '' if (!$org_unit->parent_ou);
197 $short_name_hack .= '%';
198 # XXX Use descendancy tree here!!!
200 my $rec_list = join(',',@recs);
202 my $cp_table = asset::copy->table;
203 my $cn_table = asset::call_number->table;
205 my $select =<<" SQL";
206 SELECT count(cp.*) as copies
208 JOIN $cp_table cp ON (cp.call_number = cn.id)
209 WHERE cn.owning_lib LIKE ? AND
210 cn.record IN ($rec_list)
213 my $sth = asset::copy->db_Main->prepare_cached($select);
214 $sth->execute($short_name_hack);
216 my $results = $sth->fetchall_hashref('record');
218 $client->respond($$results{$_}{copies} || 0) for (@recs);
222 __PACKAGE__->register_method(
223 method => 'record_copy_count',
224 api_name => 'open-ils.storage.direct.biblio.record_copy_count',
228 __PACKAGE__->register_method(
229 method => 'record_copy_count',
230 api_name => 'open-ils.storage.direct.biblio.record_copy_count.batch',
238 sub global_record_copy_count {
244 my $cn_table = asset::call_number->table;
245 my $cp_table = asset::copy->table;
246 my $cl_table = asset::copy_location->table;
247 my $cs_table = config::copy_status->table;
249 my $copies_visible = 'AND cp.opac_visible IS TRUE AND cs.holdable IS TRUE AND cl.opac_visible IS TRUE';
250 $copies_visible = '' if ($self->api_name =~ /staff/o);
254 SELECT owning_lib, sum(avail), sum(tot)
256 SELECT cn.owning_lib, count(cp.id) as avail, 0 as tot
258 JOIN $cp_table cp ON (cn.id = cp.call_number)
259 JOIN $cs_table cs ON (cs.id = cp.status)
260 JOIN $cl_table cl ON (cl.id = cp.location)
266 SELECT cn.owning_lib, 0 as avail, count(cp.id) as tot
268 JOIN $cp_table cp ON (cn.id = cp.call_number)
269 JOIN $cs_table cs ON (cs.id = cp.status)
270 JOIN $cl_table cl ON (cl.id = cp.location)
278 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
279 $sth->execute("$rec", "$rec");
281 $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
284 __PACKAGE__->register_method(
285 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count',
286 method => 'global_record_copy_count',
291 __PACKAGE__->register_method(
292 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
293 method => 'global_record_copy_count',
299 sub record_copy_status_count {
305 my $cn_table = asset::call_number->table;
306 my $cp_table = asset::copy->table;
307 my $cl_table = asset::copy_location->table;
308 my $cs_table = config::copy_status->table;
312 SELECT cp.circ_lib, cn.label, cp.status, count(cp.id)
318 AND cp.call_number = cn.id
319 AND cp.location = cl.id
320 AND cp.status = cs.id
321 AND cl.opac_visible IS TRUE
322 AND cp.opac_visible IS TRUE
328 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
329 $sth->execute("$rec");
331 my ($ou,$cn) = (0,'');
333 for my $row (@{$sth->fetchall_arrayref}) {
334 if ($ou and $ou ne $$row[0]) {
336 $client->respond( [$ou, $cn, {%data}] );
339 ($ou,$cn) = ($$row[0],$$row[1]);
340 $data{$$row[2]} = $$row[3];
342 return [$ou, $cn, {%data}] if ($ou);
345 __PACKAGE__->register_method(
346 api_name => 'open-ils.storage.biblio.record_entry.status_copy_count',
347 method => 'record_copy_status_count',