]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/biblio.pm
25873cced6143a9750060fe3f5719a98b7e41e84
[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.available IS TRUE)
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
62 =comment Old version
63
64 my $org_unit_lookup;
65 sub record_copy_count {
66         my $self = shift;
67         my $client = shift;
68         my $oid = shift;
69         my @recs = @_;
70
71         if ($self->api_name !~ /batch/o) {
72                 @recs = ($recs[0]);
73         }
74
75         throw OpenSRF::EX::InvalidArg ( "No org_unit id passed!" )
76                 unless ($oid);
77
78         throw OpenSRF::EX::InvalidArg ( "No record id passed!" )
79                 unless (@recs);
80
81         $org_unit_lookup ||= $self->method_lookup('open-ils.storage.direct.actor.org_unit.retrieve');
82         my ($org_unit) = $org_unit_lookup->run($oid);
83
84         # XXX Use descendancy tree here!!!
85         my $short_name_hack = $org_unit->shortname;
86         $short_name_hack = '' if (!$org_unit->parent_ou);
87         $short_name_hack .= '%';
88         # XXX Use descendancy tree here!!!
89
90         my $rec_list = join(',',@recs);
91
92         my $cp_table = asset::copy->table;
93         my $cn_table = asset::call_number->table;
94
95         my $select =<<" SQL";
96                 SELECT  count(cp.*) as copies
97                   FROM  $cn_table cn
98                         JOIN $cp_table cp ON (cp.call_number = cn.id)
99                   WHERE cn.owning_lib LIKE ? AND
100                         cn.record IN ($rec_list)
101         SQL
102
103         my $sth = asset::copy->db_Main->prepare_cached($select);
104         $sth->execute($short_name_hack);
105
106         my $results = $sth->fetchall_hashref('record');
107
108         $client->respond($$results{$_}{copies} || 0) for (@recs);
109
110         return undef;
111 }
112 __PACKAGE__->register_method(
113         method          => 'record_copy_count',
114         api_name        => 'open-ils.storage.direct.biblio.record_copy_count',
115         api_level       => 1,
116         argc            => 1,
117 );
118 __PACKAGE__->register_method(
119         method          => 'record_copy_count',
120         api_name        => 'open-ils.storage.direct.biblio.record_copy_count.batch',
121         api_level       => 1,
122         argc            => 1,
123         stream          => 1,
124 );
125
126 =cut
127
128 1;