]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/StatCat.pm
sanity checks and server side sorting when fetching all stat cats
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / StatCat.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2005  Georgia Public Library Service 
3 # Bill Erickson <highfalutin@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15
16 package OpenILS::Application::Circ::StatCat;
17 use base qw/OpenSRF::Application/;
18 use strict; use warnings;
19
20 use OpenSRF::EX qw/:try/;
21 use OpenILS::Application::AppUtils;
22 my $apputils = "OpenILS::Application::AppUtils";
23
24
25
26 __PACKAGE__->register_method(
27         method  => "retrieve_stat_cats",
28         api_name        => "open-ils.circ.stat_cat.actor.retrieve.all");
29
30 __PACKAGE__->register_method(
31         method  => "retrieve_stat_cats",
32         api_name        => "open-ils.circ.stat_cat.asset.retrieve.all");
33
34 # retrieves all of the stat cats for a given org unit
35 # if no orgid, user_session->home_ou is used
36
37 sub retrieve_stat_cats {
38         my( $self, $client, $user_session, $orgid ) = @_;
39
40         my $method = "open-ils.storage.ranged.fleshed.actor.stat_cat.all.atomic"; 
41         if( $self->api_name =~ /asset/ ) {
42                 $method = "open-ils.storage.ranged.fleshed.asset.stat_cat.all.atomic"; 
43         }
44
45
46         my $user_obj = $apputils->check_user_session($user_session); 
47         if(!$orgid) { $orgid = $user_obj->home_ou; }
48         my $cats = $apputils->simple_scalar_request(
49                                 "open-ils.storage", $method, $orgid );
50
51         return [ sort { $a->name cmp $b->name } @$cats ];
52 }
53
54
55
56 __PACKAGE__->register_method(
57         method  => "retrieve_ranged_intersect_stat_cats",
58         api_name        => "open-ils.circ.stat_cat.asset.multirange.intersect.retrieve");
59
60 sub retrieve_ranged_intersect_stat_cats {
61         my( $self, $client, $user_session, $orglist ) = @_;
62
63         my $user_obj = $apputils->check_user_session($user_session); 
64         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
65
66         # uniquify, yay!
67         my %hash = map { ($_ => 1) } @$orglist;
68         $orglist = [ keys %hash ];
69
70         warn "range: @$orglist\n";
71
72         my      $method = "open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all.atomic";
73         return $apputils->simple_scalar_request(
74                                 "open-ils.storage", $method, $orglist );
75 }
76
77
78 __PACKAGE__->register_method(
79         method  => "retrieve_ranged_union_stat_cats",
80         api_name        => "open-ils.circ.stat_cat.asset.multirange.union.retrieve");
81
82 sub retrieve_ranged_union_stat_cats {
83         my( $self, $client, $user_session, $orglist ) = @_;
84
85         my      $method = "open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all.atomic";
86         use Data::Dumper;
87         warn "Retrieving stat_cats with method $method and orgs " . Dumper($orglist) . "\n";
88
89         my $user_obj = $apputils->check_user_session($user_session); 
90         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
91
92         # uniquify, yay!
93         my %hash = map { ($_ => 1) } @$orglist;
94         $orglist = [ keys %hash ];
95
96         warn "range: @$orglist\n";
97
98         return $apputils->simple_scalar_request(
99                                 "open-ils.storage", $method, $orglist );
100 }
101
102
103
104
105
106 __PACKAGE__->register_method(
107         method  => "stat_cat_create",
108         api_name        => "open-ils.circ.stat_cat.asset.create");
109
110 __PACKAGE__->register_method(
111         method  => "stat_cat_create",
112         api_name        => "open-ils.circ.stat_cat.actor.create");
113
114 sub stat_cat_create {
115         my( $self, $client, $user_session, $stat_cat ) = @_;
116
117         if(!$stat_cat) {
118                 throw OpenSRF::EX::ERROR
119                         ("stat_cat.*.create requires a stat_cat object");
120         }
121
122         my $user_obj = $apputils->check_user_session($user_session); 
123         my $orgid = $user_obj->home_ou();
124
125         my $method = "open-ils.storage.direct.actor.stat_cat.create";
126         my $entry_create = "open-ils.storage.direct.actor.stat_cat_entry.create";
127
128         if($self->api_name =~ /asset/) {
129                 $method = "open-ils.storage.direct.asset.stat_cat.create";
130                 $entry_create = "open-ils.storage.direct.asset.stat_cat_entry.create";
131         }
132
133         my $session = $apputils->start_db_session();
134         my $newid = _create_stat_cat($session, $stat_cat, $method);
135
136         if( ref($stat_cat->entries) ) {
137                 for my $entry (@{$stat_cat->entries}) {
138                         $entry->stat_cat($newid);
139                         _create_stat_entry($session, $entry, $entry_create);
140                 }
141         }
142
143         $apputils->commit_db_session($session);
144
145         warn "Stat cat creation successful with id $newid\n";
146
147         if( $self->api_name =~ /asset/ ) {
148                 return _flesh_asset_cat($newid, $orgid);
149         } else {
150                 return _flesh_user_cat($newid, $orgid);
151         }
152
153 }
154
155
156 sub _flesh_user_cat {
157         my $id = shift;
158         my $orgid = shift;
159
160         my $session = OpenSRF::AppSession->create("open-ils.storage");
161         my $cat = $session->request(
162                 "open-ils.storage.direct.actor.stat_cat.retrieve",
163                 $id )->gather(1);
164
165         $cat->entries( 
166                 $session->request(
167                         "open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat.atomic",
168                         $orgid, $id )->gather(1) );
169
170         return $cat;
171 }
172
173
174 sub _flesh_asset_cat {
175         my $id = shift;
176         my $orgid = shift;
177
178         my $session = OpenSRF::AppSession->create("open-ils.storage");
179         my $cat = $session->request(
180                 "open-ils.storage.direct.asset.stat_cat.retrieve",
181                 $id )->gather(1);
182
183         $cat->entries( 
184                 $session->request(
185                         "open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat.atomic",
186                         $orgid,  $id )->gather(1) );
187
188         return $cat;
189
190 }
191
192
193 sub _create_stat_cat {
194         my( $session, $stat_cat, $method) = @_;
195         warn "Creating new stat cat with name " . $stat_cat->name . "\n";
196         $stat_cat->clear_id();
197         my $req = $session->request( $method, $stat_cat );
198         my $id = $req->gather(1);
199         if(!$id) {
200                 throw OpenSRF::EX::ERROR 
201                 ("Error creating new statistical category"); }
202
203         warn "Stat cat create returned id $id\n";
204         return $id;
205 }
206
207
208 sub _create_stat_entry {
209         my( $session, $stat_entry, $method) = @_;
210
211         warn "Creating new stat entry with value " . $stat_entry->value . "\n";
212         $stat_entry->clear_id();
213
214         my $req = $session->request($method, $stat_entry);
215         my $id = $req->gather(1);
216
217         warn "Stat entry " . Dumper($stat_entry) . "\n";        
218         
219         if(!$id) {
220                 throw OpenSRF::EX::ERROR 
221                 ("Error creating new stat cat entry"); }
222
223         warn "Stat cat entry create returned id $id\n";
224         return $id;
225 }
226
227
228 __PACKAGE__->register_method(
229         method  => "update_stat_entry",
230         api_name        => "open-ils.circ.stat_cat.actor.entry.update");
231
232 __PACKAGE__->register_method(
233         method  => "update_stat_entry",
234         api_name        => "open-ils.circ.stat_cat.asset.entry.update");
235
236 sub update_stat_entry {
237         my( $self, $client, $user_session, $entry ) = @_;
238
239         my $user_obj = $apputils->check_user_session($user_session); 
240
241         my $method = "open-ils.storage.direct.actor.stat_cat_entry.update";
242         if($self->api_name =~ /asset/) {
243                 $method = "open-ils.storage.direct.asset.stat_cat_entry.update";
244         }
245
246         my $session = $apputils->start_db_session();
247         my $req = $session->request($method, $entry); 
248         my $status = $req->gather(1);
249         $apputils->commit_db_session($session);
250         warn "stat cat entry with value " . $entry->value . " updated with status $status\n";
251         return 1;
252 }
253
254
255 __PACKAGE__->register_method(
256         method  => "update_stat",
257         api_name        => "open-ils.circ.stat_cat.actor.update");
258
259 __PACKAGE__->register_method(
260         method  => "update_stat",
261         api_name        => "open-ils.circ.stat_cat.asset.update");
262
263 sub update_stat {
264         my( $self, $client, $user_session, $cat ) = @_;
265
266         my $user_obj = $apputils->check_user_session($user_session); 
267
268         my $method = "open-ils.storage.direct.actor.stat_cat.update";
269         if($self->api_name =~ /asset/) {
270                 $method = "open-ils.storage.direct.asset.stat_cat.update";
271         }
272
273         my $session = $apputils->start_db_session();
274         my $req = $session->request($method, $cat); 
275         my $status = $req->gather(1);
276         $apputils->commit_db_session($session);
277         warn "stat cat with id " . $cat->id . " updated with status $status\n";
278         return 1;
279 }
280
281
282 __PACKAGE__->register_method(
283         method  => "create_stat_entry",
284         api_name        => "open-ils.circ.stat_cat.actor.entry.create");
285
286 __PACKAGE__->register_method(
287         method  => "create_stat_entry",
288         api_name        => "open-ils.circ.stat_cat.asset.entry.create");
289
290 sub create_stat_entry {
291         my( $self, $client, $user_session, $entry ) = @_;
292
293         my $user_obj = $apputils->check_user_session($user_session); 
294
295         $entry->clear_id();
296         use Data::Dumper;
297         warn Dumper($entry);
298
299         my $method = "open-ils.storage.direct.actor.stat_cat_entry.create";
300         if($self->api_name =~ /asset/) {
301                 $method = "open-ils.storage.direct.asset.stat_cat_entry.create";
302         }
303
304         my $session = $apputils->start_db_session();
305         my $req = $session->request($method, $entry); 
306         my $status = $req->gather(1);
307         $apputils->commit_db_session($session);
308
309         warn "stat cat entry with id " . $status . " updated with status $status\n";
310         return $status;
311 }
312
313
314
315 __PACKAGE__->register_method(
316         method  => "create_stat_map",
317         api_name        => "open-ils.circ.stat_cat.actor.user_map.create");
318
319 __PACKAGE__->register_method(
320         method  => "create_stat_map",
321         api_name        => "open-ils.circ.stat_cat.asset.copy_map.create");
322
323 sub create_stat_map {
324         my( $self, $client, $user_session, $map ) = @_;
325
326         my $user_obj = $apputils->check_user_session($user_session); 
327
328         warn "Creating stat_cat_map\n";
329
330         $map->clear_id();
331
332         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
333         my $ret = "open-ils.storage.direct.actor.stat_cat_entry_user_map.retrieve";
334         if($self->api_name =~ /asset/) {
335                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.create";
336                 $ret = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.retrieve";
337         }
338
339         my $session = $apputils->start_db_session();
340         my $req = $session->request($method, $map); 
341         my $newid = $req->gather(1);
342         warn "Created new stat cat map with id $newid\n";
343         $apputils->commit_db_session($session);
344
345         return $apputils->simple_scalar_request( "open-ils.storage", $ret, $newid );
346
347 }
348
349
350 __PACKAGE__->register_method(
351         method  => "update_stat_map",
352         api_name        => "open-ils.circ.stat_cat.actor.user_map.update");
353
354 __PACKAGE__->register_method(
355         method  => "update_stat_map",
356         api_name        => "open-ils.circ.stat_cat.asset.copy_map.update");
357
358 sub update_stat_map {
359         my( $self, $client, $user_session, $map ) = @_;
360
361         my $user_obj = $apputils->check_user_session($user_session); 
362
363         warn "Updating stat_cat_map\n";
364
365         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
366         if($self->api_name =~ /asset/) {
367                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.update";
368         }
369
370         my $session = $apputils->start_db_session();
371         my $req = $session->request($method, $map); 
372         my $newid = $req->gather(1);
373         warn "Updated new stat cat map with id $newid\n";
374         $apputils->commit_db_session($session);
375
376         return $newid;
377 }
378
379
380
381 __PACKAGE__->register_method(
382         method  => "retrieve_maps",
383         api_name        => "open-ils.circ.stat_cat.actor.user_map.retrieve");
384
385 __PACKAGE__->register_method(
386         method  => "retrieve_maps",
387         api_name        => "open-ils.circ.stat_cat.asset.copy_map.retrieve");
388
389 sub retrieve_maps {
390         my( $self, $client, $user_session, $target ) = @_;
391
392         my $user_obj = $apputils->check_user_session($user_session); 
393
394         my      $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.search.owning_copy.atomic";
395         if($self->api_name =~ /actor/ ) {
396                 if(!$target) { $target = $user_obj->id; }
397                 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic";
398         }
399
400         return $apputils->simple_scalar_request("open-ils.storage", $method, $target);
401 }
402
403
404
405
406 __PACKAGE__->register_method(
407         method  => "delete_stats",
408         api_name        => "open-ils.circ.stat_cat.actor.delete");
409
410 __PACKAGE__->register_method(
411         method  => "delete_stats",
412         api_name        => "open-ils.circ.stat_cat.asset.delete");
413
414 sub delete_stats {
415         my( $self, $client, $user_session, $target ) = @_;
416         my $user_obj = $apputils->check_user_session($user_session); 
417         my $session = OpenSRF::AppSession->create("open-ils.storage");
418         my $type = "actor";
419         if($self->api_name =~ /asset/) { $type = "asset"; }
420         return _delete_stats($session, $target, $type);
421 }
422
423 sub _delete_stats {
424         my( $session, $stat, $type) = @_;
425
426         my      $method = "open-ils.storage.direct.asset.stat_cat.delete";
427         if($type =~ /actor/ ) {
428                 $method = "open-ils.storage.direct.actor.stat_cat.delete";
429         }
430         return $session->request($method, $stat)->gather(1);
431 }
432
433
434
435 __PACKAGE__->register_method(
436         method  => "delete_entry",
437         api_name        => "open-ils.circ.stat_cat.actor.entry.delete");
438
439 __PACKAGE__->register_method(
440         method  => "delete_entry",
441         api_name        => "open-ils.circ.stat_cat.asset.entry.delete");
442
443 sub delete_entry {
444         my( $self, $client, $user_session, $target ) = @_;
445         my $user_obj = $apputils->check_user_session($user_session); 
446         my $session = OpenSRF::AppSession->create("open-ils.storage");
447         my $type = "actor";
448         if($self->api_name =~ /asset/) { $type = "asset"; }
449         return _delete_entry($session, $target, $type);
450 }
451
452 sub _delete_entry {
453         my( $session, $stat_entry, $type) = @_;
454
455         my      $method = "open-ils.storage.direct.asset.stat_cat_entry.delete";
456         if($type =~ /actor/ ) {
457                 $method = "open-ils.storage.direct.actor.stat_cat_entry.delete";
458         }
459
460         return $session->request($method, $stat_entry)->gather(1);
461 }
462
463
464
465 1;