KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > impl > DbGroup


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.impl;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.nemesis.forum.Authorization;
37 import org.nemesis.forum.ForumPermissions;
38 import org.nemesis.forum.Group;
39 import org.nemesis.forum.ProfileManager;
40 import org.nemesis.forum.User;
41 import org.nemesis.forum.exception.GroupNotFoundException;
42 import org.nemesis.forum.exception.UnauthorizedException;
43 import org.nemesis.forum.exception.UserNotFoundException;
44 import org.nemesis.forum.util.cache.CacheSizes;
45 import org.nemesis.forum.util.cache.Cacheable;
46 import org.nemesis.forum.util.jdbc.DbConnectionManager;
47 /**
48  * Database implementation of the Group interface.
49  *
50  * @see Group
51  */

52 public class DbGroup implements Group, Cacheable {
53     static protected Log log = LogFactory.getLog(DbGroup.class);
54     /** DATABASE QUERIES **/
55     private static final String JavaDoc ADD_ADMIN = "INSERT INTO yazdGroupUser(groupID,userID,administrator) VALUES(?,?,1)";
56     private static final String JavaDoc REMOVE_ADMIN = "DELETE FROM yazdGroupUser WHERE groupID=? AND userID=? AND administrator=1";
57     private static final String JavaDoc ADD_USER = "INSERT INTO yazdGroupUser(groupID,userID,administrator) VALUES(?,?,0)";
58     private static final String JavaDoc REMOVE_USER = "DELETE FROM yazdGroupUser WHERE groupID=? AND userID=? AND administrator=0";
59     private static final String JavaDoc ADMIN_TEST = "SELECT userID FROM yazdGroupUser WHERE groupID=? AND userID=? AND " + "administrator=1";
60     private static final String JavaDoc MEMBER_TEST = "SELECT userID FROM yazdGroupUser WHERE groupID=? AND userID=?";
61     private static final String JavaDoc ADMIN_COUNT = "SELECT count(*) FROM yazdGroupUser WHERE groupID=? " + "AND administrator=1";
62     private static final String JavaDoc MEMBER_COUNT = "SELECT DISTINCT count(userID) FROM yazdGroupUser " + "WHERE groupID=?";
63     private static final String JavaDoc LOAD_ADMINS = "SELECT userID FROM yazdGroupUser WHERE administrator=1 AND groupID=?";
64     private static final String JavaDoc LOAD_USERS = "SELECT userID FROM yazdGroupUser WHERE groupID=?";
65     private static final String JavaDoc LOAD_GROUP_BY_ID = "SELECT * FROM yazdGroup WHERE groupID=?";
66     private static final String JavaDoc LOAD_GROUP_BY_NAME = "SELECT * FROM yazdGroup WHERE name=?";
67     private static final String JavaDoc INSERT_GROUP = "INSERT INTO yazdGroup(name,description,groupID) VALUES(?,?,?)";
68     private static final String JavaDoc SAVE_GROUP = "UPDATE yazdGroup SET name=?, description=? WHERE groupID=?";
69
70     private int id;
71     private String JavaDoc name = null;
72     private String JavaDoc description = "";
73
74     private ProfileManager profileManager;
75     private DbForumFactory factory;
76
77     /**
78      * Creates a new group.
79      *
80      * @param the name of the group.
81      * @param profileManager a ProfileManager that can be used to perform user
82      * and group operations.
83      */

84     protected DbGroup(String JavaDoc name, DbForumFactory factory) {
85         this.name = name;
86         this.factory = factory;
87         this.profileManager = factory.getProfileManager();
88         this.id = DbSequenceManager.nextID("Group");
89         insertIntoDb();
90     }
91
92     /**
93      * Loads a group from the database based on its id.
94      *
95      * @param id the id of the group to load.
96      * @param profileManager a ProfileManager that can be used to perform user
97      * and group operations.
98      */

99     protected DbGroup(int id, DbForumFactory factory) throws GroupNotFoundException {
100         this.id = id;
101         this.factory = factory;
102         this.profileManager = factory.getProfileManager();
103         loadFromDb();
104     }
105
106     /**
107      * Loads a group from the database based on its name. The implementation
108      * of this method is rather hackish since it includes a fake parameter just
109      * so that it can have a different method signature than the first
110      * constructor. Even so, this methodology makes this class behave more like
111      * our other classes, so we're gleefully leaving it this way. :)
112      *
113      * @param name the name of the group to load.
114      * @param fake a fake paramater that can always be null.
115      * @param profileManager a ProfileManager that can be used to perform user
116      * and group operations.
117      */

118     protected DbGroup(String JavaDoc name, Object JavaDoc fake, DbForumFactory factory) throws GroupNotFoundException {
119         this.name = name;
120         this.factory = factory;
121         this.profileManager = factory.getProfileManager();
122         loadFromDb();
123     }
124
125     //FROM THE USER INTERFACE//
126

127     public int getID() {
128         return id;
129     }
130
131     public String JavaDoc getName() {
132         return name;
133     }
134
135     public void setName(String JavaDoc name) throws UnauthorizedException {
136         this.name = name;
137         saveToDb();
138     }
139
140     public String JavaDoc getDescription() {
141         return description;
142     }
143
144     public void setDescription(String JavaDoc description) throws UnauthorizedException {
145         this.description = description;
146         saveToDb();
147     }
148
149     public void addAdministrator(User user) throws UnauthorizedException {
150         Connection JavaDoc con = null;
151         PreparedStatement JavaDoc pstmt = null;
152         try {
153             con = DbConnectionManager.getConnection();
154             pstmt = con.prepareStatement(ADD_ADMIN);
155             pstmt.setInt(1, id);
156             pstmt.setInt(2, user.getID());
157             pstmt.execute();
158         } catch (SQLException JavaDoc sqle) {
159             log.error("",sqle);
160         } finally {
161             try {
162                 pstmt.close();
163             } catch (Exception JavaDoc e) {
164                 log.error("",e);
165             }
166             try {
167                 con.close();
168             } catch (Exception JavaDoc e) {
169                 log.error("",e);
170             }
171         }
172
173         //Now, remove the user from the USER_PERM_CACHE since being in the
174
//group could affect their permissions.
175
DbCacheManager cacheManager = factory.getCacheManager();
176         cacheManager.removeUserPerm(new Integer JavaDoc(user.getID()));
177     }
178
179     public void removeAdministrator(User user) throws UnauthorizedException {
180         Connection JavaDoc con = null;
181         PreparedStatement JavaDoc pstmt = null;
182         try {
183             con = DbConnectionManager.getConnection();
184             pstmt = con.prepareStatement(REMOVE_ADMIN);
185             pstmt.setInt(1, id);
186             pstmt.setInt(2, user.getID());
187             pstmt.execute();
188         } catch (SQLException JavaDoc sqle) {
189             log.error("",sqle);
190         } finally {
191             try {
192                 pstmt.close();
193             } catch (Exception JavaDoc e) {
194                 log.error("",e);
195             }
196             try {
197                 con.close();
198             } catch (Exception JavaDoc e) {
199                 log.error("",e);
200             }
201         }
202
203         //Now, remove the user from the USER_PERM_CACHE since being in the
204
//group could affect their permissions.
205
DbCacheManager cacheManager = factory.getCacheManager();
206         cacheManager.removeUserPerm(new Integer JavaDoc(user.getID()));
207     }
208
209     public void addMember(User user) throws UnauthorizedException {
210         Connection JavaDoc con = null;
211         PreparedStatement JavaDoc pstmt = null;
212         try {
213             con = DbConnectionManager.getConnection();
214             pstmt = con.prepareStatement(ADD_USER);
215             pstmt.setInt(1, id);
216             pstmt.setInt(2, user.getID());
217             pstmt.execute();
218         } catch (SQLException JavaDoc sqle) {
219             log.error("",sqle);
220         } finally {
221             try {
222                 pstmt.close();
223             } catch (Exception JavaDoc e) {
224                 log.error("",e);
225             }
226             try {
227                 con.close();
228             } catch (Exception JavaDoc e) {
229                 log.error("",e);
230             }
231         }
232
233         //Now, remove the user from the USER_PERM_CACHE since being in the
234
//group could affect their permissions.
235
DbCacheManager cacheManager = factory.getCacheManager();
236         cacheManager.removeUserPerm(new Integer JavaDoc(user.getID()));
237     }
238
239     public void removeMember(User user) throws UnauthorizedException {
240         Connection JavaDoc con = null;
241         PreparedStatement JavaDoc pstmt = null;
242         try {
243             con = DbConnectionManager.getConnection();
244             pstmt = con.prepareStatement(REMOVE_USER);
245             pstmt.setInt(1, id);
246             pstmt.setInt(2, user.getID());
247             pstmt.execute();
248         } catch (SQLException JavaDoc sqle) {
249             log.error("",sqle);
250         } finally {
251             try {
252                 pstmt.close();
253             } catch (Exception JavaDoc e) {
254                 log.error("",e);
255             }
256             try {
257                 con.close();
258             } catch (Exception JavaDoc e) {
259                 log.error("",e);
260             }
261         }
262
263         //Now, remove the user from the USER_PERM_CACHE since being in the
264
//group could affect their permissions.
265
DbCacheManager cacheManager = factory.getCacheManager();
266         cacheManager.removeUserPerm(new Integer JavaDoc(user.getID()));
267     }
268
269     public boolean isAdministrator(User user) {
270         boolean answer = false;
271         Connection JavaDoc con = null;
272         PreparedStatement JavaDoc pstmt = null;
273         try {
274             con = DbConnectionManager.getConnection();
275             pstmt = con.prepareStatement(ADMIN_TEST);
276             pstmt.setInt(1, id);
277             pstmt.setInt(2, user.getID());
278             ResultSet JavaDoc rs = pstmt.executeQuery();
279             if (rs.next()) {
280                 answer = true;
281             }
282         } catch (SQLException JavaDoc sqle) {
283             log.error("",sqle);
284         } finally {
285             try {
286                 pstmt.close();
287             } catch (Exception JavaDoc e) {
288                 log.error("",e);
289             }
290             try {
291                 con.close();
292             } catch (Exception JavaDoc e) {
293                 log.error("",e);
294             }
295         }
296         return answer;
297     }
298
299     public boolean isMember(User user) {
300         boolean answer = false;
301         Connection JavaDoc con = null;
302         PreparedStatement JavaDoc pstmt = null;
303         try {
304             con = DbConnectionManager.getConnection();
305             pstmt = con.prepareStatement(MEMBER_TEST);
306             pstmt.setInt(1, id);
307             pstmt.setInt(2, user.getID());
308             ResultSet JavaDoc rs = pstmt.executeQuery();
309             if (rs.next()) {
310                 answer = true;
311             }
312         } catch (SQLException JavaDoc sqle) {
313             log.error("",sqle);
314         } finally {
315             try {
316                 pstmt.close();
317             } catch (Exception JavaDoc e) {
318                 log.error("",e);
319             }
320             try {
321                 con.close();
322             } catch (Exception JavaDoc e) {
323                 log.error("",e);
324             }
325         }
326         return answer;
327     }
328
329     public int getAdministratorCount() {
330         int count = 0;
331         boolean answer = false;
332         Connection JavaDoc con = null;
333         PreparedStatement JavaDoc pstmt = null;
334         try {
335             con = DbConnectionManager.getConnection();
336             pstmt = con.prepareStatement(ADMIN_COUNT);
337             pstmt.setInt(1, id);
338             ResultSet JavaDoc rs = pstmt.executeQuery();
339             if (rs.next()) {
340                 count = rs.getInt(1);
341             }
342         } catch (SQLException JavaDoc sqle) {
343             log.error("",sqle);
344         } finally {
345             try {
346                 pstmt.close();
347             } catch (Exception JavaDoc e) {
348                 log.error("",e);
349             }
350             try {
351                 con.close();
352             } catch (Exception JavaDoc e) {
353                 log.error("",e);
354             }
355         }
356         return count;
357     }
358
359     public int getMemberCount() {
360         int count = 0;
361         boolean answer = false;
362         Connection JavaDoc con = null;
363         PreparedStatement JavaDoc pstmt = null;
364         try {
365             con = DbConnectionManager.getConnection();
366             pstmt = con.prepareStatement(MEMBER_COUNT);
367             pstmt.setInt(1, id);
368             ResultSet JavaDoc rs = pstmt.executeQuery();
369             if (rs.next()) {
370                 count = rs.getInt(1);
371             }
372         } catch (SQLException JavaDoc sqle) {
373             log.error("",sqle);
374         } finally {
375             try {
376                 pstmt.close();
377             } catch (Exception JavaDoc e) {
378                 log.error("",e);
379             }
380             try {
381                 con.close();
382             } catch (Exception JavaDoc e) {
383                 log.error("",e);
384             }
385         }
386         return count;
387     }
388
389     public Iterator JavaDoc members() {
390         ArrayList JavaDoc admins = new ArrayList JavaDoc();
391         //Load list of group admins from db.
392
Connection JavaDoc con = null;
393         PreparedStatement JavaDoc pstmt = null;
394
395         try {
396             con = DbConnectionManager.getConnection();
397             pstmt = con.prepareStatement(LOAD_USERS);
398             pstmt.setInt(1, id);
399             ResultSet JavaDoc rs = pstmt.executeQuery();
400             User user = null;
401             while (rs.next()) {
402                 try {
403                     user = profileManager.getUser(rs.getInt("userID"));
404                 } catch (UserNotFoundException unfe) {
405                     log.error("",unfe);
406                 }
407                 admins.add(user);
408             }
409         } catch (SQLException JavaDoc sqle) {
410             log.error("SQLException in DbGroup.java:" + "users():reading group data " , sqle);
411             
412         } finally {
413             try {
414                 pstmt.close();
415             } catch (Exception JavaDoc e) {
416                 log.error("",e);
417             }
418             try {
419                 con.close();
420             } catch (Exception JavaDoc e) {
421                 log.error("",e);
422             }
423         }
424         return admins.iterator();
425     }
426
427     public Iterator JavaDoc administrators() {
428         ArrayList JavaDoc admins = new ArrayList JavaDoc();
429         //Load list of group admins from db.
430
Connection JavaDoc con = null;
431         PreparedStatement JavaDoc pstmt = null;
432
433         try {
434             con = DbConnectionManager.getConnection();
435             pstmt = con.prepareStatement(LOAD_ADMINS);
436             pstmt.setInt(1, id);
437             ResultSet JavaDoc rs = pstmt.executeQuery();
438             User user = null;
439             while (rs.next()) {
440                 try {
441                     user = profileManager.getUser(rs.getInt("userID"));
442                 } catch (UserNotFoundException unfe) {
443                     log.error("" , unfe);
444                 }
445                 admins.add(user);
446             }
447         } catch (SQLException JavaDoc sqle) {
448             log.error("SQLException in DbGroup.java:" + "administrators():reading group data " , sqle);
449             
450         } finally {
451             try {
452                 pstmt.close();
453             } catch (Exception JavaDoc e) {
454                 log.error("",e);
455             }
456             try {
457                 con.close();
458             } catch (Exception JavaDoc e) {
459                 log.error("",e);
460             }
461         }
462         return admins.iterator();
463     }
464
465     public ForumPermissions getPermissions(Authorization authorization) {
466         int userID = authorization.getUserID();
467         try {
468             User user = profileManager.getUser(userID);
469             if (isAdministrator(user)) {
470                 return new ForumPermissions(false, false, false, false, true, false, false, false);
471             }
472         } catch (Exception JavaDoc e) {
473         }
474
475         return ForumPermissions.none();
476     }
477
478     public boolean hasPermission(int type) {
479         return true;
480     }
481
482     //FROM THE CACHEABLE INTERFACE//
483

484     public int getSize() {
485         //Approximate the size of the object in bytes by calculating the size
486
//of each field.
487
int size = 0;
488         size += CacheSizes.sizeOfObject(); //overhead of object
489
size += CacheSizes.sizeOfInt(); //id
490
size += CacheSizes.sizeOfString(name); //name
491
size += CacheSizes.sizeOfString(description); //description
492
size += CacheSizes.sizeOfObject(); //profile manager ref.
493
size += CacheSizes.sizeOfObject(); //forum factory ref.
494

495         return size;
496     }
497
498     //OTHER METHODS
499

500     /**
501      * Returns a String representation of the Group object using the group name.
502      *
503      * @return a String representation of the Group object.
504      */

505     public String JavaDoc toString() {
506         return name;
507     }
508
509     public int hashCode() {
510         return id;
511     }
512
513     public boolean equals(Object JavaDoc object) {
514         if (this == object) {
515             return true;
516         }
517         if (object != null && object instanceof DbGroup) {
518             return id == ((DbGroup) object).getID();
519         } else {
520             return false;
521         }
522     }
523
524     /**
525      * Load the group data from the database.
526      */

527     private synchronized void loadFromDb() throws GroupNotFoundException {
528         String JavaDoc query;
529         if (name == null) {
530             query = LOAD_GROUP_BY_ID;
531         } else {
532             query = LOAD_GROUP_BY_NAME;
533         }
534         Connection JavaDoc con = null;
535         PreparedStatement JavaDoc pstmt = null;
536         try {
537             con = DbConnectionManager.getConnection();
538             pstmt = con.prepareStatement(query);
539             if (name == null) {
540                 pstmt.setInt(1, id);
541             } else {
542                 pstmt.setString(1, name);
543             }
544             ResultSet JavaDoc rs = pstmt.executeQuery();
545             if (!rs.next()) {
546                 throw new GroupNotFoundException();
547             }
548             this.id = rs.getInt("groupID");
549             this.name = rs.getString("name");
550             this.description = rs.getString("description");
551         } catch (SQLException JavaDoc sqle) {
552             log.error("SQLException in DbGroup.java:" + "loadFromDb():reading group data " , sqle);
553             throw new GroupNotFoundException();
554         } finally {
555             try {
556                 pstmt.close();
557             } catch (Exception JavaDoc e) {
558                 log.error("",e);
559             }
560             try {
561                 con.close();
562             } catch (Exception JavaDoc e) {
563                 log.error("",e);
564             }
565         }
566     }
567
568     /**
569      * Inserts a new record into the database.
570      */

571     private void insertIntoDb() {
572         StringBuffer JavaDoc insert = new StringBuffer JavaDoc();
573         Connection JavaDoc con = null;
574         PreparedStatement JavaDoc pstmt = null;
575         try {
576             con = DbConnectionManager.getConnection();
577             pstmt = con.prepareStatement(INSERT_GROUP);
578             pstmt.setString(1, name);
579             pstmt.setString(2, description);
580             pstmt.setInt(3, id);
581             pstmt.executeUpdate();
582         } catch (SQLException JavaDoc sqle) {
583             log.error("Error in DbGroup:insertIntoDb()-" , sqle);
584             
585         } finally {
586             try {
587                 pstmt.close();
588             } catch (Exception JavaDoc e) {
589                 log.error("",e);
590             }
591             try {
592                 con.close();
593             } catch (Exception JavaDoc e) {
594                 log.error("",e);
595             }
596         }
597     }
598
599     /**
600      * Saves group data to the db.
601      */

602     private synchronized void saveToDb() {
603         Connection JavaDoc con = null;
604         PreparedStatement JavaDoc pstmt = null;
605         try {
606             con = DbConnectionManager.getConnection();
607             pstmt = con.prepareStatement(SAVE_GROUP);
608             pstmt.setString(1, name);
609             pstmt.setString(2, description);
610             pstmt.setInt(3, id);
611             pstmt.executeUpdate();
612         } catch (SQLException JavaDoc sqle) {
613             log.error("SQLException in DbGroup.java:saveToDb(): " , sqle);
614             
615         } finally {
616             try {
617                 pstmt.close();
618             } catch (Exception JavaDoc e) {
619                 log.error("" , e);
620             }
621             try {
622                 con.close();
623             } catch (Exception JavaDoc e) {
624                 log.error("" , e);
625             }
626         }
627     }
628 }
629
Popular Tags