1 # ---------------------------------------------------------------
2 # Copyright (C) 2006 Georgia Public Library Service
3 # Bill Erickson <billserickson@gmail.com>
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.
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 # ---------------------------------------------------------------
16 package OpenILS::Application::Circ::StatCat;
17 use base qw/OpenILS::Application/;
18 use strict; use warnings;
20 use OpenSRF::Utils::Logger qw($logger);
21 use OpenSRF::EX qw/:try/;
22 use OpenILS::Application::AppUtils;
23 my $apputils = "OpenILS::Application::AppUtils";
28 __PACKAGE__->register_method(
29 method => "retrieve_stat_cat_list",
31 api_name => "open-ils.circ.stat_cat.actor.retrieve.batch");
33 __PACKAGE__->register_method(
34 method => "retrieve_stat_cat_list",
36 api_name => "open-ils.circ.stat_cat.asset.retrieve.batch");
38 # retrieves all of the stat cats for a given org unit
39 # if no orgid, user_session->home_ou is used
41 sub retrieve_stat_cat_list {
42 my( $self, $client, $user_session, @sc ) = @_;
48 my $method = "open-ils.storage.fleshed.actor.stat_cat.retrieve.batch.atomic";
49 if( $self->api_name =~ /asset/ ) {
50 $method = "open-ils.storage.fleshed.asset.stat_cat.retrieve.batch.atomic";
53 my($user_obj, $evt) = $apputils->checkses($user_session);
56 my $cats = $apputils->simple_scalar_request(
57 "open-ils.storage", $method, @sc);
59 return [ sort { $a->name cmp $b->name } @$cats ];
62 __PACKAGE__->register_method(
63 method => "retrieve_stat_cats",
64 api_name => "open-ils.circ.stat_cat.actor.retrieve.all");
66 __PACKAGE__->register_method(
67 method => "retrieve_stat_cats",
68 api_name => "open-ils.circ.stat_cat.asset.retrieve.all");
70 # retrieves all of the stat cats for a given org unit
71 # if no orgid, user_session->home_ou is used
73 sub retrieve_stat_cats {
74 my( $self, $client, $user_session, $orgid ) = @_;
76 my $method = "open-ils.storage.ranged.fleshed.actor.stat_cat.all.atomic";
77 if( $self->api_name =~ /asset/ ) {
78 $method = "open-ils.storage.ranged.fleshed.asset.stat_cat.all.atomic";
81 my($user_obj, $evt) = $apputils->checkses($user_session);
84 if(!$orgid) { $orgid = $user_obj->home_ou; }
85 my $cats = $apputils->simple_scalar_request(
86 "open-ils.storage", $method, $orgid );
88 return [ sort { $a->name cmp $b->name } @$cats ];
92 __PACKAGE__->register_method(
93 method => "retrieve_ranged_intersect_stat_cats",
94 api_name => "open-ils.circ.stat_cat.asset.multirange.intersect.retrieve");
96 sub retrieve_ranged_intersect_stat_cats {
97 my( $self, $client, $user_session, $orglist ) = @_;
99 my($user_obj, $evt) = $apputils->checkses($user_session);
102 if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
105 my %hash = map { ($_ => 1) } @$orglist;
106 $orglist = [ keys %hash ];
108 warn "range: @$orglist\n";
110 my $method = "open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all.atomic";
111 return $apputils->simple_scalar_request(
112 "open-ils.storage", $method, $orglist );
116 __PACKAGE__->register_method(
117 method => "retrieve_ranged_union_stat_cats",
118 api_name => "open-ils.circ.stat_cat.asset.multirange.union.retrieve");
120 sub retrieve_ranged_union_stat_cats {
121 my( $self, $client, $user_session, $orglist ) = @_;
123 my $method = "open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all.atomic";
125 warn "Retrieving stat_cats with method $method and orgs " . Dumper($orglist) . "\n";
127 my($user_obj, $evt) = $apputils->checkses($user_session);
130 if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
133 my %hash = map { ($_ => 1) } @$orglist;
134 $orglist = [ keys %hash ];
136 warn "range: @$orglist\n";
138 return $apputils->simple_scalar_request(
139 "open-ils.storage", $method, $orglist );
144 __PACKAGE__->register_method(
145 method => "stat_cat_create",
146 api_name => "open-ils.circ.stat_cat.asset.create");
148 __PACKAGE__->register_method(
149 method => "stat_cat_create",
150 api_name => "open-ils.circ.stat_cat.actor.create");
152 sub stat_cat_create {
153 my( $self, $client, $user_session, $stat_cat ) = @_;
155 my $method = "open-ils.storage.direct.actor.stat_cat.create";
156 my $entry_create = "open-ils.storage.direct.actor.stat_cat_entry.create";
157 my $perm = 'CREATE_PATRON_STAT_CAT';
158 my $eperm = 'CREATE_PATRON_STAT_CAT_ENTRY';
160 if($self->api_name =~ /asset/) {
161 $method = "open-ils.storage.direct.asset.stat_cat.create";
162 $entry_create = "open-ils.storage.direct.asset.stat_cat_entry.create";
163 $perm = 'CREATE_COPY_STAT_CAT_ENTRY';
166 #my $user_obj = $apputils->check_user_session($user_session);
167 #my $orgid = $user_obj->home_ou();
168 my( $user_obj, $evt ) = $apputils->checkses($user_session);
170 $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $perm);
173 if($stat_cat->entries) {
174 $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $eperm);
179 my $session = $apputils->start_db_session();
180 $apputils->set_audit_info($user_session, $user_obj->id, $user_obj->wsid);
181 my $newid = _create_stat_cat($session, $stat_cat, $method);
183 if( ref($stat_cat->entries) ) {
184 for my $entry (@{$stat_cat->entries}) {
185 $entry->stat_cat($newid);
186 _create_stat_entry($session, $entry, $entry_create);
190 $apputils->commit_db_session($session);
192 $logger->debug("Stat cat creation successful with id $newid");
194 my $orgid = $user_obj->home_ou;
195 if( $self->api_name =~ /asset/ ) {
196 return _flesh_asset_cat($newid, $orgid);
198 return _flesh_user_cat($newid, $orgid);
203 sub _flesh_user_cat {
207 my $session = OpenSRF::AppSession->create("open-ils.storage");
208 my $cat = $session->request(
209 "open-ils.storage.direct.actor.stat_cat.retrieve",
214 "open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat.atomic",
215 $orgid, $id )->gather(1) );
221 sub _flesh_asset_cat {
225 my $session = OpenSRF::AppSession->create("open-ils.storage");
226 my $cat = $session->request(
227 "open-ils.storage.direct.asset.stat_cat.retrieve",
232 "open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat.atomic",
233 $orgid, $id )->gather(1) );
240 sub _create_stat_cat {
241 my( $session, $stat_cat, $method) = @_;
242 warn "Creating new stat cat with name " . $stat_cat->name . "\n";
243 $stat_cat->clear_id();
244 my $req = $session->request( $method, $stat_cat );
245 my $id = $req->gather(1);
247 throw OpenSRF::EX::ERROR
248 ("Error creating new statistical category"); }
250 warn "Stat cat create returned id $id\n";
255 sub _create_stat_entry {
256 my( $session, $stat_entry, $method) = @_;
258 warn "Creating new stat entry with value " . $stat_entry->value . "\n";
259 $stat_entry->clear_id();
261 my $req = $session->request($method, $stat_entry);
262 my $id = $req->gather(1);
264 warn "Stat entry " . Dumper($stat_entry) . "\n";
267 throw OpenSRF::EX::ERROR
268 ("Error creating new stat cat entry"); }
270 warn "Stat cat entry create returned id $id\n";
275 __PACKAGE__->register_method(
276 method => "update_stat_entry",
277 api_name => "open-ils.circ.stat_cat.actor.entry.update");
279 __PACKAGE__->register_method(
280 method => "update_stat_entry",
281 api_name => "open-ils.circ.stat_cat.asset.entry.update");
283 sub update_stat_entry {
284 my( $self, $client, $user_session, $entry ) = @_;
287 my $method = "open-ils.storage.direct.actor.stat_cat_entry.update";
288 my $perm = 'UPDATE_PATRON_STAT_CAT_ENTRY';
289 if($self->api_name =~ /asset/) {
290 $method = "open-ils.storage.direct.asset.stat_cat_entry.update";
291 $perm = 'UPDATE_COPY_STAT_CAT_ENTRY';
294 my( $user_obj, $evt ) = $apputils->checkses($user_session);
296 $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
299 my $session = $apputils->start_db_session();
300 $apputils->set_audit_info($user_session, $user_obj->id, $user_obj->wsid);
301 my $req = $session->request($method, $entry);
302 my $status = $req->gather(1);
303 $apputils->commit_db_session($session);
304 warn "stat cat entry with value " . $entry->value . " updated with status $status\n";
309 __PACKAGE__->register_method(
310 method => "update_stat",
311 api_name => "open-ils.circ.stat_cat.actor.update");
313 __PACKAGE__->register_method(
314 method => "update_stat",
315 api_name => "open-ils.circ.stat_cat.asset.update");
318 my( $self, $client, $user_session, $cat ) = @_;
320 my $method = "open-ils.storage.direct.actor.stat_cat.update";
321 my $perm = 'UPDATE_PATRON_STAT_CAT';
322 if($self->api_name =~ /asset/) {
323 $method = "open-ils.storage.direct.asset.stat_cat.update";
324 $perm = 'UPDATE_COPY_STAT_CAT';
327 my( $user_obj, $evt ) = $apputils->checkses($user_session);
329 $evt = $apputils->check_perms( $user_obj->id, $cat->owner, $perm );
332 my $session = $apputils->start_db_session();
333 $apputils->set_audit_info($user_session, $user_obj->id, $user_obj->wsid);
334 my $req = $session->request($method, $cat);
335 my $status = $req->gather(1);
336 $apputils->commit_db_session($session);
337 warn "stat cat with id " . $cat->id . " updated with status $status\n";
342 __PACKAGE__->register_method(
343 method => "create_stat_entry",
344 api_name => "open-ils.circ.stat_cat.actor.entry.create");
346 __PACKAGE__->register_method(
347 method => "create_stat_entry",
348 api_name => "open-ils.circ.stat_cat.asset.entry.create");
350 sub create_stat_entry {
351 my( $self, $client, $user_session, $entry ) = @_;
353 my $method = "open-ils.storage.direct.actor.stat_cat_entry.create";
354 my $perm = 'CREATE_PATRON_STAT_CAT_ENTRY';
355 if($self->api_name =~ /asset/) {
356 $method = "open-ils.storage.direct.asset.stat_cat_entry.create";
357 $perm = 'CREATE_COPY_STAT_CAT_ENTRY';
360 my( $user_obj, $evt ) = $apputils->checkses($user_session);
362 $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
366 my $session = $apputils->start_db_session();
367 $apputils->set_audit_info($user_session, $user_obj->id, $user_obj->wsid);
368 my $req = $session->request($method, $entry);
369 my $status = $req->gather(1);
370 $apputils->commit_db_session($session);
372 $logger->info("created stat cat entry $status");
378 __PACKAGE__->register_method(
379 method => "create_stat_map",
380 api_name => "open-ils.circ.stat_cat.actor.user_map.create");
382 __PACKAGE__->register_method(
383 method => "create_stat_map",
384 api_name => "open-ils.circ.stat_cat.asset.copy_map.create");
386 sub create_stat_map {
387 my( $self, $client, $user_session, $map ) = @_;
390 my ( $evt, $copy, $volume, $patron, $user_obj );
392 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
393 my $ret = "open-ils.storage.direct.actor.stat_cat_entry_user_map.retrieve";
394 my $perm = 'CREATE_PATRON_STAT_CAT_ENTRY_MAP';
397 if($self->api_name =~ /asset/) {
398 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.create";
399 $ret = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.retrieve";
400 $perm = 'CREATE_COPY_STAT_CAT_ENTRY_MAP';
401 ( $copy, $evt ) = $apputils->fetch_copy($map->owning_copy);
403 ( $volume, $evt ) = $apputils->fetch_callnumber($copy->call_number);
405 $perm_org = $volume->owning_lib;
408 ($patron, $evt) = $apputils->fetch_user($map->target_usr);
410 $perm_org = $patron->home_ou;
413 ( $user_obj, $evt ) = $apputils->checkses($user_session);
415 $evt = $apputils->check_perms( $user_obj->id, $perm_org, $perm );
418 $logger->debug( $user_obj->id . " creating new stat cat map" );
422 my $session = $apputils->start_db_session();
423 $apputils->set_audit_info($user_session, $user_obj->id, $user_obj->wsid);
424 my $req = $session->request($method, $map);
425 my $newid = $req->gather(1);
426 warn "Created new stat cat map with id $newid\n";
427 $apputils->commit_db_session($session);
429 return $apputils->simple_scalar_request( "open-ils.storage", $ret, $newid );
434 __PACKAGE__->register_method(
435 method => "update_stat_map",
436 api_name => "open-ils.circ.stat_cat.actor.user_map.update");
438 __PACKAGE__->register_method(
439 method => "update_stat_map",
440 api_name => "open-ils.circ.stat_cat.asset.copy_map.update");
442 sub update_stat_map {
443 my( $self, $client, $user_session, $map ) = @_;
445 my ( $evt, $copy, $volume, $patron, $user_obj );
447 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
448 my $perm = 'UPDATE_PATRON_STAT_ENTRY_MAP';
451 if($self->api_name =~ /asset/) {
452 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.update";
453 $perm = 'UPDATE_COPY_STAT_ENTRY_MAP';
454 ( $copy, $evt ) = $apputils->fetch_copy($map->owning_copy);
456 ( $volume, $evt ) = $apputils->fetch_callnumber($copy->call_number);
458 $perm_org = $volume->owning_lib;
461 ($patron, $evt) = $apputils->fetch_user($map->target_usr);
463 $perm_org = $patron->home_ou;
467 ( $user_obj, $evt ) = $apputils->checkses($user_session);
469 $evt = $apputils->check_perms( $user_obj->id, $perm_org, $perm );
473 my $session = $apputils->start_db_session();
474 $apputils->set_audit_info($user_session, $user_obj->id, $user_obj->wsid);
475 my $req = $session->request($method, $map);
476 my $newid = $req->gather(1);
477 warn "Updated new stat cat map with id $newid\n";
478 $apputils->commit_db_session($session);
485 __PACKAGE__->register_method(
486 method => "retrieve_maps",
487 api_name => "open-ils.circ.stat_cat.actor.user_map.retrieve");
489 __PACKAGE__->register_method(
490 method => "retrieve_maps",
491 api_name => "open-ils.circ.stat_cat.asset.copy_map.retrieve");
494 my( $self, $client, $user_session, $target ) = @_;
497 my( $user_obj, $evt ) = $apputils->checkses($user_session);
500 my $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.search.owning_copy.atomic";
501 if($self->api_name =~ /actor/ ) {
502 if(!$target) { $target = $user_obj->id; }
503 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic";
506 return $apputils->simple_scalar_request("open-ils.storage", $method, $target);
512 __PACKAGE__->register_method(
513 method => "delete_stats",
514 api_name => "open-ils.circ.stat_cat.actor.delete");
516 __PACKAGE__->register_method(
517 method => "delete_stats",
518 api_name => "open-ils.circ.stat_cat.asset.delete");
521 my( $self, $client, $user_session, $target ) = @_;
526 my $perm = 'DELETE_PATRON_STAT_CAT';
527 if($self->api_name =~ /asset/) {
529 $perm = 'DELETE_COPY_STAT_CAT';
532 my( $user_obj, $evt ) = $apputils->checkses($user_session);
535 ( $cat, $evt ) = $apputils->fetch_stat_cat( $type, $target );
538 $evt = $apputils->check_perms( $user_obj->id, $cat->owner, $perm );
541 my $session = OpenSRF::AppSession->create("open-ils.storage");
542 return _delete_stats($session, $target, $type);
546 my( $session, $stat, $type) = @_;
548 my $method = "open-ils.storage.direct.asset.stat_cat.delete";
549 if($type =~ /actor/ ) {
550 $method = "open-ils.storage.direct.actor.stat_cat.delete";
552 return $session->request($method, $stat)->gather(1);
557 __PACKAGE__->register_method(
558 method => "delete_entry",
559 api_name => "open-ils.circ.stat_cat.actor.entry.delete");
561 __PACKAGE__->register_method(
562 method => "delete_entry",
563 api_name => "open-ils.circ.stat_cat.asset.entry.delete");
566 my( $self, $client, $user_session, $target ) = @_;
569 my $perm = 'DELETE_PATRON_STAT_CAT_ENTRY';
570 if($self->api_name =~ /asset/) {
572 $perm = 'DELETE_COPY_STAT_CAT_ENTRY';
576 my( $user_obj, $evt ) = $apputils->checkses($user_session);
579 ( $entry, $evt ) = $apputils->fetch_stat_cat_entry( $type, $target );
582 $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
585 my $session = OpenSRF::AppSession->create("open-ils.storage");
586 return _delete_entry($session, $target, $type);
590 my( $session, $stat_entry, $type) = @_;
592 my $method = "open-ils.storage.direct.asset.stat_cat_entry.delete";
593 if($type =~ /actor/ ) {
594 $method = "open-ils.storage.direct.actor.stat_cat_entry.delete";
597 return $session->request($method, $stat_entry)->gather(1);
601 __PACKAGE__->register_method(
602 method => 'fetch_stats_by_copy',
603 api_name => 'open-ils.circ.asset.stat_cat_entries.fleshed.retrieve_by_copy',
607 sub fetch_stats_by_copy {
608 my( $self, $conn, $args ) = @_;
612 if( $$args{public} ) {
613 my $maps = $U->cstorereq(
614 'open-ils.cstore.direct.asset.stat_cat_entry_copy_map.search.atomic', { owning_copy => $$args{copyid} });
618 for my $map (@$maps) {
620 warn "map ".$map->id."\n";
621 warn "map ".$map->stat_cat_entry."\n";
623 my $entry = $U->cstorereq(
624 'open-ils.cstore.direct.asset.stat_cat_entry.retrieve', $map->stat_cat_entry);
626 warn "Found entry ".$entry->id."\n";
628 my $cat = $U->cstorereq(
629 'open-ils.cstore.direct.asset.stat_cat.retrieve', $entry->stat_cat );
630 $entry->stat_cat( $cat );
631 push( @entries, $entry );