1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
6 # ---------------------------------------------------------------------------
7 # Pile of utilty methods used accross applications.
8 # ---------------------------------------------------------------------------
11 # ---------------------------------------------------------------------------
12 # on sucess, returns the created session, on failure throws ERROR exception
13 # ---------------------------------------------------------------------------
14 sub start_db_session {
17 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
18 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
20 my $trans_resp = $trans_req->recv();
21 if(ref($trans_resp) and $trans_resp->isa("Error")) { throw $trans_resp; }
22 if( ! $trans_resp->content() ) {
24 ("Unable to Begin Transaction with database" );
30 # ---------------------------------------------------------------------------
31 # commits and destroys the session
32 # ---------------------------------------------------------------------------
33 sub commit_db_session {
34 my( $self, $session ) = @_;
36 my $req = $session->request( "open-ils.storage.transaction.commit" );
37 my $resp = $req->recv();
40 throw OpenSRF::EX::ERROR ("Unable to commit db session");
43 if(ref($resp) and $resp->isa("Error")) {
44 throw $resp ($resp->stringify);
48 throw OpenSRF::EX::ERROR ("Unable to commit db session");
52 $session->disconnect();
56 sub rollback_db_session {
57 my( $self, $session ) = @_;
59 my $req = $session->request("open-ils.storage.transaction.rollback");
60 my $resp = $req->recv();
61 if(ref($resp) and $resp->isa("Error")) { throw $resp; }
64 $session->disconnect();
68 # ---------------------------------------------------------------------------
69 # Checks to see if a user is logged in. Returns the user record on success,
70 # throws an exception on error.
71 # ---------------------------------------------------------------------------
72 sub check_user_session {
74 my( $self, $user_session ) = @_;
76 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
77 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
78 my $response = $request->recv();
81 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
84 if($response->isa("OpenSRF::EX")) {
85 throw $response ($response->stringify);
88 my $user = $response->content;
90 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
93 $session->disconnect();
101 # generic simple request returning a scalar value
102 sub simple_scalar_request {
103 my($self, $service, $method, @params) = @_;
105 my $session = OpenSRF::AppSession->create( $service );
106 my $request = $session->request( $method, @params );
107 my $response = $request->recv();
110 throw OpenSRF::EX::ERROR
111 ("No response from $service for method $method with params @params" );
114 if($response->isa("Error")) {
115 throw $response ("Call to $service for method $method with params @params" .
116 "\n failed with exception: " . $response->stringify );
119 my $value = $response->content;
122 $session->disconnect();
133 my $org_typelist = undef;
134 my $org_typelist_hash = {};
141 $orglist = $self->simple_scalar_request(
142 "open-ils.storage", "open-ils.storage.direct.actor.org_unit.retrieve.all" );
145 if( ! $org_typelist ) {
146 $org_typelist = $self->simple_scalar_request(
147 "open-ils.storage", "open-ils.storage.direct.actor.org_unit_type.retrieve.all" );
148 $self->build_org_type( $org_typelist );
151 return $self->build_org_tree($orglist, $org_typelist);
156 my($self, $org_typelist) = @_;
157 for my $type (@$org_typelist) {
158 $org_typelist_hash->{$type->id()} = $type;
166 my( $self, $orglist, $org_typelist ) = @_;
170 return $orglist unless (
171 ref($orglist) and @$orglist > 1 );
174 $a->ou_type <=> $b->ou_type ||
175 $a->name cmp $b->name } @$orglist;
177 for my $org (@list) {
178 next unless ($org and defined($org->parent_ou));
180 if(!ref($org->ou_type())) {
181 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
184 my ($parent) = grep { $_->id == $org->parent_ou } @list;
186 $parent->children([]) unless defined($parent->children);
187 push( @{$parent->children}, $org );