]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm
e8ee242ff082df50e684b9de34b6d51b08233451
[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_pagedown {
14         my $self = shift;
15         my $client = shift;
16
17         my %args = @_;
18
19         my $cn = uc($args{label});
20         my $org = $args{org_unit};
21         my $depth = $args{depth};
22         my $boundry_id = $args{boundry_id};
23         my $size = $args{page_size} || 20;
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 $sql = <<"   SQL";
34                 select
35                         cn.label,
36                         cn.owning_lib,
37                         cn.record,
38                         cn.id
39                 from
40                         $table cn
41                         join $descendants d
42                                 on (d.id = cn.owning_lib)
43                 where
44                         upper(label) > ?
45                         or ( cn.id > ? and upper(label) = ? )
46                 order by upper(label), 4, 2
47                 limit $size;
48         SQL
49
50         my $sth = asset::call_number->db_Main->prepare($sql);
51         $sth->execute($cn, $boundry_id, $cn);
52         while ( my @row = $sth->fetchrow_array ) {
53                 $client->respond([@row]);
54         }
55         $sth->finish;
56
57         return undef;
58 }
59 __PACKAGE__->register_method(
60         method          => 'cn_browse_pagedown',
61         api_name        => 'open-ils.storage.asset.call_number.browse.page_down',
62         argc            => 4,
63         stream          => 1,
64 );
65
66 sub cn_browse_pageup {
67         my $self = shift;
68         my $client = shift;
69
70         my %args = @_;
71
72         my $cn = uc($args{label});
73         my $org = $args{org_unit};
74         my $depth = $args{depth};
75         my $boundry_id = $args{boundry_id};
76         my $size = $args{page_size} || 20;
77         $size = int($size);
78
79         my $table = asset::call_number->table;
80
81         my $descendants = "actor.org_unit_descendants($org)";
82         if (defined $depth) {
83                 $descendants = "actor.org_unit_descendants($org,$depth)";
84         }
85
86         my $sql = <<"   SQL";
87                 select * from (
88                         select
89                                 cn.label,
90                                 cn.owning_lib,
91                                 cn.record,
92                                 cn.id
93                         from
94                                 $table cn
95                                 join $descendants d
96                                         on (d.id = cn.owning_lib)
97                         where
98                                 upper(label) < ?
99                                 or ( cn.id < ? and upper(label) = ? )
100                         order by upper(label) desc, 4 desc, 2 desc
101                         limit $size
102                 ) as bar
103                 order by 1,4,2;
104         SQL
105
106         my $sth = asset::call_number->db_Main->prepare($sql);
107         $sth->execute($cn, $boundry_id, $cn);
108         while ( my @row = $sth->fetchrow_array ) {
109                 $client->respond([@row]);
110         }
111         $sth->finish;
112
113         return undef;
114 }
115 __PACKAGE__->register_method(
116         method          => 'cn_browse_pageup',
117         api_name        => 'open-ils.storage.asset.call_number.browse.page_up',
118         argc            => 4,
119         stream          => 1,
120 );
121
122 sub cn_browse_target {
123         my $self = shift;
124         my $client = shift;
125
126         my %args = @_;
127
128         my $cn = uc($args{label});
129         my $org = $args{org_unit};
130         my $depth = $args{depth};
131         my $size = $args{page_size} || 20;
132         my $topsize = $size / 2;
133         $topsize = int($topsize);
134         $bottomsize = $size - $topsize;
135
136         my $table = asset::call_number->table;
137
138         my $descendants = "actor.org_unit_descendants($org)";
139         if (defined $depth) {
140                 $descendants = "actor.org_unit_descendants($org,$depth)";
141         }
142
143         my $top_sql = <<"       SQL";
144                 select * from (
145                         select
146                                 cn.label,
147                                 cn.owning_lib,
148                                 cn.record,
149                                 cn.id
150                         from
151                                 $table cn
152                                 join $descendants d
153                                         on (d.id = cn.owning_lib)
154                         where
155                                 upper(label) < ?
156                         order by upper(label) desc, 4 desc, 2 desc
157                         limit $topsize
158                 ) as bar
159                 order by 1,4,2;
160         SQL
161
162         my $bottom_sql = <<"    SQL";
163                 select
164                         cn.label,
165                         cn.owning_lib,
166                         cn.record,
167                         cn.id
168                 from
169                         $table cn
170                         join $descendants d
171                                 on (d.id = cn.owning_lib)
172                 where
173                         upper(label) >= ?
174                 order by upper(label),4,2
175                 limit $bottomsize;
176         SQL
177
178         my $sth = asset::call_number->db_Main->prepare($top_sql);
179         $sth->execute($cn);
180         while ( my @row = $sth->fetchrow_array ) {
181                 $client->respond([@row]);
182         }
183         $sth->finish;
184
185         $sth = asset::call_number->db_Main->prepare($bottom_sql);
186         $sth->execute($cn);
187         while ( my @row = $sth->fetchrow_array ) {
188                 $client->respond([@row]);
189         }
190         $sth->finish;
191
192         return undef;
193 }
194 __PACKAGE__->register_method(
195         method          => 'cn_browse_target',
196         api_name        => 'open-ils.storage.asset.call_number.browse.target',
197         argc            => 4,
198         stream          => 1,
199 );
200
201
202 sub copy_proximity {
203         my $self = shift;
204         my $client = shift;
205
206         my $cp = shift;
207         my $org = shift;
208
209         return unless ($cp && $org);
210
211         $cp = $cp->id if (ref $cp);
212         $cp = asset::copy->retrieve($cp);
213         return 999 unless $copy;
214         my $ol = $cp->call_number->owning_lib;
215
216         return asset::copy->db_Main->selectcol_arrayref('SELECT actor.org_unit_proximity(?,?)',{},"$ol","$org")->[0];
217 }
218 __PACKAGE__->register_method(
219         method          => 'copy_proximity',
220         api_name        => 'open-ils.storage.asset.copy.proximity',
221         argc            => 2,
222         stream          => 1,
223 );
224
225 sub asset_copy_location_all {
226         my $self = shift;
227         my $client = shift;
228
229         for my $rec ( asset::copy_location->retrieve_all ) {
230                 $client->respond( $rec->to_fieldmapper );
231         }
232
233         return undef;
234 }
235 __PACKAGE__->register_method(
236         method          => 'asset_copy_location_all',
237         api_name        => 'open-ils.storage.direct.asset.copy_location.retrieve.all',
238         argc            => 0,
239         stream          => 1,
240 );
241
242 # XXX arg, with the descendancy SPs...
243 sub ranged_asset_copy_location {
244         my $self = shift;
245         my $client = shift;
246         my @binds = @_;
247         
248         my $ctable = asset::copy_location->table;
249         
250         my $descendants = defined($binds[1]) ?
251                 "actor.org_unit_full_path(?, ?)" :
252                 "actor.org_unit_full_path(?)" ;
253
254         
255         my $sql = <<"   SQL";
256                 SELECT  DISTINCT c.*
257                   FROM  $ctable c
258                         JOIN $descendants d
259                                 ON (d.id = c.owning_lib)
260         SQL
261         
262         my $sth = asset::copy_location->db_Main->prepare($sql);
263         $sth->execute(@binds);
264         
265         while ( my $rec = $sth->fetchrow_hashref ) {
266         
267                 my $cnct = new Fieldmapper::asset::copy_location;
268                 map {$cnct->$_($$rec{$_})} keys %$rec;
269                 $client->respond( $cnct );
270         }
271
272         return undef;
273 }
274 __PACKAGE__->register_method(
275         method          => 'ranged_asset_copy_location',
276         api_name        => 'open-ils.storage.ranged.asset.copy_location.retrieve',
277         argc            => 1,
278         stream          => 1,
279         notes           => <<"  NOTES",
280                 Returns 
281         NOTES
282 );
283
284
285 sub fleshed_copy {
286         my $self = shift;
287         my $client = shift;
288         my @ids = @_;
289
290         return undef unless (@ids);
291
292         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
293
294         for my $id ( @ids ) {
295                 next unless $id;
296                 my $cp = asset::copy->retrieve($id);
297                 next unless $cp;
298
299                 my $cp_fm = $cp->to_fieldmapper;
300                 $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
301                 $cp_fm->location( $cp->location->to_fieldmapper );
302                 $cp_fm->status( $cp->status->to_fieldmapper );
303                 $cp_fm->stat_cat_entries( [ map { $_->to_fieldmapper } $cp->stat_cat_entries ] );
304
305                 $client->respond( $cp_fm );
306         }
307
308         return undef;
309 }
310 __PACKAGE__->register_method(
311         api_name        => 'open-ils.storage.fleshed.asset.copy.batch.retrieve',
312         method          => 'fleshed_copy',
313         argc            => 1,
314         stream          => 1,
315 );
316 __PACKAGE__->register_method(
317         api_name        => 'open-ils.storage.fleshed.asset.copy.retrieve',
318         method          => 'fleshed_copy',
319         argc            => 1,
320 );
321
322 sub fleshed_copy_by_barcode {
323         my $self = shift;
324         my $client = shift;
325         my $bc = ''.shift;
326
327         my ($cp) = asset::copy->search( { barcode => $bc } );
328
329         return undef unless ($cp);
330
331         my $cp_fm = $cp->to_fieldmapper;
332         $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
333         $cp_fm->location( $cp->location->to_fieldmapper );
334         $cp_fm->status( $cp->status->to_fieldmapper );
335
336         return $cp_fm;
337 }       
338 __PACKAGE__->register_method(
339         api_name        => 'open-ils.storage.fleshed.asset.copy.search.barcode',
340         method          => 'fleshed_copy_by_barcode',
341         argc            => 1,
342         stream          => 1,
343 );
344
345 #XXX Fix stored proc calls
346 sub ranged_asset_stat_cat {
347         my $self = shift;
348         my $client = shift;
349         my $ou = ''.shift();
350
351         return undef unless ($ou);
352         my $s_table = asset::stat_cat->table;
353
354         my $select = <<"        SQL";
355                 SELECT  s.*
356                   FROM  $s_table s
357                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
358                   ORDER BY name
359         SQL
360
361         $fleshed = 0;
362         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
363
364         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
365         $sth->execute($ou);
366
367         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
368                 my $sc_fm = $sc->to_fieldmapper;
369                 $sc_fm->entries(
370                         [ $self->method_lookup( 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
371                 ) if ($fleshed);
372                 $client->respond( $sc_fm );
373         }
374
375         return undef;
376 }
377 __PACKAGE__->register_method(
378         api_name        => 'open-ils.storage.ranged.fleshed.asset.stat_cat.all',
379         api_level       => 1,
380         stream          => 1,
381         method          => 'ranged_asset_stat_cat',
382 );
383
384 __PACKAGE__->register_method(
385         api_name        => 'open-ils.storage.ranged.asset.stat_cat.all',
386         api_level       => 1,
387         stream          => 1,
388         method          => 'ranged_asset_stat_cat',
389 );
390
391
392 #XXX Fix stored proc calls
393 sub multiranged_asset_stat_cat {
394         my $self = shift;
395         my $client = shift;
396         my $ous = shift;
397
398         return undef unless (defined($ous) and @$ous);
399         my $s_table = asset::stat_cat->table;
400
401         my $select = <<"        SQL";
402                 SELECT  s.*
403                   FROM  $s_table s
404                   WHERE s.owner IN ( XXX )
405                   ORDER BY name
406         SQL
407
408         my $collector = ' INTERSECT ';
409         my $entry_method = 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat';
410         if ($self->api_name =~ /union/o) {
411                 $collector = ' UNION ';
412                 $entry_method = 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat';
413         }
414
415         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
416         $select =~ s/XXX/$binds/so;
417         
418         $fleshed = 0;
419         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
420
421         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
422         $sth->execute(map { "$_" } grep {defined} @$ous);
423
424         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
425                 my $sc_fm = $sc->to_fieldmapper;
426                 $sc_fm->entries(
427                         [ $self->method_lookup( $entry_method )->run($ous, $sc->id) ]
428                 ) if ($fleshed);
429                 $client->respond( $sc_fm );
430         }
431
432         return undef;
433 }
434 __PACKAGE__->register_method(
435         api_name        => 'open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all',
436         api_level       => 1,
437         stream          => 1,
438         method          => 'multiranged_asset_stat_cat',
439 );
440 __PACKAGE__->register_method(
441         api_name        => 'open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all',
442         api_level       => 1,
443         stream          => 1,
444         method          => 'multiranged_asset_stat_cat',
445 );
446
447 #XXX Fix stored proc calls
448 sub ranged_asset_stat_cat_entry {
449         my $self = shift;
450         my $client = shift;
451         my $ou = ''.shift();
452         my $sc = ''.shift();
453
454         return undef unless ($ou);
455         my $s_table = asset::stat_cat_entry->table;
456
457         my $select = <<"        SQL";
458                 SELECT  s.*
459                   FROM  $s_table s
460                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
461                   WHERE stat_cat = ?
462                   ORDER BY name
463         SQL
464
465         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
466         $sth->execute($ou,$sc);
467
468         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
469                 $client->respond( $sce->to_fieldmapper );
470         }
471
472         return undef;
473 }
474 __PACKAGE__->register_method(
475         api_name        => 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat',
476         api_level       => 1,
477         stream          => 1,
478         method          => 'ranged_asset_stat_cat_entry',
479 );
480
481 #XXX Fix stored proc calls
482 sub multiranged_asset_stat_cat_entry {
483         my $self = shift;
484         my $client = shift;
485         my $ous = shift;
486         my $sc = ''.shift();
487
488         return undef unless (defined($ous) and @$ous);
489         my $s_table = asset::stat_cat_entry->table;
490
491         my $collector = ' INTERSECT ';
492         $collector = ' UNION ' if ($self->api_name =~ /union/o);
493
494         my $select = <<"        SQL";
495                 SELECT  s.*
496                   FROM  $s_table s
497                   WHERE s.owner IN ( XXX ) and s.stat_cat = ?
498                   ORDER BY value
499         SQL
500
501         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
502         $select =~ s/XXX/$binds/so;
503         
504         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
505         $sth->execute(map {"$_"} @$ous,$sc);
506
507         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
508                 $client->respond( $sce->to_fieldmapper );
509         }
510
511         return undef;
512 }
513 __PACKAGE__->register_method(
514         api_name        => 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat',
515         api_level       => 1,
516         stream          => 1,
517         method          => 'multiranged_asset_stat_cat_entry',
518 );
519 __PACKAGE__->register_method(
520         api_name        => 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat',
521         api_level       => 1,
522         stream          => 1,
523         method          => 'multiranged_asset_stat_cat_entry',
524 );
525
526
527
528 1;