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