1 25 29 package org.jresearch.gossip.dao; 30 31 import java.lang.reflect.InvocationTargetException ; 32 import java.sql.Connection ; 33 import java.sql.PreparedStatement ; 34 import java.sql.ResultSet ; 35 import java.sql.SQLException ; 36 import java.sql.Timestamp ; 37 import java.util.ArrayList ; 38 import java.util.Collections ; 39 import java.util.Date ; 40 import java.util.HashMap ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.Map ; 44 import java.util.Set ; 45 import java.util.StringTokenizer ; 46 47 import org.apache.struts.util.MessageResources; 48 import org.jresearch.gossip.IConst; 49 import org.jresearch.gossip.beans.LogEntry; 50 import org.jresearch.gossip.beans.LogSearchCriteria; 51 import org.jresearch.gossip.beans.RankInfoDTO; 52 import org.jresearch.gossip.beans.forum.Forum; 53 import org.jresearch.gossip.beans.forum.Group; 54 import org.jresearch.gossip.beans.forum.LastTopic; 55 import org.jresearch.gossip.beans.forum.Message; 56 import org.jresearch.gossip.beans.forum.NewTopic; 57 import org.jresearch.gossip.beans.forum.SearchResult; 58 import org.jresearch.gossip.beans.forum.Topic; 59 import org.jresearch.gossip.beans.forum.attachment.FileData; 60 import org.jresearch.gossip.beans.forum.attachment.FileDataInfo; 61 import org.jresearch.gossip.beans.subscription.Subscriber; 62 import org.jresearch.gossip.beans.user.Entry; 63 import org.jresearch.gossip.beans.user.EntryList; 64 import org.jresearch.gossip.beans.user.User; 65 import org.jresearch.gossip.configuration.Configurator; 66 import org.jresearch.gossip.dao.drivers.DbDriver; 67 import org.jresearch.gossip.dao.drivers.keygen.IKeyGenConst; 68 import org.jresearch.gossip.dao.drivers.keygen.KeyGenerator; 69 import org.jresearch.gossip.dao.drivers.keygen.KeyGeneratorFactory; 70 import org.jresearch.gossip.dao.file.FileProcessorFactory; 71 import org.jresearch.gossip.dao.file.IFileProcConst; 72 import org.jresearch.gossip.dao.file.IFileProcessor; 73 import org.jresearch.gossip.exception.ConfiguratorException; 74 import org.jresearch.gossip.exception.SystemException; 75 import org.jresearch.gossip.forms.ForumForm; 76 import org.jresearch.gossip.forms.GroupForm; 77 import org.jresearch.gossip.forms.MessageForm; 78 import org.jresearch.gossip.forms.ProcessForumForm; 79 import org.jresearch.gossip.forms.ProcessMessageForm; 80 import org.jresearch.gossip.forms.ProcessTopicForm; 81 import org.jresearch.gossip.forms.SearchForm; 82 import org.jresearch.gossip.forms.StylesForm; 83 import org.jresearch.gossip.list.Mapping; 84 import org.jresearch.gossip.list.RecordsData; 85 import org.jresearch.gossip.util.MySQLCodec; 86 87 92 public class ForumDAO extends DAO { 93 94 private static ForumDAO instance; 95 96 private final DbDriver dbDriver; 97 98 private final KeyGenerator keyGen; 99 100 private static Object lock = new Object (); 101 102 private Map fileProcessors = Collections.synchronizedMap(new HashMap ()); 103 104 private ForumDAO() { 105 try { 106 this.dbDriver = DbDriver.getInstance(); 107 this.keyGen = KeyGeneratorFactory 108 .getKeyGenerator(IKeyGenConst.DEFAULT_KEYGEN); 109 } catch (SQLException ex) { 110 throw new RuntimeException ("ForumDAO not initialized", ex); 111 } 112 } 113 114 119 public static ForumDAO getInstance() { 120 if (instance == null) { 121 synchronized (lock) { 122 if (instance == null) { 123 instance = new ForumDAO(); 124 } 125 } 126 } 127 return instance; 128 } 129 130 private IFileProcessor getFileProcessor(String fileProcessorName) 131 throws SystemException { 132 IFileProcessor fileProcessor = (IFileProcessor) fileProcessors 133 .get(fileProcessorName); 134 if (fileProcessor == null) { 135 synchronized (fileProcessors) { 136 if (fileProcessor == null) { 137 fileProcessor = FileProcessorFactory.getInstance() 138 .getFileProcessor(fileProcessorName); 139 fileProcessors.put(fileProcessorName, fileProcessor); 140 } 141 } 142 } 143 return fileProcessor; 144 } 145 146 154 public void addForum(ForumForm form) throws SQLException { 155 Connection connection = this.dataSource.getConnection(); 156 PreparedStatement st = connection.prepareStatement(dbDriver 157 .getQueries().getForumQueries().getSql_ADD_FORUM()); 158 159 try { 160 int fid = ((Integer ) keyGen.generateKey( 161 IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_FORUM], connection)) 162 .intValue(); 163 ; 164 st.setString(1, form.getForum_name()); 165 st.setString(2, form.getForum_desc()); 166 st.setInt(3, Integer.parseInt(form.getGroupid())); 167 st.setString(4, form.getForum_sort()); 168 st.setInt(5, fid); 169 st.execute(); 170 } finally { 171 st.close(); 172 connection.close(); 173 } 174 } 175 176 184 public void addGroup(GroupForm form) throws SQLException { 185 Connection connection = this.dataSource.getConnection(); 186 187 PreparedStatement st = connection.prepareStatement(dbDriver 188 .getQueries().getForumQueries().getSql_ADD_GROUP()); 189 190 int gid = ((Integer ) keyGen.generateKey( 191 IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_GROUP], connection)) 192 .intValue(); 193 try { 194 st.setString(1, form.getGroup_name()); 195 st.setString(2, form.getGroup_sort()); 196 st.setInt(3, gid); 197 st.execute(); 198 } finally { 199 st.close(); 200 connection.close(); 201 } 202 } 203 204 218 public int addMessage(MessageForm form, String ip, boolean announce) 219 throws SQLException { 220 Connection connection = this.dataSource.getConnection(); 221 PreparedStatement st = connection.prepareStatement(dbDriver 222 .getQueries().getForumQueries().getSql_ADD_MESSAGE()); 223 int mid = ((Integer ) keyGen.generateKey( 224 IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_MESSAGE], connection)) 225 .intValue(); 226 227 try { 228 Timestamp now = new Timestamp (now().getTime()); 229 st.setString(1, form.getName()); 230 st.setString(2, form.getText()); 231 st.setTimestamp(3, now); 232 st.setString(4, form.getTitle()); 233 st.setInt(5, Integer.parseInt(form.getTid())); 234 st.setString(6, ip); 235 st.setInt(7, mid); 236 st.execute(); 237 238 if (announce) { 239 setThreadSortBy(form.getTid(), 5); 240 } 241 242 updateThreadIntime(form.getTid(), now); 243 } finally { 244 st.close(); 245 connection.close(); 246 } 247 248 return mid; 249 } 250 251 260 public void addMod(String fid, String uid) throws SQLException { 261 Connection connection = this.dataSource.getConnection(); 262 PreparedStatement st = connection.prepareStatement(dbDriver 263 .getQueries().getForumQueries().getSql_CHECK_USER_MOD()); 264 ResultSet rs = null; 265 266 try { 267 st.setString(1, uid); 268 st.setInt(2, Integer.parseInt(fid)); 269 rs = st.executeQuery(); 270 271 if (!rs.next()) { 272 st = connection.prepareStatement(dbDriver.getQueries() 273 .getForumQueries().getSql_ADD_MOD()); 274 st.setString(1, uid); 275 st.setInt(2, Integer.parseInt(fid)); 276 st.execute(); 277 } 278 } finally { 279 if (rs != null) { 280 rs.close(); 281 } 282 283 st.close(); 284 connection.close(); 285 } 286 } 287 288 301 public boolean checkMod(int forumId, User user) throws SQLException { 302 boolean mod = false; 303 Connection connection = this.dataSource.getConnection(); 304 PreparedStatement st = connection.prepareStatement(dbDriver 305 .getQueries().getForumQueries().getSql_CHECK_USER_MOD()); 306 ResultSet rs = null; 307 308 try { 309 st.setInt(1, forumId); 310 st.setString(2, user.getName()); 311 rs = (ResultSet ) st.executeQuery(); 312 mod = (rs.next() || (user.getStatus() > 7)); 313 } finally { 314 if (rs != null) { 315 rs.close(); 316 } 317 318 st.close(); 319 connection.close(); 320 } 321 322 return mod; 323 } 324 325 337 private int countForumTopics(int forumid, PreparedStatement st) 338 throws SQLException { 339 ResultSet rs = null; 340 341 try { 342 st.setInt(1, forumid); 343 rs = (ResultSet ) st.executeQuery(); 344 rs.next(); 345 346 return rs.getInt(1); 347 } finally { 348 if (rs != null) { 349 rs.close(); 350 } 351 } 352 } 353 354 363 public int countForumMessages(int fid) throws SQLException { 364 Connection connection = this.dataSource.getConnection(); 365 PreparedStatement st = connection.prepareStatement(dbDriver 366 .getQueries().getForumQueries().getSql_COUNT_FORUM_MESSAGES()); 367 ResultSet rs = null; 368 369 try { 370 st.setInt(1, fid); 371 rs = st.executeQuery(); 372 rs.next(); 373 374 return rs.getInt(1); 375 } finally { 376 if (rs != null) { 377 rs.close(); 378 } 379 380 st.close(); 381 connection.close(); 382 } 383 } 384 385 394 public int countThreadMessages(int tid) throws SQLException { 395 Connection connection = this.dataSource.getConnection(); 396 PreparedStatement st = connection.prepareStatement(dbDriver 397 .getQueries().getForumQueries().getSql_COUNT_THREAD_MESSAGES()); 398 ResultSet rs = null; 399 400 try { 401 st.setInt(1, tid); 402 rs = st.executeQuery(); 403 rs.next(); 404 405 return rs.getInt(1); 406 } finally { 407 if (rs != null) { 408 rs.close(); 409 } 410 411 st.close(); 412 connection.close(); 413 } 414 } 415 416 private int countLogEntries(LogSearchCriteria criteria) throws SQLException { 417 Connection connection = this.dataSource.getConnection(); 418 PreparedStatement st = connection.prepareStatement(dbDriver 419 .getQueries().getForumQueries().getSql_COUNT_LOG_ENTRIES()); 420 ResultSet rs = null; 421 422 try { 423 st.setTimestamp(1, new Timestamp (criteria.getFrom().getTime())); 424 st.setTimestamp(2, new Timestamp (criteria.getTo().getTime())); 425 if (IConst.VALUES.ALL.equals(criteria.getLogger())) { 426 st.setString(3, "%"); 427 } else { 428 st.setString(3, criteria.getLogger()); 429 } 430 if (IConst.VALUES.ALL.equals(criteria.getLog_level())) { 431 st.setString(4, "%"); 432 } else { 433 st.setString(4, criteria.getLog_level()); 434 } 435 if (IConst.VALUES.ALL.equals(criteria.getRemote_ip())) { 436 st.setString(5, "%"); 437 } else { 438 st.setString(5, criteria.getRemote_ip()); 439 } 440 if (IConst.VALUES.ALL.equals(criteria.getSession_id())) { 441 st.setString(6, "%"); 442 } else { 443 st.setString(6, criteria.getSession_id()); 444 } 445 if (IConst.VALUES.ALL.equals(criteria.getUser_name())) { 446 st.setString(7, "%"); 447 } else { 448 st.setString(7, criteria.getUser_name()); 449 } 450 451 rs = st.executeQuery(); 452 rs.next(); 453 454 return rs.getInt(1); 455 } finally { 456 if (rs != null) { 457 rs.close(); 458 } 459 460 st.close(); 461 connection.close(); 462 } 463 } 464 465 473 public void deleteForum(String fid) throws SQLException { 474 Connection connection = this.dataSource.getConnection(); 475 476 PreparedStatement st = connection.prepareStatement(dbDriver 477 .getQueries().getForumQueries().getSql_DELETE_FORUM()); 478 ResultSet rs = null; 479 480 try { 481 st.setInt(1, Integer.parseInt(fid)); 482 st.execute(); 483 st = connection.prepareStatement(dbDriver.getQueries() 484 .getForumQueries().getSql_GET_THREAD_ID_LIST()); 485 st.setInt(1, Integer.parseInt(fid)); 486 rs = st.executeQuery(); 487 488 while (rs.next()) { 489 deleteThread(rs.getString(1), true); 490 } 491 } finally { 492 if (rs != null) { 493 rs.close(); 494 } 495 496 st.close(); 497 connection.close(); 498 } 499 } 500 501 509 public void deleteGroup(String gid) throws SQLException { 510 Connection connection = this.dataSource.getConnection(); 511 512 PreparedStatement st = connection.prepareStatement(dbDriver 513 .getQueries().getForumQueries().getSql_DELETE_GROUP()); 514 ResultSet rs = null; 515 516 try { 517 st.setInt(1, Integer.parseInt(gid)); 518 st.execute(); 519 st = connection.prepareStatement(dbDriver.getQueries() 520 .getForumQueries().getSql_GET_FORUMS_ID_LIST()); 521 st.setInt(1, Integer.parseInt(gid)); 522 rs = st.executeQuery(); 523 524 while (rs.next()) { 525 deleteForum(rs.getString(1)); 526 } 527 } finally { 528 if (rs != null) { 529 rs.close(); 530 } 531 532 st.close(); 533 connection.close(); 534 } 535 } 536 537 546 public void deleteMessage(ProcessMessageForm form) throws SQLException , 547 SystemException { 548 Connection connection = this.dataSource.getConnection(); 549 PreparedStatement st = connection.prepareStatement(dbDriver 550 .getQueries().getForumQueries().getSql_DELETE_MESSAGE()); 551 ResultSet rs = null; 552 553 try { 554 555 Message mes = getMessage(form.getMid()); 557 if (mes.isHasAttachment()) { 558 FileDataInfo[] attach = mes.getAttachments(); 559 for (int i = 0; i < attach.length; i++) { 560 removeAttachment(attach[i].getId()); 561 } 562 } 563 st.setInt(1, Integer.parseInt(form.getMid())); 565 st.execute(); 566 567 st = connection.prepareStatement(dbDriver.getQueries() 568 .getForumQueries().getSql_GET_THREAD_LAST_INTIME()); 569 st.setInt(1, Integer.parseInt(form.getTid())); 570 rs = st.executeQuery(); 571 572 if (rs.next()) { 573 updateThreadIntime(form.getTid(), rs.getTimestamp(1)); 574 } else { deleteThread(form.getTid()); 576 } 577 } finally { 578 if (rs != null) { 579 rs.close(); 580 } 581 582 st.close(); 583 connection.close(); 584 } 585 } 586 587 595 public void deleteThread(String tid) throws SQLException { 596 deleteThread(tid, false); 597 } 598 599 610 public void deleteThread(String tid, boolean clear) throws SQLException { 611 Connection connection = this.dataSource.getConnection(); 612 int id = Integer.parseInt(tid); 613 PreparedStatement st = connection.prepareStatement(dbDriver 614 .getQueries().getForumQueries().getSql_DELETE_THREAD()); 615 616 try { 617 st.setInt(1, id); 618 st.execute(); 619 620 if (clear) { st = connection.prepareStatement(dbDriver.getQueries() 622 .getForumQueries().getSql_DELETE_THREAD_MESSAGES()); 623 st.setInt(1, id); 624 st.execute(); 625 } 626 627 st = connection.prepareStatement(dbDriver.getQueries() 628 .getForumQueries().getSql_DELETE_THREAD_SUBSCRIPTIONS()); 629 st.setInt(1, id); 630 st.execute(); 631 } finally { 632 st.close(); 633 connection.close(); 634 } 635 } 636 637 646 public void dropMod(String fid, String uid) throws SQLException { 647 Connection connection = this.dataSource.getConnection(); 648 String queryString = (dbDriver.getQueries().getForumQueries() 649 .getSql_DROP_USER_MOD()); 650 PreparedStatement st = connection.prepareStatement(queryString); 651 652 try { 653 st.setString(1, uid); 654 st.setInt(2, Integer.parseInt(fid)); 655 st.execute(); 656 } finally { 657 st.close(); 658 connection.close(); 659 } 660 } 661 662 670 public void fillLogEntryList(LogSearchCriteria criteria, 671 RecordsData recordsData, int currBlock) 672 throws InstantiationException , IllegalAccessException , 673 InvocationTargetException , NoSuchMethodException , SQLException { 674 Connection connection = this.dataSource.getConnection(); 675 String sqlStatement; 676 if (criteria.isSortOrder()) { 677 sqlStatement = dbDriver.getQueries().getForumQueries() 678 .getSql_GET_LOG_ENTRIES_DESC(); 679 } else { 680 sqlStatement = dbDriver.getQueries().getForumQueries() 681 .getSql_GET_LOG_ENTRIES_ASC(); 682 } 683 PreparedStatement st = connection.prepareStatement(sqlStatement); 684 ResultSet rs = null; 685 686 try { 687 st.setTimestamp(1, new Timestamp (criteria.getFrom().getTime())); 688 st.setTimestamp(2, new Timestamp (criteria.getTo().getTime())); 689 if (IConst.VALUES.ALL.equals(criteria.getLogger())) { 690 st.setString(3, "%"); 691 } else { 692 st.setString(3, criteria.getLogger()); 693 } 694 if (IConst.VALUES.ALL.equals(criteria.getLog_level())) { 695 st.setString(4, "%"); 696 } else { 697 st.setString(4, criteria.getLog_level()); 698 } 699 if (IConst.VALUES.ALL.equals(criteria.getRemote_ip())) { 700 st.setString(5, "%"); 701 } else { 702 st.setString(5, criteria.getRemote_ip()); 703 } 704 if (IConst.VALUES.ALL.equals(criteria.getSession_id())) { 705 st.setString(6, "%"); 706 } else { 707 st.setString(6, criteria.getSession_id()); 708 } 709 if (IConst.VALUES.ALL.equals(criteria.getUser_name())) { 710 st.setString(7, "%"); 711 } else { 712 st.setString(7, criteria.getUser_name()); 713 } 714 st.setInt(8, currBlock); 715 st.setInt(9, dbDriver.getLastRowIdx(currBlock, recordsData 716 .getBlockSize())); 717 rs = (ResultSet ) st.executeQuery(); 718 recordsData.fillRecords(rs, Mapping.getInstance().LogEntryMapping, 719 LogEntry.class); 720 recordsData.setCurrBlock(currBlock); 721 recordsData.setRecordsCount(countLogEntries(criteria)); 722 } finally { 723 if (rs != null) { 724 rs.close(); 725 } 726 727 st.close(); 728 connection.close(); 729 } 730 } 731 732 737 public void clearLog() throws SQLException { 738 Connection connection = this.dataSource.getConnection(); 739 PreparedStatement st = connection.prepareStatement(dbDriver 740 .getQueries().getForumQueries().getSql_DELETE_LOG_ALL()); 741 742 try { 743 st.execute(); 744 } finally { 745 st.close(); 746 connection.close(); 747 } 748 } 749 750 768 public void fillMessagesList(User user, RecordsData recordsData, 769 ProcessTopicForm form) throws InstantiationException , 770 IllegalAccessException , InvocationTargetException , 771 NoSuchMethodException , SQLException { 772 Connection connection = this.dataSource.getConnection(); 773 PreparedStatement st = connection.prepareStatement(dbDriver 774 .getQueries().getForumQueries().getSql_GET_THREAD_MESSAGES()); 775 ResultSet rs = null; 776 777 try { 778 int currBlock = Integer.parseInt(form.getBlock()); 779 int tid = Integer.parseInt(form.getTid()); 780 st.setInt(1, tid); 781 st.setInt(2, currBlock); 782 st.setInt(3, dbDriver.getLastRowIdx(currBlock, user.getSettings() 783 .getMes_per_page())); 784 rs = (ResultSet ) st.executeQuery(); 785 recordsData.fillRecords(rs, Mapping.getInstance().MessageMapping, 786 Message.class); 787 recordsData.setBlockSize(user.getSettings().getMes_per_page()); 788 recordsData.setCurrBlock(currBlock); 789 recordsData.setRecordsCount(countThreadMessages(tid)); 790 } finally { 791 if (rs != null) { 792 rs.close(); 793 } 794 795 st.close(); 796 connection.close(); 797 } 798 } 799 800 818 public void fillThreadList(User user, RecordsData recordsData, 819 ProcessForumForm form) throws InstantiationException , 820 IllegalAccessException , InvocationTargetException , 821 NoSuchMethodException , SQLException { 822 Connection connection = this.dataSource.getConnection(); 823 PreparedStatement st = connection.prepareStatement(dbDriver 824 .getQueries().getForumQueries().getSql_GET_THREADS()); 825 ResultSet rs = null; 826 827 try { 828 int currBlock = Integer.parseInt(form.getBlock()); 829 st.setInt(1, Integer.parseInt(form.getFid())); 830 st.setInt(2, currBlock); 831 st.setInt(3, dbDriver.getLastRowIdx(currBlock, user.getSettings() 832 .getMes_per_page())); 833 rs = (ResultSet ) st.executeQuery(); 834 recordsData.fillRecords(rs, Mapping.getInstance().TreadMapping, 835 Topic.class); 836 837 Iterator it = recordsData.getRecords().iterator(); 838 st = connection.prepareStatement(dbDriver.getQueries() 839 .getForumQueries().getSql_GET_THREAD_LAST_MESS()); 840 841 while (it.hasNext()) { 842 setLastMessage((Topic) it.next(), st); 843 } 844 845 st = connection.prepareStatement(dbDriver.getQueries() 846 .getForumQueries().getSql_COUNT_THREADS()); 847 recordsData.setRecordsCount(countForumTopics(Integer.parseInt(form 848 .getFid()), st)); 849 recordsData.setBlockSize(user.getSettings().getMes_per_page()); 850 851 recordsData.setCurrBlock(currBlock); 852 } finally { 853 if (rs != null) { 854 rs.close(); 855 } 856 857 st.close(); 858 connection.close(); 859 } 860 } 861 862 866 public ArrayList getAttachmentsInfo(int mess_id) throws SQLException { 867 ArrayList attachments = new ArrayList (); 868 Connection connection = this.dataSource.getConnection(); 869 PreparedStatement st = connection.prepareStatement(dbDriver 870 .getQueries().getForumQueries().getSql_GET_ATTACHMENTS()); 871 ResultSet rs = null; 872 try { 873 874 st.setInt(1, mess_id); 875 rs = (ResultSet ) st.executeQuery(); 876 while (rs.next()) { 877 FileDataInfo fileData = new FileDataInfo(); 878 fileData.setId(rs.getInt("id")); 879 fileData.setName(rs.getString("attach_name")); 880 fileData.setContentType(rs.getString("attach_content_type")); 881 fileData.setDescription(rs.getString("attach_description")); 882 fileData.setMessageId(rs.getInt("message_id")); 883 fileData.setSize(rs.getInt("attach_size")); 884 attachments.add(fileData); 885 } 886 } finally { 887 if (rs != null) { 888 rs.close(); 889 } 890 891 st.close(); 892 connection.close(); 893 } 894 return attachments; 895 896 } 897 898 905 public FileData getAttachment(int id) throws SQLException , SystemException { 906 FileData fd = new FileData(); 907 fd.setInfo(getAttachmentInfo(id)); 908 IFileProcessor fProc = getFileProcessor(IFileProcConst.ATTACH_FILE_PROCESSOR); 909 byte[] data = fProc.getFileData(IFileProcConst.ATTACH_KEY_PREFIX + id); 910 if (data == null) { 911 data = new byte[0]; 912 } 913 fd.setData(data); 914 return fd; 915 916 } 917 918 922 public void saveAttachments(FileData[] files) throws SQLException , 923 SystemException { 924 Connection connection = this.dataSource.getConnection(); 925 PreparedStatement st = connection.prepareStatement(dbDriver 926 .getQueries().getForumQueries().getSql_ADD_ATTACH_INFO()); 927 IFileProcessor fProc = getFileProcessor(IFileProcConst.ATTACH_FILE_PROCESSOR); 928 try { 929 for (int i = 0; i < files.length; i++) { 930 int id = ((Integer ) keyGen.generateKey( 931 IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_ATTACH], 932 connection)).intValue(); 933 FileData fd = files[i]; 934 st.setInt(1, id); 935 st.setString(2, fd.getInfo().getContentType()); 936 st.setString(3, fd.getInfo().getName()); 937 st.setString(4, fd.getInfo().getDescription()); 938 st.setInt(5, fd.getInfo().getSize()); 939 st.setInt(6, fd.getInfo().getMessageId()); 940 st.execute(); 941 942 fProc.saveFileData(fd.getData(), 943 IFileProcConst.ATTACH_KEY_PREFIX + id); 944 } 945 } finally { 946 st.close(); 947 connection.close(); 948 } 949 } 950 951 955 public void removeAttachment(int id) throws SQLException , SystemException { 956 Connection connection = this.dataSource.getConnection(); 957 PreparedStatement st = connection.prepareStatement(dbDriver 958 .getQueries().getForumQueries().getSql_DELETE_ATTACH()); 959 IFileProcessor fProc = getFileProcessor(IFileProcConst.ATTACH_FILE_PROCESSOR); 960 try { 961 st.setInt(1, id); 962 st.execute(); 963 fProc.removeFileData(IFileProcConst.ATTACH_KEY_PREFIX + id); 964 } finally { 965 st.close(); 966 connection.close(); 967 } 968 } 969 970 973 public void updateAttachmentInfo(FileDataInfo fileInfo) throws SQLException { 974 Connection connection = this.dataSource.getConnection(); 975 PreparedStatement st = connection.prepareStatement(dbDriver 976 .getQueries().getForumQueries().getSql_UPDATE_ATTACH_INFO()); 977 try { 978 st.setInt(3, fileInfo.getId()); 979 st.setString(1, fileInfo.getName()); 980 st.setString(2, fileInfo.getDescription()); 981 st.executeUpdate(); 982 } finally { 983 st.close(); 984 connection.close(); 985 } 986 } 987 988 999 public Forum getForumInfo(int forumid) throws SQLException { 1000 Connection connection = this.dataSource.getConnection(); 1001 PreparedStatement st = connection.prepareStatement(dbDriver 1002 .getQueries().getForumQueries().getSql_FORUM_INFO()); 1003 ResultSet rs = null; 1004 Forum _forum = new Forum(); 1005 1006 try { 1007 st.setInt(1, forumid); 1008 rs = (ResultSet ) st.executeQuery(); 1009 1010 if (rs.next()) { 1011 _forum.setLocked(rs.getInt("locked")); 1012 _forum.setTitle(rs.getString("forumtitle")); 1013 _forum.setDescription(rs.getString("forumdesc")); 1014 _forum.setSort(rs.getString("forum_sort")); 1015 _forum.setGroupid(rs.getInt("groupid")); 1016 } 1017 1018 return _forum; 1019 } finally { 1020 if (rs != null) { 1021 rs.close(); 1022 } 1023 1024 st.close(); 1025 connection.close(); 1026 } 1027 } 1028 1029 1042 public void getForums(Group group, int userStatus) 1043 throws NumberFormatException , SQLException , ConfiguratorException { 1044 getForums(group, userStatus, true); 1045 } 1046 1047 1062 public void getForums(Group group, int userStatus, boolean filled) 1063 throws SQLException , NumberFormatException , ConfiguratorException { 1064 String queryString = ((userStatus < Integer.parseInt(Configurator 1065 .getInstance().get(IConst.CONFIG.INVADER1))) ? dbDriver 1066 .getQueries().getForumQueries().getSql_GET_FORUMS() : dbDriver 1067 .getQueries().getForumQueries().getSql_GET_ALL_FORUMS()); 1068 Connection connection = this.dataSource.getConnection(); 1069 PreparedStatement st = connection.prepareStatement(queryString); 1070 PreparedStatement st2 = connection.prepareStatement(dbDriver 1071 .getQueries().getForumQueries().getSql_GET_LAST_MESS()); 1072 PreparedStatement st3 = connection.prepareStatement(dbDriver 1073 .getQueries().getForumQueries().getSql_COUNT_THREADS()); 1074 ResultSet rs = null; 1075 1076 try { 1077 st.setInt(1, group.getGroupid()); 1078 rs = (ResultSet ) st.executeQuery(); 1079 1080 while (rs.next()) { 1081 Forum forum = new Forum(); 1082 forum.setDescription(rs.getString("forumdesc")); 1083 forum.setForumid(rs.getInt("forumid")); 1084 forum.setLocked(rs.getInt("locked")); 1085 forum.setTitle(rs.getString("forumtitle")); 1086 1087 if (filled) { 1088 1089 forum.setThreadsCount(countForumTopics(forum.getForumid(), 1090 st3)); 1091 forum.setMessCount(this.countForumMessages(forum 1092 .getForumid())); 1093 setLastMessage(forum, st2); 1094 } 1095 1096 group.addForum(forum); 1097 } 1098 } finally { 1099 if (rs != null) { 1100 rs.close(); 1101 } 1102 1103 st3.close(); 1104 st2.close(); 1105 st.close(); 1106 connection.close(); 1107 } 1108 } 1109 1110 1117 public ArrayList getForumsForMod() throws SQLException { 1118 Connection connection = this.dataSource.getConnection(); 1119 PreparedStatement st = connection.prepareStatement(dbDriver 1120 .getQueries().getForumQueries().getSql_GET_FORUMS_FOR_MOD()); 1121 ResultSet rs = null; 1122 ArrayList forumsForMod = new ArrayList (); 1123 1124 try { 1125 rs = st.executeQuery(); 1126 1127 int gid = -1; 1128 1129 while (rs.next()) { 1130 if (rs.getInt("gid") != gid) { 1131 gid = rs.getInt("gid"); 1132 1133 Forum f = new Forum(); 1134 f.setTitle(IConst.JSP.OPTIONS_SEPERATOR); 1135 forumsForMod.add(f); 1136 } 1137 1138 Forum f = new Forum(); 1139 f.setForumid(rs.getInt("fid")); 1140 f.setTitle(rs.getString("forumtitle")); 1141 forumsForMod.add(f); 1142 } 1143 } finally { 1144 if (rs != null) { 1145 rs.close(); 1146 } 1147 1148 st.close(); 1149 connection.close(); 1150 } 1151 1152 return forumsForMod; 1153 } 1154 1155 1171 public void getGroupList(RecordsData recordsData) 1172 throws InstantiationException , IllegalAccessException , 1173 InvocationTargetException , NoSuchMethodException , SQLException { 1174 Connection connection = this.dataSource.getConnection(); 1175 1176 PreparedStatement st = connection.prepareStatement(dbDriver 1177 .getQueries().getForumQueries().getSql_GET_GROUP_LIST()); 1178 ResultSet rs = null; 1179 1180 try { 1181 rs = st.executeQuery(); 1182 recordsData.fillRecords(rs, Mapping.getInstance().GroupMapping, 1183 Group.class); 1184 } finally { 1185 if (rs != null) { 1186 rs.close(); 1187 } 1188 1189 st.close(); 1190 connection.close(); 1191 } 1192 } 1193 1194 1207 public ArrayList getGroups(int userStatus, MessageResources messages) 1208 throws NumberFormatException , SQLException , ConfiguratorException { 1209 return getGroups(userStatus, true); 1210 } 1211 1212 1227 public ArrayList getGroups(int userStatus, boolean filled) 1228 throws NumberFormatException , SQLException , ConfiguratorException { 1229 Connection connection = this.dataSource.getConnection(); 1230 PreparedStatement st = connection.prepareStatement(dbDriver 1231 .getQueries().getForumQueries().getSql_GET_GROUPS()); 1232 ResultSet rs = null; 1233 1234 try { 1235 ArrayList groups = new ArrayList (); 1236 rs = (ResultSet ) st.executeQuery(); 1237 1238 while (rs.next()) { 1239 Group gr = new Group(); 1240 gr.setGroupid(rs.getInt("groupid")); 1241 gr.setName(rs.getString("group_name")); 1242 getForums(gr, userStatus, filled); 1243 groups.add(gr); 1244 } 1245 1246 return groups; 1247 } finally { 1248 if (rs != null) { 1249 rs.close(); 1250 } 1251 1252 st.close(); 1253 connection.close(); 1254 } 1255 } 1256 1257 1269 public List getLastTopics(int maxCount, Date since) throws SQLException { 1270 if (null == since) { 1271 throw new IllegalArgumentException (); 1272 } 1273 1274 java.sql.Date dateSince = new java.sql.Date (since.getTime()); 1275 ArrayList topics = new ArrayList (); 1276 Connection connection = this.dataSource.getConnection(); 1277 PreparedStatement st = connection.prepareStatement(dbDriver 1278 .getQueries().getForumQueries().getSql_GET_LAST_TOPICS()); 1279 PreparedStatement st2 = connection.prepareStatement(dbDriver 1280 .getQueries().getForumQueries().getSql_GET_ROOT_MESS()); 1281 ResultSet rs = null; 1282 1283 try { 1284 st.setTimestamp(1, new Timestamp (dateSince.getTime())); 1285 st.setInt(2, maxCount); 1286 rs = (ResultSet ) st.executeQuery(); 1287 1288 while (rs.next()) { 1289 LastTopic topic = new LastTopic(); 1290 topic.setForumid(rs.getInt("fid")); 1291 topic.setThreadid(rs.getInt("tid")); 1292 topic.setLocked(rs.getInt("locked")); 1293 topic.setSortby(rs.getInt("sortby")); 1294 topic.setMessagesCount(rs.getLong("tot_mes")); 1295 topic.setForumName(rs.getString("forumtitle")); 1296 setRootMessage(topic, st2); 1297 topics.add(topic); 1298 } 1299 } finally { 1300 if (rs != null) { 1301 try { 1302 rs.close(); 1303 } catch (SQLException e) { 1304 } 1305 } 1306 1307 st2.close(); 1308 st.close(); 1309 connection.close(); 1310 } 1311 1312 return topics; 1313 } 1314 1315 1329 public List getLastTopics(int fid, int maxCount, Date since) 1330 throws SQLException { 1331 if (null == since) { 1332 throw new IllegalArgumentException (); 1333 } 1334 1335 java.sql.Date dateSince = new java.sql.Date (since.getTime()); 1336 ArrayList topics = new ArrayList (); 1337 Connection connection = this.dataSource.getConnection(); 1338 PreparedStatement st = connection.prepareStatement(dbDriver 1339 .getQueries().getForumQueries() 1340 .getSql_GET_LAST_TOPICS_IN_FORUM()); 1341 PreparedStatement st2 = connection.prepareStatement(dbDriver 1342 .getQueries().getForumQueries().getSql_GET_ROOT_MESS()); 1343 ResultSet rs = null; 1344 1345 try { 1346 st.setInt(1, fid); 1347 st.setTimestamp(2, new Timestamp (dateSince.getTime())); 1348 st.setInt(3, maxCount); 1349 rs = (ResultSet ) st.executeQuery(); 1350 1351 while (rs.next()) { 1352 LastTopic topic = new LastTopic(); 1353 topic.setForumid(rs.getInt("fid")); 1354 topic.setThreadid(rs.getInt("tid")); 1355 topic.setLocked(rs.getInt("locked")); 1356 topic.setMessagesCount(rs.getLong("tot_mes")); 1357 topic.setForumName(rs.getString("forumtitle")); 1358 setRootMessage(topic, st2); 1359 topics.add(topic); 1360 } 1361 } finally { 1362 if (rs != null) { 1363 try { 1364 rs.close(); 1365 } catch (SQLException e) { 1366 } 1367 } 1368 1369 st2.close(); 1370 st.close(); 1371 connection.close(); 1372 } 1373 1374 return topics; 1375 } 1376 1377 1386 public Message getMessage(String mid) throws SQLException { 1387 Connection connection = this.dataSource.getConnection(); 1388 PreparedStatement st = connection.prepareStatement(dbDriver 1389 .getQueries().getForumQueries().getSql_GET_MESSAGE()); 1390 Message mess = null; 1391 ResultSet rs = null; 1392 1393 try { 1394 st.setInt(1, Integer.parseInt(mid)); 1395 rs = st.executeQuery(); 1396 1397 if (rs.next()) { 1398 mess = new Message(); 1399 mess.setCentents(rs.getString("centents")); 1400 mess.setHeading(rs.getString("heading")); 1401 mess.setId(rs.getInt("id")); 1402 mess.setIntime(rs.getDate("intime")); 1403 mess.setIp(rs.getString("ip")); 1404 mess.setSender(rs.getString("sender")); 1405 } 1406 } finally { 1407 if (rs != null) { 1408 rs.close(); 1409 } 1410 1411 st.close(); 1412 connection.close(); 1413 } 1414 1415 return mess; 1416 } 1417 1418 1429 public int getMessCount(String uid) throws SQLException { 1430 Connection connection = this.dataSource.getConnection(); 1431 PreparedStatement st = connection.prepareStatement(dbDriver 1432 .getQueries().getForumQueries().getSql_GET_USER_MESS_COUNT()); 1433 ResultSet rs = null; 1434 int count = 0; 1435 1436 try { 1437 st.setString(1, uid); 1438 rs = st.executeQuery(); 1439 1440 if (rs.next()) { 1441 count = rs.getInt(1); 1442 } 1443 } finally { 1444 if (rs != null) { 1445 rs.close(); 1446 } 1447 1448 st.close(); 1449 connection.close(); 1450 } 1451 1452 return count; 1453 } 1454 1455 1464 public Topic getThreadInfo(int tid) throws SQLException { 1465 Connection connection = this.dataSource.getConnection(); 1466 PreparedStatement st = connection.prepareStatement(dbDriver 1467 .getQueries().getForumQueries().getSql_THREAD_INFO()); 1468 ResultSet rs = null; 1469 Topic _thread = new Topic(); 1470 1471 try { 1472 st.setInt(1, tid); 1473 rs = (ResultSet ) st.executeQuery(); 1474 1475 if (rs.next()) { 1476 _thread.setLocked(rs.getInt("locked")); 1477 } 1478 _thread.setSubject(getThreadSubject(Integer.toString(tid))); 1479 return _thread; 1480 } finally { 1481 if (rs != null) { 1482 rs.close(); 1483 } 1484 1485 st.close(); 1486 connection.close(); 1487 } 1488 } 1489 1490 1499 public String getThreadSubject(String tid) throws SQLException { 1500 Connection connection = this.dataSource.getConnection(); 1501 PreparedStatement st = connection.prepareStatement(dbDriver 1502 .getQueries().getForumQueries().getSql_GET_THREAD_SUBJ()); 1503 ResultSet rs = null; 1504 String subject = ""; 1505 1506 try { 1507 st.setInt(1, Integer.parseInt(tid)); 1508 rs = st.executeQuery(); 1509 1510 if (rs.next()) { 1511 subject = rs.getString(1); 1512 } 1513 } finally { 1514 if (rs != null) { 1515 rs.close(); 1516 } 1517 1518 st.close(); 1519 connection.close(); 1520 } 1521 1522 return subject; 1523 } 1524 1525 1534 public ArrayList getUserModForums(String login) throws SQLException { 1535 Connection connection = this.dataSource.getConnection(); 1536 1537 PreparedStatement st = connection.prepareStatement(dbDriver 1538 .getQueries().getForumQueries().getSql_GET_USER_MOD_FORUMS()); 1539 ResultSet rs = null; 1540 ArrayList userModForums = new ArrayList (); 1541 1542 try { 1543 st.setString(1, login); 1544 rs = st.executeQuery(); 1545 1546 while (rs.next()) { 1547 Forum f = new Forum(); 1548 f.setForumid(rs.getInt("forumid")); 1549 f.setTitle(rs.getString("forumtitle")); 1550 userModForums.add(f); 1551 } 1552 } finally { 1553 if (rs != null) { 1554 rs.close(); 1555 } 1556 1557 st.close(); 1558 connection.close(); 1559 } 1560 1561 return userModForums; 1562 } 1563 1564 1574 public String insertNewThread(String fid) throws SQLException { 1575 Connection connection = this.dataSource.getConnection(); 1576 PreparedStatement st = connection.prepareStatement(dbDriver 1577 .getQueries().getForumQueries().getSql_INSERT_THREAD()); 1578 try { 1579 int tid = ((Integer ) keyGen 1580 .generateKey( 1581 IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_THREAD], 1582 connection)).intValue(); 1583 st.setInt(1, Integer.parseInt(fid)); 1584 st.setInt(2, tid); 1585 st.execute(); 1586 return String.valueOf(tid); 1587 } finally { 1588 st.close(); 1589 connection.close(); 1590 } 1591 } 1592 1593 1604 public boolean isUserMod(String uid) throws SQLException { 1605 Connection connection = this.dataSource.getConnection(); 1606 1607 PreparedStatement st = connection.prepareStatement(dbDriver 1608 .getQueries().getForumQueries().getSql_IS_USER_MOD()); 1609 ResultSet rs = null; 1610 boolean modFlag = false; 1611 1612 try { 1613 st.setString(1, uid); 1614 rs = st.executeQuery(); 1615 1616 modFlag = rs.next(); 1617 } finally { 1618 if (rs != null) { 1619 rs.close(); 1620 } 1621 1622 st.close(); 1623 connection.close(); 1624 } 1625 1626 return modFlag; 1627 } 1628 1629 1637 public void lockForum(String fid, int locked) throws SQLException { 1638 Connection connection = this.dataSource.getConnection(); 1639 PreparedStatement st = connection.prepareStatement(dbDriver 1640 .getQueries().getForumQueries().getSql_GET_FORUM_LOCKED()); 1641 ResultSet rs = null; 1642 int id = Integer.parseInt(fid); 1643 1644 try { 1645 st.setInt(1, id); 1646 rs = st.executeQuery(); 1647 1648 if (rs.next()) { 1649 st = connection.prepareStatement(dbDriver.getQueries() 1650 .getForumQueries().getSql_SET_FORUM_LOCKED_STATUS()); 1651 st.setInt(1, locked); 1652 st.setInt(2, id); 1653 st.execute(); 1654 } 1655 } finally { 1656 if (rs != null) { 1657 rs.close(); 1658 } 1659 1660 st.close(); 1661 connection.close(); 1662 } 1663 } 1664 1665 1673 public void lockThread(String tid) throws SQLException { 1674 Connection connection = this.dataSource.getConnection(); 1675 PreparedStatement st = connection.prepareStatement(dbDriver 1676 .getQueries().getForumQueries().getSql_GET_THREAD_LOCKED()); 1677 ResultSet rs = null; 1678 int id = Integer.parseInt(tid); 1679 1680 try { 1681 st.setInt(1, id); 1682 rs = st.executeQuery(); 1683 1684 if (rs.next() && (rs.getInt(1) < 2)) { 1685 int newStatus = Math.abs(rs.getInt(1) - 1); 1686 st = connection.prepareStatement(dbDriver.getQueries() 1687 .getForumQueries().getSql_SET_THREAD_LOCKED_STATUS()); 1688 st.setInt(1, newStatus); 1689 st.setInt(2, id); 1690 st.execute(); 1691 } 1692 } finally { 1693 if (rs != null) { 1694 rs.close(); 1695 } 1696 1697 st.close(); 1698 connection.close(); 1699 } 1700 } 1701 1702 1711 public void moveThread(String tid, String nfid) throws SQLException { 1712 Connection connection = this.dataSource.getConnection(); 1713 PreparedStatement st = connection.prepareStatement(dbDriver 1714 .getQueries().getForumQueries().getSql_MOVE_THREAD()); 1715 1716 try { 1717 st.setInt(1, Integer.parseInt(nfid)); 1718 st.setInt(2, Integer.parseInt(tid)); 1719 st.execute(); 1720 } finally { 1721 st.close(); 1722 connection.close(); 1723 } 1724 } 1725 1726 1736 private void setLastMessage(Forum forum, PreparedStatement st) 1737 throws SQLException { 1738 ResultSet rs = null; 1739 1740 try { 1741 st.setInt(1, forum.getForumid()); 1742 rs = (ResultSet ) st.executeQuery(); 1743 1744 Message last = new Message(); 1745 1746 if (rs.next()) { 1747 last.setSender(rs.getString("m_from")); 1748 last.setIntime(rs.getTimestamp("t_stamp")); 1749 last.setCentents(rs.getString("cont")); 1750 last.setThreadSort(rs.getInt("sortby")); 1751 last.setId(rs.getInt("id")); 1752 forum.setLastMessThreadId(rs.getInt("tid")); 1753 } 1754 1755 forum.setLastMessage(last); 1756 } finally { 1757 if (rs != null) { 1758 rs.close(); 1759 } 1760 } 1761 } 1762 1763 private void setLastMessage(Topic thread, PreparedStatement st) 1764 throws SQLException { 1765 ResultSet rs = null; 1766 1767 try { 1768 st.setInt(1, thread.getThreadid()); 1769 rs = (ResultSet ) st.executeQuery(); 1770 1771 Message last = new Message(); 1772 1773 if (rs.next()) { 1774 last.setSender(rs.getString("sender")); 1775 last.setIntime(rs.getTimestamp("intime")); 1776 last.setCentents(rs.getString("centents")); 1777 last.setId(rs.getInt("id")); 1778 } 1779 1780 thread.setLastMessage(last); 1781 1782 thread.setSubject(getThreadSubject(Integer.toString(thread 1784 .getThreadid()))); 1785 } finally { 1786 if (rs != null) { 1787 rs.close(); 1788 } 1789 } 1790 } 1791 1792 private void setRootMessage(LastTopic topic, PreparedStatement st) 1793 throws SQLException { 1794 ResultSet rs = null; 1795 1796 try { 1797 st.setInt(1, topic.getThreadid()); 1798 rs = (ResultSet ) st.executeQuery(); 1799 1800 Message root = new Message(); 1801 1802 if (rs.next()) { 1803 root.setSender(rs.getString("sender")); 1804 root.setIntime(rs.getTimestamp("t_stamp")); 1805 root.setCentents(rs.getString("centents")); 1806 root.setId(rs.getInt("id")); 1807 root.setHeading(rs.getString("subject")); 1808 root.setIp(rs.getString("ip")); 1809 root.setThreadid(rs.getInt("tid")); 1810 root.setThreadSort(rs.getInt("sortby")); 1811 } 1812 1813 topic.setRootMessage(root); 1814 topic.setSubject(root.getHeading()); 1815 } finally { 1816 if (rs != null) { 1817 rs.close(); 1818 } 1819 } 1820 } 1821 1822 1830 public void updateMessage(MessageForm messageForm) throws SQLException { 1831 Connection connection = this.dataSource.getConnection(); 1832 PreparedStatement st = connection.prepareStatement(dbDriver 1833 .getQueries().getForumQueries().getSql_UPDATE_MESSAGE()); 1834 1835 try { 1836 st.setString(1, messageForm.getText()); 1837 st.setString(2, messageForm.getTitle()); 1838 st.setInt(3, Integer.parseInt(messageForm.getMid())); 1839 st.execute(); 1840 } finally { 1841 st.close(); 1842 connection.close(); 1843 } 1844 } 1845 1846 private void updateThreadIntime(String tid, Timestamp intime) 1847 throws SQLException { 1848 Connection connection = this.dataSource.getConnection(); 1849 PreparedStatement st = connection.prepareStatement(dbDriver 1850 .getQueries().getForumQueries().getSql_UPDATE_THREAD_LINTIME()); 1851 1852 try { 1853 st.setTimestamp(1, intime); 1854 st.setInt(2, Integer.parseInt(tid)); 1855 st.execute(); 1856 } finally { 1857 st.close(); 1858 connection.close(); 1859 } 1860 } 1861 1862 1871 public Group getGroupInfo(String gid) throws SQLException { 1872 Connection connection = this.dataSource.getConnection(); 1873 PreparedStatement st = connection.prepareStatement(dbDriver 1874 .getQueries().getForumQueries().getSql_GET_GROUP_INFO()); 1875 ResultSet rs = null; 1876 Group group = new Group(); 1877 1878 try { 1879 st.setInt(1, Integer.parseInt(gid)); 1880 rs = st.executeQuery(); 1881 1882 if (rs.next()) { 1883 group.setName(rs.getString("group_name")); 1884 group.setSort(rs.getString("group_sort")); 1885 group.setGroupid(rs.getInt("groupid")); 1886 } 1887 } finally { 1888 if (rs != null) { 1889 rs.close(); 1890 } 1891 1892 st.close(); 1893 connection.close(); 1894 } 1895 1896 return group; 1897 } 1898 1899 1907 public void updateGroup(GroupForm form) throws SQLException { 1908 Connection connection = this.dataSource.getConnection(); 1909 PreparedStatement st = connection.prepareStatement(dbDriver 1910 .getQueries().getForumQueries().getSql_UPDATE_GROUP()); 1911 1912 try { 1913 st.setString(1, form.getGroup_name()); 1914 st.setString(2, form.getGroup_sort()); 1915 st.setInt(3, Integer.parseInt(form.getGid())); 1916 st.execute(); 1917 } finally { 1918 st.close(); 1919 connection.close(); 1920 } 1921 } 1922 1923 1931 public void updateForum(ForumForm form) throws SQLException { 1932 Connection connection = this.dataSource.getConnection(); 1933 PreparedStatement st = connection.prepareStatement(dbDriver 1934 .getQueries().getForumQueries().getSql_UPDATE_FORUM()); 1935 1936 try { 1937 st.setString(1, form.getForum_name()); 1938 st.setString(2, form.getForum_desc()); 1939 st.setInt(3, Integer.parseInt(form.getGroupid())); 1940 st.setString(4, form.getForum_sort()); 1941 st.setInt(5, Integer.parseInt(form.getForumid())); 1942 st.execute(); 1943 } finally { 1944 st.close(); 1945 connection.close(); 1946 } 1947 } 1948 1949 1967 public void processSearch(SearchForm form, RecordsData recordsData, 1968 boolean showInvisible) throws InstantiationException , 1969 IllegalAccessException , InvocationTargetException , 1970 NoSuchMethodException , SQLException { 1971 Connection connection = this.dataSource.getConnection(); 1972 StringBuffer queryString = new StringBuffer (); 1973 1974 if (showInvisible) { 1975 queryString.append(" WHERE ("); 1976 } else { 1977 queryString.append(" WHERE jrf_forum.locked < 3 AND ("); 1978 } 1979 1980 String [] fields = { " centents", " heading" }; 1981 String searchType = (form.getType()); 1982 String search = MySQLCodec.encode(form.getSearch()); 1983 1984 if (searchType.equals(IConst.VALUES.WHOLE)) { 1985 queryString.append(fields[0]); 1986 queryString.append(dbDriver.getQueries().getSql_LIKE()); 1987 queryString.append("'%"); 1988 queryString.append(search); 1989 queryString.append("%'"); 1990 queryString.append(dbDriver.getQueries().getSql_OR()); 1991 queryString.append(fields[1]); 1992 queryString.append(dbDriver.getQueries().getSql_LIKE()); 1993 queryString.append("'%"); 1994 queryString.append(search); 1995 queryString.append("%'"); 1996 } else { 1997 String term = ""; 1998 1999 if (searchType.equals(IConst.VALUES.ALL)) { 2000 term = dbDriver.getQueries().getSql_AND(); 2001 } else { 2002 term = dbDriver.getQueries().getSql_OR(); 2003 } 2004 2005 for (int i = 0; i < fields.length; i++) { 2006 queryString.append("("); 2007 2008 StringTokenizer tokens = new StringTokenizer (search, " "); 2009 2010 while (tokens.hasMoreTokens()) { 2011 String token = tokens.nextToken(); 2012 queryString.append(fields[i]); 2013 queryString.append(dbDriver.getQueries().getSql_LIKE()); 2014 queryString.append("'%"); 2015 queryString.append(token); 2016 queryString.append("%'"); 2017 2018 if (tokens.hasMoreTokens()) { 2019 queryString.append(term); 2020 } 2021 } 2022 2023 queryString.append(")"); 2024 2025 if (i < (fields.length - 1)) { 2026 queryString.append(dbDriver.getQueries().getSql_OR()); 2027 } 2028 } 2029 } 2030 2031 queryString.append(")"); 2032 queryString.append(dbDriver.getQueries().getForumQueries() 2033 .getSql_SEARCH_QUERY_END()); 2034 2035 PreparedStatement st = connection.prepareStatement(dbDriver 2036 .getQueries().getForumQueries().getSql_SEARCH_QUERY_SUFF() 2037 + queryString.toString()); 2038 ResultSet rs = null; 2039 2040 try { 2041 rs = st.executeQuery(); 2042 recordsData.fillRecords(rs, Mapping.getInstance().SearchMapping, 2043 SearchResult.class); 2044 st = connection.prepareStatement(dbDriver.getQueries() 2045 .getForumQueries().getSql_SEARCH_QUERY_SUFF_COUNT() 2046 + queryString.toString()); 2047 rs = st.executeQuery(); 2048 2049 if (rs.next()) { 2050 recordsData.setRecordsCount(rs.getInt(1)); 2051 } 2052 } finally { 2053 if (rs != null) { 2054 rs.close(); 2055 } 2056 2057 st.close(); 2058 connection.close(); 2059 } 2060 } 2061 2062 2071 public void setThreadSortBy(String tid, int i) throws SQLException { 2072 Connection connection = this.dataSource.getConnection(); 2073 2074 PreparedStatement st = connection.prepareStatement(dbDriver 2075 .getQueries().getForumQueries().getSql_SET_THREAD_SORT_BY()); 2076 2077 try { 2078 st.setInt(1, i); 2079 st.setInt(2, Integer.parseInt(tid)); 2080 st.execute(); 2081 } finally { 2082 st.close(); 2083 connection.close(); 2084 } 2085 } 2086 2087 2095 public void updateConstants(Map map) throws SQLException { 2096 Connection connection = this.dataSource.getConnection(); 2097 Set keySet = map.keySet(); 2098 Iterator it = keySet.iterator(); 2099 2100 PreparedStatement st = null; 2101 2102 try { 2103 st = connection.prepareStatement(dbDriver.getQueries() 2104 .getForumQueries().getSql_UPDATE_CONSTANTS()); 2105 2106 while (it.hasNext()) { 2107 String key = (String ) it.next(); 2108 2109 st.setString(1, (String ) map.get(key)); 2110 st.setString(2, key); 2111 st.execute(); 2112 } 2113 } finally { 2114 if (st != null) { 2115 st.close(); 2116 } 2117 2118 connection.close(); 2119 } 2120 } 2121 2122 2133 public ArrayList getSubscribersList(String tid, String uname) 2134 throws SQLException { 2135 ArrayList list = new ArrayList (); 2136 Connection connection = this.dataSource.getConnection(); 2137 PreparedStatement st = connection.prepareStatement(dbDriver 2138 .getQueries().getForumQueries().getSql_GET_SUBSCRIBERS_LIST()); 2139 ResultSet rs = null; 2140 2141 try { 2142 st.setInt(1, Integer.parseInt(tid)); 2143 st.setString(2, uname); 2144 rs = st.executeQuery(); 2145 2146 while (rs.next()) { 2147 Subscriber s = new Subscriber(); 2148 s.setEmail(rs.getString("user_mail")); 2149 s.setName(rs.getString("user_name")); 2150 list.add(s); 2151 } 2152 } finally { 2153 if (rs != null) { 2154 rs.close(); 2155 } 2156 2157 st.close(); 2158 connection.close(); 2159 } 2160 2161 return list; 2162 } 2163 2164 2174 public void subscribe(String tid, String email, String name) 2175 throws SQLException { 2176 Connection connection = this.dataSource.getConnection(); 2177 PreparedStatement st = connection.prepareStatement(dbDriver 2178 .getQueries().getForumQueries().getSql_GET_SUBSCRIPTION()); 2179 ResultSet rs = null; 2180 2181 try { 2182 st.setInt(1, Integer.parseInt(tid)); 2183 st.setString(2, email); 2184 st.setString(3, name); 2185 rs = st.executeQuery(); 2186 2187 if (!rs.next()) { 2188 st = connection.prepareStatement(dbDriver.getQueries() 2189 .getForumQueries().getSql_ADD_SUBSCRIPTION()); 2190 st.setInt(1, Integer.parseInt(tid)); 2191 st.setString(2, email); 2192 st.setString(3, name); 2193 st.execute(); 2194 } 2195 } finally { 2196 if (rs != null) { 2197 rs.close(); 2198 } 2199 2200 st.close(); 2201 connection.close(); 2202 } 2203 } 2204 2205 2217 public boolean unsubscribe(String email, String name, String tid) 2218 throws SQLException { 2219 Connection connection = this.dataSource.getConnection(); 2220 2221 PreparedStatement st = null; 2222 2223 if (Integer.parseInt(tid) > 0) { 2224 st = connection.prepareStatement(dbDriver.getQueries() 2225 .getForumQueries().getSql_GET_SUBSCRIPTION()); 2226 st.setInt(1, Integer.parseInt(tid)); 2227 st.setString(2, email); 2228 st.setString(3, name); 2229 } else { 2230 st = connection.prepareStatement(dbDriver.getQueries() 2231 .getForumQueries().getSql_GET_SUBSCRIPTION_ALL()); 2232 st.setString(1, email); 2233 st.setString(2, name); 2234 } 2235 2236 ResultSet rs = null; 2237 boolean success = false; 2238 2239 try { 2240 rs = st.executeQuery(); 2241 2242 if (rs.next()) { 2243 if (Integer.parseInt(tid) > 0) { 2244 st = connection.prepareStatement(dbDriver.getQueries() 2245 .getForumQueries().getSql_DELETE_SUBSCRIPTION()); 2246 st.setInt(1, Integer.parseInt(tid)); 2247 st.setString(2, email); 2248 st.setString(3, name); 2249 } else { 2250 st = connection 2251 .prepareStatement(dbDriver.getQueries() 2252 .getForumQueries() 2253 .getSql_DELETE_SUBSCRIPTION_ALL()); 2254 st.setString(1, email); 2255 st.setString(2, name); 2256 } 2257 2258 st.execute(); 2259 success = true; 2260 } 2261 } finally { 2262 if (rs != null) { 2263 rs.close(); 2264 } 2265 2266 st.close(); 2267 connection.close(); 2268 } 2269 2270 return success; 2271 } 2272 2273 2291 public void fillSubscriptionList(User user, RecordsData recordsData, 2292 String block) throws InstantiationException , 2293 IllegalAccessException , InvocationTargetException , 2294 NoSuchMethodException , SQLException { 2295 Connection connection = this.dataSource.getConnection(); 2296 PreparedStatement st = connection.prepareStatement(dbDriver 2297 .getQueries().getForumQueries().getSql_COUNT_SUBSCRIPTIONS()); 2298 ResultSet rs = null; 2299 2300 try { 2301 st.setString(1, user.getName()); 2302 rs = (ResultSet ) st.executeQuery(); 2303 2304 if (rs.next()) { 2305 int blockSize = user.getSettings().getMes_per_page() * 2; 2306 int currBlock = Integer.parseInt(block); 2307 2308 recordsData.setRecordsCount(rs.getInt(1)); 2309 recordsData.setBlockSize(blockSize); 2310 recordsData.setCurrBlock(currBlock); 2311 st = connection.prepareStatement(dbDriver.getQueries() 2312 .getForumQueries().getSql_GET_USER_SUBSCRIPTIONS()); 2313 st.setString(1, user.getName()); 2314 st.setInt(2, currBlock); 2315 st.setInt(3, dbDriver.getLastRowIdx(currBlock, blockSize)); 2316 rs = st.executeQuery(); 2317 recordsData.fillRecords(rs, 2318 Mapping.getInstance().SubscriptionMapping, 2319 NewTopic.class); 2320 } 2321 } finally { 2322 if (rs != null) { 2323 rs.close(); 2324 } 2325 2326 st.close(); 2327 connection.close(); 2328 } 2329 } 2330 2331 2340 public int[] dropOld(String period) throws SQLException { 2341 Connection connection = this.dataSource.getConnection(); 2342 PreparedStatement st = connection.prepareStatement(dbDriver 2343 .getQueries().getForumQueries().getSql_GET_OLD_TOPICS()); 2344 ResultSet rs = null; 2345 int[] wasDeleted = new int[2]; 2346 2347 try { 2348 st.setInt(1, Integer.parseInt(period)); 2349 rs = (ResultSet ) st.executeQuery(); 2350 2351 while (rs.next()) { 2352 wasDeleted[0]++; 2353 wasDeleted[1] += rs.getInt("cc"); 2354 this.deleteThread(rs.getString("threadid"), true); 2355 } 2356 } finally { 2357 if (rs != null) { 2358 rs.close(); 2359 } 2360 2361 st.close(); 2362 connection.close(); 2363 } 2364 2365 return wasDeleted; 2366 } 2367 2368 2386 public void fillLastUpdatedTopicList(User user, RecordsData recordsData, 2387 String block) throws InstantiationException , 2388 IllegalAccessException , InvocationTargetException , 2389 NoSuchMethodException , SQLException { 2390 Connection connection = this.dataSource.getConnection(); 2391 PreparedStatement st = null; 2392 2393 if (user.getStatus() < 7) { 2394 st = connection.prepareStatement(dbDriver.getQueries() 2395 .getForumQueries().getSql_GET_LAST_UPDATED_TOPICS()); 2396 } else { 2397 st = connection.prepareStatement(dbDriver.getQueries() 2398 .getForumQueries().getSql_GET_LAST_UPDATED_TOPICS_ALL()); 2399 } 2400 2401 ResultSet rs = null; 2402 2403 try { 2404 int blockSize = user.getSettings().getMes_per_page() * 2; 2405 int currBlock = Integer.parseInt(block); 2406 st.setTimestamp(1, new Timestamp (user.getIntime().getTime())); 2407 st.setInt(2, currBlock); 2408 st.setInt(3, dbDriver.getLastRowIdx(currBlock, blockSize)); 2409 rs = (ResultSet ) st.executeQuery(); 2410 recordsData.fillRecords(rs, Mapping.getInstance().NewTreadMapping, 2411 NewTopic.class); 2412 recordsData.setBlockSize(blockSize); 2413 2414 recordsData.setCurrBlock(currBlock); 2415 Iterator it = recordsData.getRecords().iterator(); 2416 st = connection.prepareStatement(dbDriver.getQueries() 2417 .getForumQueries().getSql_GET_THREAD_LAST_MESS()); 2418 2419 while (it.hasNext()) { 2420 NewTopic nt = (NewTopic) it.next(); 2421 setLastMessage(nt, st); 2422 nt.setForumName(getForumInfo(nt.getForumid()).getTitle()); 2423 } 2424 2425 if (user.getStatus() < 7) { 2426 st = connection.prepareStatement(dbDriver.getQueries() 2427 .getForumQueries().getSql_COUNT_NEW_THREADS()); 2428 } else { 2429 st = connection.prepareStatement(dbDriver.getQueries() 2430 .getForumQueries().getSql_COUNT_NEW_THREADS_ALL()); 2431 } 2432 2433 st.setTimestamp(1, new Timestamp (user.getIntime().getTime())); 2434 rs = (ResultSet ) st.executeQuery(); 2435 rs.next(); 2436 recordsData.setRecordsCount(rs.getInt(1)); 2437 } finally { 2438 if (rs != null) { 2439 rs.close(); 2440 } 2441 2442 st.close(); 2443 connection.close(); 2444 } 2445 } 2446 2447 2457 public int getMessBlock(User user, ProcessMessageForm form) 2458 throws SQLException { 2459 int block = 0; 2460 Connection connection = this.dataSource.getConnection(); 2461 PreparedStatement st = connection.prepareStatement(dbDriver 2462 .getQueries().getForumQueries().getSql_GET_MESS_INTIME()); 2463 ResultSet rs = null; 2464 2465 try { 2466 st.setInt(1, Integer.parseInt(form.getMid())); 2467 rs = st.executeQuery(); 2468 2469 int position = 0; 2470 Timestamp intime = null; 2471 2472 if (rs.next()) { 2473 intime = rs.getTimestamp(1); 2474 } else { 2475 return IConst.VALUES.NOT_EXIST; 2476 } 2477 2478 st = connection.prepareStatement(dbDriver.getQueries() 2479 .getForumQueries().getSql_GET_MESS_POS_BY_INTIME()); 2480 st.setInt(1, Integer.parseInt(form.getTid())); 2481 st.setTimestamp(2, intime); 2482 rs = st.executeQuery(); 2483 2484 if (rs.next()) { 2485 position = rs.getInt(1); 2486 } 2487 int mpp = user.getSettings().getMes_per_page(); 2488 block = (int) (((Math.floor(position / mpp)) * mpp)); 2489 block = (block > 0 && (position % mpp == 0)) ? block - mpp : block; 2490 } finally { 2491 if (rs != null) { 2492 rs.close(); 2493 } 2494 2495 st.close(); 2496 connection.close(); 2497 } 2498 2499 return block; 2500 } 2501 2502 2510 public void fillEntryList(EntryList entrylist) throws SQLException { 2511 Connection connection = this.dataSource.getConnection(); 2512 PreparedStatement st = connection.prepareStatement(dbDriver 2513 .getQueries().getForumQueries().getSql_GET_ENTRY_LIST()); 2514 ResultSet rs = null; 2515 2516 try { 2517 rs = st.executeQuery(); 2518 2519 while (rs.next()) { 2520 entrylist.put(new Entry(rs.getString("user_name"), rs 2521 .getString("sessionid"), rs.getString("ip"))); 2522 } 2523 2524 st = connection.prepareStatement(dbDriver.getQueries() 2525 .getForumQueries().getSql_COUNT_ENTRIES()); 2526 rs = st.executeQuery(); 2527 2528 if (rs.next()) { 2529 entrylist.setTotal(rs.getInt(1)); 2530 } 2531 } finally { 2532 if (rs != null) { 2533 rs.close(); 2534 } 2535 2536 st.close(); 2537 connection.close(); 2538 } 2539 } 2540 2541 2549 public void updateEntry(Entry entry) throws SQLException { 2550 Connection connection = this.dataSource.getConnection(); 2551 PreparedStatement st = connection.prepareStatement(dbDriver 2552 .getQueries().getForumQueries().getSql_UPDATE_ENTRY()); 2553 2554 try { 2555 st.setString(1, entry.getLogin()); 2556 st.setString(2, entry.getSessionId()); 2557 st.execute(); 2558 } finally { 2559 st.close(); 2560 connection.close(); 2561 } 2562 } 2563 2564 2572 public void addEntry(Entry entry) throws SQLException { 2573 Connection connection = this.dataSource.getConnection(); 2574 PreparedStatement st = connection.prepareStatement(dbDriver 2575 .getQueries().getForumQueries().getSql_ADD_ENTRY()); 2576 2577 try { 2578 int id = ((Integer ) keyGen.generateKey( 2579 IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_WHOIS], connection)) 2580 .intValue(); 2581 ; 2582 st.setString(1, entry.getSessionId()); 2583 st.setString(2, entry.getIp()); 2584 st.setString(3, entry.getLogin()); 2585 st.setInt(4, id); 2586 st.execute(); 2587 } finally { 2588 st.close(); 2589 connection.close(); 2590 } 2591 } 2592 2593 2601 public void removeEntry(String string) throws SQLException { 2602 Connection connection = this.dataSource.getConnection(); 2603 PreparedStatement st = connection.prepareStatement(dbDriver 2604 .getQueries().getForumQueries().getSql_DELETE_ENTRY()); 2605 2606 try { 2607 st.setString(1, string); 2608 st.setString(2, string); 2609 st.execute(); 2610 } finally { 2611 st.close(); 2612 connection.close(); 2613 } 2614 } 2615 2616 2625 public boolean isEntryExist(String string) throws SQLException { 2626 Connection connection = this.dataSource.getConnection(); 2627 PreparedStatement st = connection.prepareStatement(dbDriver 2628 .getQueries().getForumQueries().getSql_CHECK_ENTRY()); 2629 ResultSet rs = null; 2630 boolean exist = false; 2631 2632 try { 2633 st.setString(1, string); 2634 rs = rs = st.executeQuery(); 2635 exist = rs.next(); 2636 } finally { 2637 if (null != rs) { 2638 rs.close(); 2639 } 2640 2641 st.close(); 2642 connection.close(); 2643 } 2644 2645 return exist; 2646 } 2647 2648 2655 public ArrayList getEntryList() throws SQLException { 2656 Connection connection = this.dataSource.getConnection(); 2657 PreparedStatement st = connection.prepareStatement(dbDriver 2658 .getQueries().getForumQueries().getSql_GET_ENTRY_LIST_FULL()); 2659 ResultSet rs = null; 2660 ArrayList list = new ArrayList (); 2661 2662 try { 2663 rs = st.executeQuery(); 2664 2665 while (rs.next()) { 2666 list.add(new Entry(rs.getString("user_name"), rs 2667 .getString("sessionid"), rs.getString("ip"))); 2668 } 2669 } finally { 2670 if (rs != null) { 2671 rs.close(); 2672 } 2673 2674 st.close(); 2675 connection.close(); 2676 } 2677 2678 return list; 2679 } 2680 2681 2690 public void loadSkinParams(int id, HashMap SkinParams) throws SQLException { 2691 Connection connection = this.dataSource.getConnection(); 2692 PreparedStatement st = connection.prepareStatement(dbDriver 2693 .getQueries().getForumQueries().getSql_GET_SKIN_PARAMS()); 2694 ResultSet rs = null; 2695 2696 try { 2697 st.setInt(1, id); 2698 rs = st.executeQuery(); 2699 2700 while (rs.next()) { 2701 SkinParams.put(rs.getString("param_name"), rs 2702 .getString("param_value")); 2703 } 2704 } finally { 2705 if (rs != null) { 2706 rs.close(); 2707 } 2708 2709 st.close(); 2710 connection.close(); 2711 } 2712 } 2713 2714 2722 public void updateStyles(StylesForm form) throws SQLException { 2723 Connection connection = this.dataSource.getConnection(); 2724 PreparedStatement st = connection.prepareStatement(dbDriver 2725 .getQueries().getForumQueries().getSql_UPDATE_SKIN_PARAM()); 2726 Set keys = form.getKeys(); 2727 2728 try { 2729 Iterator it = keys.iterator(); 2730 2731 while (it.hasNext()) { 2732 String key = (String ) it.next(); 2733 st.setInt(3, Integer.parseInt(form.getSkinid())); 2734 st.setString(2, key); 2735 st.setString(1, (String ) form.getValue(key)); 2736 st.executeUpdate(); 2737 } 2738 } finally { 2739 st.close(); 2740 connection.close(); 2741 } 2742 } 2743 2744 2750 public List getUpdatedTopics(User user) throws SQLException { 2751 if (null == user) { 2752 throw new IllegalArgumentException (); 2753 } 2754 2755 ArrayList topics = new ArrayList (); 2756 Connection connection = this.dataSource.getConnection(); 2757 PreparedStatement st = connection.prepareStatement(dbDriver 2758 .getQueries().getForumQueries().getSql_GET_UPDATED_TOPICS()); 2759 ResultSet rs = null; 2760 2761 try { 2762 st.setString(1, user.getName()); 2763 rs = (ResultSet ) st.executeQuery(); 2764 2765 while (rs.next()) { 2766 topics.add(rs.getString("tid")); 2767 } 2768 } finally { 2769 if (rs != null) { 2770 try { 2771 rs.close(); 2772 } catch (SQLException e) { 2773 } 2774 } 2775 2776 st.close(); 2777 connection.close(); 2778 } 2779 2780 return topics; 2781 } 2782 2783 2790 public List getUpdatedTopics(User user, int fid) throws SQLException { 2791 if (null == user) { 2792 throw new IllegalArgumentException (); 2793 } 2794 2795 ArrayList topics = new ArrayList (); 2796 Connection connection = this.dataSource.getConnection(); 2797 PreparedStatement st = connection.prepareStatement(dbDriver 2798 .getQueries().getForumQueries() 2799 .getSql_GET_UPDATED_TOPICS_IN_FORUM()); 2800 ResultSet rs = null; 2801 2802 try { 2803 st.setString(1, user.getName()); 2804 st.setInt(2, fid); 2805 rs = (ResultSet ) st.executeQuery(); 2806 2807 while (rs.next()) { 2808 topics.add(rs.getString("tid")); 2809 } 2810 } finally { 2811 if (rs != null) { 2812 try { 2813 rs.close(); 2814 } catch (SQLException e) { 2815 } 2816 } 2817 2818 st.close(); 2819 connection.close(); 2820 } 2821 2822 return topics; 2823 } 2824 2825 2830 public FileDataInfo getAttachmentInfo(int id) throws SQLException { 2831 Connection connection = this.dataSource.getConnection(); 2832 PreparedStatement st = connection.prepareStatement(dbDriver 2833 .getQueries().getForumQueries().getSql_GET_ATTACH_INFO()); 2834 ResultSet rs = null; 2835 FileDataInfo fileData = new FileDataInfo(); 2836 2837 try { 2838 st.setInt(1, id); 2839 rs = (ResultSet ) st.executeQuery(); 2840 2841 if (rs.next()) { 2842 fileData.setId(rs.getInt("id")); 2843 fileData.setName(rs.getString("attach_name")); 2844 fileData.setContentType(rs.getString("attach_content_type")); 2845 fileData.setDescription(rs.getString("attach_description")); 2846 fileData.setMessageId(rs.getInt("message_id")); 2847 fileData.setSize(rs.getInt("attach_size")); 2848 } 2849 2850 return fileData; 2851 } finally { 2852 if (rs != null) { 2853 rs.close(); 2854 } 2855 2856 st.close(); 2857 connection.close(); 2858 } 2859 2860 } 2861 2862 2865 public List getRankList() throws SQLException { 2866 ArrayList ranks = new ArrayList (); 2867 Connection connection = this.dataSource.getConnection(); 2868 PreparedStatement st = connection.prepareStatement(dbDriver 2869 .getQueries().getForumQueries().getSql_GET_RANKS()); 2870 ResultSet rs = null; 2871 2872 try { 2873 rs = (ResultSet ) st.executeQuery(); 2874 2875 while (rs.next()) { 2876 ranks.add(new RankInfoDTO(rs.getInt("id"), rs 2877 .getString("rank_name"), rs.getInt("rank_count"))); 2878 } 2879 } finally { 2880 if (rs != null) { 2881 try { 2882 rs.close(); 2883 } catch (SQLException e) { 2884 } 2885 } 2886 2887 st.close(); 2888 connection.close(); 2889 } 2890 2891 return ranks; 2892 } 2893 2894 2897 public void removeRank(int id) throws SQLException { 2898 Connection connection = this.dataSource.getConnection(); 2899 PreparedStatement st = connection.prepareStatement(dbDriver 2900 .getQueries().getForumQueries().getSql_REMOVE_RANK()); 2901 2902 try { 2903 st.setInt(1, id); 2904 st.execute(); 2905 2906 } finally { 2907 st.close(); 2908 connection.close(); 2909 } 2910 } 2911 2912 2916 public RankInfoDTO getRankInfo(int id) throws SQLException { 2917 RankInfoDTO rank = null; 2918 Connection connection = this.dataSource.getConnection(); 2919 PreparedStatement st = connection.prepareStatement(dbDriver 2920 .getQueries().getForumQueries().getSql_GET_RANK()); 2921 ResultSet rs = null; 2922 2923 try { 2924 rs = (ResultSet ) st.executeQuery(); 2925 2926 if (rs.next()) { 2927 rank = new RankInfoDTO(rs.getInt("id"), rs 2928 .getString("rank_name"), rs.getInt("rank_count")); 2929 } 2930 } finally { 2931 if (rs != null) { 2932 try { 2933 rs.close(); 2934 } catch (SQLException e) { 2935 } 2936 } 2937 st.close(); 2938 connection.close(); 2939 } 2940 2941 return rank; 2942 } 2943 2944 2947 public void addRank(RankInfoDTO rank) throws SQLException { 2948 if (null == rank) { 2949 throw new IllegalArgumentException ("rank can't have null value"); 2950 } 2951 Connection connection = this.dataSource.getConnection(); 2952 PreparedStatement st = connection.prepareStatement(dbDriver 2953 .getQueries().getForumQueries().getSql_ADD_RANK()); 2954 2955 try { 2956 int id = ((Integer ) keyGen.generateKey( 2957 IKeyGenConst.KEY_NAMES[IKeyGenConst.KEY_RANK], connection)) 2958 .intValue(); 2959 st.setInt(1, id); 2960 st.setInt(2, rank.getCount()); 2961 st.setString(3, rank.getName()); 2962 st.execute(); 2963 2964 } finally { 2965 st.close(); 2966 connection.close(); 2967 } 2968 2969 } 2970 2971 2974 public void updateRank(RankInfoDTO rank) throws SQLException { 2975 if (null == rank) { 2976 throw new IllegalArgumentException ("rank can't have null value"); 2977 } 2978 Connection connection = this.dataSource.getConnection(); 2979 PreparedStatement st = connection.prepareStatement(dbDriver 2980 .getQueries().getForumQueries().getSql_UPDATE_RANK()); 2981 2982 try { 2983 st.setInt(3, rank.getId()); 2984 st.setInt(1, rank.getCount()); 2985 st.setString(2, rank.getName()); 2986 st.execute(); 2987 2988 } finally { 2989 st.close(); 2990 connection.close(); 2991 } 2992 2993 } 2994 2995 3000 public void saveAvatar(User user, byte[] data) throws SystemException { 3001 IFileProcessor fProc = getFileProcessor(IFileProcConst.AVATAR_PROCESSOR); 3002 fProc.saveFileData(data, IFileProcConst.AVATAR_KEY_PREFIX 3003 + user.getName()); 3004 } 3005 3006 3011 public byte[] getAvatar(String uid) throws SystemException { 3012 IFileProcessor fProc = getFileProcessor(IFileProcConst.AVATAR_PROCESSOR); 3013 byte[] data = fProc.getFileData(IFileProcConst.AVATAR_KEY_PREFIX + uid); 3014 if (data == null) { 3015 data = IConst.VALUES.BLANK_GIF; 3016 } 3017 return data; 3018 } 3019 3020 3024 public void removeAvatar(String uid) throws SystemException { 3025 IFileProcessor fProc = getFileProcessor(IFileProcConst.AVATAR_PROCESSOR); 3026 fProc.removeFileData(IFileProcConst.AVATAR_KEY_PREFIX + uid); 3027 } 3028} | Popular Tags |