]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm
call number targeter method -- usable for lib or system right now
[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 # XXX
10 # see /home/miker/cn_browse-test.sql for page up and down sql ...
11 # XXX
12
13 sub cn_browse_target {
14         my $self = shift;
15         my $client = shift;
16
17         my %args = @_;
18
19         my $cn = $args{label};
20         my $org = $args{org_unit};
21         my $depth = $args{depth};
22         my $size = $args{page_size} || 10;
23         $size /= 2;
24         $size = int($size);
25
26         my $table = asset::call_number->table;
27
28         my $descendants = "actor.org_unit_descendants($org)";
29         if (defined $depth) {
30                 $descendants = "actor.org_unit_descendants($org,$depth)";
31         }
32
33         my $top_sql = <<"       SQL";
34                 select * from (
35                         select
36                                 cn.label,
37                                 cn.owning_lib,
38                                 cn.record,
39                                 cn.id
40                         from
41                                 $table cn
42                                 join $descendants d
43                                         on (d.id = cn.owning_lib)
44                         where
45                                 upper(label) < ?
46                         order by 1 desc, 4, 2, 3
47                         limit $size
48                 ) as foo
49                 order by 1,4;
50         SQL
51
52         my $bottom_sql = <<"    SQL";
53                 select
54                         cn.label,
55                         cn.owning_lib,
56                         cn.record,
57                         cn.id
58                 from
59                         $table cn
60                         join $descendants d
61                                 on (d.id = cn.owning_lib)
62                 where
63                         upper(label) >= ?
64                 order by 1,4
65                 limit $size;
66         SQL
67
68         my $sth = asset::call_number->db_Main->prepare($top_sql);
69         $sth->execute($cn);
70         while ( my $row = $sth->fetchrow_hashref ) {
71                 $client->respond($row);
72         }
73         $sth->finish;
74
75         $sth = asset::call_number->db_Main->prepare($bottom_sql);
76         $sth->execute($cn);
77         while ( my $row = $sth->fetchrow_hashref ) {
78                 $client->respond($row);
79         }
80         $sth->finish;
81
82         return undef;
83 }
84 __PACKAGE__->register_method(
85         method          => 'cn_browse_target',
86         api_name        => 'open-ils.storage.asset.call_number.browse.target',
87         argc            => 0,
88         stream          => 1,
89 );
90
91
92 sub copy_proximity {
93         my $self = shift;
94         my $client = shift;
95
96         my $cp = shift;
97         my $org = shift;
98
99         return unless ($cp && $org);
100
101         $cp = $cp->id if (ref $cp);
102         $cp = asset::copy->retrieve($cp);
103         return 999 unless $copy;
104         my $ol = $cp->call_number->owning_lib;
105
106         return asset::copy->db_Main->selectcol_arrayref('SELECT actor.org_unit_proximity(?,?)',{},"$ol","$org")->[0];
107 }
108 __PACKAGE__->register_method(
109         method          => 'copy_proximity',
110         api_name        => 'open-ils.storage.asset.copy.proximity',
111         argc            => 4,
112         stream          => 1,
113 );
114
115 sub asset_copy_location_all {
116         my $self = shift;
117         my $client = shift;
118
119         for my $rec ( asset::copy_location->retrieve_all ) {
120                 $client->respond( $rec->to_fieldmapper );
121         }
122
123         return undef;
124 }
125 __PACKAGE__->register_method(
126         method          => 'asset_copy_location_all',
127         api_name        => 'open-ils.storage.direct.asset.copy_location.retrieve.all',
128         argc            => 0,
129         stream          => 1,
130 );
131
132 sub fleshed_copy {
133         my $self = shift;
134         my $client = shift;
135         my @ids = @_;
136
137         return undef unless (@ids);
138
139         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
140
141         for my $id ( @ids ) {
142                 next unless $id;
143                 my $cp = asset::copy->retrieve($id);
144
145                 my $cp_fm = $cp->to_fieldmapper;
146                 $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
147                 $cp_fm->location( $cp->location->to_fieldmapper );
148                 $cp_fm->status( $cp->status->to_fieldmapper );
149                 $cp_fm->stat_cat_entries( [ map { $_->to_fieldmapper } $cp->stat_cat_entries ] );
150
151                 $client->respond( $cp_fm );
152         }
153
154         return undef;
155 }
156 __PACKAGE__->register_method(
157         api_name        => 'open-ils.storage.fleshed.asset.copy.batch.retrieve',
158         method          => 'fleshed_copy',
159         argc            => 1,
160         stream          => 1,
161 );
162 __PACKAGE__->register_method(
163         api_name        => 'open-ils.storage.fleshed.asset.copy.retrieve',
164         method          => 'fleshed_copy',
165         argc            => 1,
166 );
167
168 sub fleshed_copy_by_barcode {
169         my $self = shift;
170         my $client = shift;
171         my $bc = ''.shift;
172
173         my ($cp) = asset::copy->search( { barcode => $bc } );
174
175         return undef unless ($cp);
176
177         my $cp_fm = $cp->to_fieldmapper;
178         $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
179         $cp_fm->location( $cp->location->to_fieldmapper );
180         $cp_fm->status( $cp->status->to_fieldmapper );
181
182         return $cp_fm;
183 }       
184 __PACKAGE__->register_method(
185         api_name        => 'open-ils.storage.fleshed.asset.copy.search.barcode',
186         method          => 'fleshed_copy_by_barcode',
187         argc            => 1,
188         stream          => 1,
189 );
190
191 #XXX Fix stored proc calls
192 sub ranged_asset_stat_cat {
193         my $self = shift;
194         my $client = shift;
195         my $ou = ''.shift();
196
197         return undef unless ($ou);
198         my $s_table = asset::stat_cat->table;
199
200         my $select = <<"        SQL";
201                 SELECT  s.*
202                   FROM  $s_table s
203                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
204                   ORDER BY name
205         SQL
206
207         $fleshed = 0;
208         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
209
210         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
211         $sth->execute($ou);
212
213         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
214                 my $sc_fm = $sc->to_fieldmapper;
215                 $sc_fm->entries(
216                         [ $self->method_lookup( 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
217                 ) if ($fleshed);
218                 $client->respond( $sc_fm );
219         }
220
221         return undef;
222 }
223 __PACKAGE__->register_method(
224         api_name        => 'open-ils.storage.ranged.fleshed.asset.stat_cat.all',
225         api_level       => 1,
226         stream          => 1,
227         method          => 'ranged_asset_stat_cat',
228 );
229
230 __PACKAGE__->register_method(
231         api_name        => 'open-ils.storage.ranged.asset.stat_cat.all',
232         api_level       => 1,
233         stream          => 1,
234         method          => 'ranged_asset_stat_cat',
235 );
236
237
238 #XXX Fix stored proc calls
239 sub multiranged_asset_stat_cat {
240         my $self = shift;
241         my $client = shift;
242         my $ous = shift;
243
244         return undef unless (defined($ous) and @$ous);
245         my $s_table = asset::stat_cat->table;
246
247         my $select = <<"        SQL";
248                 SELECT  s.*
249                   FROM  $s_table s
250                   WHERE s.owner IN ( XXX )
251                   ORDER BY name
252         SQL
253
254         my $collector = ' INTERSECT ';
255         my $entry_method = 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat';
256         if ($self->api_name =~ /union/o) {
257                 $collector = ' UNION ';
258                 $entry_method = 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat';
259         }
260
261         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
262         $select =~ s/XXX/$binds/so;
263         
264         $fleshed = 0;
265         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
266
267         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
268         $sth->execute(map { "$_" } grep {defined} @$ous);
269
270         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
271                 my $sc_fm = $sc->to_fieldmapper;
272                 $sc_fm->entries(
273                         [ $self->method_lookup( $entry_method )->run($ous, $sc->id) ]
274                 ) if ($fleshed);
275                 $client->respond( $sc_fm );
276         }
277
278         return undef;
279 }
280 __PACKAGE__->register_method(
281         api_name        => 'open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all',
282         api_level       => 1,
283         stream          => 1,
284         method          => 'multiranged_asset_stat_cat',
285 );
286 __PACKAGE__->register_method(
287         api_name        => 'open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all',
288         api_level       => 1,
289         stream          => 1,
290         method          => 'multiranged_asset_stat_cat',
291 );
292
293 #XXX Fix stored proc calls
294 sub ranged_asset_stat_cat_entry {
295         my $self = shift;
296         my $client = shift;
297         my $ou = ''.shift();
298         my $sc = ''.shift();
299
300         return undef unless ($ou);
301         my $s_table = asset::stat_cat_entry->table;
302
303         my $select = <<"        SQL";
304                 SELECT  s.*
305                   FROM  $s_table s
306                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
307                   WHERE stat_cat = ?
308                   ORDER BY name
309         SQL
310
311         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
312         $sth->execute($ou,$sc);
313
314         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
315                 $client->respond( $sce->to_fieldmapper );
316         }
317
318         return undef;
319 }
320 __PACKAGE__->register_method(
321         api_name        => 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat',
322         api_level       => 1,
323         stream          => 1,
324         method          => 'ranged_asset_stat_cat_entry',
325 );
326
327 #XXX Fix stored proc calls
328 sub multiranged_asset_stat_cat_entry {
329         my $self = shift;
330         my $client = shift;
331         my $ous = shift;
332         my $sc = ''.shift();
333
334         return undef unless (defined($ous) and @$ous);
335         my $s_table = asset::stat_cat_entry->table;
336
337         my $collector = ' INTERSECT ';
338         $collector = ' UNION ' if ($self->api_name =~ /union/o);
339
340         my $select = <<"        SQL";
341                 SELECT  s.*
342                   FROM  $s_table s
343                   WHERE s.owner IN ( XXX ) and s.stat_cat = ?
344                   ORDER BY value
345         SQL
346
347         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
348         $select =~ s/XXX/$binds/so;
349         
350         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
351         $sth->execute(map {"$_"} @$ous,$sc);
352
353         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
354                 $client->respond( $sce->to_fieldmapper );
355         }
356
357         return undef;
358 }
359 __PACKAGE__->register_method(
360         api_name        => 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat',
361         api_level       => 1,
362         stream          => 1,
363         method          => 'multiranged_asset_stat_cat_entry',
364 );
365 __PACKAGE__->register_method(
366         api_name        => 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat',
367         api_level       => 1,
368         stream          => 1,
369         method          => 'multiranged_asset_stat_cat_entry',
370 );
371
372
373
374 1;