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