1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
4 use OpenSRF::Utils::Cache;
7 my $cache_client = OpenSRF::Utils::Cache->new( "global", 0 );
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" );
34 # ---------------------------------------------------------------------------
35 # commits and destroys the session
36 # ---------------------------------------------------------------------------
37 sub commit_db_session {
38 my( $self, $session ) = @_;
40 my $req = $session->request( "open-ils.storage.transaction.commit" );
41 my $resp = $req->recv();
44 throw OpenSRF::EX::ERROR ("Unable to commit db session");
47 if(UNIVERSAL::isa($resp,"Error")) {
48 throw $resp ($resp->stringify);
52 throw OpenSRF::EX::ERROR ("Unable to commit db session");
56 $session->disconnect();
60 sub rollback_db_session {
61 my( $self, $session ) = @_;
63 my $req = $session->request("open-ils.storage.transaction.rollback");
64 my $resp = $req->recv();
65 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
68 $session->disconnect();
72 # ---------------------------------------------------------------------------
73 # Checks to see if a user is logged in. Returns the user record on success,
74 # throws an exception on error.
75 # ---------------------------------------------------------------------------
76 sub check_user_session {
78 my( $self, $user_session ) = @_;
80 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
81 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
82 my $response = $request->recv();
85 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
88 if($response->isa("OpenSRF::EX")) {
89 throw $response ($response->stringify);
92 my $user = $response->content;
94 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
97 $session->disconnect();
105 # generic simple request returning a scalar value
106 sub simple_scalar_request {
107 my($self, $service, $method, @params) = @_;
109 my $session = OpenSRF::AppSession->create( $service );
110 my $request = $session->request( $method, @params );
111 my $response = $request->recv(30);
113 $request->wait_complete;
115 if(!$request->complete) {
116 throw $response ("Call to $service for method $method with params @params" .
117 "\n did not complete successfully");
121 warn "No response from $service for method $method with params @params";
124 if(UNIVERSAL::isa($response,"Error")) {
125 throw $response ("Call to $service for method $method with params @params" .
126 "\n failed with exception: " . $response->stringify );
132 $session->disconnect();
136 if($response) { $value = $response->content; }
137 else { $value = undef; }
148 my $org_typelist = undef;
149 my $org_typelist_hash = {};
154 if($tree) { return $tree; }
156 # see if it's in the cache
157 $tree = $cache_client->get_cache('orgtree');
158 if($tree) { return $tree; }
161 warn "Retrieving Org Tree\n";
162 $orglist = $self->simple_scalar_request(
164 "open-ils.storage.direct.actor.org_unit.retrieve.all" );
167 if( ! $org_typelist ) {
168 warn "Retrieving org types\n";
169 $org_typelist = $self->simple_scalar_request(
171 "open-ils.storage.direct.actor.org_unit_type.retrieve.all" );
172 $self->build_org_type($org_typelist);
175 $tree = $self->build_org_tree($orglist,1);
176 $cache_client->put_cache('orgtree', $tree);
181 my $slimtree = undef;
182 sub get_slim_org_tree {
185 if($slimtree) { return $slimtree; }
187 # see if it's in the cache
188 $slimtree = $cache_client->get_cache('slimorgtree');
189 if($slimtree) { return $slimtree; }
192 warn "Retrieving Org Tree\n";
193 $orglist = $self->simple_scalar_request(
195 "open-ils.storage.direct.actor.org_unit.retrieve.all" );
198 $slimtree = $self->build_org_tree($orglist);
199 $cache_client->put_cache('slimorgtree', $slimtree);
206 my($self, $org_typelist) = @_;
207 for my $type (@$org_typelist) {
208 $org_typelist_hash->{$type->id()} = $type;
216 my( $self, $orglist, $add_types ) = @_;
220 return $orglist unless (
221 ref($orglist) and @$orglist > 1 );
224 $a->ou_type <=> $b->ou_type ||
225 $a->name cmp $b->name } @$orglist;
227 for my $org (@list) {
231 if(!ref($org->ou_type()) and $add_types) {
232 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
235 next unless (defined($org->parent_ou));
237 my ($parent) = grep { $_->id == $org->parent_ou } @list;
239 $parent->children([]) unless defined($parent->children);
240 push( @{$parent->children}, $org );
248 sub start_long_request {
250 my($self, $service, $method, @params) = @_;
252 my $session = OpenSRF::AppSession->create( $service );
253 my $request = $session->request( $method, @params );
254 my $response = $request->recv(30);
256 $request->wait_complete;
258 if(!$request->complete) {
259 throw $response ("Call to $service for method $method with params @params" .
260 "\n did not complete successfully");
264 warn "No response from $service for method $method with params @params";
267 if($response and UNIVERSAL::isa($response,"Error")) {
268 throw $response ("Call to $service for method $method with params @params" .
269 "\n failed with exception: " . $response->stringify );
276 if($response) { $value = $response->content; }
277 else { $value = undef; }
279 return [ $session, $value ];
284 my($self, $session, $service, $method, @params) = @_;
286 my $request = $session->request( $method, @params );
287 my $response = $request->recv(30);
289 $request->wait_complete;
291 if(!$request->complete) {
292 throw $response ("Call to $service for method $method with params @params" .
293 "\n did not complete successfully");
297 warn "No response from $service for method $method with params @params";
300 if($response and UNIVERSAL::isa($response,"Error")) {
301 throw $response ("Call to $service for method $method with params @params" .
302 "\n failed with exception: " . $response->stringify );
309 if($response) { $value = $response->content; }
310 else { $value = undef; }
315 sub finish_long_request {
316 my($self, $session) = @_;
319 $session->disconnect();