1 43 package net.jforum.dao.generic; 44 45 import java.sql.PreparedStatement ; 46 import java.sql.ResultSet ; 47 import java.sql.Timestamp ; 48 import java.text.SimpleDateFormat ; 49 import java.util.ArrayList ; 50 import java.util.Collection ; 51 import java.util.HashMap ; 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Map ; 55 56 import net.jforum.JForumExecutionContext; 57 import net.jforum.SessionFacade; 58 import net.jforum.dao.DataAccessDriver; 59 import net.jforum.dao.ForumDAO; 60 import net.jforum.dao.PollDAO; 61 import net.jforum.dao.PostDAO; 62 import net.jforum.entities.KarmaStatus; 63 import net.jforum.entities.Topic; 64 import net.jforum.entities.User; 65 import net.jforum.util.preferences.ConfigKeys; 66 import net.jforum.util.preferences.SystemGlobals; 67 68 72 public class GenericTopicDAO extends AutoKeys implements net.jforum.dao.TopicDAO 73 { 74 77 public void fixFirstLastPostId(int topicId) throws Exception 78 { 79 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getFirstLastPostId")); 80 p.setInt(1, topicId); 81 82 ResultSet rs = p.executeQuery(); 83 if (rs.next()) { 84 int first = rs.getInt("first_post_id"); 85 int last = rs.getInt("last_post_id"); 86 87 rs.close(); 88 p.close(); 89 90 p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.fixFirstLastPostId")); 91 p.setInt(1, first); 92 p.setInt(2, last); 93 p.setInt(3, topicId); 94 p.executeUpdate(); 95 } 96 97 rs.close(); 98 p.close(); 99 } 100 101 104 public Topic selectById(int topicId) throws Exception 105 { 106 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.selectById")); 107 p.setInt(1, topicId); 108 109 Topic t = new Topic(); 110 List l = this.fillTopicsData(p); 111 112 if (l.size() > 0) { 113 t = (Topic)l.get(0); 114 } 115 116 return t; 117 } 118 119 122 public Topic selectRaw(int topicId) throws Exception 123 { 124 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.selectRaw")); 125 p.setInt(1, topicId); 126 127 Topic t = new Topic(); 128 ResultSet rs = p.executeQuery(); 129 if (rs.next()) { 130 t = this.getBaseTopicData(rs); 131 } 132 133 rs.close(); 134 p.close(); 135 return t; 136 } 137 138 141 public void delete(final Topic topic) throws Exception 142 { 143 this.deleteTopics(new ArrayList () {{ add(topic); }}); 144 } 145 146 public void deleteTopics(List topics) throws Exception 147 { 148 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.delete")); 150 ForumDAO fm = DataAccessDriver.getInstance().newForumDAO(); 151 152 PostDAO pm = DataAccessDriver.getInstance().newPostDAO(); 153 PollDAO plm = DataAccessDriver.getInstance().newPollDAO(); 154 155 for (Iterator iter = topics.iterator(); iter.hasNext(); ) { 156 Topic topic = (Topic)iter.next(); 157 158 this.removeSubscriptionByTopic(topic.getId()); 160 161 pm.deleteByTopic(topic.getId()); 163 164 plm.deleteByTopicId(topic.getId()); 166 167 p.setInt(1, topic.getId()); 168 p.executeUpdate(); 169 } 170 171 p.close(); 172 } 173 174 177 public void deleteByForum(int forumId) throws Exception 178 { 179 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.deleteByForum")); 180 p.setInt(1, forumId); 181 182 ResultSet rs = p.executeQuery(); 183 List topics = new ArrayList (); 184 while (rs.next()) { 185 Topic t = new Topic(); 186 t.setId(rs.getInt("topic_id")); 187 188 topics.add(t); 189 } 190 191 rs.close(); 192 p.close(); 193 194 this.deleteTopics(topics); 195 } 196 197 200 public void update(Topic topic) throws Exception 201 { 202 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.update")); 203 204 p.setString(1, topic.getTitle()); 205 p.setInt(2, topic.getLastPostId()); 206 p.setInt(3, topic.getFirstPostId()); 207 p.setInt(4, topic.getType()); 208 p.setInt(5, topic.isModerated() ? 1 : 0); 209 p.setInt(6, topic.getVoteId()); 210 p.setInt(7, topic.getId()); 211 p.executeUpdate(); 212 213 p.close(); 214 } 215 216 219 public int addNew(Topic topic) throws Exception 220 { 221 PreparedStatement p = this.getStatementForAutoKeys("TopicModel.addNew"); 222 223 p.setInt(1, topic.getForumId()); 224 p.setString(2, topic.getTitle()); 225 p.setInt(3, topic.getPostedBy().getId()); 226 p.setTimestamp(4, new Timestamp (topic.getTime().getTime())); 227 p.setInt(5, topic.getFirstPostId()); 228 p.setInt(6, topic.getLastPostId()); 229 p.setInt(7, topic.getType()); 230 p.setInt(8, topic.isModerated() ? 1 : 0); 231 232 this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("TopicModel.lastGeneratedTopicId")); 233 int topicId = this.executeAutoKeysQuery(p); 234 235 p.close(); 236 return topicId; 237 } 238 239 242 public void incrementTotalViews(int topicId) throws Exception 243 { 244 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.incrementTotalViews")); 245 p.setInt(1, topicId); 246 p.executeUpdate(); 247 p.close(); 248 } 249 250 253 public void incrementTotalReplies(int topicId) throws Exception 254 { 255 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.incrementTotalReplies")); 256 p.setInt(1, topicId); 257 p.executeUpdate(); 258 p.close(); 259 } 260 261 264 public void decrementTotalReplies(int topicId) throws Exception 265 { 266 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.decrementTotalReplies")); 267 p.setInt(1, topicId); 268 p.executeUpdate(); 269 p.close(); 270 } 271 272 275 public void setLastPostId(int topicId, int postId) throws Exception 276 { 277 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.setLastPostId")); 278 p.setInt(1, postId); 279 p.setInt(2, topicId); 280 p.executeUpdate(); 281 p.close(); 282 } 283 284 287 public List selectAllByForum(int forumId) throws Exception 288 { 289 return this.selectAllByForumByLimit(forumId, 0, Integer.MAX_VALUE); 290 } 291 292 295 public List selectAllByForumByLimit( 296 int forumId, 297 int startFrom, 298 int count) 299 throws Exception 300 { 301 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 302 SystemGlobals.getSql("TopicModel.selectAllByForumByLimit")); 303 p.setInt(1, forumId); 304 p.setInt(2, startFrom); 305 p.setInt(3, count); 306 307 return this.fillTopicsData(p); 308 } 309 310 313 public List selectByUserByLimit(int userId, int startFrom, int count) throws Exception 314 { 315 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 316 SystemGlobals.getSql("TopicModel.selectByUserByLimit")); 317 318 p.setInt(1,userId); 319 p.setInt(2, startFrom); 320 p.setInt(3, count); 321 322 return this.fillTopicsData(p); 323 } 324 325 328 public int countUserTopics(int userId) throws Exception 329 { 330 int total = 0; 331 332 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 333 SystemGlobals.getSql("TopicModel.countUserTopics")); 334 p.setInt(1, userId); 335 336 ResultSet rs = p.executeQuery(); 337 338 if (rs.next()) { 339 total = rs.getInt(1); 340 } 341 342 rs.close(); 343 p.close(); 344 345 return total; 346 } 347 348 protected Topic getBaseTopicData(ResultSet rs) throws Exception 349 { 350 Topic t = new Topic(); 351 352 t.setTitle(rs.getString("topic_title")); 353 t.setId(rs.getInt("topic_id")); 354 t.setTime(rs.getTimestamp("topic_time")); 355 t.setStatus(rs.getInt("topic_status")); 356 t.setTotalViews(rs.getInt("topic_views")); 357 t.setTotalReplies(rs.getInt("topic_replies")); 358 t.setFirstPostId(rs.getInt("topic_first_post_id")); 359 t.setLastPostId(rs.getInt("topic_last_post_id")); 360 t.setType(rs.getInt("topic_type")); 361 t.setForumId(rs.getInt("forum_id")); 362 t.setModerated(rs.getInt("moderated") == 1); 363 t.setVoteId(rs.getInt("topic_vote_id")); 364 365 return t; 366 } 367 368 371 public int getMaxPostId(int topicId) throws Exception 372 { 373 int id = -1; 374 375 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getMaxPostId")); 376 p.setInt(1, topicId); 377 378 ResultSet rs = p.executeQuery(); 379 if (rs.next()) { 380 id = rs.getInt("post_id"); 381 } 382 383 rs.close(); 384 p.close(); 385 386 return id; 387 } 388 389 392 public int getTotalPosts(int topicId) throws Exception 393 { 394 int total = 0; 395 396 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getTotalPosts")); 397 p.setInt(1, topicId); 398 399 ResultSet rs = p.executeQuery(); 400 if (rs.next()) { 401 total = rs.getInt("total"); 402 } 403 404 rs.close(); 405 p.close(); 406 407 return total; 408 } 409 410 413 public List selectLastN(int count) throws Exception 414 { 415 List topics = new ArrayList (); 416 417 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.selectLastN")); 418 p.setInt(1, count); 419 420 ResultSet rs = p.executeQuery(); 421 422 while (rs.next()) { 425 Topic t = new Topic(); 426 427 t.setTitle(rs.getString("topic_title")); 428 t.setId(rs.getInt("topic_id")); 429 t.setTime(rs.getTimestamp("topic_time")); 430 t.setType(rs.getInt("topic_type")); 431 432 topics.add(t); 433 } 434 435 rs.close(); 436 p.close(); 437 438 return topics; 439 } 440 441 444 public List notifyUsers(Topic topic) throws Exception 445 { 446 int posterId = SessionFacade.getUserSession().getUserId(); 447 int anonUser = SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID); 448 449 PreparedStatement stmt = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.notifyUsers")); 450 ResultSet rs = null; 451 452 stmt.setInt(1, topic.getId()); 453 stmt.setInt(2, posterId); stmt.setInt(3, anonUser); 456 rs = stmt.executeQuery(); 457 458 List users = new ArrayList (); 459 while(rs.next()) { 460 User user = new User(); 461 462 user.setId(rs.getInt("user_id")); 463 user.setEmail(rs.getString("user_email")); 464 user.setUsername(rs.getString("username")); 465 user.setLang(rs.getString("user_lang")); 466 467 users.add(user); 468 } 469 470 stmt = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.markAllAsUnread")); 472 stmt.setInt(1, topic.getId()); 473 stmt.setInt(2, posterId); stmt.setInt(3, anonUser); 476 stmt.executeUpdate(); 477 478 rs.close(); 479 stmt.close(); 480 481 return users; 482 } 483 484 487 public void subscribeUser(int topicId, int userId) throws Exception 488 { 489 PreparedStatement p = JForumExecutionContext.getConnection(). prepareStatement(SystemGlobals.getSql("TopicModel.subscribeUser")); 490 491 p.setInt(1, topicId); 492 p.setInt(2, userId); 493 494 p.executeUpdate(); 495 p.close(); 496 } 497 498 501 public boolean isUserSubscribed(int topicId, int userId) throws Exception 502 { 503 PreparedStatement stmt = JForumExecutionContext.getConnection(). prepareStatement( SystemGlobals.getSql("TopicModel.isUserSubscribed")); 504 ResultSet rs = null; 505 506 stmt.setInt(1, topicId); 507 stmt.setInt(2, userId); 508 509 rs = stmt.executeQuery(); 510 boolean status = rs.next(); 511 512 rs.close(); 513 stmt.close(); 514 515 return status; 516 } 517 518 521 public void removeSubscription(int topicId, int userId) throws Exception 522 { 523 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.removeSubscription")); 524 p.setInt(1, topicId); 525 p.setInt(2, userId); 526 527 p.executeUpdate(); 528 p.close(); 529 } 530 531 534 public void removeSubscriptionByTopic(int topicId) throws Exception 535 { 536 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.removeSubscriptionByTopic")); 537 p.setInt(1, topicId); 538 539 p.executeUpdate(); 540 p.close(); 541 } 542 543 546 public void updateReadStatus(int topicId, int userId, boolean read) throws Exception 547 { 548 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.updateReadStatus")); 549 p.setInt(1, read ? 1 : 0); 550 p.setInt(2, topicId); 551 p.setInt(3, userId); 552 553 p.executeUpdate(); 554 p.close(); 555 } 556 557 560 public void lockUnlock(int[] topicId, int status) throws Exception 561 { 562 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.lockUnlock")); 563 p.setInt(1, status); 564 565 for (int i = 0; i < topicId.length; i++) { 566 p.setInt(2, topicId[i]); 567 p.executeUpdate(); 568 } 569 p.close(); 570 } 571 572 583 public List fillTopicsData(PreparedStatement p) throws Exception 584 { 585 List l = new ArrayList (); 586 587 ResultSet rs = p.executeQuery(); 588 589 SimpleDateFormat df = new SimpleDateFormat (SystemGlobals.getValue(ConfigKeys.DATE_TIME_FORMAT)); 590 591 StringBuffer sbFirst = new StringBuffer (128); 592 StringBuffer sbLast = new StringBuffer (128); 593 594 while (rs.next()) { 595 Topic t = this.getBaseTopicData(rs); 596 597 User u = new User(); 599 u.setId(rs.getInt("user_id")); 600 t.setPostedBy(u); 601 602 u = new User(); 604 u.setId(rs.getInt("last_user_id")); 605 t.setLastPostBy(u); 606 607 t.setHasAttach(rs.getInt("attach") > 0); 608 t.setFirstPostTime(df.format(rs.getTimestamp("topic_time"))); 609 t.setLastPostTime(df.format(rs.getTimestamp("post_time"))); 610 t.setLastPostDate(rs.getTimestamp("post_time")); 611 612 l.add(t); 613 614 sbFirst.append(rs.getInt("user_id")).append(','); 615 sbLast.append(rs.getInt("last_user_id")).append(','); 616 } 617 618 rs.close(); 619 p.close(); 620 621 if (sbFirst.length() > 0) { 623 sbLast.delete(sbLast.length() - 1, sbLast.length()); 624 625 String sql = SystemGlobals.getSql("TopicModel.getUserInformation"); 626 sql = sql.replaceAll("#ID#", sbFirst.toString() + sbLast.toString()); 627 628 Map users = new HashMap (); 629 630 p = JForumExecutionContext.getConnection().prepareStatement(sql); 631 rs = p.executeQuery(); 632 633 while (rs.next()) { 634 users.put(new Integer (rs.getInt("user_id")), rs.getString("username")); 635 } 636 637 rs.close(); 638 p.close(); 639 640 for (Iterator iter = l.iterator(); iter.hasNext(); ) { 641 Topic t = (Topic)iter.next(); 642 t.getPostedBy().setUsername((String )users.get(new Integer (t.getPostedBy().getId()))); 643 t.getLastPostBy().setUsername((String )users.get(new Integer (t.getLastPostBy().getId()))); 644 } 645 } 646 647 return l; 648 } 649 650 653 public List selectRecentTopics (int limit) throws Exception 654 { 655 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 656 SystemGlobals.getSql("TopicModel.selectRecentTopicsByLimit")); 657 p.setInt(1, limit); 658 659 return this.fillTopicsData(p); 660 } 661 662 665 public void setFirstPostId(int topicId, int postId) throws Exception 666 { 667 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.setFirstPostId")); 668 p.setInt(1, postId); 669 p.setInt(2, topicId); 670 p.executeUpdate(); 671 p.close(); 672 } 673 674 677 public int getMinPostId(int topicId) throws Exception 678 { 679 int id = -1; 680 681 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.getMinPostId")); 682 p.setInt(1, topicId); 683 684 ResultSet rs = p.executeQuery(); 685 if (rs.next()) { 686 id = rs.getInt("post_id"); 687 } 688 689 rs.close(); 690 p.close(); 691 692 return id; 693 } 694 695 698 public void setModerationStatus(int forumId, boolean status) throws Exception 699 { 700 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.setModerationStatus")); 701 p.setInt(1, status ? 1 : 0); 702 p.setInt(2, forumId); 703 p.executeUpdate(); 704 p.close(); 705 } 706 707 710 public void setModerationStatusByTopic(int topicId, boolean status) throws Exception 711 { 712 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement( 713 SystemGlobals.getSql("TopicModel.setModerationStatusByTopic")); 714 p.setInt(1, status ? 1 : 0); 715 p.setInt(2, topicId); 716 p.executeUpdate(); 717 p.close(); 718 } 719 720 723 public List selectTopicTitlesByIds(Collection idList) throws Exception 724 { 725 List l = new ArrayList (); 726 String sql = SystemGlobals.getSql("TopicModel.selectTopicTitlesByIds"); 727 728 StringBuffer sb = new StringBuffer (idList.size() * 2); 729 for (Iterator iter = idList.iterator(); iter.hasNext(); ) { 730 sb.append(iter.next()).append(","); 731 } 732 733 int len = sb.length(); 734 sql = sql.replaceAll(":ids:", len > 0 ? sb.toString().substring(0, len - 1) : "0"); 735 PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(sql); 736 737 ResultSet rs = p.executeQuery(); 738 while (rs.next()) { 739 Map m = new HashMap (); 740 m.put("id", new Integer (rs.getInt("topic_id"))); 741 m.put("title", rs.getString("topic_title")); 742 743 l.add(m); 744 } 745 746 rs.close(); 747 p.close(); 748 749 return l; 750 } 751 752 755 public Map topicPosters(int topicId) throws Exception 756 { 757 Map m = new HashMap (); 758 PreparedStatement p; 759 760 StringBuffer sql = new StringBuffer (SystemGlobals.getSql("TopicModel.topicPosters")); 761 762 if (SystemGlobals.getBoolValue(ConfigKeys.DATABASE_SUPPORT_SUBQUERIES)) { 763 int index = sql.indexOf(":ids:"); 764 sql.replace(index, index + 5, SystemGlobals.getSql("TopicModel.distinctPosters")); 765 766 p = JForumExecutionContext.getConnection().prepareStatement(sql.toString()); 767 p.setInt(1, topicId); 768 } 769 else { 770 p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("TopicModel.distinctPosters")); 771 p.setInt(1, topicId); 772 773 ResultSet rs = p.executeQuery(); 774 775 StringBuffer sb = new StringBuffer (); 776 777 while (rs.next()) { 778 sb.append(rs.getInt("user_id")).append(','); 779 } 780 781 rs.close(); 782 p.close(); 783 784 int index = sql.indexOf(":ids:"); 785 sql.replace(index, index + 5, sb.substring(0, sb.length() - 1)); 786 787 p = JForumExecutionContext.getConnection().prepareStatement(sql.toString()); 788 } 789 790 ResultSet rs = p.executeQuery(); 791 792 while (rs.next()) { 793 User u = new User(); 794 795 u.setId(rs.getInt("user_id")); 796 u.setUsername(rs.getString("username")); 797 u.setKarma(new KarmaStatus(u.getId(), rs.getDouble("user_karma"))); 798 u.setAvatar(rs.getString("user_avatar")); 799 u.setAvatarEnabled(rs.getInt("user_allowavatar") == 1); 800 u.setRegistrationDate(rs.getTimestamp("user_regdate")); 801 u.setTotalPosts(rs.getInt("user_posts")); 802 u.setFrom(rs.getString("user_from")); 803 u.setEmail(rs.getString("user_email")); 804 u.setRankId(rs.getInt("rank_id")); 805 u.setViewEmailEnabled(rs.getInt("user_viewemail") == 1); 806 u.setIcq(rs.getString("user_icq")); 807 u.setAttachSignatureEnabled(rs.getInt("user_attachsig") == 1); 808 u.setMsnm(rs.getString("user_msnm")); 809 u.setYim(rs.getString("user_yim")); 810 u.setWebSite(rs.getString("user_website")); 811 u.setAim(rs.getString("user_aim")); 812 u.setSignature(rs.getString("user_sig")); 813 814 m.put(new Integer (u.getId()), u); 815 } 816 817 rs.close(); 818 p.close(); 819 820 return m; 821 } 822 } 823 | Popular Tags |