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

📄 dbforumthread.java

📁 Jive Forums 1.0 src
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        catch( SQLException sqle ) {
            System.err.println("DbForumThread:getMessageCount() failed: "+sqle);
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        return messageCount;
    }

    /**
     * Adds a new message to the thread.
     *
     * @param parentMessage some message in the thread that will be parent
     * @param newMessage message to add to the thread under the parent
     */
    public void addMessage(ForumMessage parentMessage, ForumMessage newMessage) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(ADD_MESSAGE1);
            pstmt.setInt(1, id);
            pstmt.setInt(2, newMessage.getID());
            pstmt.executeUpdate();
            pstmt.close();

            pstmt = con.prepareStatement(ADD_MESSAGE2);
            pstmt.setInt(1, parentMessage.getID());
            pstmt.setInt(2, newMessage.getID());
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForumThread:addMessage()-" + sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
        //Added new message, so update the modified date of this thread
        updateModifiedDate(newMessage.getModifiedDate());
        //Also, update the modified date of the forum
        forum.updateModifiedDate(modifiedDate);
    }

    /**
     * Deletes a message from the thread. Throws a MessageNotFoundException
     * if the message is not in the thread. If the message is deleted, it
     * should be entirely erased from the Forum system. Therefore, the
     * behavior is unspecified if a message object is first removed from a
     * thread and then added to another (this action not recommended).
     *
     * @throws UnauthorizedException if does not have ADMIN permissions.
     */
    public void deleteMessage(ForumMessage message) {
        //First, delete from the cache.
        factory.messageCache.remove(message.getID());
        
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            //Delete the message from the parent/child table
            pstmt = con.prepareStatement(DELETE_MESSAGE1);
            pstmt.setInt(1, message.getID());
            pstmt.execute();
            pstmt.close();
            con.close();

            //Recursively delete all children
            TreeWalker walker = treeWalker();
            int childCount = walker.getChildCount(message);
            for (int i=0; i<childCount; i++) {
                deleteMessage(walker.getChild(message,i));
            }

            //Delete the actual message.
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(DELETE_MESSAGE2);
            pstmt.setInt(1, message.getID());
            pstmt.execute();
            pstmt.close();

            //Delete any message properties.
            pstmt = con.prepareStatement(DELETE_MESSAGE_PROPERTIES);
            pstmt.setInt(1, message.getID());
            pstmt.execute();
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForumThread:deleteMessage()-" + sqle);
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Returns a TreeWalker for the entire thread. A TreeWalker is used
     * to navigate through the tree of messages in the thread.
     */
    public TreeWalker treeWalker() {
        return new DbTreeWalker(this, factory);
    }

    /**
     * Return an Iterator for all the messages in a thread. Similar
     * to an iterator, but it allows backwards and forwards navigation.
     * The add(), remove(), and set() operations should not be supported by
     * implementations of this interface for security reasons.
     */
    public Iterator messages() {
        return new DbThreadIterator(this);
    }

    /**
     * Return an Iterator for all the messages in a thread. Similar
     * to an iterator, but it allows backwards and forwards navigation.
     * The add(), remove(), and set() operations should not be supported by
     * implementations of this interface for security reasons.
     */
    public Iterator messages(int startIndex, int numResults) {
        return new DbThreadIterator(this, startIndex, numResults);
    }

    /**
     * Returns true if the handle on the object has the permission specified.
     * A list of possible permissions can be found in the ForumPermissions
     * class. Certain methods of this class are restricted to certain
     * permissions as specified in the method comments.
     *
     * @see ForumPermissions
     */
    public boolean hasPermission(int type) {
        return true;
    }

    /**
     * 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 name;
    }

    /**
     * Updates the modified date but doesn't require a security check since
     * it is a protected method.
     */
    protected void updateModifiedDate(java.util.Date modifiedDate) {
        this.modifiedDate = modifiedDate;
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(UPDATE_THREAD_MODIFIED_DATE);
            pstmt.setString(1, ""+modifiedDate.getTime());
            pstmt.setInt(2, id);
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForumThread:updateModifiedDate()-" + sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Loads a ForumThread from the database.
     */
    private void loadFromDb() throws ForumThreadNotFoundException {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_THREAD);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            if( !rs.next() ) { 
                throw new ForumThreadNotFoundException("Thread " + id +
                    " could not be loaded from the database.");
            }
            name = rs.getString("name");
            rootMessageID = rs.getInt("rootMessageID");
            creationDate = new java.util.Date(Long.parseLong(rs.getString("creationDate").trim()));
            modifiedDate =  new java.util.Date(Long.parseLong(rs.getString("modifiedDate").trim()));
            forumID = rs.getInt("forumID");
        }
        catch( SQLException sqle ) {
            throw new ForumThreadNotFoundException("Thread " + id +
                " could not be loaded from the database.");
        }
        catch (NumberFormatException nfe) {
            System.err.println("WARNING: In DbForumThread.loadFromDb() -- there " +
                "was an error parsing the dates returned from the database. Ensure " +
                "that they're being stored correctly.");
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Inserts a new forum thread into the database.
     */
    private void insertIntoDb() {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(INSERT_THREAD1);
            pstmt.setInt(1, id);
            pstmt.setInt(2, rootMessageID);
            pstmt.setString(3, ""+creationDate.getTime());
            pstmt.setString(4, ""+modifiedDate.getTime());
            pstmt.setString(5, name);
            pstmt.setInt(6, approved?1:0);
            pstmt.executeUpdate();
            pstmt.close();

            pstmt = con.prepareStatement(INSERT_THREAD2);
            pstmt.setInt(1, id);
            pstmt.setInt(2, rootMessageID);
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForumThread:insertIntoDb()-"+sqle);
            sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Saves the ForumThread to the database.
     */
    private synchronized void saveToDb() {
        Connection con = null;
        PreparedStatement pstmt = null;
         try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SAVE_THREAD);
            pstmt.setString(1, name);
            pstmt.setInt(2, rootMessageID);
            pstmt.setString(3, ""+creationDate.getTime());
            pstmt.setString(4, ""+modifiedDate.getTime());
            pstmt.setInt(5, id);
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println("Error in DbForumThread:saveToDb()-" + sqle);
        }
        finally {
            try {  pstmt.close(); }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }
} 

⌨️ 快捷键说明

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