dbforumthread.java

来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,412 行 · 第 1/4 页

JAVA
1,412 行
字号
            // If already in cache, return the count.            if (count != null) {                return count.intValue();            }            // Otherwise, we have to load the count from the db.            else {                Connection con = null;                Statement stmt = null;                try {                    con = ConnectionManager.getConnection();                    stmt = con.createStatement();                    ResultSet rs = stmt.executeQuery(key.getSql());                    rs.next();                    count = new Integer(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 count to cache                factory.cacheManager.queryCache.put(key, count);                addQueryKey(key);                return count.intValue();            }        }    }    /**     * Adds a key to the queryKey list. The method ensures that the list never     * grows larger than fifty elements by removing keys from the end of the     * list. This doesn't necessarily ensure proper caching behavior, but the     * queryKeys list will only grow large in rare circumstances, such as when     * statistics are being run.     *     * @param key the key to add to the queryKey list.     */    protected void addQueryKey(Object key) {        synchronized (queryKeys) {        queryKeys.add(key);            if (queryKeys.size() > 50) {                factory.cacheManager.queryCache.remove(queryKeys.removeLast());            }        }        // Re-add thread to cache.        factory.cacheManager.threadCache.put(new Long(id), this);    }    /**     * Loads a ForumThread from the database.     */    private void loadFromDb() throws ForumThreadNotFoundException {        if (id < 0) {            throw new ForumThreadNotFoundException("Thread " + id + " is not valid.");        }        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 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();        // See if this is a new message or an existing one. If it's an existing one, we need        // to move the existing message and all sub-messages from its current thread to the new        // one.        boolean newMessage = rootMessage.getForumThread() == null;        // Get a handle on the dbMessage object. Depending on if this method was        // called internally or not, the rootMessage object might be wrapped by a proxy or not.        DbForumMessage dbMessage = null;        if (newMessage) {            if (rootMessage instanceof ForumMessageProxy) {                ForumMessageProxy proxyMessage = (ForumMessageProxy)rootMessage;                dbMessage = (DbForumMessage)proxyMessage.getProxiedForumMessage();            }            else {                dbMessage = (DbForumMessage)rootMessage;            }        }        else {            try {                dbMessage = factory.cacheManager.getMessage(rootMessage.getID());            }            catch (Exception e) { e.printStackTrace(); }        }        // If this is a new message, base the message moderation value on the thread's value.        if (newMessage) {            // If the moderation value hasn't already been set, use the default.            if (this.moderationValue == Integer.MIN_VALUE) {                this.moderationValue = forum.getModerationDefaultThreadValue();                // Update the root message moderation value as well since they                // must be linked.                dbMessage.moderationValue = this.moderationValue;            }        }        // Otherwise, this is an existing message so use its moderation value for the thread.        else {            // If the moderation value hasn't already been set, use the moderation value of            // the message.            if (this.moderationValue == Integer.MIN_VALUE) {                this.moderationValue = dbMessage.moderationValue;            }        }        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(); }        }        if (newMessage) {            // Now, insert the message into the database.            dbMessage.insertIntoDb(this, -1, con);        }        else {            // Move the message and all sub-messages from their existing thread to this new            // thread.            dbMessage.moveMessage(this, -1, con);        }        // Now, insert properties into database        for (Iterator i=properties.keySet().iterator(); i.hasNext(); ) {            String name = (String)i.next();            String value = (String)properties.get(name);            insertPropertyIntoDb(name, value, con);        }        // 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.        readyToSave = 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(); }        }    }    /**     * Inserts a new property into the datatabase.     */    private void insertPropertyIntoDb(String name, String value, Connection con) {        PreparedStatement pstmt = null;        try {            pstmt = con.prepareStatement(INSERT_PROPERTY);            pstmt.setLong(1, id);            pstmt.setString(2, name);            pstmt.setString(3, value);            pstmt.executeUpdate();        }        catch( SQLException sqle ) {           sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Updates a property value in the database.     */    private void updatePropertyInDb(String name, String value) {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(UPDATE_PROPERTY);            pstmt.setString(1, value);            pstmt.setString(2, name);            pstmt.setLong(3, id);            pstmt.executeUpdate();        }        catch( SQLException sqle ) {           sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.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 + =
减小字号Ctrl + -
显示快捷键?