1 53 54 106 107 package com.Yasna.forum.database; 108 109 import java.util.Iterator ; 110 import java.util.Enumeration ; 111 import java.util.Properties ; 112 import java.util.ArrayList ; 113 import java.util.Date ; 114 import java.sql.*; 115 import java.io.*; 116 117 import com.Yasna.forum.*; 118 import com.Yasna.forum.Exceptions.RapidPostingException; 119 import com.Yasna.forum.Exceptions.UserBlackListedException; 120 import com.Yasna.forum.util.ClientIP; 121 import com.Yasna.forum.filter.*; 122 import com.Yasna.util.Cache; 123 import com.Yasna.util.Cacheable; 124 import com.Yasna.util.CacheSizes; 125 126 132 public class DbForum implements Forum, Cacheable { 133 134 135 private static final String ADD_THREAD = 136 "UPDATE yazdThread set forumID=? WHERE threadID=?"; 137 protected static final String DELETE_THREAD = "DELETE FROM yazdThread WHERE threadID=?"; 138 private static final String THREAD_COUNT = 139 "SELECT count(*) FROM yazdThread WHERE forumID=?"; 140 private static final String MESSAGE_COUNT = 141 "SELECT count(*) FROM yazdThread, yazdMessage WHERE " + 142 "yazdThread.forumID=? AND yazdThread.threadID=yazdMessage.threadID"; 143 private static final String ADD_USER_PERM = 144 "INSERT INTO yazdUserPerm(forumID,userID,permission) VALUES(?,?,?)"; 145 private static final String REMOVE_USER_PERM = 146 "DELETE FROM yazdUserPerm WHERE forumID=? AND userID=? AND permission=?"; 147 private static final String USERS_WITH_PERM = 148 "SELECT DISTINCT userID FROM yazdUserPerm WHERE forumID=? AND permission=?"; 149 private static final String ADD_GROUP_PERM = 150 "INSERT INTO yazdGroupPerm(forumID,groupID,permission) VALUES(?,?,?)"; 151 private static final String REMOVE_GROUP_PERM = 152 "DELETE FROM yazdGroupPerm WHERE forumID=? AND groupID=? AND permission=?"; 153 private static final String GROUPS_WITH_PERM = 154 "SELECT DISTINCT groupID FROM yazdGroupPerm WHERE forumID=? AND permission=?"; 155 private static final String LOAD_FILTERS = 156 "SELECT filterObject, filterIndex FROM yazdFilter WHERE forumID=? ORDER BY filterIndex ASC"; 157 private static final String DELETE_FILTERS = "DELETE FROM yazdFilter WHERE forumID=?"; 158 private static final String ADD_FILTER = 159 "INSERT INTO yazdFilter(forumID,filterIndex,filterObject) VALUES(?,?,?)"; 160 private static final String LOAD_PROPERTIES = 161 "SELECT name, propValue FROM yazdForumProp WHERE forumID=?"; 162 private static final String DELETE_PROPERTIES = 163 "DELETE FROM yazdForumProp WHERE forumID=?"; 164 private static final String INSERT_PROPERTY = 165 "INSERT INTO yazdForumProp(forumID,name,propValue) VALUES(?,?,?)"; 166 private static final String LOAD_FORUM_BY_ID = 167 "SELECT forumID, name, description, creationDate, modifiedDate, moderated, article,forumorder FROM yazdForum WHERE forumID=?"; 168 private static final String LOAD_FORUM_BY_NAME = 169 "SELECT forumID, name, description, creationDate, modifiedDate, moderated, article,forumorder FROM yazdForum WHERE name=?"; 170 private static final String ADD_FORUM = 171 "INSERT INTO yazdForum(forumID, name, description, creationDate, " + 172 "modifiedDate, moderated, forumGroupID,article,forumorder) VALUES (?,?,?,?,?,?,?,?,0)"; 173 private static final String SAVE_FORUM = 174 "UPDATE yazdForum SET name=?, description=?, creationDate=?, " + 175 "modifiedDate=?, moderated=?,forumorder=? WHERE forumID=?"; 176 private static final String UPDATE_FORUM_MODIFIED_DATE = 177 "UPDATE yazdForum SET modifiedDate=? WHERE forumID=?"; 178 private static final String INSERT_ARTICLE_MAP="insert into yazdArticleMap(threadID,pageKey,forumID) values(?,?,?)"; 179 180 private int id = -1; 181 private String name; 182 private String description; 183 private int forumGroupID; 184 private java.util.Date creationDate; 185 private java.util.Date modifiedDate; 186 private boolean moderated; 187 private boolean isarticle=false; 188 private ForumMessageFilter[] filters; 189 private Properties properties; 190 private Object saveLock = new Object (); 192 private int order; 193 194 private DbForumFactory factory; 195 196 205 protected DbForum(String name, String description, boolean moderated, 206 int forumGroupID, DbForumFactory factory,boolean article) { 207 this.id = DbSequenceManager.nextID("Forum"); 208 this.name = name; 209 this.description = description; 210 this.moderated = moderated; 211 this.forumGroupID = forumGroupID; 212 long now = System.currentTimeMillis(); 213 creationDate = new java.util.Date (now); 214 modifiedDate = new java.util.Date (now); 215 this.factory = factory; 216 this.isarticle=article; 217 insertIntoDb(); 218 properties = new Properties (); 219 filters = new ForumMessageFilter[2]; 222 filters[0] = new FilterHtml(); 223 filters[1] = new FilterNewline(); 224 saveFiltersToDb(); 225 } 234 235 238 protected DbForum(int id, DbForumFactory factory) 239 throws ForumNotFoundException 240 { 241 this.id = id; 242 this.factory = factory; 243 loadFromDb(); 244 loadFiltersFromDb(); 245 loadProperties(); 246 } 247 248 251 protected DbForum(String name, DbForumFactory factory) 252 throws ForumNotFoundException 253 { 254 this.name = name; 255 this.factory = factory; 256 loadFromDb(); 257 loadFiltersFromDb(); 258 loadProperties(); 259 } 260 261 263 public int getID() { 264 return id; 265 } 266 267 public String getName() { 268 return name; 269 } 270 271 public void setName(String name) throws UnauthorizedException { 272 this.name = name; 273 saveToDb(); 274 } 275 276 public String getDescription() { 277 return description; 278 } 279 280 public void setDescription(String description) throws UnauthorizedException 281 { 282 this.description = description; 283 saveToDb(); 284 } 285 286 public java.util.Date getCreationDate() { 287 return creationDate; 288 } 289 290 public void setCreationDate(java.util.Date creationDate) 291 throws UnauthorizedException 292 { 293 this.creationDate = creationDate; 294 saveToDb(); 295 } 296 297 public java.util.Date getModifiedDate() { 298 return modifiedDate; 299 } 300 301 public void setModifiedDate(java.util.Date modifiedDate) 302 throws UnauthorizedException 303 { 304 this.modifiedDate = modifiedDate; 305 saveToDb(); 306 } 307 308 public String getProperty(String name) { 309 return (String )properties.get(name); 310 } 311 312 public void setProperty(String name, String value) 313 throws UnauthorizedException 314 { 315 properties.put(name, value); 316 saveProperties(); 317 } 318 319 public Enumeration propertyNames() { 320 return properties.keys(); 321 } 322 323 public boolean isModerated() { 324 return moderated; 325 } 326 327 public void setModerated(boolean moderated) 328 throws UnauthorizedException 329 { 330 this.moderated = moderated; 331 saveToDb(); 332 } 333 334 public ForumThread createThread(ForumMessage rootMessage,ThreadType typeid) 335 throws UnauthorizedException 336 { 337 boolean approved = !isModerated(); 340 return new DbForumThread(rootMessage, approved, this, factory,typeid); 341 } 342 343 public ForumMessage createMessage(User user,ClientIP clientIP) 344 throws UnauthorizedException,RapidPostingException, UserBlackListedException 345 { 346 boolean approved = !isModerated(); 349 ForumMessage message = new DbForumMessage(user, factory, approved,clientIP); 350 return message; 351 } 352 public ForumMessage createDummyMessage(User user) 353 throws UnauthorizedException 354 { 355 return new DbForumMessage(user, factory); 356 } 357 358 public void addThread(ForumThread thread) throws UnauthorizedException { 359 boolean abortTransaction = false; 360 boolean supportsTransactions = false; 361 Connection con = null; 363 PreparedStatement pstmt = null; 364 try { 365 con = DbConnectionManager.getConnection(); 366 supportsTransactions = con.getMetaData().supportsTransactions(); 367 if (supportsTransactions) { 368 con.setAutoCommit(false); 369 } 370 371 pstmt = con.prepareStatement(ADD_THREAD); 372 pstmt.setInt(1,id); 373 pstmt.setInt(2,thread.getID()); 374 pstmt.executeUpdate(); 375 pstmt.close(); 376 377 ((ForumThreadProxy)thread).insertIntoDb(con); 379 } 380 catch(Exception e) { 381 e.printStackTrace(); 382 abortTransaction = true; 383 return; 384 } 385 finally { 386 try { 387 if (supportsTransactions) { 388 if (abortTransaction == true) { 389 con.rollback(); 390 } 391 else { 392 con.commit(); 393 } 394 } 395 } 396 catch (Exception e) { e.printStackTrace(); } 397 try { 398 if (supportsTransactions) { 399 con.setAutoCommit(true); 400 } 401 con.close(); 402 } 403 catch (Exception e) { e.printStackTrace(); } 404 } 405 406 updateModifiedDate(thread.getModifiedDate()); 408 User newUser = thread.getRootMessage().getUser(); 410 if(newUser.getThreadSubscribe() && newUser.getID() > 1){ 411 newUser.setProperty("WatchThread"+thread.getID(),"true"); 412 } 413 414 } 415 416 public ForumThread getThread(int threadID) throws 417 ForumThreadNotFoundException 418 { 419 return factory.getThread(threadID, this); 420 } 421 422 public void deleteThread(ForumThread thread) throws UnauthorizedException 423 { 424 ForumMessage message = thread.getRootMessage(); 427 thread.deleteMessage(message); 428 } 429 430 protected void deleteThreadRecord(int threadID) { 431 432 Connection con = null; 434 PreparedStatement pstmt = null; 435 try { 436 con = DbConnectionManager.getConnection(); 437 pstmt = con.prepareStatement(DELETE_THREAD); 438 pstmt.setInt(1, threadID); 439 pstmt.execute(); 440 } 441 catch( Exception sqle ) { 442 System.err.println("Error in DbForum:deleteThread()-" + sqle); 443 } 444 finally { 445 try { pstmt.close(); } 446 catch (Exception e) { e.printStackTrace(); } 447 try { con.close(); } 448 catch (Exception e) { e.printStackTrace(); } 449 } 450 451 Integer threadIDInteger = new Integer (threadID); 453 factory.getCacheManager().remove(DbCacheManager.THREAD_CACHE, threadIDInteger); 454 } 455 456 public void moveThread(ForumThread thread, Forum forum) 457 throws UnauthorizedException 458 { 459 if (thread.getForum().getID() != this.id) { 461 throw new IllegalArgumentException ("The thread does not belong to this forum."); 462 } 463 if (thread.getForum().getID() == forum.getID()) { 465 throw new IllegalArgumentException ("The thread is already in this forum."); 466 } 467 468 Connection con = null; 472 PreparedStatement pstmt = null; 473 try { 474 con = DbConnectionManager.getConnection(); 475 pstmt = con.prepareStatement(ADD_THREAD); 476 pstmt.setInt(1,forum.getID()); 477 pstmt.setInt(2,thread.getID()); 478 pstmt.executeUpdate(); 479 pstmt.close(); 480 } 481 catch( SQLException sqle ) { 482 System.err.println("Error in DbForum:addThread()-" + sqle); 483 return; 484 } 485 finally { 486 try { pstmt.close(); } 487 catch (Exception e) { e.printStackTrace(); } 488 try { con.close(); } 489 catch (Exception e) { e.printStackTrace(); } 490 } 491 492 DbCacheManager cacheManager = factory.getCacheManager(); 493 SearchIndexer indexer = factory.getSearchIndexer(); 494 495 Integer key = new Integer (this.id); 497 cacheManager.remove(DbCacheManager.FORUM_CACHE, key); 498 key = new Integer (forum.getID()); 499 cacheManager.remove(DbCacheManager.FORUM_CACHE, key); 500 501 key = new Integer (thread.getID()); 503 cacheManager.remove(DbCacheManager.THREAD_CACHE, key); 504 505 Iterator messages = thread.messages(); 507 while (messages.hasNext()) { 508 ForumMessage message = (ForumMessage)messages.next(); 509 key = new Integer (message.getID()); 511 cacheManager.remove(DbCacheManager.MESSAGE_CACHE, key); 512 indexer.removeFromIndex(message); 514 indexer.addToIndex(message); 515 } 516 517 Date now = new Date (); 519 thread.setModifiedDate(now); 520 forum.setModifiedDate(now); 522 } 523 524 public Iterator threads() { 525 return new DbForumIterator(this, factory); 526 } 527 528 public Iterator threads(int startIndex, int numResults, int sortBy) { 529 return new DbForumIterator(this, factory, startIndex, numResults, sortBy); 530 } 531 532 public int getThreadCount() { 533 int threadCount = 0; 534 Connection con = null; 536 PreparedStatement pstmt = null; 537 try { 538 con = DbConnectionManager.getConnection(); 539 pstmt = con.prepareStatement(THREAD_COUNT); 540 pstmt.setInt(1, id); 541 ResultSet rs = pstmt.executeQuery(); 542 rs.next(); 543 threadCount = rs.getInt(1 ); 544 } 545 catch( SQLException sqle ) { 546 System.err.println("DbForum:getThreadCount() failed: " + sqle); 547 } 548 finally { 549 try { pstmt.close(); } 550 catch (Exception e) { e.printStackTrace(); } 551 try { con.close(); } 552 catch (Exception e) { e.printStackTrace(); } 553 } 554 return threadCount; 555 } 556 557 public int getMessageCount() { 558 int messageCount = 0; 559 Connection con = null; 560 PreparedStatement pstmt = null; 561 try { 562 con = DbConnectionManager.getConnection(); 563 pstmt = con.prepareStatement(MESSAGE_COUNT); 564 pstmt.setInt(1, id); 565 ResultSet rs = pstmt.executeQuery(); 566 rs.next(); 567 messageCount = rs.getInt(1 ); 568 } 569 catch( SQLException sqle ) { 570 System.err.println("DbForum:getMessageCount() failed: " + sqle); 571 } 572 finally { 573 try { pstmt.close(); } 574 catch (Exception e) { e.printStackTrace(); } 575 try { con.close(); } 576 catch (Exception e) { e.printStackTrace(); } 577 } 578 return messageCount; 579 } 580 581 public Query createQuery() { 582 return new DbQuery(this, factory); 583 } 584 585 public void addUserPermission(User user, int permissionType) 586 throws UnauthorizedException 587 { 588 Connection con = null; 589 PreparedStatement pstmt = null; 590 try { 591 con = DbConnectionManager.getConnection(); 592 pstmt = con.prepareStatement(ADD_USER_PERM); 593 pstmt.setInt(1,id); 594 pstmt.setInt(2,user.getID()); 595 pstmt.setInt(3,permissionType); 596 pstmt.execute(); 597 factory.getCacheManager().removeUserPerm( 599 new Integer (user.getID()), 600 new Integer (id) 601 ); 602 } 603 catch( SQLException sqle ) { 604 System.err.println("Error in DbForum.java:" + sqle); 605 sqle.printStackTrace(); 606 } 607 finally { 608 try { pstmt.close(); } 609 catch (Exception e) { e.printStackTrace(); } 610 try { con.close(); } 611 catch (Exception e) { e.printStackTrace(); } 612 } 613 } 614 615 public void removeUserPermission(User user, int permissionType) 616 throws UnauthorizedException 617 { 618 Connection con = null; 619 PreparedStatement pstmt = null; 620 try { 621 con = DbConnectionManager.getConnection(); 622 pstmt = con.prepareStatement(REMOVE_USER_PERM); 623 pstmt.setInt(1,id); 624 pstmt.setInt(2,user.getID()); 625 pstmt.setInt(3,permissionType); 626 pstmt.execute(); 627 factory.getCacheManager().removeUserPerm( 629 new Integer (user.getID()), 630 new Integer (id) 631 ); 632 } 633 catch( SQLException sqle ) { 634 System.err.println("Error in DbForum.java:" + sqle); 635 sqle.printStackTrace(); 636 } 637 finally { 638 try { pstmt.close(); } 639 catch (Exception e) { e.printStackTrace(); } 640 try { con.close(); } 641 catch (Exception e) { e.printStackTrace(); } 642 } 643 } 644 645 public int[] usersWithPermission(int permissionType) 646 throws UnauthorizedException 647 { 648 int [] users = new int[0]; 649 Connection con = null; 650 PreparedStatement pstmt = null; 651 try { 652 con = DbConnectionManager.getConnection(); 653 pstmt = con.prepareStatement(USERS_WITH_PERM); 654 pstmt.setInt(1,id); 655 pstmt.setInt(2,permissionType); 656 ResultSet rs = pstmt.executeQuery(); 657 ArrayList userList = new ArrayList (); 658 while (rs.next()) { 659 userList.add(new Integer (rs.getInt("userID"))); 660 } 661 users = new int[userList.size()]; 662 for (int i=0; i<users.length; i++) { 663 users[i] = ((Integer )userList.get(i)).intValue(); 664 } 665 } 666 catch( SQLException sqle ) { 667 System.err.println("Error in DbForum.java:" + sqle); 668 sqle.printStackTrace(); 669 } 670 finally { 671 try { pstmt.close(); } 672 catch (Exception e) { e.printStackTrace(); } 673 try { con.close(); } 674 catch (Exception e) { e.printStackTrace(); } 675 } 676 return users; 677 } 678 679 public void addGroupPermission(Group group, int permissionType) 680 throws UnauthorizedException 681 { 682 Connection con = null; 683 PreparedStatement pstmt = null; 684 try { 685 con = DbConnectionManager.getConnection(); 686 pstmt = con.prepareStatement(ADD_GROUP_PERM); 687 pstmt.setInt(1,id); 688 pstmt.setInt(2,group.getID()); 689 pstmt.setInt(3,permissionType); 690 pstmt.execute(); 691 factory.getCacheManager().remove( 696 DbCacheManager.USER_PERMS_CACHE, 697 new Integer (id) 698 ); 699 } 700 catch( SQLException sqle ) { 701 System.err.println("Error in DbForum.java:" + sqle); 702 sqle.printStackTrace(); 703 } 704 finally { 705 try { pstmt.close(); } 706 catch (Exception e) { e.printStackTrace(); } 707 try { con.close(); } 708 catch (Exception e) { e.printStackTrace(); } 709 } 710 } 711 712 public void removeGroupPermission(Group group, int permissionType) 713 throws UnauthorizedException 714 { 715 Connection con = null; 716 PreparedStatement pstmt = null; 717 try { 718 con = DbConnectionManager.getConnection(); 719 pstmt = con.prepareStatement(REMOVE_GROUP_PERM); 720 pstmt.setInt(1,id); 721 pstmt.setInt(2,group.getID()); 722 pstmt.setInt(3,permissionType); 723 pstmt.execute(); 724 factory.getCacheManager().remove( 729 DbCacheManager.USER_PERMS_CACHE, 730 new Integer (id) 731 ); 732 } 733 catch( SQLException sqle ) { 734 System.err.println("Error in DbForum.java:" + sqle); 735 sqle.printStackTrace(); 736 } 737 finally { 738 try { pstmt.close(); } 739 catch (Exception e) { e.printStackTrace(); } 740 try { con.close(); } 741 catch (Exception e) { e.printStackTrace(); } 742 } 743 } 744 745 public int[] groupsWithPermission(int permissionType) 746 throws UnauthorizedException 747 { 748 int [] groups = new int[0]; 749 Connection con = null; 750 PreparedStatement pstmt = null; 751 try { 752 con = DbConnectionManager.getConnection(); 753 pstmt = con.prepareStatement(GROUPS_WITH_PERM); 754 pstmt.setInt(1,id); 755 pstmt.setInt(2,permissionType); 756 ResultSet rs = pstmt.executeQuery(); 757 ArrayList groupList = new ArrayList (); 758 while (rs.next()) { 759 groupList.add(new Integer (rs.getInt("groupID"))); 760 } 761 groups = new int[groupList.size()]; 762 for (int i=0; i<groups.length; i++) { 763 groups[i] = ((Integer )groupList.get(i)).intValue(); 764 } 765 } 766 catch( SQLException sqle ) { 767 System.err.println("Error in DbForum.groupsWithPermission:" + sqle); 768 sqle.printStackTrace(); 769 } 770 finally { 771 try { pstmt.close(); } 772 catch (Exception e) { e.printStackTrace(); } 773 try { con.close(); } 774 catch (Exception e) { e.printStackTrace(); } 775 } 776 return groups; 777 } 778 779 public ForumMessage applyFilters(ForumMessage message) { 780 for (int i=0; i < filters.length; i++) { 782 message = filters[i].clone(message); 783 } 784 return message; 785 } 786 787 public ForumMessageFilter[] getForumMessageFilters() 788 throws UnauthorizedException 789 { 790 ForumMessageFilter [] dbFilters = new ForumMessageFilter[filters.length]; 791 for (int i=0; i<filters.length; i++) { 792 dbFilters[i] = new DbForumMessageFilter((ForumMessage)filters[i], this); 793 } 794 return dbFilters; 795 } 796 797 public void addForumMessageFilter(ForumMessageFilter filter) 798 throws UnauthorizedException 799 { 800 ArrayList newFilters = new ArrayList (filters.length+1); 801 for (int i=0; i<filters.length; i++) { 802 newFilters.add(filters[i]); 803 } 804 newFilters.add(filter); 805 ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()]; 806 for (int i=0; i<newArray.length; i++) { 807 newArray[i] = (ForumMessageFilter)newFilters.get(i); 808 } 809 filters = newArray; 811 saveFiltersToDb(); 812 } 813 814 public void addForumMessageFilter(ForumMessageFilter filter, int index) 815 throws UnauthorizedException 816 { 817 ArrayList newFilters = new ArrayList (filters.length+1); 818 for (int i=0; i<filters.length; i++) { 819 newFilters.add(filters[i]); 820 } 821 newFilters.add(index, filter); 822 ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()]; 823 for (int i=0; i<newArray.length; i++) { 824 newArray[i] = (ForumMessageFilter)newFilters.get(i); 825 } 826 filters = newArray; 828 saveFiltersToDb(); 829 } 830 831 public void removeForumMessageFilter(int index) 832 throws UnauthorizedException 833 { 834 ArrayList newFilters = new ArrayList (filters.length); 835 for (int i=0; i<filters.length; i++) { 836 newFilters.add(filters[i]); 837 } 838 newFilters.remove(index); 839 ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()]; 840 for (int i=0; i<newArray.length; i++) { 841 newArray[i] = (ForumMessageFilter)newFilters.get(i); 842 } 843 filters = newArray; 845 saveFiltersToDb(); 846 } 847 848 public ForumPermissions getPermissions(Authorization authorization) { 849 int userID = authorization.getUserID(); 850 851 Cache userPermCache = (Cache)factory.getCacheManager().get( 853 DbCacheManager.USER_PERMS_CACHE, 854 new Integer (id) 855 ); 856 857 if (userPermCache != null) { 860 ForumPermissions permissions = 861 (ForumPermissions)userPermCache.get(new Integer (userID)); 862 if (permissions != null) { 863 return permissions; 864 } 865 } 866 867 boolean isAnonymous = (userID == -1); 870 boolean isUser = !isAnonymous; 871 872 ForumPermissions finalPermissions = ForumPermissions.none(); 873 874 if (isUser) { 877 ForumPermissions userPermissions = factory.getUserPermissions(userID, id); 878 finalPermissions = new ForumPermissions(finalPermissions, userPermissions); 880 } 881 ForumPermissions anonyPermissions = null; 883 if (userPermCache != null) { 884 anonyPermissions = (ForumPermissions)userPermCache.get(new Integer (-1)); 885 } 886 if (anonyPermissions == null) { 888 anonyPermissions = factory.getUserPermissions(-1, id); 889 if (userPermCache != null) { 891 userPermCache.add(new Integer (-1), anonyPermissions); 892 } 893 } 894 finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions); 896 897 if (isUser) { 899 ForumPermissions specialUserPermissions = null; 900 if (userPermCache != null) { 902 specialUserPermissions = (ForumPermissions)userPermCache.get(new Integer (0)); 903 } 904 if (specialUserPermissions == null) { 906 specialUserPermissions = factory.getUserPermissions(0, id); 907 if (userPermCache != null) { 909 userPermCache.add(new Integer (0), specialUserPermissions); 910 } 911 } 912 finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions); 914 } 915 916 int [] groups = ((DbProfileManager)factory.getProfileManager()).getUserGroups(userID); 918 for (int i=0; i<groups.length; i++) { 919 ForumPermissions groupPermissions = factory.getGroupPermissions(groups[i], id); 920 finalPermissions = new ForumPermissions(finalPermissions, groupPermissions); 921 } 922 923 if (isUser && userPermCache != null) { 925 userPermCache.add(new Integer (userID), finalPermissions); 926 } 927 928 return finalPermissions; 929 } 930 931 public boolean hasPermission(int type) { 932 return true; 933 } 934 935 937 public int getSize() { 938 int size = 0; 941 size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfInt(); size += CacheSizes.sizeOfString(name); size += CacheSizes.sizeOfString(description); size += CacheSizes.sizeOfDate(); size += CacheSizes.sizeOfDate(); size += CacheSizes.sizeOfBoolean(); size += filters.length * 8; size += CacheSizes.sizeOfProperties(properties); size += CacheSizes.sizeOfObject(); size += CacheSizes.sizeOfBoolean(); 953 return size; 954 } 955 956 958 963 public String toString() { 964 return name; 965 } 966 967 public int hashCode() { 968 return id; 969 } 970 971 public boolean equals(Object object) { 972 if (this == object) { 973 return true; 974 } 975 if (object != null && object instanceof DbForum) { 976 return id == ((DbForum)object).getID(); 977 } 978 else { 979 return false; 980 } 981 } 982 983 987 protected void updateModifiedDate(java.util.Date modifiedDate) { 988 this.modifiedDate = modifiedDate; 989 Connection con = null; 990 PreparedStatement pstmt = null; 991 try { 992 con = DbConnectionManager.getConnection(); 993 pstmt = con.prepareStatement(UPDATE_FORUM_MODIFIED_DATE); 994 pstmt.setString(1, ""+modifiedDate.getTime()); 995 pstmt.setInt(2, id); 996 pstmt.executeUpdate(); 997 } 998 catch( SQLException sqle ) { 999 System.err.println("Error in DbForum:updateModifiedDate()-" + sqle); 1000 sqle.printStackTrace(); 1001 } 1002 finally { 1003 try { pstmt.close(); } 1004 catch (Exception e) { e.printStackTrace(); } 1005 try { con.close(); } 1006 catch (Exception e) { e.printStackTrace(); } 1007 } 1008 } 1009 1010 1013 private void loadProperties() { 1014 synchronized(saveLock) { 1015 Properties newProps = new Properties (); 1016 Connection con = null; 1017 PreparedStatement pstmt = null; 1018 try { 1019 con = DbConnectionManager.getConnection(); 1020 pstmt = con.prepareStatement(LOAD_PROPERTIES); 1021 pstmt.setInt(1, id); 1022 ResultSet rs = pstmt.executeQuery(); 1023 while(rs.next()) { 1024 String name = rs.getString("name"); 1025 String value = rs.getString("propValue"); 1026 newProps.put(name, value); 1027 } 1028 } 1029 catch( SQLException sqle ) { 1030 System.err.println("Error in DbForum:loadProperties():" + sqle); 1031 sqle.printStackTrace(); 1032 } 1033 finally { 1034 try { pstmt.close(); } 1035 catch (Exception e) { e.printStackTrace(); } 1036 try { con.close(); } 1037 catch (Exception e) { e.printStackTrace(); } 1038 } 1039 this.properties = newProps; 1040 } 1041 } 1042 1043 1046 private void saveProperties() { 1047 synchronized(saveLock) { 1048 Connection con = null; 1049 PreparedStatement pstmt = null; 1050 try { 1051 con = DbConnectionManager.getConnection(); 1052 pstmt = con.prepareStatement(DELETE_PROPERTIES); 1054 pstmt.setInt(1, id); 1055 pstmt.execute(); 1056 pstmt.close(); 1057 pstmt = con.prepareStatement(INSERT_PROPERTY); 1059 Enumeration enume = properties.keys(); 1060 while (enume.hasMoreElements()) { 1061 String name = (String )enume.nextElement(); 1062 String value = (String )properties.get(name); 1063 pstmt.setInt(1, id); 1064 pstmt.setString(2, name); 1065 pstmt.setString(3, value); 1066 pstmt.executeUpdate(); 1067 } 1068 } 1069 catch( SQLException sqle ) { 1070 System.err.println(sqle); 1071 } 1072 finally { 1073 try { pstmt.close(); } 1074 catch (Exception e) { e.printStackTrace(); } 1075 try { con.close(); } 1076 catch (Exception e) { e.printStackTrace(); } 1077 } 1078 } 1079 } 1080 1081 1084 private void loadFiltersFromDb() { 1085 ArrayList newFilters = new ArrayList (); 1086 Connection con = null; 1087 boolean abort = false; 1088 boolean supportsTransactions = false; 1089 PreparedStatement pstmt = null; 1090 try { 1091 con = DbConnectionManager.getConnection(); 1092 supportsTransactions = con.getMetaData().supportsTransactions(); 1093 if (supportsTransactions) { 1094 con.setAutoCommit(false); 1095 } 1096 1097 pstmt = con.prepareStatement(LOAD_FILTERS); 1098 pstmt.setInt(1,id); 1099 ResultSet rs = pstmt.executeQuery(); 1100 while(rs.next()) { 1101 try { 1102 ObjectInputStream in = new ObjectInputStream(rs.getBinaryStream("filterObject")); 1103 newFilters.add(in.readObject()); 1104 } 1105 catch (ClassCastException cce) { 1106 } 1110 catch (Exception e) { 1111 e.printStackTrace(); 1112 } 1113 } 1114 } 1115 catch( SQLException sqle ) { 1116 sqle.printStackTrace(); 1117 } 1118 finally { 1119 try { 1120 if (supportsTransactions) { 1121 if (abort == true) { 1122 con.rollback(); 1123 } 1124 else { 1125 con.commit(); 1126 } 1127 } 1128 } 1129 catch (Exception e) { e.printStackTrace(); } 1130 try { 1131 if (supportsTransactions) { 1132 con.setAutoCommit(true); 1133 } 1134 } 1135 catch (Exception e) { e.printStackTrace(); } 1136 try { pstmt.close(); } 1137 catch (Exception e) { e.printStackTrace(); } 1138 try { con.close(); } 1139 catch (Exception e) { e.printStackTrace(); } 1140 1141 } 1142 filters = new ForumMessageFilter[newFilters.size()]; 1143 for (int i=0; i<filters.length; i++) { 1144 filters[i] = (ForumMessageFilter)newFilters.get(i); 1145 } 1146 } 1150 1151 1156 protected void saveFiltersToDb() { 1157 boolean abort = false; 1158 boolean supportsTransactions = false; 1159 synchronized (saveLock) { 1160 Connection con = null; 1161 PreparedStatement pstmt = null; 1162 try { 1163 con = DbConnectionManager.getConnection(); 1164 1165 supportsTransactions = con.getMetaData().supportsTransactions(); 1166 if (supportsTransactions) { 1167 con.setAutoCommit(false); 1168 } 1169 1170 pstmt = con.prepareStatement(DELETE_FILTERS); 1171 pstmt.setInt(1,id); 1172 pstmt.execute(); 1173 pstmt.close(); 1175 pstmt = con.prepareStatement(ADD_FILTER); 1176 for (int i=0; i<filters.length; i++) { 1177 try { 1178 ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); 1179 ObjectOutputStream out = new ObjectOutputStream(byteOut); 1180 out.writeObject(filters[i]); 1181 pstmt.setInt(1,id); 1182 pstmt.setInt(2,i); 1183 pstmt.setBytes(3,byteOut.toByteArray()); 1184 pstmt.execute(); 1185 } 1186 catch (Exception e) { 1187 abort = true; 1188 e.printStackTrace(); 1189 } 1190 } 1191 pstmt.close(); 1192 } 1193 catch( SQLException sqle ) { 1194 abort = true; 1195 sqle.printStackTrace(); 1196 } 1197 finally { 1198 try { 1199 if (supportsTransactions) { 1200 if (abort == true) { 1201 con.rollback(); 1202 } 1203 else { 1204 con.commit(); 1205 } 1206 } 1207 } 1208 catch (Exception e) { e.printStackTrace(); } 1209 try { 1210 if (supportsTransactions) { 1211 con.setAutoCommit(true); 1212 } 1213 } 1214 catch (Exception e) { e.printStackTrace(); } 1215 try { con.close(); } 1216 catch (Exception e) { e.printStackTrace(); } 1217 } 1218 } 1219 } 1220 1221 1224 private void loadFromDb() throws ForumNotFoundException { 1225 Connection con = null; 1226 PreparedStatement pstmt = null; 1227 try { 1228 con = DbConnectionManager.getConnection(); 1229 if (id == -1) { 1231 pstmt = con.prepareStatement(LOAD_FORUM_BY_NAME); 1232 pstmt.setString(1,name); 1233 } 1234 else { 1235 pstmt = con.prepareStatement(LOAD_FORUM_BY_ID); 1236 pstmt.setInt(1, id); 1237 } 1238 ResultSet rs = pstmt.executeQuery(); 1239 if( !rs.next() ) { 1240 throw new ForumNotFoundException("Forum " + getID() + 1241 " could not be loaded from the database."); 1242 } 1243 id = rs.getInt("forumID"); 1244 name = rs.getString("name"); 1245 description = rs.getString("description"); 1246 this.creationDate = 1247 new java.util.Date (Long.parseLong(rs.getString("creationDate").trim())); 1248 this.modifiedDate = 1249 new java.util.Date (Long.parseLong(rs.getString("modifiedDate").trim())); 1250 moderated = rs.getInt("moderated")==1 ? true : false; 1251 isarticle = rs.getInt("article")==1 ? true : false; 1252 order = rs.getInt("forumorder"); 1253 } 1254 catch( SQLException sqle ) { 1255 sqle.printStackTrace(); 1256 throw new ForumNotFoundException("Forum " + getID() + 1257 " could not be loaded from the database."); 1258 } 1259 catch (NumberFormatException nfe) { 1260 System.err.println("WARNING: In DbForum.loadFromDb() -- there " + 1261 "was an error parsing the dates returned from the database. Ensure " + 1262 "that they're being stored correctly."); 1263 } 1264 finally { 1265 try { pstmt.close(); } 1266 catch (Exception e) { e.printStackTrace(); } 1267 try { con.close(); } 1268 catch (Exception e) { e.printStackTrace(); } 1269 } 1270 } 1271 1272 1275 private void insertIntoDb() { 1276 Connection con = null; 1277 PreparedStatement pstmt = null; 1278 try { 1279 con = DbConnectionManager.getConnection(); 1280 pstmt = con.prepareStatement(ADD_FORUM); 1281 pstmt.setInt(1,id); 1282 pstmt.setString(2,name); 1283 pstmt.setString(3,description); 1284 pstmt.setString(4, Long.toString(creationDate.getTime())); 1285 pstmt.setString(5, Long.toString(modifiedDate.getTime())); 1286 pstmt.setInt(6, moderated?1:0); 1287 pstmt.setInt(7, forumGroupID); 1288 pstmt.setInt(8,isarticle?1:0); 1289 pstmt.executeUpdate(); 1290 } 1291 catch( SQLException sqle ) { 1292 System.err.println("Error in DbForum:insertIntoDb()-" + sqle); 1293 sqle.printStackTrace(); 1294 } 1295 finally { 1296 try { pstmt.close(); } 1297 catch (Exception e) { e.printStackTrace(); } 1298 try { con.close(); } 1299 catch (Exception e) { e.printStackTrace(); } 1300 } 1301 } 1302 1303 1306 private synchronized void saveToDb() { 1307 Connection con = null; 1308 PreparedStatement pstmt = null; 1309 try { 1310 con = DbConnectionManager.getConnection(); 1311 pstmt = con.prepareStatement(SAVE_FORUM); 1312 pstmt.setString(1, name); 1313 pstmt.setString(2, description); 1314 pstmt.setString(3, Long.toString(creationDate.getTime())); 1315 pstmt.setString(4, Long.toString(modifiedDate.getTime())); 1316 pstmt.setInt(5, moderated?1:0); 1317 pstmt.setInt(6,order); 1318 pstmt.setInt(7, id); 1319 pstmt.executeUpdate(); 1320 } 1321 catch( SQLException sqle ) { 1322 System.err.println("Error in DbForum:saveToDb()-" + sqle); 1323 sqle.printStackTrace(); 1324 } 1325 finally { 1326 try { pstmt.close(); } 1327 catch (Exception e) { e.printStackTrace(); } 1328 try { con.close(); } 1329 catch (Exception e) { e.printStackTrace(); } 1330 } 1331 } 1332 public boolean isArticleForum(){ 1333 return this.isarticle; 1334 } 1335 public void addArticleMap(String pageKey,ForumThread thread) throws UnauthorizedException{ 1336 Connection con = null; 1337 PreparedStatement pstmt = null; 1338 try { 1339 con = DbConnectionManager.getConnection(); 1340 pstmt = con.prepareStatement(INSERT_ARTICLE_MAP); 1341 pstmt.setInt(1, thread.getID()); 1342 pstmt.setString(2, pageKey); 1343 pstmt.setInt(3, id); 1344 pstmt.executeUpdate(); 1345 } 1346 catch( SQLException sqle ) { 1347 sqle.printStackTrace(); 1348 } 1349 finally { 1350 try { pstmt.close(); } 1351 catch (Exception e) { e.printStackTrace(); } 1352 try { con.close(); } 1353 catch (Exception e) { e.printStackTrace(); } 1354 } 1355 1356 } 1357 public int forumOrder(){ 1358 return order; 1359 } 1360 public void setForumOrder(int param) throws UnauthorizedException{ 1361 this.order=param; 1362 saveToDb(); 1363 } 1364 1365} 1366 | Popular Tags |