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