⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbforumthread.java

📁 一个jive论坛管理的源码 学习Jive源程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            pstmt.executeUpdate();
        }
        finally {
            try {  pstmt.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)
            throws SQLException, ForumMessageNotFoundException
    {
        // Skip null messages.
        if (message == null) {
            return;
        }

        // Recursively delete all children
        TreeWalker walker = treeWalker();
        int childCount = walker.getChildCount(message);
        for (int i=childCount-1; i >= 0; i--) {
            ForumMessage childMessage = walker.getChild(message, i);
            deleteMessage(childMessage, con);
        }

        // 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 the data from the jiveMessage table.
            pstmt = con.prepareStatement(DELETE_MESSAGE);
            pstmt.setLong(1, message.getID());
            pstmt.execute();
        }
        finally {
            try {  pstmt.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        // Now, delete from the cache.
        factory.cacheManager.messageCache.remove(message.getID());
        // Finally, delete it from the search index
        factory.getSearchManager().removeFromIndex(message);
    }

    /**
     * Returns the number of reward points for the thread.
     */
    protected int getRewardPoints() {
        return rewardPoints;
    }

    /**
     * Sets the number of reward points for the thread.
     */
    protected void setRewardPoints(int rewardPoints, Connection con)
            throws SQLException
    {
        // Save old point value in case something goes wrong.
        int oldPoints = this.rewardPoints;
        this.rewardPoints = rewardPoints;
        try {
            saveToDb(con);
        }
        catch (SQLException sqle) {
            // Switch back to old point value.
            this.rewardPoints = oldPoints;
            throw sqle;
        }
    }

    /**
     * Returns the SQL statement corresponding to a ResultFilter for messages.
     */
    private String getMessageListSQL(ResultFilter resultFilter, boolean countQuery) {
        int sortField = resultFilter.getSortField();
        // Make sure the sort field is valid.
        if (!countQuery && !( sortField == JiveGlobals.MODIFIED_DATE ||
                sortField == JiveGlobals.CREATION_DATE ||
                sortField == JiveGlobals.MESSAGE_SUBJECT ||
                sortField == JiveGlobals.MESSAGE_BODY ||
                ( sortField == JiveGlobals.EXTENDED_PROPERTY &&
                  resultFilter.getSortPropertyName() != null
                )
              )
            )
        {
            throw new IllegalArgumentException("The specified sort field is not valid.");
        }

        // We'll accumlate the query in a StringBuffer.
        StringBuffer query = new StringBuffer(160);
        if (!countQuery) {
            query.append("SELECT jiveMessage.messageID");
        }
        else {
            query.append("SELECT count(1)");
        }

        boolean filterUser = resultFilter.getUserID() != ResultFilter.NULL_INT;
        boolean filterCreationDate = resultFilter.getCreationDateRangeMin() != null ||
                                     resultFilter.getCreationDateRangeMax() != null;
        boolean filterModifiedDate = resultFilter.getModifiedDateRangeMin() != null ||
                                     resultFilter.getModifiedDateRangeMax() != null;
        int propertyCount = resultFilter.getPropertyCount();

        // SELECT -- need to add value that we sort on
        if(!countQuery) {
            switch(sortField) {
                case JiveGlobals.MESSAGE_SUBJECT:
                    query.append(", jiveMessage.subject");
                    break;
                case JiveGlobals.MODIFIED_DATE:
                    query.append(", jiveMessage.modifiedDate");
                    break;
                case JiveGlobals.CREATION_DATE:
                    query.append(", jiveMessage.creationDate");
                    break;
                case JiveGlobals.EXTENDED_PROPERTY:
                    query.append(", propTable.propValue");
                    break;
            }
        }

        // FROM -- values (in addition to jiveThread)
        query.append(" FROM jiveMessage");
        for (int i=0; i<propertyCount; i++) {
            query.append(", jiveMessageProp p").append(i);
        }
        if (!countQuery &&
                resultFilter.getSortField() == JiveGlobals.EXTENDED_PROPERTY)
        {
            query.append(", jiveMessageProp propTable");
        }

        // WHERE BLOCK
        query.append(" WHERE jiveMessage.threadID=").append(this.id);
        // User
        if (filterUser) {
            query.append(" AND jiveMessage.userID=").append(resultFilter.getUserID());
        }
        // Properties
        for (int i=0; i<propertyCount; i++) {
            query.append(" AND jiveMessage.messageID=p").append(i).append(".messageID");
            query.append(" AND p").append(i).append(".name='");
            query.append(resultFilter.getPropertyName(i));
            query.append("' AND p").append(i).append(".propValue='");
            query.append(resultFilter.getPropertyValue(i)).append("'");
        }
        // Sort on properties
        if (!countQuery && sortField == JiveGlobals.EXTENDED_PROPERTY) {
            query.append(" AND jiveMessage.messageID=propTable.messageID");
        }

        // Creation date range
        if (filterCreationDate) {
            if (resultFilter.getCreationDateRangeMin() != null) {
                query.append(" AND jiveMessage.creationDate >= '");
                query.append(StringUtils.dateToMillis(
                        resultFilter.getCreationDateRangeMin()));
                query.append("'");
            }
            if (resultFilter.getCreationDateRangeMax() != null) {
                query.append(" AND jiveMessage.creationDate <= '");
                query.append(StringUtils.dateToMillis(
                        resultFilter.getCreationDateRangeMax()));
                query.append("'");
            }
        }

        // Modified date range
        if (filterModifiedDate) {
            if (resultFilter.getModifiedDateRangeMin() != null) {
                query.append(" AND jiveMessage.modifiedDate >= '");
                query.append(StringUtils.dateToMillis(
                        resultFilter.getModifiedDateRangeMin()));
                query.append("'");
            }
            if (resultFilter.getModifiedDateRangeMax() != null) {
                query.append(" AND jiveMessage.modifiedDate <= '");
                query.append(StringUtils.dateToMillis(
                        resultFilter.getModifiedDateRangeMax()));
                query.append("'");
            }
        }

        // Moderation range
        int moderationRangeMin = resultFilter.getModerationRangeMin();
        // If the minimum moderation value was not set, it should default
        // to the minimum threshold for the forum.
        if (moderationRangeMin == ResultFilter.NULL_INT) {
            moderationRangeMin = getForum().getModerationMinMessageValue();
        }
        int moderationRangeMax = resultFilter.getModerationRangeMax();
        if (moderationRangeMin == moderationRangeMax) {
            query.append(" AND jiveMessage.modValue = ");
            query.append(moderationRangeMin);
        }
        else {
            // Ignore any min less than negative 1 million.
            if (moderationRangeMin > -1000000) {
                query.append(" AND jiveMessage.modValue >= ");
                query.append(moderationRangeMin);
            }
            // Use a max value if it's defined.
            if (moderationRangeMax != ResultFilter.NULL_INT) {
                query.append(" AND jiveMessage.modValue <= ");
                query.append(moderationRangeMax);
            }
        }

        // ORDER BY
        if (!countQuery) {
            query.append(" ORDER BY ");
            switch(sortField) {
                case JiveGlobals.MESSAGE_SUBJECT:
                    query.append("jiveMessage.subject");
                    break;
                case JiveGlobals.MODIFIED_DATE:
                    query.append("jiveMessage.modifiedDate");
                    break;
                case JiveGlobals.CREATION_DATE:
                    query.append("jiveMessage.creationDate");
                    break;
                case JiveGlobals.EXTENDED_PROPERTY:
                    query.append("propTable.propValue");
                    break;
            }
            if (resultFilter.getSortOrder() == ResultFilter.DESCENDING) {
                query.append(" DESC");
            }
            else {
                query.append(" ASC");
            }
        }

        return query.toString();
    }

    /**
     * Resets the object.
     */
    protected void reset() {
        // Reload the actual forum data.
        try {
            loadFromDb();
        } catch (Exception e) { }
        // Clear out the message list and message count cache.
        messageListCache.clear();
        messageCountCache.clear();
        // Clear tree
        this.treeWalker = null;
    }

    /**
     * Returns a block of messageID's from a query and performs transparent
     * caching of those blocks. The two parameters specify a database query
     * and a startIndex for the results in that query.
     *
     * @param query the SQL message list query to cache blocks from.
     * @param startIndex the startIndex in the list to get a block for.
     */
    protected long[] getMessageBlock(String query, int startIndex) {
        // First, discover what block number the results will be in.
        int blockID = startIndex / DbForum.MESSAGE_BLOCK_SIZE;
        int blockStart = blockID * DbForum.MESSAGE_BLOCK_SIZE;
        // Now, check cache to see if the block is already cached. The key is
        // simply the query plus the blockID.
        String key = query + blockID;
        CacheableLongArray longArray = (CacheableLongArray)messageListCache.get(key);
        // If already in cache, return the block.
        if (longArray != null) {
            /**
             * The actual block may be smaller than MESSAGE_BLOCK_SIZE. If that's
             * the case, it means two things:
             *  1) We're at the end boundary of all the results.
             *  2) If the start index is greater than the length of the current
             *     block, than there aren't really any results to return.
             */
            long [] messages = longArray.getLongArray();
            if (startIndex >= blockStart + messages.length) {
                //Return an empty array
                return EMPTY_BLOCK;
            }
            else {
                return messages;
            }
        }
        // Otherwise, we have to load up the block from the database.
        else {
            LongList messagesList = new LongList(DbForum.MESSAGE_BLOCK_SIZE);
            Connection con = null;
            Statement stmt = null;
            try {
                con = ConnectionManager.getConnection();
                stmt = con.createStatement();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -