1 25 package org.nemesis.forum.impl; 26 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.sql.Connection ; 31 import java.sql.PreparedStatement ; 32 import java.sql.ResultSet ; 33 import java.sql.SQLException ; 34 import java.util.ArrayList ; 35 import java.util.Date ; 36 import java.util.Enumeration ; 37 import java.util.Iterator ; 38 import java.util.Properties ; 39 40 import org.apache.commons.logging.Log; 41 import org.apache.commons.logging.LogFactory; 42 import org.nemesis.forum.Authorization; 43 import org.nemesis.forum.Forum; 44 import org.nemesis.forum.ForumPermissions; 45 import org.nemesis.forum.ForumThread; 46 import org.nemesis.forum.Group; 47 import org.nemesis.forum.Message; 48 import org.nemesis.forum.MessageFilter; 49 import org.nemesis.forum.User; 50 import org.nemesis.forum.config.Constants; 51 import org.nemesis.forum.exception.ForumMessageNotFoundException; 52 import org.nemesis.forum.exception.ForumNotFoundException; 53 import org.nemesis.forum.exception.ForumThreadNotFoundException; 54 import org.nemesis.forum.exception.UnauthorizedException; 55 import org.nemesis.forum.filter.FilterHtml; 56 import org.nemesis.forum.filter.FilterNewline; 57 import org.nemesis.forum.proxy.ForumThreadProxy; 58 import org.nemesis.forum.proxy.MessageProxy; 59 import org.nemesis.forum.util.cache.Cache; 60 import org.nemesis.forum.util.cache.CacheSizes; 61 import org.nemesis.forum.util.cache.Cacheable; 62 import org.nemesis.forum.util.jdbc.DbConnectionManager; 63 69 public class DbForum implements Forum, Cacheable { 70 71 static protected Log log = LogFactory.getLog(DbForum.class); 72 73 private static final String ADD_THREAD = "UPDATE yazdThread set forumID=? WHERE threadID=?"; 74 protected static final String DELETE_THREAD = "DELETE FROM yazdThread WHERE threadID=?"; 75 private static final String THREAD_COUNT = "SELECT count(*) FROM yazdThread WHERE forumID=?"; 76 private static final String MESSAGE_COUNT = "SELECT count(*) FROM yazdThread, yazdMessage WHERE " + "yazdThread.forumID=? AND yazdThread.threadID=yazdMessage.threadID"; 77 private static final String ADD_USER_PERM = "INSERT INTO yazdUserPerm(forumID,userID,permission) VALUES(?,?,?)"; 78 private static final String REMOVE_USER_PERM = "DELETE FROM yazdUserPerm WHERE forumID=? AND userID=? AND permission=?"; 79 private static final String USERS_WITH_PERM = "SELECT DISTINCT userID FROM yazdUserPerm WHERE forumID=? AND permission=?"; 80 private static final String ADD_GROUP_PERM = "INSERT INTO yazdGroupPerm(forumID,groupID,permission) VALUES(?,?,?)"; 81 private static final String REMOVE_GROUP_PERM = "DELETE FROM yazdGroupPerm WHERE forumID=? AND groupID=? AND permission=?"; 82 private static final String GROUPS_WITH_PERM = "SELECT DISTINCT groupID FROM yazdGroupPerm WHERE forumID=? AND permission=?"; 83 private static final String LOAD_FILTERS = "SELECT filterObject, filterIndex FROM yazdFilter WHERE forumID=? ORDER BY filterIndex ASC"; 84 private static final String DELETE_FILTERS = "DELETE FROM yazdFilter WHERE forumID=?"; 85 private static final String ADD_FILTER = "INSERT INTO yazdFilter(forumID,filterIndex,filterObject) VALUES(?,?,?)"; 86 private static final String LOAD_PROPERTIES = "SELECT name, propValue FROM yazdForumProp WHERE forumID=?"; 87 private static final String DELETE_PROPERTIES = "DELETE FROM yazdForumProp WHERE forumID=?"; 88 private static final String INSERT_PROPERTY = "INSERT INTO yazdForumProp(forumID,name,propValue) VALUES(?,?,?)"; 89 private static final String LOAD_FORUM_BY_ID = "SELECT forumID, name, description, creationDate, modifiedDate, moderated FROM yazdForum WHERE forumID=?"; 90 private static final String LOAD_FORUM_BY_NAME = "SELECT forumID, name, description, creationDate, modifiedDate, moderated FROM yazdForum WHERE name=?"; 91 private static final String ADD_FORUM = "INSERT INTO yazdForum(forumID, name, description, creationDate, " + "modifiedDate, moderated) VALUES (?,?,?,?,?,?)"; 92 private static final String SAVE_FORUM = "UPDATE yazdForum SET name=?, description=?, creationDate=?, " + "modifiedDate=?, moderated=? WHERE forumID=?"; 93 private static final String UPDATE_FORUM_MODIFIED_DATE = "UPDATE yazdForum SET modifiedDate=? WHERE forumID=?"; 94 95 private static final String APPROVED_MESSAGE_COUNT = "SELECT count(*) FROM yazdThread, yazdMessage WHERE " + "yazdThread.forumID=? AND yazdMessage.approved=? AND yazdThread.threadID=yazdMessage.threadID"; 96 private static final String APPROVED_THREAD_COUNT = "SELECT count(*) FROM yazdThread WHERE forumID=? AND approved=? "; 97 98 99 private int id = -1; 100 private String name; 101 private String description; 102 private java.util.Date creationDate; 103 private java.util.Date modifiedDate; 104 private int moderation = 0; 105 private MessageFilter[] filters; 106 private Properties properties; 107 private Object saveLock = new Object (); 109 110 private DbForumFactory factory; 111 112 119 protected DbForum(String name, String description, DbForumFactory factory) { 120 this.id = DbSequenceManager.nextID("Forum"); 121 this.name = name; 122 this.description = description; 123 long now = System.currentTimeMillis(); 124 creationDate = new java.util.Date (now); 125 modifiedDate = new java.util.Date (now); 126 this.factory = factory; 127 insertIntoDb(); 128 properties = new Properties (); 129 filters = new MessageFilter[2]; 132 filters[0] = new FilterHtml(); 133 filters[1] = new FilterNewline(); 134 saveFiltersToDb(); 135 } 144 145 148 protected DbForum(int id, DbForumFactory factory) throws ForumNotFoundException { 149 this.id = id; 150 this.factory = factory; 151 loadFromDb(); 152 loadFiltersFromDb(); 153 loadProperties(); 154 } 155 156 159 protected DbForum(String name, DbForumFactory factory) throws ForumNotFoundException { 160 this.name = name; 161 this.factory = factory; 162 loadFromDb(); 163 loadFiltersFromDb(); 164 loadProperties(); 165 } 166 167 169 170 public int getModerationType(){ 171 return moderation; 172 } 173 174 public void setModerationType(int type) throws UnauthorizedException { 175 this.moderation=type; 176 saveToDb(); 177 } 178 179 180 public int getID() { 181 return id; 182 } 183 184 public String getName() { 185 return name; 186 } 187 188 public void setName(String name) throws UnauthorizedException { 189 this.name = name; 190 saveToDb(); 191 } 192 193 public String getDescription() { 194 return description; 195 } 196 197 public void setDescription(String description) throws UnauthorizedException { 198 this.description = description; 199 saveToDb(); 200 } 201 202 public java.util.Date getCreationDate() { 203 return creationDate; 204 } 205 206 public void setCreationDate(java.util.Date creationDate) throws UnauthorizedException { 207 this.creationDate = creationDate; 208 saveToDb(); 209 } 210 211 public java.util.Date getModifiedDate() { 212 return modifiedDate; 213 } 214 215 public void setModifiedDate(java.util.Date modifiedDate) throws UnauthorizedException { 216 this.modifiedDate = modifiedDate; 217 saveToDb(); 218 } 219 220 public String getProperty(String name) { 221 return (String ) properties.get(name); 222 } 223 224 public void setProperty(String name, String value) throws UnauthorizedException { 225 properties.put(name, value); 226 saveProperties(); 227 } 228 229 public Enumeration propertyNames() { 230 return properties.keys(); 231 } 232 233 234 235 236 public ForumThread createThread(Message rootMessage) throws UnauthorizedException { 237 boolean approved = !(getModerationType()==Constants.MODERATION_MESSAGE || getModerationType()==Constants.MODERATION_THREAD); 240 ((MessageProxy)rootMessage).setApproved2(approved); 242 return new DbForumThread(rootMessage, approved, this, factory); 243 } 244 245 public Message createMessage(User user) throws UnauthorizedException { 246 boolean approved = getModerationType()!=Constants.MODERATION_MESSAGE ; 249 return new DbForumMessage(user, approved, factory); } 251 252 public void addThread(ForumThread thread) throws UnauthorizedException { 253 boolean abortTransaction = false; 254 boolean supportsTransactions = false; 255 Connection con = null; 257 PreparedStatement pstmt = null; 258 try { 259 con = DbConnectionManager.getConnection(); 260 supportsTransactions = con.getMetaData().supportsTransactions(); 261 if (supportsTransactions) { 262 con.setAutoCommit(false); 263 } 264 265 pstmt = con.prepareStatement(ADD_THREAD); 266 pstmt.setInt(1, id); 267 pstmt.setInt(2, thread.getID()); 268 pstmt.executeUpdate(); 269 pstmt.close(); 270 271 ((ForumThreadProxy) thread).insertIntoDb(con); 273 } catch (Exception e) { 274 log.error("",e); 275 abortTransaction = true; 276 return; 277 } finally { 278 try { 279 if (supportsTransactions) { 280 if (abortTransaction == true) { 281 con.rollback(); 282 } else { 283 con.commit(); 284 } 285 } 286 } catch (Exception e) { 287 log.error("",e); 288 } 289 try { 290 if (supportsTransactions) { 291 con.setAutoCommit(true); 292 } 293 con.close(); 294 } catch (Exception e) { 295 log.error("",e); 296 } 297 } 298 299 updateModifiedDate(thread.getModifiedDate()); 301 } 302 303 public ForumThread getThread(int threadID) throws ForumThreadNotFoundException { 304 return factory.getThread(threadID, this); 305 } 306 307 public Message getMessage(int messageID) throws ForumMessageNotFoundException { 308 return factory.getMessage(messageID); 309 } 310 311 public void deleteThread(ForumThread thread) throws UnauthorizedException { 312 Message message = thread.getRootMessage(); 315 thread.deleteMessage(message); 316 } 317 318 protected void deleteThreadRecord(int threadID) { 319 320 Connection con = null; 322 PreparedStatement pstmt = null; 323 try { 324 con = DbConnectionManager.getConnection(); 325 pstmt = con.prepareStatement(DELETE_THREAD); 326 pstmt.setInt(1, threadID); 327 pstmt.execute(); 328 } catch (Exception sqle) { 329 log.error("Error in DbForum:deleteThread()-" + sqle); 330 } finally { 331 try { 332 pstmt.close(); 333 } catch (Exception e) { 334 log.error("",e); 335 } 336 try { 337 con.close(); 338 } catch (Exception e) { 339 log.error("",e); 340 } 341 } 342 343 Integer threadIDInteger = new Integer (threadID); 345 factory.getCacheManager().remove(DbCacheManager.THREAD_CACHE, threadIDInteger); 346 } 347 348 public void moveThread(ForumThread thread, Forum forum) throws UnauthorizedException { 349 if (thread.getForum().getID() != this.id) { 351 throw new IllegalArgumentException ("The thread does not belong to this forum."); 352 } 353 354 Connection con = null; 358 PreparedStatement pstmt = null; 359 try { 360 con = DbConnectionManager.getConnection(); 361 pstmt = con.prepareStatement(ADD_THREAD); 362 pstmt.setInt(1, forum.getID()); 363 pstmt.setInt(2, thread.getID()); 364 pstmt.executeUpdate(); 365 pstmt.close(); 366 } catch (SQLException sqle) { 367 log.error("Error in DbForum:addThread()-" , sqle); 368 return; 369 } finally { 370 try { 371 pstmt.close(); 372 } catch (Exception e) { 373 log.error("",e); 374 } 375 try { 376 con.close(); 377 } catch (Exception e) { 378 log.error("",e); 379 } 380 } 381 382 DbCacheManager cacheManager = factory.getCacheManager(); 383 385 Integer key = new Integer (this.id); 387 cacheManager.remove(DbCacheManager.FORUM_CACHE, key); 388 key = new Integer (forum.getID()); 389 cacheManager.remove(DbCacheManager.FORUM_CACHE, key); 390 391 key = new Integer (thread.getID()); 393 cacheManager.remove(DbCacheManager.THREAD_CACHE, key); 394 395 Iterator messages = thread.messages(); 397 while (messages.hasNext()) { 398 Message message = (Message) messages.next(); 399 key = new Integer (message.getID()); 401 cacheManager.remove(DbCacheManager.MESSAGE_CACHE, key); 402 } 406 407 Date now = new Date (); 409 thread.setModifiedDate(now); 410 forum.setModifiedDate(now); 412 } 413 414 public Iterator threads() { 415 return new DbForumIterator(this, factory); 416 } 417 418 public Iterator threads(int startIndex, int numResults) { 419 return new DbForumIterator(this, factory, startIndex, numResults); 420 } 421 422 public Iterator threads(boolean approved) { 423 return new DbForumIterator(this, factory,approved); 424 } 425 426 public Iterator threads(boolean approved,int startIndex, int numResults) { 427 return new DbForumIterator(this, factory, startIndex, numResults,approved); 428 } 429 430 public int getThreadCount() { 431 int threadCount = 0; 432 Connection con = null; 434 PreparedStatement pstmt = null; 435 try { 436 con = DbConnectionManager.getConnection(); 437 pstmt = con.prepareStatement(THREAD_COUNT); 438 pstmt.setInt(1, id); 439 ResultSet rs = pstmt.executeQuery(); 440 rs.next(); 441 threadCount = rs.getInt(1 442 ); 443 } catch (SQLException sqle) { 444 log.error("DbForum:getThreadCount() failed: " , sqle); 445 } finally { 446 try { 447 pstmt.close(); 448 } catch (Exception e) { 449 log.error("",e); 450 } 451 try { 452 con.close(); 453 } catch (Exception e) { 454 log.error("",e); 455 } 456 } 457 return threadCount; 458 } 459 460 public int getThreadCount(boolean approved) { 461 int threadCount = 0; 462 Connection con = null; 464 PreparedStatement pstmt = null; 465 try { 466 con = DbConnectionManager.getConnection(); 467 pstmt = con.prepareStatement(APPROVED_THREAD_COUNT); 468 pstmt.setInt(1, id); 469 pstmt.setInt(2, approved?1:0); 470 ResultSet rs = pstmt.executeQuery(); 471 rs.next(); 472 threadCount = rs.getInt(1 473 ); 474 } catch (SQLException sqle) { 475 log.error("DbForum:getThreadCount() failed: " , sqle); 476 } finally { 477 try { 478 pstmt.close(); 479 } catch (Exception e) { 480 log.error("",e); 481 } 482 try { 483 con.close(); 484 } catch (Exception e) { 485 log.error("",e); 486 } 487 } 488 return threadCount; 489 } 490 491 public int getMessageCount() { 492 int messageCount = 0; 493 Connection con = null; 494 PreparedStatement pstmt = null; 495 try { 496 con = DbConnectionManager.getConnection(); 497 pstmt = con.prepareStatement(MESSAGE_COUNT); 498 pstmt.setInt(1, id); 499 ResultSet rs = pstmt.executeQuery(); 500 rs.next(); 501 messageCount = rs.getInt(1 502 ); 503 } catch (SQLException sqle) { 504 log.error("DbForum:getMessageCount() failed: " , sqle); 505 } finally { 506 try { 507 pstmt.close(); 508 } catch (Exception e) { 509 log.error("",e); 510 } 511 try { 512 con.close(); 513 } catch (Exception e) { 514 log.error("",e); 515 } 516 } 517 return messageCount; 518 } 519 520 public int getMessageCount(boolean approved) { 521 int messageCount = 0; 522 Connection con = null; 523 PreparedStatement pstmt = null; 524 try { 525 con = DbConnectionManager.getConnection(); 526 pstmt = con.prepareStatement(APPROVED_MESSAGE_COUNT); 527 pstmt.setInt(1, id); 528 pstmt.setInt(2, approved? 1:0); 529 ResultSet rs = pstmt.executeQuery(); 530 rs.next(); 531 messageCount = rs.getInt(1 532 ); 533 } catch (SQLException sqle) { 534 log.error("DbForum:getMessageCount() failed: " , sqle); 535 } finally { 536 try { 537 pstmt.close(); 538 } catch (Exception e) { 539 log.error("",e); 540 } 541 try { 542 con.close(); 543 } catch (Exception e) { 544 log.error("",e); 545 } 546 } 547 return messageCount; 548 } 549 550 551 552 public void addUserPermission(User user, int permissionType) throws UnauthorizedException { 553 Connection con = null; 554 PreparedStatement pstmt = null; 555 try { 556 con = DbConnectionManager.getConnection(); 557 pstmt = con.prepareStatement(ADD_USER_PERM); 558 pstmt.setInt(1, id); 559 pstmt.setInt(2, user.getID()); 560 pstmt.setInt(3, permissionType); 561 pstmt.execute(); 562 factory.getCacheManager().removeUserPerm(new Integer (user.getID()), new Integer (id)); 564 } catch (SQLException sqle) { 565 log.error("Error in DbForum.java:" , sqle); 566 } finally { 567 try { 568 pstmt.close(); 569 } catch (Exception e) { 570 log.error("",e); 571 } 572 try { 573 con.close(); 574 } catch (Exception e) { 575 log.error("",e); 576 } 577 } 578 } 579 580 public void removeUserPermission(User user, int permissionType) throws UnauthorizedException { 581 Connection con = null; 582 PreparedStatement pstmt = null; 583 try { 584 con = DbConnectionManager.getConnection(); 585 pstmt = con.prepareStatement(REMOVE_USER_PERM); 586 pstmt.setInt(1, id); 587 pstmt.setInt(2, user.getID()); 588 pstmt.setInt(3, permissionType); 589 pstmt.execute(); 590 factory.getCacheManager().removeUserPerm(new Integer (user.getID()), new Integer (id)); 592 } catch (SQLException sqle) { 593 log.error("Error in DbForum.java:", sqle); 594 } finally { 595 try { 596 pstmt.close(); 597 } catch (Exception e) { 598 log.error("Error in DbForum.java:", e); 599 } 600 try { 601 con.close(); 602 } catch (Exception e) { 603 log.error("Error in DbForum.java:", e); 604 } 605 } 606 } 607 608 public int[] usersWithPermission(int permissionType) throws UnauthorizedException { 609 int[] users = new int[0]; 610 Connection con = null; 611 PreparedStatement pstmt = null; 612 try { 613 con = DbConnectionManager.getConnection(); 614 pstmt = con.prepareStatement(USERS_WITH_PERM); 615 pstmt.setInt(1, id); 616 pstmt.setInt(2, permissionType); 617 ResultSet rs = pstmt.executeQuery(); 618 ArrayList userList = new ArrayList (); 619 while (rs.next()) { 620 userList.add(new Integer (rs.getInt("userID"))); 621 } 622 users = new int[userList.size()]; 623 for (int i = 0; i < users.length; i++) { 624 users[i] = ((Integer ) userList.get(i)).intValue(); 625 } 626 } catch (SQLException sqle) { 627 log.error("Error in DbForum.java:" , sqle); 628 629 } finally { 630 try { 631 pstmt.close(); 632 } catch (Exception e) { 633 log.error("Error in DbForum.java:", e); 634 } 635 try { 636 con.close(); 637 } catch (Exception e) { 638 log.error("Error in DbForum.java:", e); 639 } 640 } 641 return users; 642 } 643 644 public void addGroupPermission(Group group, int permissionType) throws UnauthorizedException { 645 Connection con = null; 646 PreparedStatement pstmt = null; 647 try { 648 con = DbConnectionManager.getConnection(); 649 pstmt = con.prepareStatement(ADD_GROUP_PERM); 650 pstmt.setInt(1, id); 651 pstmt.setInt(2, group.getID()); 652 pstmt.setInt(3, permissionType); 653 pstmt.execute(); 654 factory.getCacheManager().remove(DbCacheManager.USER_PERMS_CACHE, new Integer (id)); 659 } catch (SQLException sqle) { 660 log.error("Error in DbForum.java:" , sqle); 661 } finally { 662 try { 663 pstmt.close(); 664 } catch (Exception e) { 665 log.error("Error in DbForum.java:", e); 666 } 667 try { 668 con.close(); 669 } catch (Exception e) { 670 log.error("Error in DbForum.java:",e); 671 } 672 } 673 } 674 675 public void removeGroupPermission(Group group, int permissionType) throws UnauthorizedException { 676 Connection con = null; 677 PreparedStatement pstmt = null; 678 try { 679 con = DbConnectionManager.getConnection(); 680 pstmt = con.prepareStatement(REMOVE_GROUP_PERM); 681 pstmt.setInt(1, id); 682 pstmt.setInt(2, group.getID()); 683 pstmt.setInt(3, permissionType); 684 pstmt.execute(); 685 factory.getCacheManager().remove(DbCacheManager.USER_PERMS_CACHE, new Integer (id)); 690 } catch (SQLException sqle) { 691 log.error("Error in DbForum.java:" , sqle); 692 693 } finally { 694 try { 695 pstmt.close(); 696 } catch (Exception e) { 697 log.error("Error in DbForum.java:",e); 698 } 699 try { 700 con.close(); 701 } catch (Exception e) { 702 log.error("Error in DbForum.java:",e); 703 } 704 } 705 } 706 707 public int[] groupsWithPermission(int permissionType) throws UnauthorizedException { 708 int[] groups = new int[0]; 709 Connection con = null; 710 PreparedStatement pstmt = null; 711 try { 712 con = DbConnectionManager.getConnection(); 713 pstmt = con.prepareStatement(GROUPS_WITH_PERM); 714 pstmt.setInt(1, id); 715 pstmt.setInt(2, permissionType); 716 ResultSet rs = pstmt.executeQuery(); 717 ArrayList groupList = new ArrayList (); 718 while (rs.next()) { 719 groupList.add(new Integer (rs.getInt("groupID"))); 720 } 721 groups = new int[groupList.size()]; 722 for (int i = 0; i < groups.length; i++) { 723 groups[i] = ((Integer ) groupList.get(i)).intValue(); 724 } 725 } catch (SQLException sqle) { 726 log.error("Error in DbForum.groupsWithPermission:" , sqle); 727 728 } finally { 729 try { 730 pstmt.close(); 731 } catch (Exception e) { 732 log.error("Error in DbForum.java:",e); 733 } 734 try { 735 con.close(); 736 } catch (Exception e) { 737 log.error("Error in DbForum.java:",e); 738 } 739 } 740 return groups; 741 } 742 743 public Message applyFilters(Message message) { 744 for (int i = 0; i < filters.length; i++) { 746 message = filters[i].clone(message); 747 } 748 return message; 749 } 750 751 public MessageFilter[] getForumMessageFilters() throws UnauthorizedException { 752 MessageFilter[] dbFilters = new MessageFilter[filters.length]; 753 for (int i = 0; i < filters.length; i++) { 754 dbFilters[i] = new DbForumMessageFilter((Message) filters[i], this); 755 } 756 return dbFilters; 757 } 758 759 public void addForumMessageFilter(MessageFilter filter) throws UnauthorizedException { 760 ArrayList newFilters = new ArrayList (filters.length + 1); 761 for (int i = 0; i < filters.length; i++) { 762 newFilters.add(filters[i]); 763 } 764 newFilters.add(filter); 765 MessageFilter[] newArray = new MessageFilter[newFilters.size()]; 766 for (int i = 0; i < newArray.length; i++) { 767 newArray[i] = (MessageFilter) newFilters.get(i); 768 } 769 filters = newArray; 771 saveFiltersToDb(); 772 } 773 774 public void addForumMessageFilter(MessageFilter filter, int index) throws UnauthorizedException { 775 ArrayList newFilters = new ArrayList (filters.length + 1); 776 for (int i = 0; i < filters.length; i++) { 777 newFilters.add(filters[i]); 778 } 779 newFilters.add(index, filter); 780 MessageFilter[] newArray = new MessageFilter[newFilters.size()]; 781 for (int i = 0; i < newArray.length; i++) { 782 newArray[i] = (MessageFilter) newFilters.get(i); 783 } 784 filters = newArray; 786 saveFiltersToDb(); 787 } 788 789 public void removeForumMessageFilter(int index) throws UnauthorizedException { 790 ArrayList newFilters = new ArrayList (filters.length); 791 for (int i = 0; i < filters.length; i++) { 792 newFilters.add(filters[i]); 793 } 794 newFilters.remove(index); 795 MessageFilter[] newArray = new MessageFilter[newFilters.size()]; 796 for (int i = 0; i < newArray.length; i++) { 797 newArray[i] = (MessageFilter) newFilters.get(i); 798 } 799 filters = newArray; 801 saveFiltersToDb(); 802 } 803 804 public ForumPermissions getPermissions(Authorization authorization) { 805 int userID = authorization.getUserID(); 806 807 Cache userPermCache = (Cache) factory.getCacheManager().get(DbCacheManager.USER_PERMS_CACHE, new Integer (id)); 809 810 if (userPermCache != null) { 813 ForumPermissions permissions = (ForumPermissions) userPermCache.get(new Integer (userID)); 814 if (permissions != null) { 815 return permissions; 816 } 817 } 818 819 boolean isAnonymous = (userID == -1); 822 boolean isUser = !isAnonymous; 823 824 ForumPermissions finalPermissions = ForumPermissions.none(); 825 826 if (isUser) { 829 ForumPermissions userPermissions = factory.getUserPermissions(userID, id); 830 finalPermissions = new ForumPermissions(finalPermissions, userPermissions); 832 } 833 ForumPermissions anonyPermissions = null; 835 if (userPermCache != null) { 836 anonyPermissions = (ForumPermissions) userPermCache.get(new Integer (-1)); 837 } 838 if (anonyPermissions == null) { 840 anonyPermissions = factory.getUserPermissions(-1, id); 841 if (userPermCache != null) { 843 userPermCache.add(new Integer (-1), anonyPermissions); 844 } 845 } 846 finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions); 848 849 if (isUser) { 851 ForumPermissions specialUserPermissions = null; 852 if (userPermCache != null) { 854 specialUserPermissions = (ForumPermissions) userPermCache.get(new Integer (0)); 855 } 856 if (specialUserPermissions == null) { 858 specialUserPermissions = factory.getUserPermissions(0, id); 859 if (userPermCache != null) { 861 userPermCache.add(new Integer (0), specialUserPermissions); 862 } 863 } 864 finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions); 866 } 867 868 int[] groups = ((DbProfileManager) factory.getProfileManager()).getUserGroups(userID); 870 for (int i = 0; i < groups.length; i++) { 871 ForumPermissions groupPermissions = factory.getGroupPermissions(groups[i], id); 872 finalPermissions = new ForumPermissions(finalPermissions, groupPermissions); 873 } 874 875 if (isUser && userPermCache != null) { 877 userPermCache.add(new Integer (userID), finalPermissions); 878 } 879 880 return finalPermissions; 881 } 882 883 public boolean hasPermission(int type) { 884 return true; 885 } 886 887 889 public int getSize() { 890 int size = 0; 893 size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfString(name); size += CacheSizes.sizeOfString(description); size += CacheSizes.sizeOfDate(); size += CacheSizes.sizeOfDate(); size += CacheSizes.sizeOfInt(); size += filters.length * 8; size += CacheSizes.sizeOfProperties(properties); size += CacheSizes.sizeOfObject(); 904 return size; 905 } 906 907 909 914 public String toString() { 915 return name; 916 } 917 918 public int hashCode() { 919 return id; 920 } 921 922 public boolean equals(Object object) { 923 if (this == object) { 924 return true; 925 } 926 if (object != null && object instanceof DbForum) { 927 return id == ((DbForum) object).getID(); 928 } else { 929 return false; 930 } 931 } 932 933 937 protected void updateModifiedDate(java.util.Date modifiedDate) { 938 this.modifiedDate = modifiedDate; 939 Connection con = null; 940 PreparedStatement pstmt = null; 941 try { 942 con = DbConnectionManager.getConnection(); 943 pstmt = con.prepareStatement(UPDATE_FORUM_MODIFIED_DATE); 944 pstmt.setString(1, "" + modifiedDate.getTime()); 945 pstmt.setInt(2, id); 946 pstmt.executeUpdate(); 947 } catch (SQLException sqle) { 948 log.error("Error in DbForum:updateModifiedDate()-" , sqle); 949 950 } finally { 951 try { 952 pstmt.close(); 953 } catch (Exception e) { 954 log.error("Error in DbForum.java:",e); 955 } 956 try { 957 con.close(); 958 } catch (Exception e) { 959 log.error("Error in DbForum.java:",e); 960 } 961 } 962 } 963 964 967 private void loadProperties() { 968 synchronized (saveLock) { 969 Properties newProps = new Properties (); 970 Connection con = null; 971 PreparedStatement pstmt = null; 972 try { 973 con = DbConnectionManager.getConnection(); 974 pstmt = con.prepareStatement(LOAD_PROPERTIES); 975 pstmt.setInt(1, id); 976 ResultSet rs = pstmt.executeQuery(); 977 while (rs.next()) { 978 String name = rs.getString("name"); 979 String value = rs.getString("propValue"); 980 newProps.put(name, value); 981 } 982 } catch (SQLException sqle) { 983 log.error("Error in DbForum:loadProperties():" , sqle); 984 985 } finally { 986 try { 987 pstmt.close(); 988 } catch (Exception e) { 989 log.error("Error in DbForum.java:",e); 990 } 991 try { 992 con.close(); 993 } catch (Exception e) { 994 log.error("Error in DbForum.java:",e); 995 } 996 } 997 this.properties = newProps; 998 } 999 } 1000 1001 1004 private void saveProperties() { 1005 synchronized (saveLock) { 1006 Connection con = null; 1007 PreparedStatement pstmt = null; 1008 try { 1009 con = DbConnectionManager.getConnection(); 1010 pstmt = con.prepareStatement(DELETE_PROPERTIES); 1012 pstmt.setInt(1, id); 1013 pstmt.execute(); 1014 pstmt.close(); 1015 pstmt = con.prepareStatement(INSERT_PROPERTY); 1017 Enumeration e = properties.keys(); 1018 while (e.hasMoreElements()) { 1019 String name = (String ) e.nextElement(); 1020 String value = (String ) properties.get(name); 1021 pstmt.setInt(1, id); 1022 pstmt.setString(2, name); 1023 pstmt.setString(3, value); 1024 pstmt.executeUpdate(); 1025 } 1026 } catch (SQLException sqle) { 1027 log.error("saveProperties",sqle); 1028 } finally { 1029 try { 1030 pstmt.close(); 1031 } catch (Exception e) { 1032 log.error("",e); 1033 } 1034 try { 1035 con.close(); 1036 } catch (Exception e) { 1037 log.error("",e); 1038 } 1039 } 1040 } 1041 } 1042 1043 1046 private void loadFiltersFromDb() { 1047 ArrayList newFilters = new ArrayList (); 1048 Connection con = null; 1049 PreparedStatement pstmt = null; 1050 try { 1051 con = DbConnectionManager.getConnection(); 1052 pstmt = con.prepareStatement(LOAD_FILTERS); 1053 pstmt.setInt(1, id); 1054 ResultSet rs = pstmt.executeQuery(); 1055 while (rs.next()) { 1056 try { 1057 ObjectInputStream in = new ObjectInputStream (rs.getBinaryStream("filterObject")); 1058 newFilters.add(in.readObject()); 1059 } catch (ClassCastException cce) { 1060 } catch (Exception e) { 1064 log.error("",e); 1065 } 1066 } 1067 } catch (SQLException sqle) { 1068 log.error("",sqle); 1069 } finally { 1070 try { 1071 pstmt.close(); 1072 } catch (Exception e) { 1073 log.error("",e); 1074 } 1075 try { 1076 con.close(); 1077 } catch (Exception e) { 1078 log.error("",e); 1079 } 1080 } 1081 filters = new MessageFilter[newFilters.size()]; 1082 for (int i = 0; i < filters.length; i++) { 1083 filters[i] = (MessageFilter) newFilters.get(i); 1084 } 1085 } 1089 1090 1095 protected void saveFiltersToDb() { 1096 boolean abort = false; 1097 boolean supportsTransactions = false; 1098 synchronized (saveLock) { 1099 Connection con = null; 1100 PreparedStatement pstmt = null; 1101 try { 1102 con = DbConnectionManager.getConnection(); 1103 1104 supportsTransactions = con.getMetaData().supportsTransactions(); 1105 if (supportsTransactions) { 1106 con.setAutoCommit(false); 1107 } 1108 1109 pstmt = con.prepareStatement(DELETE_FILTERS); 1110 pstmt.setInt(1, id); 1111 pstmt.execute(); 1112 pstmt.close(); 1114 pstmt = con.prepareStatement(ADD_FILTER); 1115 for (int i = 0; i < filters.length; i++) { 1116 try { 1117 ByteArrayOutputStream byteOut = new ByteArrayOutputStream (); 1118 ObjectOutputStream out = new ObjectOutputStream (byteOut); 1119 out.writeObject(filters[i]); 1120 pstmt.setInt(1, id); 1121 pstmt.setInt(2, i); 1122 pstmt.setBytes(3, byteOut.toByteArray()); 1123 pstmt.execute(); 1124 } catch (Exception e) { 1125 abort = true; 1126 log.error("",e); 1127 } 1128 } 1129 pstmt.close(); 1130 } catch (SQLException sqle) { 1131 abort = true; 1132 log.error("",sqle); 1133 } finally { 1134 try { 1135 if (supportsTransactions) { 1136 if (abort == true) { 1137 con.rollback(); 1138 } else { 1139 con.commit(); 1140 } 1141 } 1142 } catch (Exception e) { 1143 log.error("",e); 1144 } 1145 try { 1146 if (supportsTransactions) { 1147 con.setAutoCommit(true); 1148 } 1149 } catch (Exception e) { 1150 log.error("",e); 1151 } 1152 try { 1153 con.close(); 1154 } catch (Exception e) { 1155 log.error("",e); 1156 } 1157 } 1158 } 1159 } 1160 1161 1164 private void loadFromDb() throws ForumNotFoundException { 1165 Connection con = null; 1166 PreparedStatement pstmt = null; 1167 try { 1168 con = DbConnectionManager.getConnection(); 1169 if (id == -1) { 1171 pstmt = con.prepareStatement(LOAD_FORUM_BY_NAME); 1172 pstmt.setString(1, name); 1173 } else { 1174 pstmt = con.prepareStatement(LOAD_FORUM_BY_ID); 1175 pstmt.setInt(1, id); 1176 } 1177 ResultSet rs = pstmt.executeQuery(); 1178 if (!rs.next()) { 1179 throw new ForumNotFoundException("Forum " + getID() + " could not be loaded from the database."); 1180 } 1181 id = rs.getInt("forumID"); 1182 name = rs.getString("name"); 1183 description = rs.getString("description"); 1184 this.creationDate = new java.util.Date (Long.parseLong(rs.getString("creationDate").trim())); 1185 this.modifiedDate = new java.util.Date (Long.parseLong(rs.getString("modifiedDate").trim())); 1186 moderation = rs.getInt("moderated"); 1187 } catch (SQLException sqle) { 1188 log.error("",sqle); 1189 throw new ForumNotFoundException("Forum " + getID() + " could not be loaded from the database."); 1190 } catch (NumberFormatException nfe) { 1191 log.error( 1192 "WARNING: In DbForum.loadFromDb() -- there " 1193 + "was an error parsing the dates returned from the database. Ensure " 1194 + "that they're being stored correctly."); 1195 } finally { 1196 try { 1197 pstmt.close(); 1198 } catch (Exception e) { 1199 log.error("",e); 1200 } 1201 try { 1202 con.close(); 1203 } catch (Exception e) { 1204 log.error("",e); 1205 } 1206 } 1207 } 1208 1209 1212 private void insertIntoDb() { 1213 Connection con = null; 1214 PreparedStatement pstmt = null; 1215 try { 1216 con = DbConnectionManager.getConnection(); 1217 pstmt = con.prepareStatement(ADD_FORUM); 1218 pstmt.setInt(1, id); 1219 pstmt.setString(2, name); 1220 pstmt.setString(3, description); 1221 pstmt.setString(4, Long.toString(creationDate.getTime())); 1222 pstmt.setString(5, Long.toString(modifiedDate.getTime())); 1223 pstmt.setInt(6, moderation); 1224 pstmt.executeUpdate(); 1225 } catch (SQLException sqle) { 1226 log.error("Error in DbForum:insertIntoDb()-" , sqle); 1227 1228 } finally { 1229 try { 1230 pstmt.close(); 1231 } catch (Exception e) { 1232 log.error("",e); 1233 } 1234 try { 1235 con.close(); 1236 } catch (Exception e) { 1237 log.error("",e); 1238 } 1239 } 1240 } 1241 1242 1245 private synchronized void saveToDb() { 1246 Connection con = null; 1247 PreparedStatement pstmt = null; 1248 try { 1249 con = DbConnectionManager.getConnection(); 1250 pstmt = con.prepareStatement(SAVE_FORUM); 1251 pstmt.setString(1, name); 1252 pstmt.setString(2, description); 1253 pstmt.setString(3, Long.toString(creationDate.getTime())); 1254 pstmt.setString(4, Long.toString(modifiedDate.getTime())); 1255 pstmt.setInt(5, moderation); 1256 pstmt.setInt(6, id); 1257 pstmt.executeUpdate(); 1258 } catch (SQLException sqle) { 1259 log.error("Error in DbForum:saveToDb()-" , sqle); 1260 1261 } finally { 1262 try { 1263 pstmt.close(); 1264 } catch (Exception e) { 1265 log.error("",e); 1266 } 1267 try { 1268 con.close(); 1269 } catch (Exception e) { 1270 log.error("",e); 1271 } 1272 } 1273 } 1274} 1275 | Popular Tags |