dbforummessage.java

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

JAVA
1,052 行
字号
                try {  con.close();   }                catch (Exception e) { e.printStackTrace(); }            }        }        return new DatabaseObjectIterator(JiveGlobals.ATTACHMENT,                attachmentIDs, factory);    }    public int getModerationValue() {        return moderationValue;    }    public void setModerationValue(int value, Authorization auth) {        // If the new value is the same as the old value, return.        if (moderationValue == value) {            return;        }        setModValue(value, auth);        // If this is the root message of the thread, we must also set the        // moderation value of the thread. Implementation note: if this is the        // root message and the root message's thread hasn't been added to the        // db yet, then it's not possible to get a handle on the thread. This        // means we can't update the moderation value of the thread, which        // is a deviation from the API. The chance of this causing a real-world        // bug is quite low. Unfortunately, there is no current easy fix.        DbForumThread thread = (DbForumThread)getForumThread();        if (thread != null && thread.getRootMessage().getID() == this.id) {            thread.setModValue(value, auth);        }    }    public String getProperty(String name) {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        // Return the cached filter value if it exists.        if (filteredProperties != null) {            return (String)filteredProperties.get(name);        }        // Otherwise, return normal value.        else {            return (String)properties.get(name);        }    }    public String getUnfilteredProperty(String name) {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        return (String)properties.get(name);    }    public void setProperty(String name, String value) {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        // Make sure the property name and value aren't null.        if (name == null || value == null || "".equals(name) || "".equals(value)) {            throw new NullPointerException("Cannot set property with empty or null value.");        }        // See if we need to update a property value or insert a new one.        if (properties.containsKey(name)) {            // Only update the value in the database if the property value            // has changed.            if (!(value.equals(properties.get(name)))) {                properties.put(name, value);                if (readyToSave) {                    updatePropertyInDb(name, value);                    // Reset the cached filter output.                    filtersCached = false;                    filteredSubject = null;                    filteredBody = null;                    filteredProperties = null;                    // Re-add message to cache.                    factory.cacheManager.messageCache.put(new Long(this.id), this);                    try {                        factory.cacheManager.getForumThread(threadID).clearCache();                        factory.cacheManager.getForum(forumID).clearCache();                    } catch (Exception e) { }                }            }        }        else {            properties.put(name, value);            if (readyToSave) {                Connection con = null;                try {                    con = ConnectionManager.getConnection();                    insertPropertyIntoDb(name, value, con);                }                catch( SQLException sqle ) {                    sqle.printStackTrace();                }                finally {                    try {  con.close();   }                    catch (Exception e) { e.printStackTrace(); }                }                // Reset the cached filter output.                this.filtersCached = false;                filteredSubject = null;                filteredBody = null;                filteredProperties = null;                // Re-add message to cache.                factory.cacheManager.messageCache.put(new Long(this.id), this);                try {                    factory.cacheManager.getForumThread(threadID).clearCache();                    factory.cacheManager.getForum(forumID).clearCache();                } catch (Exception e) { }            }        }    }    public void deleteProperty(String name) {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        // Only delete the property if it exists.        if (properties.containsKey(name)) {            properties.remove(name);            // Only save to the db if the object is ready            if (readyToSave) {                deletePropertyFromDb(name);                // Update objects in cache.                filtersCached = false;                filteredSubject = null;                filteredBody = null;                filteredProperties = null;                factory.cacheManager.messageCache.put(new Long(this.id), this);                try {                    factory.cacheManager.getForumThread(threadID).clearCache();                    factory.cacheManager.getForum(forumID).clearCache();                } catch (Exception e) { }            }        }    }    public Iterator propertyNames() {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        return Collections.unmodifiableSet(properties.keySet()).iterator();    }    public boolean isAnonymous() {        return (userID == -1);    }    public ForumThread getForumThread() {        if (threadID == -1) {            return null;        }        try {            return factory.cacheManager.getForumThread(threadID);        }        catch (Exception e) {            e.printStackTrace();            return null;        }    }    public boolean hasPermission(int type) {        return true;    }    //FROM CACHEABLE INTERFACE//    public int getCachedSize() {        // Approximate the size of the object in bytes by calculating the size        // of each field. Filtered variable sizes are approximated since they        // are lazily loaded.        int size = 0;        size += CacheSizes.sizeOfObject();              // overhead of object        size += CacheSizes.sizeOfLong();                // id        size += CacheSizes.sizeOfString(subject) * 2;   // subject & filtered        size += CacheSizes.sizeOfString(body) * 2;      // body & filtered        size += CacheSizes.sizeOfDate();                // creation date        size += CacheSizes.sizeOfDate();                // modified date        size += CacheSizes.sizeOfLong();                // userID        size += CacheSizes.sizeOfLong();                // threadID        size += CacheSizes.sizeOfLong();                // forumID        size += CacheSizes.sizeOfMap(properties) * 2;   // map object * filtered        size += CacheSizes.sizeOfObject();              // ref to factory        size += CacheSizes.sizeOfInt();                 // moderation points        size += CacheSizes.sizeOfInt();                 // reward points        size += CacheSizes.sizeOfBoolean();             // filter cache flag.        return size;    }    //OTHER METHODS//    /**     * Returns a String representation of the message object using the subject.     *     * @return a String representation of the ForumMessage object.     */    public String toString() {        return subject;    }    public int hashCode() {        return (int)id;    }    public boolean equals(Object object) {        if (this == object) {            return true;        }        if (object != null && object instanceof DbForumMessage) {            return id == ((DbForumMessage)object).getID();        }        else {            return false;        }    }    /**     * 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;        }        // Re-add message to cache.        factory.cacheManager.messageCache.put(new Long(id), this);    }    protected void setModValue(int value, Authorization auth) {        if (moderationValue == value) {            return;        }        int oldValue = moderationValue;        moderationValue = value;        // Only save to the db if the object is ready        if (!readyToSave) {            return;        }        saveToDb();        long now = System.currentTimeMillis();        // We modified the moderation value of the message, so update the        // modified date.        this.modifiedDate.setTime(now);        // If the message is switching from hidden to visible, we should update        // the modified date of the thread and forum it belongs to. We'll also        // trigger a watch update and send out the message to gateways. These        // last actions have the potential for problems since it's possible that        // a message could switch from being below the moderation threshold to        // above multiple times. However, it's probably ok to let there be        // be multiple watch updates, and the gateway implementations should        // ensure that no message gets exported twice.        try {            DbForum dbForum = factory.cacheManager.getForum(this.forumID);            if (oldValue < dbForum.getModerationMinMessageValue()                    && value >= dbForum.getModerationMinMessageValue())            {                DbForumThread dbThread = (DbForumThread)dbForum.getThread(this.threadID);                Connection con = null;                boolean abortTransaction = false;                try {                    con = ConnectionManager.getTransactionConnection();                    dbThread.updateModifiedDate(now, con);                }                catch (SQLException sqle) {                    sqle.printStackTrace();                    abortTransaction = true;                }                finally {                    ConnectionManager.closeTransactionConnection(con, abortTransaction);                }                // Notify the watch manager that the thread has been updated.                factory.watchManager.notifyWatches(dbThread, this);                // Notify the gateway manager that we're ready to be exported.                dbForum.getGatewayManager().exportData(this);            }        }        catch (ForumNotFoundException fnfe) { /* ignore */ }        catch (ForumThreadNotFoundException ftnfe) { /* ignore */ }        // Re-add message to cache.        factory.cacheManager.messageCache.put(new Long(this.id), this);        // Update thread and forum caches.        try {            factory.cacheManager.getForumThread(threadID).clearCache();            factory.cacheManager.getForum(forumID).clearCache();        } catch (Exception e) { }        // Finally, make an entry into the jiveModeration table for moderation        // auditing purposes.        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(INSERT_MODERATION_ENTRY);            pstmt.setLong(1, id);            pstmt.setInt(2, JiveGlobals.MESSAGE);            if (auth.isAnonymous()) {                pstmt.setNull(3, Types.NUMERIC);            }            else {                pstmt.setLong(3, auth.getUserID());            }            pstmt.setString(4, StringUtils.dateToMillis(modifiedDate));            pstmt.setInt(5, moderationValue);            pstmt.executeUpdate();        }        catch (Exception e) {            e.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.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));            }

⌨️ 快捷键说明

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