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 throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
41 unless( defined($user_id) and defined($org_id) and @perm_types);
43 my $session = OpenSRF::AppSession->create("open-ils.storage");
44 for my $type (@perm_types) {
45 my $req = $session->request(
46 "open-ils.storage.permission.user_has_perm",
47 $user_id, $type, $org_id );
48 my $resp = $req->gather(1);
50 $session->disconnect();
55 $session->disconnect();
61 # ---------------------------------------------------------------------------
62 # commits and destroys the session
63 # ---------------------------------------------------------------------------
64 sub commit_db_session {
65 my( $self, $session ) = @_;
67 my $req = $session->request( "open-ils.storage.transaction.commit" );
68 my $resp = $req->recv();
71 throw OpenSRF::EX::ERROR ("Unable to commit db session");
74 if(UNIVERSAL::isa($resp,"Error")) {
75 throw $resp ($resp->stringify);
79 throw OpenSRF::EX::ERROR ("Unable to commit db session");
83 $session->disconnect();
87 sub rollback_db_session {
88 my( $self, $session ) = @_;
90 my $req = $session->request("open-ils.storage.transaction.rollback");
91 my $resp = $req->recv();
92 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
95 $session->disconnect();
99 # ---------------------------------------------------------------------------
100 # Checks to see if a user is logged in. Returns the user record on success,
101 # throws an exception on error.
102 # ---------------------------------------------------------------------------
103 sub check_user_session {
105 my( $self, $user_session ) = @_;
107 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
108 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
109 my $response = $request->recv();
112 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
115 if($response->isa("OpenSRF::EX")) {
116 throw $response ($response->stringify);
119 my $user = $response->content;
121 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
124 $session->disconnect();
132 # generic simple request returning a scalar value
133 sub simple_scalar_request {
134 my($self, $service, $method, @params) = @_;
136 my $session = OpenSRF::AppSession->create( $service );
137 my $request = $session->request( $method, @params );
138 my $response = $request->recv(30);
140 $request->wait_complete;
142 if(!$request->complete) {
143 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" .
144 "\n did not complete successfully");
148 warn "No response from $service for method $method with params @params";
151 if(UNIVERSAL::isa($response,"Error")) {
152 throw $response ("Call to $service for method $method with params @params" .
153 "\n failed with exception: " . $response->stringify );
159 $session->disconnect();
163 if($response) { $value = $response->content; }
164 else { $value = undef; }
175 my $org_typelist = undef;
176 my $org_typelist_hash = {};
181 if($tree) { return $tree; }
183 # see if it's in the cache
184 $tree = $cache_client->new()->get_cache('_orgtree');
185 if($tree) { return $tree; }
188 warn "Retrieving Org Tree\n";
189 $orglist = $self->simple_scalar_request(
191 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
194 if( ! $org_typelist ) {
195 warn "Retrieving org types\n";
196 $org_typelist = $self->simple_scalar_request(
198 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
199 $self->build_org_type($org_typelist);
202 $tree = $self->build_org_tree($orglist,1);
203 $cache_client->new()->put_cache('_orgtree', $tree);
208 my $slimtree = undef;
209 sub get_slim_org_tree {
212 if($slimtree) { return $slimtree; }
214 # see if it's in the cache
215 $slimtree = $cache_client->new()->get_cache('slimorgtree');
216 if($slimtree) { return $slimtree; }
219 warn "Retrieving Org Tree\n";
220 $orglist = $self->simple_scalar_request(
222 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
225 $slimtree = $self->build_org_tree($orglist);
226 $cache_client->new->put_cache('slimorgtree', $slimtree);
233 my($self, $org_typelist) = @_;
234 for my $type (@$org_typelist) {
235 $org_typelist_hash->{$type->id()} = $type;
243 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 );
275 sub start_long_request {
277 my($self, $service, $method, @params) = @_;
279 my $session = OpenSRF::AppSession->create( $service );
280 my $request = $session->request( $method, @params );
281 my $response = $request->recv(30);
283 $request->wait_complete;
285 if(!$request->complete) {
286 throw $response ("Call to $service for method $method with params @params" .
287 "\n did not complete successfully");
291 warn "No response from $service for method $method with params @params";
294 if($response and UNIVERSAL::isa($response,"Error")) {
295 throw $response ("Call to $service for method $method with params @params" .
296 "\n failed with exception: " . $response->stringify );
303 if($response) { $value = $response->content; }
304 else { $value = undef; }
306 return [ $session, $value ];
311 my($self, $session, $service, $method, @params) = @_;
313 my $request = $session->request( $method, @params );
314 my $response = $request->recv(30);
316 $request->wait_complete;
318 if(!$request->complete) {
319 throw $response ("Call to $service for method $method with params @params" .
320 "\n did not complete successfully");
324 warn "No response from $service for method $method with params @params";
327 if($response and UNIVERSAL::isa($response,"Error")) {
328 throw $response ("Call to $service for method $method with params @params" .
329 "\n failed with exception: " . $response->stringify );
336 if($response) { $value = $response->content; }
337 else { $value = undef; }
342 sub finish_long_request {
343 my($self, $session) = @_;
346 $session->disconnect();