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