Show
Ignore:
Timestamp:
07/24/08 15:12:50 (4 months ago)
Author:
jani.laakso@…
Message:

Fixed #1599: JForum sends multiple copies of all message notifications

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • jforum/WEB-INF/src/net/jforum/dao/generic/GenericForumDAO.java

    r2167 r5116  
    5252import java.util.ArrayList; 
    5353import java.util.Date; 
     54import java.util.HashSet; 
    5455import java.util.List; 
    5556 
     
    6970import net.jforum.util.preferences.SystemGlobals; 
    7071 
     72import org.apache.log4j.Logger; 
     73 
    7174/** 
    7275 * @author Rafael Steil 
     
    7780 */ 
    7881public class GenericForumDAO extends AutoKeys implements 
    79                 net.jforum.dao.ForumDAO { 
    80         /** 
    81          * @see net.jforum.dao.ForumDAO#selectById(int) 
    82          */ 
    83         public Forum selectById(int forumId) { 
    84                 PreparedStatement p = null; 
    85                 ResultSet rs = null; 
    86                 try { 
    87                         p = JForumExecutionContext.getConnection().prepareStatement( 
    88                                         SystemGlobals.getSql("ForumModel.selectById")); 
    89                         p.setInt(1, forumId); 
    90  
    91                         rs = p.executeQuery(); 
    92  
    93                         Forum f = new Forum(); 
    94  
    95                         if (rs.next()) { 
    96                                 f = this.fillForum(rs); 
    97                         } 
    98                         return f; 
    99                 } catch (SQLException e) { 
    100                         throw new DatabaseException(e); 
    101                 } finally { 
    102                         DbUtils.close(rs, p); 
    103                 } 
    104         } 
    105  
    106         protected Forum fillForum(ResultSet rs) throws SQLException { 
    107                 Forum f = new Forum(); 
    108  
    109                 f.setId(rs.getInt("forum_id")); 
    110                 f.setIdCategories(rs.getInt("categories_id")); 
    111                 f.setName(rs.getString("forum_name")); 
    112                 f.setDescription(rs.getString("forum_desc")); 
    113                 f.setOrder(rs.getInt("forum_order")); 
    114                 f.setTotalTopics(rs.getInt("forum_topics")); 
    115                 f.setLastPostId(rs.getInt("forum_last_post_id")); 
    116                 f.setModerated(rs.getInt("moderated") > 0); 
    117                 f.setTotalPosts(this.countForumPosts(f.getId())); 
    118  
    119                 return f; 
    120         } 
    121  
    122         protected int countForumPosts(int forumId) { 
    123                 PreparedStatement p = null; 
    124                 ResultSet rs = null; 
    125                 try { 
    126                         p = JForumExecutionContext.getConnection().prepareStatement( 
    127                                         SystemGlobals.getSql("ForumModel.countForumPosts")); 
    128                         p.setInt(1, forumId); 
    129                         rs = p.executeQuery(); 
    130  
    131                         if (rs.next()) { 
    132                                 return rs.getInt(1); 
    133                         } 
    134  
    135                         return 0; 
    136                 } catch (SQLException e) { 
    137                         throw new DatabaseException(e); 
    138                 } finally { 
    139                         DbUtils.close(rs, p); 
    140                 } 
    141         } 
    142  
    143         /** 
    144          * @see net.jforum.dao.ForumDAO#selectAll() 
    145          */ 
    146         public List selectAll() { 
    147                 PreparedStatement p = null; 
    148                 ResultSet rs = null; 
    149                 try { 
    150                         p = JForumExecutionContext.getConnection().prepareStatement( 
    151                                         SystemGlobals.getSql("ForumModel.selectAll")); 
    152                         List l = new ArrayList(); 
    153  
    154                         rs = p.executeQuery(); 
    155  
    156                         while (rs.next()) { 
    157                                 l.add(this.fillForum(rs)); 
    158                         } 
    159  
    160                         return l; 
    161                 } catch (SQLException e) { 
    162                         throw new DatabaseException(e); 
    163                 } finally { 
    164                         DbUtils.close(rs, p); 
    165                 } 
    166         } 
    167  
    168         /** 
    169          * @see net.jforum.dao.ForumDAO#setOrderUp(Forum, Forum) 
    170          */ 
    171         public Forum setOrderUp(Forum forum, Forum related) { 
    172                 return this.changeForumOrder(forum, related, true); 
    173         } 
    174  
    175         /** 
    176          * @see net.jforum.dao.ForumDAO#setOrderDown(Forum, Forum) 
    177          */ 
    178         public Forum setOrderDown(Forum forum, Forum related) { 
    179                 return this.changeForumOrder(forum, related, false); 
    180         } 
    181  
    182         private Forum changeForumOrder(Forum forum, Forum related, boolean up) { 
    183                 int tmpOrder = related.getOrder(); 
    184                 related.setOrder(forum.getOrder()); 
    185                 forum.setOrder(tmpOrder); 
    186  
    187                 PreparedStatement p = null; 
    188                 try { 
    189                         p = JForumExecutionContext.getConnection().prepareStatement( 
    190                                         SystemGlobals.getSql("ForumModel.setOrderById")); 
    191                         p.setInt(1, forum.getOrder()); 
    192                         p.setInt(2, forum.getId()); 
    193                         p.executeUpdate(); 
    194                         p.close(); 
    195                         p = null; 
    196  
    197                         p = JForumExecutionContext.getConnection().prepareStatement( 
    198                                         SystemGlobals.getSql("ForumModel.setOrderById")); 
    199                         p.setInt(1, related.getOrder()); 
    200                         p.setInt(2, related.getId()); 
    201                         p.executeUpdate(); 
    202  
    203                         return this.selectById(forum.getId()); 
    204                 } catch (SQLException e) { 
    205                         throw new DatabaseException(e); 
    206                 } finally { 
    207                         DbUtils.close(p); 
    208                 } 
    209         } 
    210  
    211         /** 
    212          * @see net.jforum.dao.ForumDAO#delete(int) 
    213          */ 
    214         public void delete(int forumId) { 
    215                 PreparedStatement p = null; 
    216                 try { 
    217                         p = JForumExecutionContext.getConnection().prepareStatement( 
    218                                         SystemGlobals.getSql("ForumModel.delete")); 
    219                         p.setInt(1, forumId); 
    220  
    221                         p.executeUpdate(); 
    222                 } catch (SQLException e) { 
    223                         throw new DatabaseException(e); 
    224                 } finally { 
    225                         DbUtils.close(p); 
    226                 } 
    227         } 
    228  
    229         /** 
    230          * @see net.jforum.dao.ForumDAO#update(net.jforum.entities.Forum) 
    231          */ 
    232         public void update(Forum forum) { 
    233                 PreparedStatement p = null; 
    234                 try { 
    235                         p = JForumExecutionContext.getConnection().prepareStatement( 
    236                                         SystemGlobals.getSql("ForumModel.update")); 
    237  
    238                         p.setInt(1, forum.getCategoryId()); 
    239                         p.setString(2, forum.getName()); 
    240                         p.setString(3, forum.getDescription()); 
    241                         p.setInt(4, forum.isModerated() ? 1 : 0); 
    242                         p.setInt(5, forum.getId()); 
    243  
    244                         p.executeUpdate(); 
    245                 } catch (SQLException e) { 
    246                         throw new DatabaseException(e); 
    247                 } finally { 
    248                         DbUtils.close(p); 
    249                 } 
    250         } 
    251  
    252         /** 
    253          * @see net.jforum.dao.ForumDAO#addNew(net.jforum.entities.Forum) 
    254          */ 
    255         public int addNew(Forum forum) { 
    256                 // Gets the higher order 
    257                 PreparedStatement pOrder = null; 
    258                 ResultSet rs = null; 
    259                 try { 
    260                         pOrder = JForumExecutionContext.getConnection().prepareStatement( 
    261                                         SystemGlobals.getSql("ForumModel.getMaxOrder")); 
    262                         rs = pOrder.executeQuery(); 
    263  
    264                         if (rs.next()) { 
    265                                 forum.setOrder(rs.getInt(1) + 1); 
    266                         } 
    267  
    268                         rs.close(); 
    269                         rs = null; 
    270                         pOrder.close(); 
    271                         pOrder = null; 
    272  
    273                         pOrder = this.getStatementForAutoKeys("ForumModel.addNew"); 
    274  
    275                         pOrder.setInt(1, forum.getCategoryId()); 
    276                         pOrder.setString(2, forum.getName()); 
    277                         pOrder.setString(3, forum.getDescription()); 
    278                         pOrder.setInt(4, forum.getOrder()); 
    279                         pOrder.setInt(5, forum.isModerated() ? 1 : 0); 
    280  
    281                         this.setAutoGeneratedKeysQuery(SystemGlobals 
    282                                         .getSql("ForumModel.lastGeneratedForumId")); 
    283                         int forumId = this.executeAutoKeysQuery(pOrder); 
    284  
    285                         forum.setId(forumId); 
    286                         return forumId; 
    287                 } catch (SQLException e) { 
    288                         throw new DatabaseException(e); 
    289                 } finally { 
    290                         DbUtils.close(rs, pOrder); 
    291                 } 
    292         } 
    293  
    294         /** 
    295          * @see net.jforum.dao.ForumDAO#setLastPost(int, int) 
    296          */ 
    297         public void setLastPost(int forumId, int postId) { 
    298                 PreparedStatement p = null; 
    299                 try { 
    300                         p = JForumExecutionContext.getConnection().prepareStatement( 
    301                                         SystemGlobals.getSql("ForumModel.updateLastPost")); 
    302  
    303                         p.setInt(1, postId); 
    304                         p.setInt(2, forumId); 
    305  
    306                         p.executeUpdate(); 
    307                 } catch (SQLException e) { 
    308                         throw new DatabaseException(e); 
    309                 } finally { 
    310                         DbUtils.close(p); 
    311                 } 
    312         } 
    313  
    314         /** 
    315          * @see net.jforum.dao.ForumDAO#setTotalTopics(int) 
    316          */ 
    317         public void incrementTotalTopics(int forumId, int count) { 
    318                 PreparedStatement p = null; 
    319                 try { 
    320                         p = JForumExecutionContext.getConnection().prepareStatement( 
    321                                         SystemGlobals.getSql("ForumModel.incrementTotalTopics")); 
    322                         p.setInt(1, count); 
    323                         p.setInt(2, forumId); 
    324                         p.executeUpdate(); 
    325                 } catch (SQLException e) { 
    326                         throw new DatabaseException(e); 
    327                 } finally { 
    328                         DbUtils.close(p); 
    329                 } 
    330         } 
    331  
    332         /** 
    333          * @see net.jforum.dao.ForumDAO#setTotalTopics(int) 
    334          */ 
    335         public void decrementTotalTopics(int forumId, int count) { 
    336                 PreparedStatement p = null; 
    337                 try { 
    338                         p = JForumExecutionContext.getConnection().prepareStatement( 
    339                                         SystemGlobals.getSql("ForumModel.decrementTotalTopics")); 
    340                         p.setInt(1, count); 
    341                         p.setInt(2, forumId); 
    342                         p.executeUpdate(); 
    343  
    344                         // If there are no more topics, then clean the 
    345                         // last post id information 
    346                         int totalTopics = this.getTotalTopics(forumId); 
    347                         if (totalTopics < 1) { 
    348                                 this.setLastPost(forumId, 0); 
    349                         } 
    350                 } catch (SQLException e) { 
    351                         throw new DatabaseException(e); 
    352                 } finally { 
    353                         DbUtils.close(p); 
    354                 } 
    355         } 
    356  
    357         private LastPostInfo getLastPostInfo(int forumId, boolean tryFix) { 
    358                 LastPostInfo lpi = new LastPostInfo(); 
    359  
    360                 PreparedStatement p = null; 
    361                 ResultSet rs = null; 
    362                 try { 
    363                         p = JForumExecutionContext.getConnection().prepareStatement( 
    364                                         SystemGlobals.getSql("ForumModel.lastPostInfo")); 
    365                         p.setInt(1, forumId); 
    366  
    367                         rs = p.executeQuery(); 
    368  
    369                         if (rs.next()) { 
    370                                 lpi.setUsername(rs.getString("username")); 
    371                                 lpi.setUserId(rs.getInt("user_id")); 
    372  
    373                                 SimpleDateFormat df = new SimpleDateFormat(SystemGlobals 
    374                                                 .getValue(ConfigKeys.DATE_TIME_FORMAT)); 
    375                                 lpi.setPostDate(df.format(rs.getTimestamp("post_time"))); 
    376                                 lpi.setPostId(rs.getInt("post_id")); 
    377                                 lpi.setTopicId(rs.getInt("topic_id")); 
    378                                 lpi.setPostTimeMillis(rs.getTimestamp("post_time").getTime()); 
    379                                 lpi.setTopicReplies(rs.getInt("topic_replies")); 
    380  
    381                                 lpi.setHasInfo(true); 
    382  
    383                                 // Check if the topic is consistent 
    384                                 TopicDAO tm = DataAccessDriver.getInstance().newTopicDAO(); 
    385                                 Topic t = tm.selectById(lpi.getTopicId()); 
    386  
    387                                 if (t.getId() == 0) { 
    388                                         // Hm, that's not good. Try to fix it 
    389                                         tm.fixFirstLastPostId(lpi.getTopicId()); 
    390                                 } 
    391  
    392                                 tryFix = false; 
    393                         } else if (tryFix) { 
    394                                 rs.close(); 
    395                                 rs = null; 
    396                                 p.close(); 
    397                                 p = null; 
    398  
    399                                 int postId = this.getMaxPostId(forumId); 
    400  
    401                                 p = JForumExecutionContext.getConnection().prepareStatement( 
    402                                                 SystemGlobals.getSql("ForumModel.latestTopicIdForfix")); 
    403                                 p.setInt(1, forumId); 
    404                                 rs = p.executeQuery(); 
    405  
    406                                 if (rs.next()) { 
    407                                         int topicId; 
    408                                         topicId = rs.getInt("topic_id"); 
    409  
    410                                         rs.close(); 
    411                                         rs = null; 
    412                                         p.close(); 
    413                                         p = null; 
    414  
    415                                         // Topic 
    416                                         p = JForumExecutionContext 
    417                                                         .getConnection() 
    418                                                         .prepareStatement( 
    419                                                                         SystemGlobals 
    420                                                                                         .getSql("ForumModel.fixLatestPostData")); 
    421                                         p.setInt(1, postId); 
    422                                         p.setInt(2, topicId); 
    423                                         p.executeUpdate(); 
    424                                         p.close(); 
    425                                         p = null; 
    426  
    427                                         // Forum 
    428                                         p = JForumExecutionContext 
    429                                                         .getConnection() 
    430                                                         .prepareStatement( 
    431                                                                         SystemGlobals 
    432                                                                                         .getSql("ForumModel.fixForumLatestPostData")); 
    433                                         p.setInt(1, postId); 
    434                                         p.setInt(2, forumId); 
    435                                         p.executeUpdate(); 
    436                                 } 
    437                         } 
    438  
    439                         return (tryFix ? this.getLastPostInfo(forumId, false) : lpi); 
    440                 } catch (SQLException e) { 
    441                         throw new DatabaseException(e); 
    442                 } finally { 
    443                         DbUtils.close(rs, p); 
    444                 } 
    445         } 
    446  
    447         /** 
    448          * @see net.jforum.dao.ForumDAO#getLastPostInfo(int) 
    449          */ 
    450         public LastPostInfo getLastPostInfo(int forumId) { 
    451                 return this.getLastPostInfo(forumId, true); 
    452         } 
    453  
    454         /** 
    455          * @see net.jforum.dao.ForumDAO#getModeratorList(int) 
    456          */ 
    457         public List getModeratorList(int forumId) { 
    458                 List l = new ArrayList(); 
    459  
    460                 PreparedStatement p = null; 
    461                 ResultSet rs = null; 
    462                 try { 
    463                         p = JForumExecutionContext.getConnection().prepareStatement( 
    464                                         SystemGlobals.getSql("ForumModel.getModeratorList")); 
    465                         p.setInt(1, forumId); 
    466  
    467                         rs = p.executeQuery(); 
    468  
    469                         while (rs.next()) { 
    470                                 ModeratorInfo mi = new ModeratorInfo(); 
    471  
    472                                 mi.setId(rs.getInt("id")); 
    473                                 mi.setName(rs.getString("name")); 
    474  
    475                                 l.add(mi); 
    476                         } 
    477  
    478                         return l; 
    479                 } catch (SQLException e) { 
    480                         throw new DatabaseException(e); 
    481                 } finally { 
    482                         DbUtils.close(rs, p); 
    483                 } 
    484         } 
    485  
    486         /** 
    487          * @see net.jforum.dao.ForumDAO#getTotalMessages() 
    488          */ 
    489         public int getTotalMessages() { 
    490                 PreparedStatement p = null; 
    491                 ResultSet rs = null; 
    492                 try { 
    493                         p = JForumExecutionContext.getConnection().prepareStatement( 
    494                                         SystemGlobals.getSql("ForumModel.totalMessages")); 
    495                         rs = p.executeQuery(); 
    496  
    497                         if (rs.next()) { 
    498                                 return rs.getInt("total_messages"); 
    499                         } 
    500  
    501                         return 0; 
    502                 } catch (SQLException e) { 
    503                         throw new DatabaseException(e); 
    504                 } finally { 
    505                         DbUtils.close(rs, p); 
    506                 } 
    507         } 
    508  
    509         /** 
    510          * @see net.jforum.dao.ForumDAO#getTotalTopics(int) 
    511          */ 
    512         public int getTotalTopics(int forumId) { 
    513                 int total = 0; 
    514                 PreparedStatement p = null; 
    515                 ResultSet rs = null; 
    516                 try { 
    517                         p = JForumExecutionContext.getConnection().prepareStatement( 
    518                                         SystemGlobals.getSql("ForumModel.getTotalTopics")); 
    519                         p.setInt(1, forumId); 
    520                         rs = p.executeQuery(); 
    521  
    522                         if (rs.next()) { 
    523                                 total = rs.getInt(1); 
    524                         } 
    525  
    526                         return total; 
    527                 } catch (SQLException e) { 
    528                         throw new DatabaseException(e); 
    529                 } finally { 
    530                         DbUtils.close(rs, p); 
    531                 } 
    532         } 
    533  
    534         /** 
    535          * @see net.jforum.dao.ForumDAO#getMaxPostId(int) 
    536          */ 
    537         public int getMaxPostId(int forumId) { 
    538                 int id = -1; 
    539  
    540                 PreparedStatement p = null; 
    541                 ResultSet rs = null; 
    542                 try { 
    543                         p = JForumExecutionContext.getConnection().prepareStatement( 
    544                                         SystemGlobals.getSql("ForumModel.getMaxPostId")); 
    545                         p.setInt(1, forumId); 
    546  
    547                         rs = p.executeQuery(); 
    548                         if (rs.next()) { 
    549                                 id = rs.getInt("post_id"); 
    550                         } 
    551  
    552                         return id; 
    553                 } catch (SQLException e) { 
    554                         throw new DatabaseException(e); 
    555                 } finally { 
    556                         DbUtils.close(rs, p); 
    557                 } 
    558         } 
    559  
    560         /** 
    561          * @see net.jforum.dao.ForumDAO#moveTopics(java.lang.String[], int, int) 
    562          */ 
    563         public void moveTopics(String[] topics, int fromForumId, int toForumId) { 
    564                 PreparedStatement p = null; 
    565                 PreparedStatement t = null; 
    566                 try { 
    567                         p = JForumExecutionContext.getConnection().prepareStatement( 
    568                                         SystemGlobals.getSql("ForumModel.moveTopics")); 
    569                         t = JForumExecutionContext.getConnection().prepareStatement( 
    570                                         SystemGlobals.getSql("PostModel.setForumByTopic")); 
    571  
    572                         p.setInt(1, toForumId); 
    573                         t.setInt(1, toForumId); 
    574  
    575                         TopicDAO tdao = DataAccessDriver.getInstance().newTopicDAO(); 
    576  
    577                         Forum f = this.selectById(toForumId); 
    578  
    579                         for (int i = 0; i < topics.length; i++) { 
    580                                 int topicId = Integer.parseInt(topics[i]); 
    581                                 p.setInt(2, topicId); 
    582                                 t.setInt(2, topicId); 
    583  
    584                                 p.executeUpdate(); 
    585                                 t.executeUpdate(); 
    586  
    587                                 tdao.setModerationStatusByTopic(topicId, f.isModerated()); 
    588                         } 
    589  
    590                         this.decrementTotalTopics(fromForumId, topics.length); 
    591                         this.incrementTotalTopics(toForumId, topics.length); 
    592  
    593                         this.setLastPost(fromForumId, this.getMaxPostId(fromForumId)); 
    594                         this.setLastPost(toForumId, this.getMaxPostId(toForumId)); 
    595                 } catch (SQLException e) { 
    596                         throw new DatabaseException(e); 
    597                 } finally { 
    598                         DbUtils.close(p); 
    599                         DbUtils.close(t); 
    600                 } 
    601         } 
    602  
    603         /** 
    604          * @see net.jforum.dao.ForumDAO#hasUnreadTopics(int, long) 
    605          */ 
    606         public List checkUnreadTopics(int forumId, long lastVisit) { 
    607                 List l = new ArrayList(); 
    608  
    609                 PreparedStatement p = null; 
    610                 ResultSet rs = null; 
    611                 try { 
    612                         p = JForumExecutionContext.getConnection().prepareStatement( 
    613                                         SystemGlobals.getSql("ForumModel.checkUnreadTopics")); 
    614                         p.setInt(1, forumId); 
    615                         p.setTimestamp(2, new Timestamp(lastVisit)); 
    616  
    617                         rs = p.executeQuery(); 
    618                         while (rs.next()) { 
    619                                 Topic t = new Topic(); 
    620                                 t.setId(rs.getInt("topic_id")); 
    621                                 t.setTime(new Date(rs.getTimestamp(1).getTime())); 
    622  
    623                                 l.add(t); 
    624                         } 
    625  
    626                         return l; 
    627                 } catch (SQLException e) { 
    628                         throw new DatabaseException(e); 
    629                 } finally { 
    630                         DbUtils.close(rs, p); 
    631                 } 
    632         } 
    633  
    634         /** 
    635          * @see net.jforum.dao.ForumDAO#setModerated(int, boolean) 
    636          */ 
    637         public void setModerated(int categoryId, boolean status) { 
    638                 PreparedStatement p = null; 
    639                 try { 
    640                         p = JForumExecutionContext.getConnection().prepareStatement( 
    641                                         SystemGlobals.getSql("ForumModel.setModerated")); 
    642                         p.setInt(1, status ? 1 : 0); 
    643                         p.setInt(2, categoryId); 
    644                         p.executeUpdate(); 
    645                 } catch (SQLException e) { 
    646                         throw new DatabaseException(e); 
    647                 } finally { 
    648                         DbUtils.close(p); 
    649                 } 
    650         } 
    651  
    652         /** 
    653          * @see net.jforum.dao.ForumDAO#getBoardStatus() 
    654          */ 
    655         public ForumStats getBoardStatus() { 
    656                 ForumStats fs = new ForumStats(); 
    657                 fs.setPosts(this.getTotalMessages()); 
    658  
    659                 Connection c = JForumExecutionContext.getConnection(); 
    660  
    661                 // Total Users 
    662                 Statement s = null; 
    663                 ResultSet rs = null; 
    664  
    665                 try { 
    666                         s = c.createStatement(); 
    667                         rs = s.executeQuery(SystemGlobals.getSql("UserModel.totalUsers")); 
    668                         rs.next(); 
    669                         fs.setUsers(rs.getInt(1)); 
    670                         rs.close(); 
    671                         rs = null; 
    672                         s.close(); 
    673                         s = null; 
    674  
    675                         // Total Topics 
    676                         s = c.createStatement(); 
    677                         rs = s.executeQuery(SystemGlobals.getSql("TopicModel.totalTopics"));