]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm
CDBI hates me... collecting stat_cat_entries by hand
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / Publisher / asset.pm
1 package OpenILS::Application::Storage::Publisher::asset;
2 use base qw/OpenILS::Application::Storage/;
3 #use OpenILS::Application::Storage::CDBI::asset;
4 #use OpenSRF::Utils::Logger qw/:level/;
5 #use OpenILS::Utils::Fieldmapper;
6 #
7 #my $log = 'OpenSRF::Utils::Logger';
8
9 sub asset_copy_location_all {
10         my $self = shift;
11         my $client = shift;
12
13         for my $rec ( asset::copy_location->retrieve_all ) {
14                 $client->respond( $rec->to_fieldmapper );
15         }
16
17         return undef;
18 }
19 __PACKAGE__->register_method(
20         method          => 'asset_copy_location_all',
21         api_name        => 'open-ils.storage.direct.asset.copy_location.retrieve.all',
22         argc            => 0,
23         stream          => 1,
24 );
25
26 sub fleshed_copy {
27         my $self = shift;
28         my $client = shift;
29         my @ids = @_;
30
31         return undef unless (@ids);
32
33         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
34
35         for my $id ( @ids ) {
36                 next unless $id;
37                 my $cp = asset::copy->retrieve($id);
38
39                 my $cp_fm = $cp->to_fieldmapper;
40                 $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
41                 $cp_fm->location( $cp->location->to_fieldmapper );
42                 $cp_fm->status( $cp->status->to_fieldmapper );
43                 my @scs;
44                 for my $map ( $cp->stat_cat_entry_copy_maps ) {
45                         push @scs, $map->to_fieldmapper;
46                 }
47                 $cp_fm->stat_cat_entries( \@scs );
48
49                 $client->respond( $cp_fm );
50         }
51
52         return undef;
53 }
54 __PACKAGE__->register_method(
55         api_name        => 'open-ils.storage.fleshed.asset.copy.batch.retrieve',
56         method          => 'fleshed_copy',
57         argc            => 1,
58         stream          => 1,
59 );
60 __PACKAGE__->register_method(
61         api_name        => 'open-ils.storage.fleshed.asset.copy.retrieve',
62         method          => 'fleshed_copy',
63         argc            => 1,
64 );
65
66 sub fleshed_copy_by_barcode {
67         my $self = shift;
68         my $client = shift;
69         my $bc = ''.shift;
70
71         my ($cp) = asset::copy->search( { barcode => $bc } );
72
73         my $cp_fm = $cp->to_fieldmapper;
74         $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
75         $cp_fm->location( $cp->location->to_fieldmapper );
76         $cp_fm->status( $cp->status->to_fieldmapper );
77
78         return [ $cp_fm ];
79 }       
80 __PACKAGE__->register_method(
81         api_name        => 'open-ils.storage.fleshed.asset.copy.search.barcode',
82         method          => 'fleshed_copy_by_barcode',
83         argc            => 1,
84         stream          => 1,
85 );
86
87 #XXX Fix stored proc calls
88 sub ranged_asset_stat_cat {
89         my $self = shift;
90         my $client = shift;
91         my $ou = ''.shift();
92
93         return undef unless ($ou);
94         my $s_table = asset::stat_cat->table;
95
96         my $select = <<"        SQL";
97                 SELECT  s.*
98                   FROM  $s_table s
99                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
100                   ORDER BY name
101         SQL
102
103         $fleshed = 0;
104         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
105
106         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
107         $sth->execute($ou);
108
109         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
110                 my $sc_fm = $sc->to_fieldmapper;
111                 $sc_fm->entries(
112                         [ $self->method_lookup( 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
113                 ) if ($fleshed);
114                 $client->respond( $sc_fm );
115         }
116
117         return undef;
118 }
119 __PACKAGE__->register_method(
120         api_name        => 'open-ils.storage.ranged.fleshed.asset.stat_cat.all',
121         api_level       => 1,
122         stream          => 1,
123         method          => 'ranged_asset_stat_cat',
124 );
125
126 __PACKAGE__->register_method(
127         api_name        => 'open-ils.storage.ranged.asset.stat_cat.all',
128         api_level       => 1,
129         stream          => 1,
130         method          => 'ranged_asset_stat_cat',
131 );
132
133
134 #XXX Fix stored proc calls
135 sub multiranged_asset_stat_cat {
136         my $self = shift;
137         my $client = shift;
138         my $ous = shift;
139
140         return undef unless (defined($ous) and @$ous);
141         my $s_table = asset::stat_cat->table;
142
143         my $select = <<"        SQL";
144                 SELECT  s.*
145                   FROM  $s_table s
146                   WHERE s.owner IN ( XXX )
147                   ORDER BY name
148         SQL
149
150         my $binds = join(' INTERSECT ', map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
151         $select =~ s/XXX/$binds/so;
152         
153         $fleshed = 0;
154         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
155
156         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
157         $sth->execute(map { "$_" } grep {defined} @$ous);
158
159         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
160                 my $sc_fm = $sc->to_fieldmapper;
161                 $sc_fm->entries(
162                         [ $self->method_lookup( 'open-ils.storage.multiranged.asset.stat_cat_entry.search.stat_cat' )->run($ous, $sc->id) ]
163                 ) if ($fleshed);
164                 $client->respond( $sc_fm );
165         }
166
167         return undef;
168 }
169 __PACKAGE__->register_method(
170         api_name        => 'open-ils.storage.multiranged.fleshed.asset.stat_cat.all',
171         api_level       => 1,
172         stream          => 1,
173         method          => 'multiranged_asset_stat_cat',
174 );
175
176 #XXX Fix stored proc calls
177 sub ranged_asset_stat_cat_entry {
178         my $self = shift;
179         my $client = shift;
180         my $ou = ''.shift();
181         my $sc = ''.shift();
182
183         return undef unless ($ou);
184         my $s_table = asset::stat_cat_entry->table;
185
186         my $select = <<"        SQL";
187                 SELECT  s.*
188                   FROM  $s_table s
189                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
190                   WHERE stat_cat = ?
191                   ORDER BY name
192         SQL
193
194         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
195         $sth->execute($ou,$sc);
196
197         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
198                 $client->respond( $sce->to_fieldmapper );
199         }
200
201         return undef;
202 }
203 __PACKAGE__->register_method(
204         api_name        => 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat',
205         api_level       => 1,
206         stream          => 1,
207         method          => 'ranged_asset_stat_cat_entry',
208 );
209
210 #XXX Fix stored proc calls
211 sub multiranged_asset_stat_cat_entry {
212         my $self = shift;
213         my $client = shift;
214         my $ous = shift;
215         my $sc = ''.shift();
216
217         return undef unless (defined($ous) and @$ous);
218         my $s_table = asset::stat_cat_entry->table;
219
220         my $select = <<"        SQL";
221                 SELECT  s.*
222                   FROM  $s_table s
223                   WHERE s.owner IN ( XXX ) and s.stat_cat = ?
224                   ORDER BY value
225         SQL
226
227         my $binds = join(' INTERSECT ', map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
228         $select =~ s/XXX/$binds/so;
229         
230         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
231         $sth->execute(map {"$_"} @$ous,$sc);
232
233         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
234                 $client->respond( $sce->to_fieldmapper );
235         }
236
237         return undef;
238 }
239 __PACKAGE__->register_method(
240         api_name        => 'open-ils.storage.multiranged.asset.stat_cat_entry.search.stat_cat',
241         api_level       => 1,
242         stream          => 1,
243         method          => 'multiranged_asset_stat_cat_entry',
244 );
245
246
247
248 1;