dbforumthread.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,412 行 · 第 1/4 页
JAVA
1,412 行
public void addMessage(ForumMessage parentMessage, ForumMessage newMessage) throws UnauthorizedException { // Get the underlying DbForumMessage object. DbForumMessage dbMessage = null; if (newMessage instanceof ForumMessageProxy) { ForumMessageProxy proxyMessage = (ForumMessageProxy)newMessage; dbMessage = (DbForumMessage)proxyMessage.getProxiedForumMessage(); } else { dbMessage = (DbForumMessage)newMessage; } // The creation date of the new message should never be before the // parent. Adjust the date if necessary. long parentCDate = parentMessage.getCreationDate().getTime(); if (dbMessage.getCreationDate().getTime() < parentCDate) { Date newDate = new Date(parentCDate + 1); dbMessage.setCreationDate(newDate); // Also set the modified date of the message if necessary. if (dbMessage.getModifiedDate().getTime() < parentCDate) { dbMessage.setModifiedDate(newDate); } } DbForum dbForum = null; boolean abortTransaction = false; Connection con = null; try { con = ConnectionManager.getTransactionConnection(); // Insert the message into the database. dbMessage.insertIntoDb(this, parentMessage.getID(), con); // Check the moderation value of the message. If the value is above // the visible threshold for the forum, then update the modified // date of the thread, forum, and categories. Otherwise, we'll wait to update // the modified dates to when the message is moderated to be visible. dbForum = factory.cacheManager.getForum(forumID); if (newMessage.getModerationValue() >= dbForum.getModerationMinMessageValue()) { updateModifiedDate(newMessage.getModifiedDate().getTime(), con); } } catch( Exception e ) { e.printStackTrace(); abortTransaction = true; } finally { ConnectionManager.closeTransactionConnection(con, abortTransaction); } // Reset treeWalker. Calling clearCache() below will re-add thread // object to the cache so that this change propogates. this.treeWalker = null; // Thread count has been modified, update thread and forum caches. this.clearCache(); dbForum.clearCache(); // Expire the global popular objects lists. factory.expirePopularObjects(); // Update the userMessageCountCache if this message was not posted // anonymously. There is a small risk that updating the cache this way // will temporarily make the count incorrect, but the speed improvement // justifies it. if (!newMessage.isAnonymous()) { Cache msgCountCache = factory.cacheManager.userMessageCountCache; Long userID = new Long(newMessage.getUser().getID()); Integer msgCount; // If the value is in cache, update it. if ((msgCount = (Integer)msgCountCache.get(userID)) != null) { msgCount = new Integer(msgCount.intValue() + 1); msgCountCache.put(userID, msgCount); } } // If above the moderation threshold... if (newMessage.getModerationValue() >= dbForum.getModerationMinMessageValue()) { // Notify the watch manager that the thread has been updated. factory.watchManager.notifyWatches(this, newMessage); // Notify the gateway manager of a new message. dbForum.getGatewayManager().exportData(dbMessage); } } public void deleteMessage(ForumMessage message) { // If the message does not belong to this thread, don't perform delete. if (message.getForumThread().getID() != this.id) { throw new IllegalArgumentException("Message " + message.getID() + " could not be deleted. It belongs to thread " + message.getForumThread().getID() + ", and not thread " + this.id + "."); } // Now, make sure that the message being deleted isn't the root message // of this thread. If it is, the whole thread should just be deleted. if (message.getID() == getRootMessage().getID()) { try { DbForum forum = factory.cacheManager.getForum(forumID); forum.deleteThread(this); } catch (Exception e) { e.printStackTrace(); } } // Otherwise, delete the message and all children. else { boolean abortTransaction = false; Connection con = null; try { con = ConnectionManager.getTransactionConnection(); // Delete the message. deleteMessage(message, con, true); } catch( Exception e ) { e.printStackTrace(); abortTransaction = true; } finally { ConnectionManager.closeTransactionConnection(con, abortTransaction); } } // Reset the userMessageCount in cache. if (message.getUser() != null) { factory.cacheManager.userMessageCountCache.remove(new Long(message.getUser().getID())); } // Delete the message from the search index. factory.getSearchManager().removeFromIndex(message); // Reset treeWalker. Calling clearCache() below will re-add thread // object to the cache so that this change propogates. this.treeWalker = null; // Thread count has been modified, remove thread and forum from cache. this.clearCache(); try { factory.cacheManager.getForum(this.forumID).clearCache(); } catch (ForumNotFoundException fnfe) { } } public TreeWalker treeWalker() { if (treeWalker == null) { treeWalker = new DbTreeWalker(this); } return treeWalker; } public Iterator messages() { return messages(DEFAULT_MESSAGE_FILTER); } public Iterator messages(ResultFilter resultFilter) { String query = getMessageListSQL(resultFilter, false); long [] messageBlock = getBlock(query.toString(), resultFilter.getStartIndex()); int startIndex = resultFilter.getStartIndex(); int endIndex; // If number of results is set to inifinite, set endIndex to the total // number of threads in the forum. if (resultFilter.getNumResults() == ResultFilter.NULL_INT) { endIndex = (int)getMessageCount(resultFilter); } else { endIndex = resultFilter.getNumResults() + startIndex; } return new ForumMessageBlockIterator(messageBlock, query.toString(), startIndex, endIndex, this.forumID, factory, this.id); } public boolean hasPermission(int type) { return true; } public int getCachedSize() { // Approximate the size of the object in bytes by calculating the size // of each field. int size = 0; size += CacheSizes.sizeOfObject(); // overhead of object size += CacheSizes.sizeOfLong(); // id size += CacheSizes.sizeOfDate(); // creation date size += CacheSizes.sizeOfDate(); // modified date size += CacheSizes.sizeOfObject(); // ref to rootMessage size += CacheSizes.sizeOfObject(); // ref to forum size += CacheSizes.sizeOfObject(); // ref to factory size += CacheSizes.sizeOfBoolean(); // ready save var size += CacheSizes.sizeOfBoolean(); // deleting var // Make rough estimation of size of queryKeys size += queryKeys.size() * 212; return size; } //OTHER METHODS// /** * Converts the object to a String by returning the name of the thread. * This functionality is primarily for Java applications that might be * accessing Jive objects through a GUI. */ public String toString() { return getName(); } public int hashCode() { return (int)id; } public boolean equals(Object object) { if (this == object) { return true; } if (object != null && object instanceof DbForumThread) { return id == ((DbForumThread)object).getID(); } else { return false; } } /** * Updates the modified date. It accepts a Connection so that it can * participate in trasactions. */ protected void updateModifiedDate(long date, Connection con) throws SQLException { // Only update the modified date if it is newer than the current one. if (date > this.modifiedDate.getTime()) { this.modifiedDate.setTime(date); PreparedStatement pstmt = null; try { pstmt = con.prepareStatement(UPDATE_THREAD_MODIFIED_DATE); pstmt.setString(1, StringUtils.dateToMillis(modifiedDate)); pstmt.setLong(2, id); pstmt.executeUpdate(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } } } // Update the modified date of the forum that this thread belongs to. ((DbForum)getForum()).updateModifiedDate(date, con); } protected void setModValue(int value, Authorization auth) { if (moderationValue == value) { return; } long now = System.currentTimeMillis(); // We modified the moderation value of the thread, so update the // modified date. this.modifiedDate.setTime(now); // If the thread is switching from hidden to visible, we should update // the modified date of forum it belongs to. try { Forum forum = factory.getForum(this.forumID); if (this.moderationValue < forum.getModerationMinThreadValue() && value >= forum.getModerationMinThreadValue()) { forum.setModifiedDate(modifiedDate); // Now, see if we need to notify the } } catch (ForumNotFoundException fnfe) { /* ignore */ } catch (UnauthorizedException ue) { /* ignore */ } // Now, set the moderation value. this.moderationValue = value; // Only save to the db if the object is ready if (!readyToSave) { return; } saveToDb(); // Update thread cache entry. factory.cacheManager.threadCache.put(new Long(this.id), this); // Reset forum. try { factory.cacheManager.getForum(this.forumID).clearCache(); } catch (ForumNotFoundException fnfe) { } // Finally, make an entry into the jiveModeration table for moderation // auditing purposes. Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(INSERT_MODERATION_ENTRY); pstmt.setLong(1, id); pstmt.setInt(2, JiveGlobals.THREAD); if (auth.isAnonymous()) { pstmt.setNull(3, Types.NUMERIC); } else { pstmt.setLong(3, auth.getUserID()); } pstmt.setString(4, StringUtils.dateToMillis(modifiedDate)); pstmt.setInt(5, moderationValue); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Delete message method that accepts a Connection as argument -- useful * for transactions. */ protected void deleteMessage(ForumMessage message, Connection con, boolean recursiveDelete) throws SQLException, ForumMessageNotFoundException { // Skip null messages. if (message == null) { return; } // Get list of all children to delete in depth-first order. if (recursiveDelete) { Iterator iter = treeWalker().recursiveChildren(message); while (iter.hasNext()) { deleteMessage((ForumMessage)iter.next(), con, false); } } // Fix any references to the message in the reward auditing table. factory.rewardManager.deleteMessageReference(message, con); PreparedStatement pstmt = null; try { // Delete any message properties. pstmt = con.prepareStatement(DELETE_MESSAGE_PROPERTIES); pstmt.setLong(1, message.getID()); pstmt.execute(); pstmt.close(); // Delete message attachments if this isn't a Basic license. if (!LicenseManager.getProduct().equals("Jive Forums Basic")) { for (Iterator i = message.attachments(); i.hasNext(); ) { DbAttachment attachment = (DbAttachment)i.next(); try { attachment.delete(con); } catch (IOException ioe) { } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?