]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
refactored sorting
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / AppUtils.pm
1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
4
5
6 # ---------------------------------------------------------------------------
7 # Pile of utilty methods used accross applications.
8 # ---------------------------------------------------------------------------
9
10
11 # ---------------------------------------------------------------------------
12 # on sucess, returns the created session, on failure throws ERROR exception
13 # ---------------------------------------------------------------------------
14 sub start_db_session {
15
16         my $self = shift;
17         my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
18         my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
19
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() ) {
23                 throw OpenSRF::ERROR 
24                         ("Unable to Begin Transaction with database" );
25         }
26         $trans_req->finish();
27         return $session;
28 }
29
30 # ---------------------------------------------------------------------------
31 # commits and destroys the session
32 # ---------------------------------------------------------------------------
33 sub commit_db_session {
34         my( $self, $session ) = @_;
35
36         my $req = $session->request( "open-ils.storage.transaction.commit" );
37         my $resp = $req->recv();
38
39         if(!$resp) {
40                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
41         }
42
43         if(ref($resp) and $resp->isa("Error")) { 
44                 throw $resp ($resp->stringify); 
45         }
46
47         if(!$resp->content) {
48                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
49         }
50
51         $session->finish();
52         $session->disconnect();
53         $session->kill_me();
54 }
55
56 sub rollback_db_session {
57         my( $self, $session ) = @_;
58
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; }
62
63         $session->finish();
64         $session->disconnect();
65         $session->kill_me();
66 }
67
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 {
73
74         my( $self, $user_session ) = @_;
75
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();
79
80         if(!$response) {
81                 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
82         }
83
84         if($response->isa("OpenSRF::EX")) {
85                 throw $response ($response->stringify);
86         }
87
88         my $user = $response->content;
89         if(!$user) {
90                 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
91         }
92
93         $session->disconnect();
94         $session->kill_me();
95
96         return $user;
97
98         
99 }
100
101 # generic simple request returning a scalar value
102 sub simple_scalar_request {
103         my($self, $service, $method, @params) = @_;
104
105         my $session = OpenSRF::AppSession->create( $service );
106         my $request = $session->request( $method, @params );
107         my $response = $request->recv();
108
109         if(!$response) {
110                 throw OpenSRF::EX::ERROR 
111                         ("No response from $service for method $method with params @params" );
112         }
113
114         if($response->isa("Error")) {
115                 throw $response ("Call to $service for method $method with params @params" . 
116                                 "\n failed with exception: " . $response->stringify );
117         }
118
119         my $value = $response->content;
120
121         $request->finish();
122         $session->disconnect();
123         $session->kill_me();
124
125         return $value;
126 }
127
128
129
130
131
132 sub get_org_tree {
133
134         my $self = shift;
135
136         my $orglist = $self->simple_scalar_request( 
137                         "open-ils.storage", "open-ils.storage.actor.org_unit_list" );
138
139         return $self->build_org_tree($orglist);
140
141 }
142
143
144 sub build_org_tree {
145
146         my( $self, $orglist ) = @_;
147
148         return $orglist unless ( 
149                         ref($orglist) and @$orglist > 1 );
150
151         my @list = sort { 
152                 $a->ou_type <=> $b->ou_type ||
153                 $a->name cmp $b->name } @$orglist;
154
155         for my $org (@list) {
156                 next unless ($org and defined($org->parent_ou));
157                 my ($parent) = grep { $_->id == $org->parent_ou } @list;
158                 next unless $parent;
159                 $parent->children([]) unless defined($parent->children); 
160                 push( @{$parent->children}, $org );
161         }
162
163         return $list[0];
164
165 }
166
167         
168
169
170 1;