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 );
9 # ---------------------------------------------------------------------------
10 # Pile of utilty methods used accross applications.
11 # ---------------------------------------------------------------------------
14 # ---------------------------------------------------------------------------
15 # on sucess, returns the created session, on failure throws ERROR exception
16 # ---------------------------------------------------------------------------
17 sub start_db_session {
20 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
21 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
23 my $trans_resp = $trans_req->recv();
24 if(ref($trans_resp) and $trans_resp->isa("Error")) { throw $trans_resp; }
25 if( ! $trans_resp->content() ) {
27 ("Unable to Begin Transaction with database" );
33 # ---------------------------------------------------------------------------
34 # commits and destroys the session
35 # ---------------------------------------------------------------------------
36 sub commit_db_session {
37 my( $self, $session ) = @_;
39 my $req = $session->request( "open-ils.storage.transaction.commit" );
40 my $resp = $req->recv();
43 throw OpenSRF::EX::ERROR ("Unable to commit db session");
46 if(ref($resp) and $resp->isa("Error")) {
47 throw $resp ($resp->stringify);
51 throw OpenSRF::EX::ERROR ("Unable to commit db session");
55 $session->disconnect();
59 sub rollback_db_session {
60 my( $self, $session ) = @_;
62 my $req = $session->request("open-ils.storage.transaction.rollback");
63 my $resp = $req->recv();
64 if(ref($resp) and $resp->isa("Error")) { throw $resp; }
67 $session->disconnect();
71 # ---------------------------------------------------------------------------
72 # Checks to see if a user is logged in. Returns the user record on success,
73 # throws an exception on error.
74 # ---------------------------------------------------------------------------
75 sub check_user_session {
77 my( $self, $user_session ) = @_;
79 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
80 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
81 my $response = $request->recv();
84 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
87 if($response->isa("OpenSRF::EX")) {
88 throw $response ($response->stringify);
91 my $user = $response->content;
93 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
96 $session->disconnect();
104 # generic simple request returning a scalar value
105 sub simple_scalar_request {
106 my($self, $service, $method, @params) = @_;
108 my $session = OpenSRF::AppSession->create( $service );
109 my $request = $session->request( $method, @params );
110 my $response = $request->recv(30);
112 $request->wait_complete;
114 if(!$request->complete) {
115 throw $response ("Call to $service for method $method with params @params" .
116 "\n did not complete successfully");
120 warn "No response from $service for method $method with params @params";
123 if($response and $response->isa("Error")) {
124 throw $response ("Call to $service for method $method with params @params" .
125 "\n failed with exception: " . $response->stringify );
131 $session->disconnect();
135 if($response) { $value = $response->content; }
136 else { $value = undef; }
147 my $org_typelist = undef;
148 my $org_typelist_hash = {};
153 if($tree) { return $tree; }
155 # see if it's in the cache
156 $tree = $cache_client->get_cache('orgtree');
157 if($tree) { return $tree; }
160 warn "Retrieving Org Tree\n";
161 $orglist = $self->simple_scalar_request(
163 "open-ils.storage.direct.actor.org_unit.retrieve.all" );
166 if( ! $org_typelist ) {
167 warn "Retrieving org types\n";
168 $org_typelist = $self->simple_scalar_request(
170 "open-ils.storage.direct.actor.org_unit_type.retrieve.all" );
171 $self->build_org_type( $org_typelist );
174 $tree = $self->build_org_tree($orglist, $org_typelist);
175 $cache_client->put_cache($tree);
181 my($self, $org_typelist) = @_;
182 for my $type (@$org_typelist) {
183 $org_typelist_hash->{$type->id()} = $type;
191 my( $self, $orglist, $org_typelist ) = @_;
195 return $orglist unless (
196 ref($orglist) and @$orglist > 1 );
199 $a->ou_type <=> $b->ou_type ||
200 $a->name cmp $b->name } @$orglist;
202 for my $org (@list) {
206 if(!ref($org->ou_type())) {
207 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
210 next unless (defined($org->parent_ou));
212 my ($parent) = grep { $_->id == $org->parent_ou } @list;
214 $parent->children([]) unless defined($parent->children);
215 push( @{$parent->children}, $org );