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();
132 my $orglist = $self->simple_scalar_request(
133 "open-ils.storage", "open-ils.storage.actor.org_unit_list" );
136 $orglist = [ sort { $a->id <=> $b->id } @$orglist ];
138 for my $org (@$orglist) {
139 next unless ($org and defined($org->parent_ou));
140 my $parent = $orglist->[$org->parent_ou - 1];
142 $parent->children([]) unless defined($parent->children);
143 push( @{$parent->children}, $org );
146 return $self->tree_child_sorter( $orglist->[0] );
149 sub tree_child_sorter {
150 my($self, $tree) = @_;
152 return unless $tree->children;
155 [ sort { $a->name cmp $b->name } @{$tree->children} ] );
157 for my $child (@{$tree->children}) {
158 $self->tree_child_sorter( $child );