]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/asset.pm
refactored copy targeter to use new JS copy tester; adjusted object relationships...
[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 );
280
281
282 sub fleshed_copy {
283         my $self = shift;
284         my $client = shift;
285         my @ids = @_;
286
287         return undef unless (@ids);
288
289         @ids = ($ids[0]) unless ($self->api_name =~ /batch/o);
290
291         for my $id ( @ids ) {
292                 next unless $id;
293                 my $cp = asset::copy->retrieve($id);
294                 next unless $cp;
295
296                 my $cp_fm = $cp->to_fieldmapper;
297                 $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
298                 $cp_fm->location( $cp->location->to_fieldmapper );
299                 $cp_fm->status( $cp->status->to_fieldmapper );
300                 $cp_fm->stat_cat_entries( [ map { $_->to_fieldmapper } $cp->stat_cat_entries ] );
301
302                 $client->respond( $cp_fm );
303         }
304
305         return undef;
306 }
307 __PACKAGE__->register_method(
308         api_name        => 'open-ils.storage.fleshed.asset.copy.batch.retrieve',
309         method          => 'fleshed_copy',
310         argc            => 1,
311         stream          => 1,
312 );
313 __PACKAGE__->register_method(
314         api_name        => 'open-ils.storage.fleshed.asset.copy.retrieve',
315         method          => 'fleshed_copy',
316         argc            => 1,
317 );
318
319 sub fleshed_copy_by_barcode {
320         my $self = shift;
321         my $client = shift;
322         my $bc = ''.shift;
323
324         my ($cp) = asset::copy->search( { barcode => $bc } );
325
326         return undef unless ($cp);
327
328         my $cp_fm = $cp->to_fieldmapper;
329         $cp_fm->circ_lib( $cp->circ_lib->to_fieldmapper );
330         $cp_fm->location( $cp->location->to_fieldmapper );
331         $cp_fm->status( $cp->status->to_fieldmapper );
332
333         return $cp_fm;
334 }       
335 __PACKAGE__->register_method(
336         api_name        => 'open-ils.storage.fleshed.asset.copy.search.barcode',
337         method          => 'fleshed_copy_by_barcode',
338         argc            => 1,
339         stream          => 1,
340 );
341
342
343 #XXX Fix stored proc calls
344 sub fleshed_asset_stat_cat {
345         my $self = shift;
346         my $client = shift;
347         my @list = @_;
348
349         @list = ($list[0]) unless ($self->api_name =~ /batch/o);
350         for my $sc (@list) {
351                 my $cat = asset::stat_cat->retrieve($sc);
352                 
353                 next unless ($cat);
354
355                 my $sc_fm = $cat->to_fieldmapper;
356                 $sc_fm->entries( [ map { $_->to_fieldmapper } $cat->entries ] );
357                 $client->respond( $sc_fm );
358         }
359
360         return undef;
361 }
362 __PACKAGE__->register_method(
363         api_name        => 'open-ils.storage.fleshed.asset.stat_cat.retrieve',
364         api_level       => 1,
365         method          => 'fleshed_asset_stat_cat',
366 );
367
368 __PACKAGE__->register_method(
369         api_name        => 'open-ils.storage.fleshed.asset.stat_cat.retrieve.batch',
370         api_level       => 1,
371         stream          => 1,
372         method          => 'fleshed_asset_stat_cat',
373 );
374
375
376 #XXX Fix stored proc calls
377 sub ranged_asset_stat_cat {
378         my $self = shift;
379         my $client = shift;
380         my $ou = ''.shift();
381
382         return undef unless ($ou);
383         my $s_table = asset::stat_cat->table;
384
385         my $select = <<"        SQL";
386                 SELECT  s.*
387                   FROM  $s_table s
388                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
389                   ORDER BY name
390         SQL
391
392         $fleshed = 0;
393         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
394
395         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
396         $sth->execute($ou);
397
398         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
399                 my $sc_fm = $sc->to_fieldmapper;
400                 $sc_fm->entries(
401                         [ $self->method_lookup( 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat' )->run($ou,$sc->id) ]
402                 ) if ($fleshed);
403                 $client->respond( $sc_fm );
404         }
405
406         return undef;
407 }
408 __PACKAGE__->register_method(
409         api_name        => 'open-ils.storage.ranged.fleshed.asset.stat_cat.all',
410         api_level       => 1,
411         stream          => 1,
412         method          => 'ranged_asset_stat_cat',
413 );
414
415 __PACKAGE__->register_method(
416         api_name        => 'open-ils.storage.ranged.asset.stat_cat.all',
417         api_level       => 1,
418         stream          => 1,
419         method          => 'ranged_asset_stat_cat',
420 );
421
422
423 #XXX Fix stored proc calls
424 sub multiranged_asset_stat_cat {
425         my $self = shift;
426         my $client = shift;
427         my $ous = shift;
428
429         return undef unless (defined($ous) and @$ous);
430         my $s_table = asset::stat_cat->table;
431
432         my $select = <<"        SQL";
433                 SELECT  s.*
434                   FROM  $s_table s
435                   WHERE s.owner IN ( XXX )
436                   ORDER BY name
437         SQL
438
439         my $collector = ' INTERSECT ';
440         my $entry_method = 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat';
441         if ($self->api_name =~ /union/o) {
442                 $collector = ' UNION ';
443                 $entry_method = 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat';
444         }
445
446         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
447         $select =~ s/XXX/$binds/so;
448         
449         $fleshed = 0;
450         $fleshed = 1 if ($self->api_name =~ /fleshed/o);
451
452         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
453         $sth->execute(map { "$_" } grep {defined} @$ous);
454
455         for my $sc ( map { asset::stat_cat->construct($_) } $sth->fetchall_hash ) {
456                 my $sc_fm = $sc->to_fieldmapper;
457                 $sc_fm->entries(
458                         [ $self->method_lookup( $entry_method )->run($ous, $sc->id) ]
459                 ) if ($fleshed);
460                 $client->respond( $sc_fm );
461         }
462
463         return undef;
464 }
465 __PACKAGE__->register_method(
466         api_name        => 'open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all',
467         api_level       => 1,
468         stream          => 1,
469         method          => 'multiranged_asset_stat_cat',
470 );
471 __PACKAGE__->register_method(
472         api_name        => 'open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all',
473         api_level       => 1,
474         stream          => 1,
475         method          => 'multiranged_asset_stat_cat',
476 );
477
478 #XXX Fix stored proc calls
479 sub ranged_asset_stat_cat_entry {
480         my $self = shift;
481         my $client = shift;
482         my $ou = ''.shift();
483         my $sc = ''.shift();
484
485         return undef unless ($ou);
486         my $s_table = asset::stat_cat_entry->table;
487
488         my $select = <<"        SQL";
489                 SELECT  s.*
490                   FROM  $s_table s
491                         JOIN actor.org_unit_full_path(?) p ON (p.id = s.owner)
492                   WHERE stat_cat = ?
493                   ORDER BY name
494         SQL
495
496         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
497         $sth->execute($ou,$sc);
498
499         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
500                 $client->respond( $sce->to_fieldmapper );
501         }
502
503         return undef;
504 }
505 __PACKAGE__->register_method(
506         api_name        => 'open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat',
507         api_level       => 1,
508         stream          => 1,
509         method          => 'ranged_asset_stat_cat_entry',
510 );
511
512 #XXX Fix stored proc calls
513 sub multiranged_asset_stat_cat_entry {
514         my $self = shift;
515         my $client = shift;
516         my $ous = shift;
517         my $sc = ''.shift();
518
519         return undef unless (defined($ous) and @$ous);
520         my $s_table = asset::stat_cat_entry->table;
521
522         my $collector = ' INTERSECT ';
523         $collector = ' UNION ' if ($self->api_name =~ /union/o);
524
525         my $select = <<"        SQL";
526                 SELECT  s.*
527                   FROM  $s_table s
528                   WHERE s.owner IN ( XXX ) and s.stat_cat = ?
529                   ORDER BY value
530         SQL
531
532         my $binds = join($collector, map { 'SELECT id FROM actor.org_unit_full_path(?)' } grep {defined} @$ous);
533         $select =~ s/XXX/$binds/so;
534         
535         my $sth = asset::stat_cat->db_Main->prepare_cached($select);
536         $sth->execute(map {"$_"} @$ous,$sc);
537
538         for my $sce ( map { asset::stat_cat_entry->construct($_) } $sth->fetchall_hash ) {
539                 $client->respond( $sce->to_fieldmapper );
540         }
541
542         return undef;
543 }
544 __PACKAGE__->register_method(
545         api_name        => 'open-ils.storage.multiranged.intersect.asset.stat_cat_entry.search.stat_cat',
546         api_level       => 1,
547         stream          => 1,
548         method          => 'multiranged_asset_stat_cat_entry',
549 );
550 __PACKAGE__->register_method(
551         api_name        => 'open-ils.storage.multiranged.union.asset.stat_cat_entry.search.stat_cat',
552         api_level       => 1,
553         stream          => 1,
554         method          => 'multiranged_asset_stat_cat_entry',
555 );
556
557
558 sub cn_ranged_tree {
559         my $self = shift;
560         my $client = shift;
561         my $cn = shift;
562         my $ou = shift;
563         my $depth = shift || 0;
564
565         my $ou_list =
566                 actor::org_unit
567                         ->db_Main
568                         ->selectcol_arrayref(
569                                 'SELECT id FROM actor.org_unit_descendants(?,?)',
570                                 {},
571                                 $ou,
572                                 $depth
573                         );
574
575         return undef unless ($ou_list and @$ou_list);
576
577         $cn = asset::call_number->retrieve( $cn );
578         return undef unless ($cn);
579
580         my $call_number = $cn->to_fieldmapper;
581         $call_number->copies([]);
582
583         $call_number->record( $cn->record->to_fieldmapper );
584         $call_number->record->fixed_fields( $cn->record->record_descriptor->next->to_fieldmapper );
585
586         for my $cp ( $cn->copies(circ_lib => $ou_list) ) {
587                 my $copy = $cp->to_fieldmapper;
588                 $copy->status( $cp->status->to_fieldmapper );
589                 $copy->location( $cp->status->to_fieldmapper );
590
591                 push @{ $call_number->copies }, $copy;
592         }
593
594         return $call_number;
595 }
596 __PACKAGE__->register_method(
597         api_name        => 'open-ils.storage.asset.call_number.ranged_tree',
598         method          => 'cn_ranged_tree',
599         argc            => 1,
600         api_level       => 1,
601 );
602
603
604 1;