1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
4 use OpenSRF::Utils::Cache;
5 use OpenSRF::EX qw(:try);
8 my $cache_client = "OpenSRF::Utils::Cache";
10 # ---------------------------------------------------------------------------
11 # Pile of utilty methods used accross applications.
12 # ---------------------------------------------------------------------------
15 # ---------------------------------------------------------------------------
16 # on sucess, returns the created session, on failure throws ERROR exception
17 # ---------------------------------------------------------------------------
18 sub start_db_session {
21 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
22 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
24 my $trans_resp = $trans_req->recv();
25 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
26 if( ! $trans_resp->content() ) {
28 ("Unable to Begin Transaction with database" );
35 # returns undef if user has all of the perms provided
36 # returns the first failed perm on failure
37 sub check_user_perms {
38 my($self, $user_id, $org_id, @perm_types ) = @_;
40 warn "Checking perm with user : $user_id , org: $org_id, @perm_types\n";
42 throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
43 unless( defined($user_id) and defined($org_id) and @perm_types);
45 my $session = OpenSRF::AppSession->create("open-ils.storage");
46 for my $type (@perm_types) {
47 my $req = $session->request(
48 "open-ils.storage.permission.user_has_perm",
49 $user_id, $type, $org_id );
50 my $resp = $req->gather(1);
52 $session->disconnect();
57 $session->disconnect();
63 # ---------------------------------------------------------------------------
64 # commits and destroys the session
65 # ---------------------------------------------------------------------------
66 sub commit_db_session {
67 my( $self, $session ) = @_;
69 my $req = $session->request( "open-ils.storage.transaction.commit" );
70 my $resp = $req->recv();
73 throw OpenSRF::EX::ERROR ("Unable to commit db session");
76 if(UNIVERSAL::isa($resp,"Error")) {
77 throw $resp ($resp->stringify);
81 throw OpenSRF::EX::ERROR ("Unable to commit db session");
85 $session->disconnect();
89 sub rollback_db_session {
90 my( $self, $session ) = @_;
92 my $req = $session->request("open-ils.storage.transaction.rollback");
93 my $resp = $req->recv();
94 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
97 $session->disconnect();
101 # ---------------------------------------------------------------------------
102 # Checks to see if a user is logged in. Returns the user record on success,
103 # throws an exception on error.
104 # ---------------------------------------------------------------------------
105 sub check_user_session {
107 my( $self, $user_session ) = @_;
109 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
110 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
111 my $response = $request->recv();
114 throw OpenSRF::EX::User ("Session [$user_session] cannot be authenticated" );
117 if($response->isa("OpenSRF::EX")) {
118 throw $response ($response->stringify);
121 my $user = $response->content;
123 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
126 $session->disconnect();
134 # generic simple request returning a scalar value
135 sub simple_scalar_request {
136 my($self, $service, $method, @params) = @_;
138 my $session = OpenSRF::AppSession->create( $service );
139 my $request = $session->request( $method, @params );
140 my $response = $request->recv(30);
142 $request->wait_complete;
144 if(!$request->complete) {
145 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" .
146 "\n did not complete successfully");
150 warn "No response from $service for method $method with params @params";
153 if(UNIVERSAL::isa($response,"Error")) {
154 throw $response ("Call to $service for method $method with params @params" .
155 "\n failed with exception: " . $response->stringify );
161 $session->disconnect();
165 if($response) { $value = $response->content; }
166 else { $value = undef; }
177 my $org_typelist = undef;
178 my $org_typelist_hash = {};
183 if($tree) { return $tree; }
185 # see if it's in the cache
186 $tree = $cache_client->new()->get_cache('_orgtree');
187 if($tree) { return $tree; }
190 warn "Retrieving Org Tree\n";
191 $orglist = $self->simple_scalar_request(
193 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
196 if( ! $org_typelist ) {
197 warn "Retrieving org types\n";
198 $org_typelist = $self->simple_scalar_request(
200 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
201 $self->build_org_type($org_typelist);
204 $tree = $self->build_org_tree($orglist,1);
205 $cache_client->new()->put_cache('_orgtree', $tree);
210 my $slimtree = undef;
211 sub get_slim_org_tree {
214 if($slimtree) { return $slimtree; }
216 # see if it's in the cache
217 $slimtree = $cache_client->new()->get_cache('slimorgtree');
218 if($slimtree) { return $slimtree; }
221 warn "Retrieving Org Tree\n";
222 $orglist = $self->simple_scalar_request(
224 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
227 $slimtree = $self->build_org_tree($orglist);
228 $cache_client->new->put_cache('slimorgtree', $slimtree);
235 my($self, $org_typelist) = @_;
236 for my $type (@$org_typelist) {
237 $org_typelist_hash->{$type->id()} = $type;
245 my( $self, $orglist, $add_types ) = @_;
247 return $orglist unless (
248 ref($orglist) and @$orglist > 1 );
251 $a->ou_type <=> $b->ou_type ||
252 $a->name cmp $b->name } @$orglist;
254 for my $org (@list) {
258 if(!ref($org->ou_type()) and $add_types) {
259 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
262 next unless (defined($org->parent_ou));
264 my ($parent) = grep { $_->id == $org->parent_ou } @list;
266 $parent->children([]) unless defined($parent->children);
267 push( @{$parent->children}, $org );