1 package org.apache.fulcrum.security.torque;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import java.sql.Connection;
21 import java.util.List;
22
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.fulcrum.security.entity.Group;
26 import org.apache.fulcrum.security.spi.AbstractGroupManager;
27 import org.apache.fulcrum.security.torque.security.TorqueAbstractSecurityEntity;
28 import org.apache.fulcrum.security.util.DataBackendException;
29 import org.apache.fulcrum.security.util.EntityExistsException;
30 import org.apache.fulcrum.security.util.GroupSet;
31 import org.apache.fulcrum.security.util.UnknownEntityException;
32 import org.apache.torque.NoRowsException;
33 import org.apache.torque.TooManyRowsException;
34 import org.apache.torque.TorqueException;
35 import org.apache.torque.util.Transaction;
36
37
38
39
40
41
42
43 public abstract class TorqueAbstractGroupManager extends AbstractGroupManager implements LazyLoadable
44 {
45
46
47 private static final long serialVersionUID = -3735730556110100621L;
48
49 private static final String LAZY_LOADING = "lazy";
50 protected Boolean lazyLoading = false;
51
52
53
54
55 @Override
56 public void configure(Configuration conf) throws ConfigurationException
57 {
58 super.configure( conf );
59 lazyLoading = conf.getAttributeAsBoolean( LAZY_LOADING, false);
60 getLogger().debug("setting lazyLoading: " + lazyLoading);
61 }
62
63
64
65
66
67
68
69
70
71
72 protected abstract <T extends Group> List<T> doSelectAllGroups(Connection con)
73 throws TorqueException;
74
75
76
77
78
79
80
81
82
83
84
85
86
87 protected abstract <T extends Group> T doSelectByName(String name, Connection con)
88 throws NoRowsException, TooManyRowsException, TorqueException;
89
90
91
92
93
94
95
96
97
98
99
100
101
102 protected abstract <T extends Group> T doSelectById(Integer id, Connection con)
103 throws NoRowsException, TooManyRowsException, TorqueException;
104
105
106
107
108
109
110
111
112
113 @Override
114 protected synchronized <T extends Group> T persistNewGroup(T group) throws DataBackendException
115 {
116 try
117 {
118
119 ((TorqueAbstractSecurityEntity)group).save();
120 }
121 catch (Exception e)
122 {
123 throw new DataBackendException("Adding Group '" + group.getName() + "' failed", e);
124 }
125
126 return group;
127 }
128
129
130
131
132
133
134
135
136
137
138 @Override
139 public synchronized void renameGroup(Group group, String name) throws DataBackendException, UnknownEntityException
140 {
141 if (checkExists(group))
142 {
143 group.setName(name);
144
145 try
146 {
147 TorqueAbstractSecurityEntityg/apache/fulcrum/security/torque/security/TorqueAbstractSecurityEntity.html#TorqueAbstractSecurityEntity">TorqueAbstractSecurityEntity g = (TorqueAbstractSecurityEntity)group;
148 g.setNew(false);
149 g.save();
150 }
151 catch (Exception e)
152 {
153 throw new DataBackendException("Renaming Group '" + group.getName() + "' failed", e);
154 }
155 }
156 else
157 {
158 throw new UnknownEntityException("Unknown group '" + group.getName() + "'");
159 }
160 }
161
162
163
164
165
166
167
168
169
170 @Override
171 public synchronized void removeGroup(Group group) throws DataBackendException, UnknownEntityException
172 {
173 try
174 {
175 ((TorqueAbstractSecurityEntity)group).delete();
176 }
177 catch (TorqueException e)
178 {
179 throw new DataBackendException("Removing Group '" + group.getName() + "' failed", e);
180 }
181 }
182
183
184
185
186
187
188
189
190
191
192 @Override
193 public <T extends Group> T getGroupByName(String name) throws DataBackendException, UnknownEntityException
194 {
195 T group = null;
196 Connection con = null;
197
198 try
199 {
200 con = Transaction.begin();
201
202 group = doSelectByName(name, con);
203
204
205 ((TorqueAbstractSecurityEntity)group).retrieveAttachedObjects(con, getLazyLoading());
206
207 Transaction.commit(con);
208 con = null;
209 }
210 catch (NoRowsException e)
211 {
212 throw new UnknownEntityException("Could not find group " + name);
213 }
214 catch (TooManyRowsException e)
215 {
216 throw new DataBackendException("Multiple Groups with same name '" + name + "'");
217 }
218 catch (TorqueException e)
219 {
220 throw new DataBackendException("Error retrieving group information", e);
221 }
222 finally
223 {
224 if (con != null)
225 {
226 Transaction.safeRollback(con);
227 }
228 }
229
230 return group;
231 }
232
233
234
235
236
237
238
239
240 @Override
241 public GroupSet getAllGroups() throws DataBackendException
242 {
243 GroupSet groupSet = new GroupSet();
244 Connection con = null;
245
246 try
247 {
248
249 con = Transaction.begin();
250
251 List<Group> groups = doSelectAllGroups(con);
252
253 for (Group group : groups)
254 {
255
256 ((TorqueAbstractSecurityEntity)group).retrieveAttachedObjects(con, getLazyLoading());
257
258 groupSet.add(group);
259 }
260
261 Transaction.commit(con);
262 con = null;
263 }
264 catch (TorqueException e)
265 {
266 throw new DataBackendException("Error retrieving group information", e);
267 }
268 finally
269 {
270 if (con != null)
271 {
272 Transaction.safeRollback(con);
273 }
274 }
275
276 return groupSet;
277 }
278
279
280
281
282
283
284
285
286
287 @Override
288 public boolean checkExists(String groupName) throws DataBackendException
289 {
290 boolean exists = false;
291
292 Connection con = null;
293
294 try
295 {
296 con = Transaction.begin();
297
298 doSelectByName(groupName, con);
299
300 Transaction.commit(con);
301 con = null;
302
303 exists = true;
304 }
305 catch (NoRowsException e)
306 {
307 exists = false;
308 }
309 catch (TooManyRowsException e)
310 {
311 throw new DataBackendException(
312 "Multiple groups with same name '" + groupName + "'");
313 }
314 catch (TorqueException e)
315 {
316 throw new DataBackendException("Error retrieving group information", e);
317 }
318 finally
319 {
320 if (con != null)
321 {
322 Transaction.safeRollback(con);
323 }
324 }
325
326 return exists;
327 }
328
329
330
331
332
333
334
335
336
337
338
339
340 @Override
341 public <T extends Group> T getGroupById(Object id) throws DataBackendException, UnknownEntityException
342 {
343 T group;
344
345 if (id != null && id instanceof Integer)
346 {
347 Connection con = null;
348
349 try
350 {
351 con = Transaction.begin();
352
353 group = doSelectById((Integer)id, con);
354
355
356 ((TorqueAbstractSecurityEntity)group).retrieveAttachedObjects(con, getLazyLoading());
357
358 Transaction.commit(con);
359 con = null;
360 }
361 catch (NoRowsException e)
362 {
363 throw new UnknownEntityException("Group with id '" + id + "' does not exist.", e);
364 }
365 catch (TorqueException e)
366 {
367 throw new DataBackendException("Error retrieving group information", e);
368 }
369 finally
370 {
371 if (con != null)
372 {
373 Transaction.safeRollback(con);
374 }
375 }
376 }
377 else
378 {
379 throw new UnknownEntityException("Invalid group id '" + id + "'");
380 }
381
382 return group;
383 }
384
385 public Boolean getLazyLoading()
386 {
387 return lazyLoading;
388 }
389
390 public void setLazyLoading( Boolean lazyLoading )
391 {
392 this.lazyLoading = lazyLoading;
393 }
394 }