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

📄 dbforumthread.java

📁 一个jive论坛管理的源码 学习Jive源程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        }
        // Remove thread from cache.
        factory.cacheManager.threadCache.remove(this.id);
    }

    public void deleteProperty(String name)
            throws UnauthorizedException
    {
        if (LAZY_PROP_LOADING) {
            if (properties == null) {
                loadPropertiesFromDb();
            }
        }
        properties.remove(name);
        // Only save to the db if the object is ready
        if (!isReadyToSave) {
            return;
        }
        deletePropertyFromDb(name);
        // Remove thread from cache.
        factory.cacheManager.threadCache.remove(this.id);
    }

    public Iterator propertyNames() {
        if (LAZY_PROP_LOADING) {
            if (properties == null) {
                loadPropertiesFromDb();
            }
        }
        return Collections.unmodifiableSet(properties.keySet()).iterator();
    }

    public Forum getForum() {
        // If the thread hasn't been added to a forum yet, return null.
        if (forumID == -1) {
            return null;
        }
        try {
            return factory.getForum(forumID);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public ForumMessage getMessage(long messageID)
            throws ForumMessageNotFoundException
    {
        return factory.getMessage(messageID, this.id, forumID);
    }

    public ForumMessage getRootMessage()  {
        ForumMessage rootMessage = null;
        try {
            rootMessage = getMessage(rootMessageID);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return rootMessage;
    }

    public int getMessageCount() {
        return getMessageCount(DEFAULT_MESSAGE_FILTER);
    }

    public int getMessageCount(ResultFilter resultFilter) {
        String query = getMessageListSQL(resultFilter, true);
        CacheableInt count = (CacheableInt)messageCountCache.get(query);
        // If already in cache, return the count.
        if (count != null) {
            return count.getInt();
        }
        // Otherwise, we have to load the count from the db.
        else {
            int messageCount = 0;
            Connection con = null;
            Statement stmt = null;
            try {
                con = ConnectionManager.getConnection();
                stmt = con.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                rs.next();
                messageCount = rs.getInt(1);
            }
            catch( SQLException sqle ) {
                sqle.printStackTrace();
            }
            finally {
                try {  stmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            // Add the thread count to cache
            messageCountCache.add(query, new CacheableInt(messageCount));
            return messageCount;
        }
    }

    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;
        }
        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 and forum. Otherwise, we'll wait to update
            // the modified dates to when the message is moderated to be visible.
            dbForum = factory.cacheManager.forumCache.get(forumID);
            if (newMessage.getModerationValue() >=
                    dbForum.getModerationMinMessageValue())
            {
                long modifiedDate = newMessage.getModifiedDate().getTime();
                updateModifiedDate(modifiedDate, con);
                dbForum.updateModifiedDate(modifiedDate, con);
            }
        }
        catch( Exception e ) {
            e.printStackTrace();
            abortTransaction = true;
        }
        finally {
            ConnectionManager.closeTransactionConnection(con, abortTransaction);
        }

        // Thread count has been modified, remove thread and forum from cache.
        factory.cacheManager.threadCache.remove(this.id);
        factory.cacheManager.forumCache.remove(this.forumID);

        // Expire the userMessageCountCache if this message was not posted
        // anonymously.
        if (!newMessage.isAnonymous()) {
            factory.userMessageCountCache.remove(newMessage.getUser().getID());
        }

        // If above the moderation threshold...
        if (newMessage.getModerationValue() >=
                    dbForum.getModerationMinMessageValue())
        {
            // Notify the watch manager that the thread has been updated.
            factory.watchManager.notifyWatches(this);
            // Notify the gateway manager of a new message.
//            dbForum.getGatewayManager().exportData(dbMessage);
        }
    }

    public void deleteMessage(ForumMessage message)
            throws UnauthorizedException
    {
        // 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.forumCache.get(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);
            }
            catch( Exception e ) {
                e.printStackTrace();
                abortTransaction = true;
            }
            finally {
                ConnectionManager.closeTransactionConnection(con, abortTransaction);
            }
        }

        // Thread count has been modified, remove thread and forum from cache.
        factory.cacheManager.threadCache.remove(this.id);
        factory.cacheManager.forumCache.remove(this.forumID);
    }

    public TreeWalker treeWalker() {
        if (treeWalker == null) {
            treeWalker = new DbTreeWalker(this, factory);
        }
        return treeWalker;
    }

    public Iterator messages() {
        return messages(DEFAULT_MESSAGE_FILTER);
    }

    public Iterator messages(ResultFilter resultFilter) {
        String query = getMessageListSQL(resultFilter, false);
        long [] messageBlock = getMessageBlock(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;
    }

    //FROM THE CACHEABLE INTERFACE//

    public int getSize() {
        // 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

        size += messageListCache.getMaxSize();          // message list cache.
        size += messageCountCache.getMaxSize();         // message count cache.

        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 but doesn't require a security check since
     * it is a protected method.
     */
    protected void updateModifiedDate(long date, Connection con)
            throws SQLException
    {
        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);

⌨️ 快捷键说明

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