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 $loc_table = asset::copy_location->table;
21 my $out_table = actor::org_unit_type->table;
23 my $descendants = "actor.org_unit_descendants(u.id)";
24 my $ancestors = "actor.org_unit_ancestors(?)";
26 my $visible = 'AND st.holdable = TRUE AND loc.opac_visible = TRUE AND cp.opac_visible = TRUE';
27 if ($self->api_name =~ /staff/o) {
37 JOIN $cp_table cp ON (cn.id = cp.call_number)
38 JOIN $descendants a ON (cp.circ_lib = a.id)
39 JOIN $st_table st ON (cp.status = st.id)
40 JOIN $loc_table loc ON (cp.location = loc.id)
43 AND cn.deleted IS FALSE
44 AND cp.deleted IS FALSE)
49 JOIN $cp_table cp ON (cn.id = cp.call_number)
50 JOIN $descendants a ON (cp.circ_lib = a.id)
51 JOIN $st_table st ON (cp.status = st.id)
52 JOIN $loc_table loc ON (cp.location = loc.id)
55 AND cn.deleted IS FALSE
56 AND cp.deleted IS FALSE
62 JOIN $cp_table cp ON (cn.id = cp.call_number)
63 JOIN $st_table st ON (cp.status = st.id)
64 JOIN $loc_table loc ON (cp.location = loc.id)
66 AND st.holdable = TRUE
67 AND loc.opac_visible = TRUE
68 AND cp.opac_visible = TRUE
69 AND cn.deleted IS FALSE
70 AND cp.deleted IS FALSE)
73 JOIN $out_table t ON (u.ou_type = t.id)
77 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
78 $sth->execute(''.$args{record}, ''.$args{record}, ''.$args{record}, ''.$args{org_unit});
79 while ( my $row = $sth->fetchrow_hashref ) {
80 $client->respond( $row );
84 __PACKAGE__->register_method(
85 api_name => 'open-ils.storage.biblio.record_entry.copy_count',
86 method => 'record_copy_count',
91 __PACKAGE__->register_method(
92 api_name => 'open-ils.storage.biblio.record_entry.copy_count.staff',
93 method => 'record_copy_count',
99 sub record_ranged_tree {
105 my $limit = shift || 0;
106 my $offset = shift || 0;
108 my $ou_sql = defined($depth) ?
109 "SELECT id FROM actor.org_unit_descendants(?,?)":
110 "SELECT id FROM actor.org_unit_descendants(?)";
115 ->selectcol_arrayref(
119 (defined($depth) ? ($depth) : ()),
122 return undef unless ($ou_list and @$ou_list);
124 $r = biblio::record_entry->retrieve( $r );
125 return undef unless ($r);
127 my $rec = $r->to_fieldmapper;
128 $rec->call_numbers([]);
130 $rec->fixed_fields( $r->record_descriptor->next->to_fieldmapper );
132 my $offset_count = 0;
134 for my $cn ( $r->call_numbers ) {
135 my $call_number = $cn->to_fieldmapper;
136 $call_number->copies([]);
139 for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
140 if ($offset > 0 && $offset_count < $offset) {
145 last if ($limit > 0 && $limit_count >= $limit);
147 my $copy = $cp->to_fieldmapper;
148 $copy->status( $cp->status->to_fieldmapper );
149 $copy->location( $cp->location->to_fieldmapper );
150 push @{ $call_number->copies }, $copy;
155 last if ($limit > 0 && $limit_count >= $limit);
157 push @{ $rec->call_numbers }, $call_number if (@{ $call_number->copies });
162 __PACKAGE__->register_method(
163 api_name => 'open-ils.storage.biblio.record_entry.ranged_tree',
164 method => 'record_ranged_tree',
169 sub record_by_barcode {
173 my $cn_table = asset::call_number->table;
174 my $cp_table = asset::copy->table;
177 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
180 JOIN $cp_table cp ON (cp.call_number = cn.id)
184 my $rec = biblio::record_entry->retrieve( $r );
186 return $rec->to_fieldmapper if ($rec);
189 __PACKAGE__->register_method(
190 api_name => 'open-ils.storage.biblio.record_entry.retrieve_by_barcode',
191 method => 'record_by_barcode',
200 my $cn_table = asset::call_number->table;
201 my $cp_table = asset::copy->table;
204 my ($r) = biblio::record_entry->db_Main->selectrow_array( <<" SQL", {}, $id );
207 JOIN $cp_table cp ON (cp.call_number = cn.id)
211 my $rec = biblio::record_entry->retrieve( $r );
212 return undef unless ($rec);
214 my $r_fm = $rec->to_fieldmapper;
215 my $ff = $rec->record_descriptor->next;
216 $r_fm->fixed_fields( $ff->to_fieldmapper ) if ($ff);
220 __PACKAGE__->register_method(
221 api_name => 'open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy',
222 method => 'record_by_copy',
231 sub record_copy_count {
237 if ($self->api_name !~ /batch/o) {
241 throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
244 throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
247 $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
248 my ($org_unit) = $org_unit_lookup->run($oid);
250 # XXX Use descendancy tree here!!!
251 my $short_name_hack = $org_unit->shortname;
252 $short_name_hack = '' if (!$org_unit->parent_ou);
253 $short_name_hack .= '%';
254 # XXX Use descendancy tree here!!!
256 my $rec_list = join(',',@recs);
258 my $cp_table = asset::copy->table;
259 my $cn_table = asset::call_number->table;
261 my $select =<<" SQL";
262 SELECT count(cp.*) as copies
264 JOIN $cp_table cp ON (cp.call_number = cn.id)
265 WHERE cn.owning_lib LIKE ? AND
266 cn.record IN ($rec_list)
269 my $sth = asset::copy->db_Main->prepare_cached($select);
270 $sth->execute($short_name_hack);
272 my $results = $sth->fetchall_hashref('record');
274 $client->respond($$results{$_}{copies} || 0) for (@recs);
278 __PACKAGE__->register_method(
279 method => 'record_copy_count',
280 api_name => 'open-ils.storage.direct.biblio.record_copy_count',
284 __PACKAGE__->register_method(
285 method => 'record_copy_count',
286 api_name => 'open-ils.storage.direct.biblio.record_copy_count.batch',
294 sub global_record_copy_count {
300 my $cn_table = asset::call_number->table;
301 my $cp_table = asset::copy->table;
302 my $cl_table = asset::copy_location->table;
303 my $cs_table = config::copy_status->table;
305 my $copies_visible = 'AND cp.opac_visible IS TRUE AND cs.holdable IS TRUE AND cl.opac_visible IS TRUE';
306 $copies_visible = '' if ($self->api_name =~ /staff/o);
310 SELECT owning_lib, sum(avail), sum(tot)
312 SELECT cn.owning_lib, count(cp.id) as avail, 0 as tot
314 JOIN $cp_table cp ON (cn.id = cp.call_number)
315 JOIN $cs_table cs ON (cs.id = cp.status)
316 JOIN $cl_table cl ON (cl.id = cp.location)
322 SELECT cn.owning_lib, 0 as avail, count(cp.id) as tot
324 JOIN $cp_table cp ON (cn.id = cp.call_number)
325 JOIN $cs_table cs ON (cs.id = cp.status)
326 JOIN $cl_table cl ON (cl.id = cp.location)
334 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
335 $sth->execute("$rec", "$rec");
337 $client->respond( $_ ) for (@{$sth->fetchall_arrayref});
340 __PACKAGE__->register_method(
341 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count',
342 method => 'global_record_copy_count',
347 __PACKAGE__->register_method(
348 api_name => 'open-ils.storage.biblio.record_entry.global_copy_count.staff',
349 method => 'global_record_copy_count',
355 sub record_copy_status_count {
361 my $depth = shift || 0;
364 my $descendants = "actor.org_unit_descendants(?,?)";
366 my $cn_table = asset::call_number->table;
367 my $cp_table = asset::copy->table;
368 my $cl_table = asset::copy_location->table;
369 my $cs_table = config::copy_status->table;
373 SELECT cp.circ_lib, cn.label, cp.status, count(cp.id)
380 AND cp.call_number = cn.id
381 AND cp.location = cl.id
382 AND cp.circ_lib = d.id
383 AND cp.status = cs.id
384 AND cl.opac_visible IS TRUE
385 AND cp.opac_visible IS TRUE
386 AND cp.deleted IS FALSE
391 my $sth = biblio::record_entry->db_Main->prepare_cached($sql);
392 $sth->execute($ou, $depth, "$rec" );
395 for my $row (@{$sth->fetchall_arrayref}) {
396 $data{$$row[0]}{$$row[1]}{$$row[2]} += $$row[3];
399 for my $ou (keys %data) {
400 for my $cn (keys %{$data{$ou}}) {
401 $client->respond( [$ou, $cn, $data{$ou}{$cn}] );
406 __PACKAGE__->register_method(
407 api_name => 'open-ils.storage.biblio.record_entry.status_copy_count',
408 method => 'record_copy_status_count',