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

📄 dbforumthread.java

📁 一个jive论坛管理的源码 学习Jive源程序
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                // Set the maxium number of rows to end at the end of this block.
                ConnectionManager.setMaxRows(stmt, DbForum.MESSAGE_BLOCK_SIZE * (blockID+1));
                ResultSet rs = stmt.executeQuery(query);
                // Grab MESSAGE_BLOCK_ROWS rows at a time.
                ConnectionManager.setFetchSize(rs, DbForum.MESSAGE_BLOCK_SIZE);
                // Many JDBC drivers don't implement scrollable cursors the real
                // way, but instead load all results into memory. Looping through
                // the results ourselves is more efficient.
                for (int i=0; i<blockStart; i++) {
                    rs.next();
                }
                // Keep reading results until the result set is exaughsted or
                // we come to the end of the block.
                int count = 0;
                while (rs.next() && count < DbForum.MESSAGE_BLOCK_SIZE) {
                    messagesList.add(rs.getLong(1));
                    count++;
                }
            }
            catch( SQLException sqle ) {
                sqle.printStackTrace();
            }
            finally {
                try {  stmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            long [] messages = messagesList.toArray();
            // Add the message block to cache
            messageListCache.add(key, new CacheableLongArray(messages));
            /**
             * 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.
             */
            if (startIndex >= blockStart + messages.length) {
                // Return an empty array
                return EMPTY_BLOCK;
            }
            else {
                return messages;
            }
        }
    }

    /**
     * Loads a ForumThread from the database.
     */
    private void loadFromDb() throws ForumThreadNotFoundException {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();

            pstmt = con.prepareStatement(LOAD_THREAD);
            pstmt.setLong(1, id);
            ResultSet rs = pstmt.executeQuery();
            if( !rs.next() ) {
                throw new ForumThreadNotFoundException("Thread " + id +
                    " could not be loaded from the database.");
            }
            forumID = rs.getLong(1);
            rootMessageID = rs.getLong(2);
            moderationValue = rs.getInt(3);
            rewardPoints = rs.getInt(4);
            creationDate = new java.util.Date(Long.parseLong(rs.getString(5).trim()));
            modifiedDate =  new java.util.Date(Long.parseLong(rs.getString(6).trim()));

            // Now, load any extended thread properties
            if (!LAZY_PROP_LOADING) {
                pstmt.close();
                properties = new Hashtable();
                pstmt = con.prepareStatement(LOAD_PROPERTIES);
                pstmt.setLong(1, id);
                rs = pstmt.executeQuery();
                while(rs.next()) {
                    properties.put(rs.getString(1), rs.getString(2));
                }
            }
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
            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. A connection object must
     * be passed in. The connection must be open when passed in, and will
     * remain open when passed back. This method allows us to make insertions
     * be transactional.
     *
     * @param con an open Connection used to insert the thread to the db.
     */
    protected void insertIntoDb(DbForum forum, Connection con)
            throws SQLException, UnauthorizedException
    {
        this.forumID = forum.getID();
        // If the moderation value hasn't already been set, use the default.
        if (this.moderationValue == Integer.MIN_VALUE) {
            this.moderationValue = forum.getModerationDefaultThreadValue();
        }
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(INSERT_THREAD);
            pstmt.setLong(1, id);
            pstmt.setLong(2, forum.getID());
            pstmt.setLong(3, rootMessageID);
            pstmt.setInt(4, moderationValue);
            pstmt.setInt(5, rewardPoints);
            pstmt.setString(6, StringUtils.dateToMillis(creationDate));
            pstmt.setString(7, StringUtils.dateToMillis(modifiedDate));
            pstmt.executeUpdate();
        }
        finally {
            try {  pstmt.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }

        // Now, insert the message into the database. Depending on if this
        // method was called internally or not, the newMessage object might
        // be wrapped by a proxy or not.
        DbForumMessage dbMessage = null;
        if (rootMessage instanceof ForumMessageProxy) {
            ForumMessageProxy proxyMessage = (ForumMessageProxy)rootMessage;
            dbMessage = (DbForumMessage)proxyMessage.getProxiedForumMessage();
            dbMessage.insertIntoDb(this, -1, con);
        }
        else {
            dbMessage = (DbForumMessage)rootMessage;
            dbMessage.insertIntoDb(this, -1, con);
        }

        // Now, insert properties into database
        if (this.properties.size() > 0) {
            savePropertiesToDb(con, false);
        }

        // Notify gateway manager of new message being inserted if the message
        // is above the min moderation threshold for the forum. If it's below
        // the threshold, the message will simply get exported later if the
        // moderation value is set high enough.
        if (dbMessage.getModerationValue() >=
                    forum.getModerationMinMessageValue())
        {
//            forum.getGatewayManager().exportData(dbMessage);
        }

        // Set root message to null sice reference is no longer needed.
        rootMessage = null;

        // Since we're done inserting the object to the database, it is ready
        // for future insertions.
        isReadyToSave = true;
    }

    /**
     * Saves the ForumThread to the database.
     */
    private synchronized void saveToDb() {
        Connection con = null;
        try {
            con = ConnectionManager.getConnection();
            saveToDb(con);
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        finally {
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * A version of saveToDb that accepts a Connection. This is useful for
     * participating in transactions.
     */
    private synchronized void saveToDb(Connection con) throws SQLException
    {
        PreparedStatement pstmt = null;
        try {
            pstmt = con.prepareStatement(SAVE_THREAD);
            pstmt.setLong(1, getRootMessage().getID());
            pstmt.setInt(2, moderationValue);
            pstmt.setInt(3, rewardPoints);
            pstmt.setString(4, StringUtils.dateToMillis(creationDate));
            pstmt.setString(5, StringUtils.dateToMillis(modifiedDate));
            pstmt.setLong(6, id);
            pstmt.executeUpdate();
        }
        finally {
            try {  pstmt.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Loads properties from the database.
     */
    private synchronized void loadPropertiesFromDb() {
        this.properties = new Hashtable();
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = ConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_PROPERTIES);
            pstmt.setLong(1, id);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                properties.put(rs.getString(1), rs.getString(2));
            }
        }
        catch( SQLException sqle ) {
           sqle.printStackTrace();
        }
        finally {
            try {  pstmt.close();   }
            catch (Exception e) { e.printStackTrace(); }
            try {  con.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Saves properties to the database. Normally, this method will clear out
     * all old property values before saving new ones. However, the deletion
     * step will be skipped if specified. This is useful for the case that you
     * know that no current properties could possibly exist.
     *
     * @param con a database connection to be used for property saving.
     * @param deleteOldProps true if old properties should be deleted before
     *      saving new ones. This should be true in most cases.
     */
    private synchronized void savePropertiesToDb(Connection con,
            boolean deleteOldProps) throws SQLException
    {
        PreparedStatement pstmt = null;
        try {
            if (deleteOldProps) {
                // Delete all old values.
                pstmt = con.prepareStatement(DELETE_PROPERTIES);
                pstmt.setLong(1, id);
                pstmt.execute();
                pstmt.close();
            }
            // Now insert new values.
            pstmt = con.prepareStatement(INSERT_PROPERTY);
            Iterator iter = properties.keySet().iterator();
            while (iter.hasNext()) {
                String name = (String)iter.next();
                String value = (String)properties.get(name);
                pstmt.setLong(1, id);
                pstmt.setString(2, name);
                pstmt.setString(3, value);
                pstmt.executeUpdate();
            }
        }
        finally {
            try {  pstmt.close();   }
            catch (Exception e) { e.printStackTrace(); }
        }
    }

    /**
     * Deletes a property from the db.
     */
    private synchronized void deletePropertyFromDb(String name) {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
           con = ConnectionManager.getConnection();
           pstmt = con.prepareStatement(DELETE_PROPERTY);
           pstmt.setLong(1, id);
           pstmt.setString(2, name);
           pstmt.execute();
        }
        catch( SQLException sqle ) {
            sqle.printStackTrace();
        }
        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 + -