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 $st_table = config::copy_status->table;
20 my $src_table = config::bib_source->table;
21 my $br_table = biblio::record_entry->table;
22 my $loc_table = asset::copy_location->table;
23 my $out_table = actor::org_unit_type->table;
25 my $descendants = "actor.org_unit_descendants(u.id)";
26 my $ancestors = "actor.org_unit_ancestors(?)";
28 my $visible = 'AND st.holdable = TRUE AND loc.opac_visible = TRUE AND cp.opac_visible = TRUE';
29 if ($self->api_name =~ /staff/o) {
39 JOIN $cp_table cp ON (cn.id = cp.call_number)
40 JOIN $descendants a ON (cp.circ_lib = a.id)
41 JOIN $st_table st ON (cp.status = st.id)
42 JOIN $loc_table loc ON (cp.location = loc.id)
45 AND cn.deleted IS FALSE
46 AND cp.deleted IS FALSE)
51 JOIN $cp_table cp ON (cn.id = cp.call_number)
52 JOIN $descendants a ON (cp.circ_lib = a.id)
53 JOIN $st_table st ON (cp.status = st.id)
54 JOIN $loc_table loc ON (cp.location = loc.id)
57 AND cn.deleted IS FALSE
58 AND cp.deleted IS FALSE
62 (SELECT count(cp.id) + sum(CASE WHEN src.transcendant IS TRUE THEN 1 ELSE 0 END)
64 JOIN $cn_table cn ON (cn.record = br.id)
65 JOIN $cp_table cp ON (cn.id = cp.call_number)
66 JOIN $st_table st ON (cp.status = st.id)
67 JOIN $loc_table loc ON (cp.location = loc.id)
68 JOIN $src_table src ON (br.source = src.id)
70 AND st.holdable = TRUE
71 AND loc.opac_visible = TRUE
72 AND cp.opac_visible = TRUE
73 AND cn.deleted IS FALSE
74 AND cp.deleted IS FALSE)
77 JOIN $out_table t ON (u.ou_type = t.id)
81 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
82 $sth->execute(''.$args{record}, ''.$args{record}, ''.$args{record}, ''.$args{org_unit});
83 while ( my $row = $sth->fetchrow_hashref ) {
84 $client->respond( $row );
88 __PACKAGE__->register_method(
89 api_name => 'open-ils.storage.biblio.record_entry.copy_count',
90 method => 'record_copy_count',
95 __PACKAGE__->register_method(
96 api_name => 'open-ils.storage.biblio.record_entry.copy_count.staff',
97 method => 'record_copy_count',
103 sub record_ranged_tree {
109 my $limit = shift || 0;
110 my $offset = shift || 0;
112 my $ou_sql = defined($depth) ?
113 "SELECT id FROM actor.org_unit_descendants(?,?)":
114 "SELECT id FROM actor.org_unit_descendants(?)";
119 ->selectcol_arrayref(
123 (defined($depth) ? ($depth) : ()),
126 return undef unless ($ou_list and @$ou_list);
128 $r = biblio::record_entry->retrieve( $r );
129 return undef unless ($r);
131 my $rec = $r->to_fieldmapper;
132 $rec->call_numbers([]);
134 $rec->fixed_fields( $r->record_descriptor->next->to_fieldmapper );
136 my $offset_count = 0;
138 for my $cn ( $r->call_numbers ) {
139 my $call_number = $cn->to_fieldmapper;
140 $call_number->copies([]);
143 for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
144 if ($offset > 0 && $offset_count < $offset) {
149 last if ($limit > 0 && $limit_count >= $limit);
151 my $copy = $cp->to_fieldmapper;
152 $copy->status( $cp->status->to_fieldmapper );
153 $copy->location( $cp->location->to_fieldmapper );
154 push @{ $call_number->copies }, $copy;
159 last if ($limit > 0 && $limit_count >= $limit);
161 push @{ $rec->call_numbers }, $call_number if (@{ $call_number->copies });
166 __PACKAGE__->register_method(
167 api_name => 'open-ils.storage.biblio.record_entry.ranged_tree',
168 method => 'record_ranged_tree',
173 sub record_by_barcode {
177 my $cn_table = asset::call_number->table;
178 my $cp_table = asset::copy->table;
181 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
184 JOIN $cp_table cp ON (cp.call_number = cn.id)
188 my $rec = biblio::record_entry->retrieve( $r );
190 return $rec->to_fieldmapper if ($rec);
193 __PACKAGE__->register_method(
194 api_name => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
195 method => 'record_by_barcode',
204 my $cn_table = asset::call_number->table;
205 my $cp_table = asset::copy->table;
208 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
211 JOIN $cp_table cp ON (cp.call_number = cn.id)
215 my $rec = biblio::record_entry->retrieve( $r );
216 return undef unless ($rec);
218 my $r_fm = $rec->to_fieldmapper;
219 my $ff = $rec->record_descriptor->next;
220 $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
224 __PACKAGE__->register_method(
225 api_name => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
226 method => 'record_by_copy',
235 sub record_copy_count {
241 if ($self->api_name !~ /batch/o) {
245 throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
248 throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
251 $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
252 my ($org_unit) = $org_unit_lookup->run($oid);
254 # XXX Use descendancy tree here!!!
255 my $short_name_hack = $org_unit->shortname;
256 $short_name_hack = '' if (!$org_unit->parent_ou);
257 $short_name_hack .= '%';
258 # XXX Use descendancy tree here!!!
260 my $rec_list = join(',',@recs);
262 my $cp_table = asset::copy->table;
263 my $cn_table = asset::call_number->table;
265 my $select =<<" SQL";
266 SELECT count(cp.*) as copies
268 JOIN $cp_table cp ON (cp.call_number = cn.id)
269 WHERE cn.owning_lib LIKE ? AND
270 cn.record IN ($rec_list)
273 my $sth = asset::copy->db_Main->prepare_cached($select);
274 $sth->execute($short_name_hack);
276 my $results = $sth->fetchall_hashref('record');
278 $client->respond($$results{$_}{copies} || 0) for (@recs);
282 __PACKAGE__->register_method(
283 method => 'record_copy_count',
284 api_name => 'open-ils.storage.direct.biblio.record_copy_count',
288 __PACKAGE__->register_method(
289 method => 'record_copy_count',
290 api_name => 'open-ils.storage.direct.biblio.record_copy_count.batch',
298 sub global_record_copy_count {
304 my $cn_table = asset::call_number->table;
305 my $cp_table = asset::copy->table;
306 my $cl_table = asset::copy_location->table;
307 my $cs_table = config::copy_status->table;
309 my $copies_visible = 'AND cp.opac_visible IS TRUE AND cs.holdable IS TRUE AND cl.opac_visible IS TRUE';
310 $copies_visible = '' if ($self->api_name =~ /staff/o);
314 SELECT owning_lib, sum(avail), sum(tot)
316 SELECT cn.owning_lib, count(cp.id) as avail, 0 as tot
318 JOIN $cp_table cp ON (cn.id = cp.call_number)
319 JOIN $cs_table cs ON (cs.id = cp.status)
320 JOIN $cl_table cl ON (cl.id = cp.location)
326 SELECT cn.owning_lib, 0 as avail, count(cp.id) as tot
328 JOIN $cp_table cp ON (cn.id = cp.call_number)
329 JOIN $cs_table cs ON (cs.id = cp.status)
330 JOIN $cl_table cl ON (cl.id = cp.location)
338 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
339 $sth->execute("$rec", "$rec");
341 $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
344 __PACKAGE__->register_method(
345 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count',
346 method => 'global_record_copy_count',
351 __PACKAGE__->register_method(
352 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
353 method => 'global_record_copy_count',
359 sub record_copy_status_count {
365 my $depth = shift || 0;
368 my $descendants = "actor.org_unit_descendants(?,?)";
370 my $cn_table = asset::call_number->table;
371 my $cp_table = asset::copy->table;
372 my $cl_table = asset::copy_location->table;
373 my $cs_table = config::copy_status->table;
377 SELECT cp.circ_lib, cn.label, cp.status, count(cp.id)
384 AND cp.call_number = cn.id
385 AND cp.location = cl.id
386 AND cp.circ_lib = d.id
387 AND cp.status = cs.id
388 AND cl.opac_visible IS TRUE
389 AND cp.opac_visible IS TRUE
390 AND cp.deleted IS FALSE
395 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
396 $sth->execute($ou, $depth, "$rec" );
399 for my $row (@{$sth->fetchall_arrayref}) {
400 $data{$$row[0]}{$$row[1]}{$$row[2]} += $$row[3];
403 for my $ou (keys %data) {
404 for my $cn (keys %{$data{$ou}}) {
405 $client->respond( [$ou, $cn, $data{$ou}{$cn}] );
410 __PACKAGE__->register_method(
411 api_name => 'open-ils.storage.biblio.record_entry.status_copy_count',
412 method => 'record_copy_status_count',