]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
"grocery" catchall billing
[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 use OpenSRF::Utils::Cache;
5 use OpenSRF::EX qw(:try);
6
7
8 my $cache_client = "OpenSRF::Utils::Cache";
9
10 # ---------------------------------------------------------------------------
11 # Pile of utilty methods used accross applications.
12 # ---------------------------------------------------------------------------
13
14
15 # ---------------------------------------------------------------------------
16 # on sucess, returns the created session, on failure throws ERROR exception
17 # ---------------------------------------------------------------------------
18 sub start_db_session {
19
20         my $self = shift;
21         my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
22         my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
23
24         my $trans_resp = $trans_req->recv();
25         if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
26         if( ! $trans_resp->content() ) {
27                 throw OpenSRF::ERROR 
28                         ("Unable to Begin Transaction with database" );
29         }
30         $trans_req->finish();
31         return $session;
32 }
33
34
35 # returns undef if user has all of the perms provided
36 # returns the first failed perm on failure
37 sub check_user_perms {
38         my($self, $user_id, $org_id, @perm_types ) = @_;
39
40         warn "Checking perm with user : $user_id , org: $org_id, @perm_types\n";
41
42         throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
43                 unless( defined($user_id) and defined($org_id) and @perm_types); 
44
45         my $session = OpenSRF::AppSession->create("open-ils.storage");
46         for my $type (@perm_types) {
47                 my $req = $session->request(
48                         "open-ils.storage.permission.user_has_perm", 
49                         $user_id, $type, $org_id );
50                 my $resp = $req->gather(1);
51                 if(!$resp) { 
52                         $session->disconnect();
53                         return $type; 
54                 }
55         }
56
57         $session->disconnect();
58         return undef;
59 }
60
61
62
63 # ---------------------------------------------------------------------------
64 # commits and destroys the session
65 # ---------------------------------------------------------------------------
66 sub commit_db_session {
67         my( $self, $session ) = @_;
68
69         my $req = $session->request( "open-ils.storage.transaction.commit" );
70         my $resp = $req->recv();
71
72         if(!$resp) {
73                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
74         }
75
76         if(UNIVERSAL::isa($resp,"Error")) { 
77                 throw $resp ($resp->stringify); 
78         }
79
80         if(!$resp->content) {
81                 throw OpenSRF::EX::ERROR ("Unable to commit db session");
82         }
83
84         $session->finish();
85         $session->disconnect();
86         $session->kill_me();
87 }
88
89 sub rollback_db_session {
90         my( $self, $session ) = @_;
91
92         my $req = $session->request("open-ils.storage.transaction.rollback");
93         my $resp = $req->recv();
94         if(UNIVERSAL::isa($resp,"Error")) { throw $resp;  }
95
96         $session->finish();
97         $session->disconnect();
98         $session->kill_me();
99 }
100
101 # ---------------------------------------------------------------------------
102 # Checks to see if a user is logged in.  Returns the user record on success,
103 # throws an exception on error.
104 # ---------------------------------------------------------------------------
105 sub check_user_session {
106
107         my( $self, $user_session ) = @_;
108
109         my $session = OpenSRF::AppSession->create( "open-ils.auth" );
110         my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
111         my $response = $request->recv();
112
113         if(!$response) {
114                 throw OpenSRF::EX::User ("Session [$user_session] cannot be authenticated" );
115         }
116
117         if($response->isa("OpenSRF::EX")) {
118                 throw $response ($response->stringify);
119         }
120
121         my $user = $response->content;
122         if(!$user) {
123                 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
124         }
125
126         $session->disconnect();
127         $session->kill_me();
128
129         return $user;
130
131         
132 }
133
134 # generic simple request returning a scalar value
135 sub simple_scalar_request {
136         my($self, $service, $method, @params) = @_;
137
138         my $session = OpenSRF::AppSession->create( $service );
139         my $request = $session->request( $method, @params );
140         my $response = $request->recv(30);
141
142         $request->wait_complete;
143
144         if(!$request->complete) {
145                 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" . 
146                                 "\n did not complete successfully");
147         }
148
149         if(!$response) {
150                 warn "No response from $service for method $method with params @params";
151         }
152
153         if(UNIVERSAL::isa($response,"Error")) {
154                 throw $response ("Call to $service for method $method with params @params" . 
155                                 "\n failed with exception: " . $response->stringify );
156         }
157
158
159         $request->finish();
160         $session->finish();
161         $session->disconnect();
162
163         my $value;
164
165         if($response) { $value = $response->content; }
166         else { $value = undef; }
167
168         return $value;
169 }
170
171
172
173
174
175 my $tree                                                = undef;
176 my $orglist                                     = undef;
177 my $org_typelist                        = undef;
178 my $org_typelist_hash   = {};
179
180 sub get_org_tree {
181
182         my $self = shift;
183         if($tree) { return $tree; }
184
185         # see if it's in the cache
186         $tree = $cache_client->new()->get_cache('_orgtree');
187         if($tree) { return $tree; }
188
189         if(!$orglist) {
190                 warn "Retrieving Org Tree\n";
191                 $orglist = $self->simple_scalar_request( 
192                         "open-ils.storage", 
193                         "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
194         }
195
196         if( ! $org_typelist ) {
197                 warn "Retrieving org types\n";
198                 $org_typelist = $self->simple_scalar_request( 
199                         "open-ils.storage", 
200                         "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
201                 $self->build_org_type($org_typelist);
202         }
203
204         $tree = $self->build_org_tree($orglist,1);
205         $cache_client->new()->put_cache('_orgtree', $tree);
206         return $tree;
207
208 }
209
210 my $slimtree = undef;
211 sub get_slim_org_tree {
212
213         my $self = shift;
214         if($slimtree) { return $slimtree; }
215
216         # see if it's in the cache
217         $slimtree = $cache_client->new()->get_cache('slimorgtree');
218         if($slimtree) { return $slimtree; }
219
220         if(!$orglist) {
221                 warn "Retrieving Org Tree\n";
222                 $orglist = $self->simple_scalar_request( 
223                         "open-ils.storage", 
224                         "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
225         }
226
227         $slimtree = $self->build_org_tree($orglist);
228         $cache_client->new->put_cache('slimorgtree', $slimtree);
229         return $slimtree;
230
231 }
232
233
234 sub build_org_type { 
235         my($self, $org_typelist)  = @_;
236         for my $type (@$org_typelist) {
237                 $org_typelist_hash->{$type->id()} = $type;
238         }
239 }
240
241
242
243 sub build_org_tree {
244
245         my( $self, $orglist, $add_types ) = @_;
246
247         return $orglist unless ( 
248                         ref($orglist) and @$orglist > 1 );
249
250         my @list = sort { 
251                 $a->ou_type <=> $b->ou_type ||
252                 $a->name cmp $b->name } @$orglist;
253
254         for my $org (@list) {
255
256                 next unless ($org);
257
258                 if(!ref($org->ou_type()) and $add_types) {
259                         $org->ou_type( $org_typelist_hash->{$org->ou_type()});
260                 }
261
262                 next unless (defined($org->parent_ou));
263
264                 my ($parent) = grep { $_->id == $org->parent_ou } @list;
265                 next unless $parent;
266                 $parent->children([]) unless defined($parent->children); 
267                 push( @{$parent->children}, $org );
268         }
269
270         return $list[0];
271
272 }
273
274
275
276
277
278
279
280
281 1;