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();
136 my $orglist = $self->simple_scalar_request(
137 "open-ils.storage", "open-ils.storage.actor.org_unit_list" );
139 return $self->build_org_tree($orglist);
146 my( $self, $orglist ) = @_;
148 return $orglist unless (
149 ref($orglist) and @$orglist > 1 );
151 my @list = sort { $a->ou_type <=> $b->ou_type } @$orglist;
153 for my $org (@list) {
154 next unless ($org and defined($org->parent_ou));
155 my ($parent) = grep { $_->id == $org->parent_ou } @list;
157 $parent->children([]) unless defined($parent->children);
158 push( @{$parent->children}, $org );
161 return $self->tree_child_sorter( $list[0] );
165 sub tree_child_sorter {
166 my($self, $tree) = @_;
168 return $tree unless ($tree and $tree->children);
171 [ sort { $a->name cmp $b->name } @{$tree->children} ] );
173 for my $child (@{$tree->children}) {
174 $self->tree_child_sorter( $child );