1 25 package org.nemesis.forum.impl; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 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.Forum; 38 import org.nemesis.forum.ForumFactory; 39 import org.nemesis.forum.ForumPermissions; 40 import org.nemesis.forum.ForumThread; 41 import org.nemesis.forum.Message; 42 import org.nemesis.forum.ProfileManager; 43 import org.nemesis.forum.exception.ForumAlreadyExistsException; 44 import org.nemesis.forum.exception.ForumMessageNotFoundException; 45 import org.nemesis.forum.exception.ForumNotFoundException; 46 import org.nemesis.forum.exception.ForumThreadNotFoundException; 47 import org.nemesis.forum.exception.UnauthorizedException; 48 import org.nemesis.forum.util.cache.Cache; 49 import org.nemesis.forum.util.cache.CacheableInteger; 50 import org.nemesis.forum.util.jdbc.DbConnectionManager; 51 54 public class DbForumFactory extends ForumFactory { 55 static protected Log log = LogFactory.getLog(DbForumFactory.class); 56 57 private static final String FORUM_COUNT = "SELECT count(*) FROM yazdForum"; 58 private static final String DELETE_FORUM = "DELETE FROM yazdForum WHERE forumID=?"; 59 private static final String DELETE_FORUM_USER_PERMS = "DELETE FROM yazdUserPerm WHERE forumID=?"; 60 private static final String DELETE_FORUM_GROUP_PERMS = "DELETE FROM yazdGroupPerm WHERE forumID=?"; 61 private static final String DELETE_FORUM_PROPERTIES = "DELETE FROM yazdForumProp WHERE forumID=?"; 62 private static final String GET_USER_PERMS = "SELECT DISTINCT permission FROM yazdUserPerm WHERE forumID=? " + "AND userID=?"; 63 private static final String USERS_WITH_PERM = "SELECT DISTINCT userID FROM yazdUserPerm WHERE forumID=? AND permission=?"; 64 private static final String GET_GROUP_PERMS = "SELECT DISTINCT permission from yazdGroupPerm WHERE forumID=? " + "AND groupID=?"; 65 private static final String GROUPS_WITH_PERM = "SELECT DISTINCT groupID FROM yazdGroupPerm WHERE forumID=? AND permission=?"; 66 private static final String ALL_MESSAGES = "SELECT messageID FROM yazdMessage"; 67 private static final String DELETE_MESSAGE = "DELETE FROM yazdMessage WHERE messageID=?"; 68 69 protected DbCacheManager cacheManager; 70 71 74 private ProfileManager profileManager; 75 76 77 78 81 public DbForumFactory() { 82 cacheManager = new DbCacheManager(); 83 84 profileManager = new DbProfileManager(this); 85 } 87 88 90 public Forum createForum(String name, String description) throws UnauthorizedException, ForumAlreadyExistsException { 91 Forum newForum = null; 92 try { 93 Forum existingForum = getForum(name); 94 95 throw new ForumAlreadyExistsException(); 97 } catch (ForumNotFoundException fnfe) { 98 newForum = new DbForum(name, description, this); 100 } 101 return newForum; 102 } 103 104 public void deleteForum(Forum forum) throws UnauthorizedException { 105 cacheManager.remove(DbCacheManager.FORUM_CACHE, new Integer (forum.getID())); 107 cacheManager.remove(DbCacheManager.USER_PERMS_CACHE, new Integer (forum.getID())); 108 cacheManager.remove(DbCacheManager.FORUM_ID_CACHE, forum.getName()); 109 110 Iterator threads = forum.threads(); 112 while (threads.hasNext()) { 113 ForumThread thread = (ForumThread) threads.next(); 114 forum.deleteThread(thread); 115 } 116 117 int filterCount = forum.getForumMessageFilters().length; 121 for (int i = filterCount - 1; i >= 0; i--) { 122 forum.removeForumMessageFilter(i); 123 } 124 125 Connection con = null; 128 PreparedStatement pstmt = null; 129 try { 130 con = DbConnectionManager.getConnection(); 131 pstmt = con.prepareStatement(DELETE_FORUM); 132 pstmt.setInt(1, forum.getID()); 133 pstmt.execute(); 134 pstmt.close(); 135 pstmt = con.prepareStatement(DELETE_FORUM_USER_PERMS); 137 pstmt.setInt(1, forum.getID()); 138 pstmt.execute(); 139 pstmt.close(); 140 pstmt = con.prepareStatement(DELETE_FORUM_GROUP_PERMS); 142 pstmt.setInt(1, forum.getID()); 143 pstmt.execute(); 144 pstmt.close(); 145 pstmt = con.prepareStatement(DELETE_FORUM_PROPERTIES); 147 pstmt.setInt(1, forum.getID()); 148 pstmt.execute(); 149 } catch (Exception sqle) { 150 log.error("Error in DbForumFactory:deleteForum()-",sqle); 151 } finally { 152 try { 153 pstmt.close(); 154 } catch (Exception e) { 155 log.error("",e); 156 } 157 try { 158 con.close(); 159 } catch (Exception e) { 160 log.error("",e); 161 } 162 } 163 } 164 public Iterator forumsModeration() { return null; 166 } 167 public Forum getForum(int forumID) throws ForumNotFoundException, UnauthorizedException { 168 if (!cacheManager.isCacheEnabled()) { 170 return new DbForum(forumID, this); 171 } 172 Integer forumIDInteger = new Integer (forumID); 174 DbForum forum = (DbForum) cacheManager.get(DbCacheManager.FORUM_CACHE, forumIDInteger); 175 if (forum == null) { 176 forum = new DbForum(forumID, this); 177 cacheManager.add(DbCacheManager.FORUM_CACHE, forumIDInteger, forum); 178 } 179 return forum; 180 } 181 182 public Forum getForum(String name) throws ForumNotFoundException, UnauthorizedException { 183 if (!cacheManager.isCacheEnabled()) { 185 Forum forum = new DbForum(name, this); 186 return forum; 187 } 188 CacheableInteger forumIDInteger = (CacheableInteger) cacheManager.get(DbCacheManager.FORUM_ID_CACHE, name); 190 if (forumIDInteger == null) { 192 Forum forum = new DbForum(name, this); 193 forumIDInteger = new CacheableInteger(new Integer (forum.getID())); 194 cacheManager.add(DbCacheManager.FORUM_ID_CACHE, name, forumIDInteger); 195 } 196 return getForum(forumIDInteger.getInteger().intValue()); 197 } 198 199 public int getForumCount() { 200 int forumCount = 0; 201 Connection con = null; 202 PreparedStatement pstmt = null; 203 try { 204 con = DbConnectionManager.getConnection(); 205 pstmt = con.prepareStatement(FORUM_COUNT); 206 ResultSet rs = pstmt.executeQuery(); 207 rs.next(); 208 forumCount = rs.getInt(1); 209 } catch (SQLException sqle) { 210 log.error("DbForumFactory:getForumCount() failed: " , sqle); 211 } finally { 212 try { 213 pstmt.close(); 214 } catch (Exception e) { 215 log.error("",e); 216 } 217 try { 218 con.close(); 219 } catch (Exception e) { 220 log.error("",e); 221 } 222 } 223 return forumCount; 224 } 225 226 public Iterator forums() { 227 return new DbForumFactoryIterator(this); 228 } 229 230 public ProfileManager getProfileManager() { 231 return profileManager; 232 } 233 234 235 public int[] usersWithPermission(int permissionType) throws UnauthorizedException { 236 int[] users = new int[0]; 237 Connection con = null; 238 PreparedStatement pstmt = null; 239 try { 240 con = DbConnectionManager.getConnection(); 241 pstmt = con.prepareStatement(USERS_WITH_PERM); 242 pstmt.setInt(1, -1); 243 pstmt.setInt(2, permissionType); 244 ResultSet rs = pstmt.executeQuery(); 245 ArrayList userList = new ArrayList (); 246 while (rs.next()) { 247 userList.add(new Integer (rs.getInt("userID"))); 248 } 249 users = new int[userList.size()]; 250 for (int i = 0; i < users.length; i++) { 251 users[i] = ((Integer ) userList.get(i)).intValue(); 252 } 253 } catch (SQLException sqle) { 254 log.error("",sqle); 255 } finally { 256 try { 257 pstmt.close(); 258 } catch (Exception e) { 259 log.error("",e); 260 } 261 try { 262 con.close(); 263 } catch (Exception e) { 264 log.error("",e); 265 } 266 } 267 return users; 268 } 269 270 public int[] groupsWithPermission(int permissionType) throws UnauthorizedException { 271 int[] groups = new int[0]; 272 Connection con = null; 273 PreparedStatement pstmt = null; 274 try { 275 con = DbConnectionManager.getConnection(); 276 pstmt = con.prepareStatement(GROUPS_WITH_PERM); 277 pstmt.setInt(1, -1); 278 pstmt.setInt(2, permissionType); 279 ResultSet rs = pstmt.executeQuery(); 280 ArrayList groupList = new ArrayList (); 281 while (rs.next()) { 282 groupList.add(new Integer (rs.getInt("groupID"))); 283 } 284 groups = new int[groupList.size()]; 285 for (int i = 0; i < groups.length; i++) { 286 groups[i] = ((Integer ) groupList.get(i)).intValue(); 287 } 288 } catch (SQLException sqle) { 289 log.error("Error in DbForum.groupsWithPermission:" , sqle); 290 291 } finally { 292 try { 293 pstmt.close(); 294 } catch (Exception e) { 295 log.error("",e); 296 } 297 try { 298 con.close(); 299 } catch (Exception e) { 300 log.error("",e); 301 } 302 } 303 return groups; 304 } 305 306 public ForumPermissions getPermissions(Authorization authorization) { 307 int userID = authorization.getUserID(); 308 309 Cache userPermCache = (Cache) getCacheManager().get(DbCacheManager.USER_PERMS_CACHE, new Integer (-1)); 311 312 if (userPermCache != null) { 315 ForumPermissions permissions = (ForumPermissions) userPermCache.get(new Integer (userID)); 316 if (permissions != null) { 317 return permissions; 318 } 319 } 320 321 boolean isAnonymous = (userID == -1); 324 boolean isUser = !isAnonymous; 325 326 ForumPermissions finalPermissions = ForumPermissions.none(); 327 Iterator allForums = this.forums(); 329 Forum forum; 330 ForumPermissions forumUserPermissions; 331 while (allForums.hasNext()) { 332 forum = (Forum) allForums.next(); 333 forumUserPermissions = getUserPermissions(userID, forum.getID()); 334 finalPermissions = new ForumPermissions(finalPermissions, forumUserPermissions); 335 } 336 337 if (isUser) { 340 ForumPermissions userPermissions = getUserPermissions(userID, -1); 341 finalPermissions = new ForumPermissions(finalPermissions, userPermissions); 343 } 344 ForumPermissions anonyPermissions = null; 346 if (userPermCache != null) { 347 anonyPermissions = (ForumPermissions) userPermCache.get(new Integer (-1)); 348 } 349 if (anonyPermissions == null) { 351 anonyPermissions = getUserPermissions(-1, -1); 352 if (userPermCache != null) { 354 userPermCache.add(new Integer (-1), anonyPermissions); 355 } 356 } 357 finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions); 359 360 if (isUser) { 362 ForumPermissions specialUserPermissions = null; 363 if (userPermCache != null) { 365 specialUserPermissions = (ForumPermissions) userPermCache.get(new Integer (0)); 366 } 367 if (specialUserPermissions == null) { 369 specialUserPermissions = getUserPermissions(0, -1); 370 if (userPermCache != null) { 372 userPermCache.add(new Integer (0), specialUserPermissions); 373 } 374 } 375 finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions); 377 } 378 379 int[] groups = ((DbProfileManager) getProfileManager()).getUserGroups(userID); 381 for (int i = 0; i < groups.length; i++) { 382 ForumPermissions groupPermissions = getGroupPermissions(groups[i], -1); 383 finalPermissions = new ForumPermissions(finalPermissions, groupPermissions); 384 } 385 386 if (isUser && userPermCache != null) { 388 userPermCache.add(new Integer (userID), finalPermissions); 389 } 390 391 return finalPermissions; 392 } 393 394 public boolean hasPermission(int type) { 395 return true; 396 } 397 398 400 403 public DbCacheManager getCacheManager() { 404 return cacheManager; 405 } 406 407 421 public void cleanDatabase() { 422 Iterator forums = forums(); 424 while (forums.hasNext()) { 425 Forum forum = (Forum) forums.next(); 426 Iterator threads = forum.threads(); 427 while (threads.hasNext()) { 428 try { 429 ForumThread thread = (ForumThread) threads.next(); 430 Iterator messages = thread.messages(); 431 while (messages.hasNext()) { 432 try { 433 Message message = (Message) messages.next(); 434 if ( 435 message.getBody() == null) { 436 437 thread.deleteMessage(message); 438 } 439 } catch (Exception me) { 440 log.error("",me); 441 } 442 } 443 } catch (Exception te) { 444 log.error("",te); 445 } 446 } 447 } 448 449 501 } 502 503 508 public DbForumThread getThread(int threadID, DbForum forum) throws ForumThreadNotFoundException { 509 if (!cacheManager.isCacheEnabled()) { 511 return new DbForumThread(threadID, forum, this); 512 } 513 Integer threadIDInteger = new Integer (threadID); 515 DbForumThread thread = (DbForumThread) cacheManager.get(DbCacheManager.THREAD_CACHE, threadIDInteger); 516 if (thread == null) { 517 thread = new DbForumThread(threadID, forum, this); 518 cacheManager.add(DbCacheManager.THREAD_CACHE, threadIDInteger, thread); 519 } 520 return thread; 521 } 522 523 529 public DbForumMessage getMessage(int messageID) throws ForumMessageNotFoundException { 530 if (!cacheManager.isCacheEnabled()) { 532 return new DbForumMessage(messageID, this); 533 } 534 Integer messageIDInteger = new Integer (messageID); 536 DbForumMessage message = (DbForumMessage) cacheManager.get(DbCacheManager.MESSAGE_CACHE, messageIDInteger); 537 if (message == null) { 538 message = new DbForumMessage(messageID, this); 540 cacheManager.add(DbCacheManager.MESSAGE_CACHE, messageIDInteger, message); 542 } 543 return message; 544 } 545 546 547 548 551 protected ForumPermissions getUserPermissions(int userID, int forumID) { 552 Connection con = null; 553 PreparedStatement pstmt = null; 554 boolean[] permissions = new boolean[8]; 556 for (int i = 0; i < permissions.length; i++) { 557 permissions[i] = false; 558 } 559 try { 560 con = DbConnectionManager.getConnection(); 561 pstmt = con.prepareStatement(GET_USER_PERMS); 562 pstmt.setInt(1, forumID); 563 pstmt.setInt(2, userID); 564 ResultSet rs = pstmt.executeQuery(); 565 while (rs.next()) { 566 int newPerm = rs.getInt("permission"); 567 permissions[newPerm] = true; 568 } 569 } catch (SQLException sqle) { 570 log.error("Error in DbForum.java:" , sqle); 571 572 } finally { 573 try { 574 pstmt.close(); 575 } catch (Exception e) { 576 log.error("",e); 577 } 578 try { 579 con.close(); 580 } catch (Exception e) { 581 log.error("",e); 582 } 583 } 584 return new ForumPermissions(permissions); 585 } 586 587 590 protected ForumPermissions getGroupPermissions(int groupID, int forumID) { 591 Connection con = null; 592 PreparedStatement pstmt = null; 593 boolean[] permissions = new boolean[8]; 595 for (int i = 0; i < permissions.length; i++) { 596 permissions[i] = false; 597 } 598 try { 599 con = DbConnectionManager.getConnection(); 600 pstmt = con.prepareStatement(GET_GROUP_PERMS); 601 pstmt.setInt(1, forumID); 602 pstmt.setInt(2, groupID); 603 ResultSet rs = pstmt.executeQuery(); 604 while (rs.next()) { 605 int newPerm = rs.getInt("permission"); 606 permissions[newPerm] = true; 607 } 608 } catch (SQLException sqle) { 609 log.error("",sqle); 610 } finally { 611 try { 612 pstmt.close(); 613 } catch (Exception e) { 614 log.error("",e); 615 } 616 try { 617 con.close(); 618 } catch (Exception e) { 619 log.error("",e); 620 } 621 } 622 return new ForumPermissions(permissions); 623 } 624 } 625 | Popular Tags |