dbforummessage.java

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

JAVA
1,052
字号
/** * $RCSfile: DbForumMessage.java,v $ * $Revision: 1.19 $ * $Date: 2002/08/02 00:11:21 $ * * Copyright (C) 1999-2002 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.database;import java.sql.*;import java.util.*;import java.io.*;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;/** * Database implementation of the ForumMessage interface. It stores messages * in the jiveMessage database table, and message properties in JiveMessageProp * table.<p> * * Messages are not inserted into the database until they are added * to a thread. Therefore, it is best to set all properties of the message * before adding it to a thread in order to avoid extra database operations.<p> * * One key feature of the Jive message architecture is the ability to apply * dynamic filters to messages (specified at the global and forum level). * However, computing the filtered value of a message can be fairly expensive. * * @author Matt Tucker */public final class DbForumMessage implements ForumMessage, Cacheable {    /** DATABASE QUERIES **/    private static final String LOAD_ATTACHMENTS =        "SELECT attachmentID FROM jiveAttachment WHERE messageID=?";    private static final String LOAD_PROPERTIES =        "SELECT name, propValue FROM jiveMessageProp WHERE messageID=?";    private static final String INSERT_PROPERTY =        "INSERT INTO jiveMessageProp(messageID,name,propValue) VALUES(?,?,?)";    private static final String UPDATE_PROPERTY =        "UPDATE jiveMessageProp SET propValue=? WHERE name=? AND messageID=?";    private static final String DELETE_PROPERTY =        "DELETE FROM jiveMessageProp WHERE messageID=? AND name=?";    private static final String LOAD_MESSAGE =        "SELECT threadID, forumID, userID, subject, body, modValue, rewardPoints, "+        "creationDate, modifiedDate FROM jiveMessage WHERE messageID=?";    private static final String INSERT_MESSAGE =        "INSERT INTO jiveMessage(messageID, parentMessageID, threadID, forumID, " +        "userID, subject, body, modValue, rewardPoints, creationDate, modifiedDate) " +        "VALUES(?,?,?,?,?,?,?,?,?,?,?)";    private static final String UPDATE_PARENT_ID =        "UPDATE jiveMessage SET parentMessageID=? WHERE messageID=?";    private static final String SAVE_MESSAGE =        "UPDATE jiveMessage SET subject=?, body=?, modValue=?, " +        "rewardPoints=?, creationDate=?, modifiedDate=? WHERE messageID=?";    private static final String INSERT_MODERATION_ENTRY =        "INSERT INTO jiveModeration(objectID, objectType, userID, modDate, " +        "modValue) VALUES(?,?,?,?,?)";    /**     * Controls whether extended properties should be lazily loaded (not loaded     * until requested). If the properties are infrequently used, this provides     * a great speedup in initial object loading time. However, if your     * application does use extended properties all the time, you may wish to     * turn lazy loading off, as it's actually faster in total db lookup time     * to load everything at once.     *     * NOTE: lazy loading turned off by default for now since new filter     * caching code always calls properties. This may be optimized in the     * future so that lazy property loading makes sense again.     */    private static final boolean LAZY_PROP_LOADING = false;    private static final long serialVersionUID = 01L;    private long id = -1;    private java.util.Date creationDate;    private java.util.Date modifiedDate;    private String subject = "";    private String body = "";    private long userID;    protected long threadID = -1;    protected long forumID = -1;    // Default the moderation point value to Integer.MIN_VALUE. Then, if the    // moderation value gets changed before being inserted into the db, we'll know.    protected int moderationValue = Integer.MIN_VALUE;    private int rewardPoints = 0;    private Map properties = null;    private long [] attachmentIDs;    // Variables to hold filtered values.    protected transient boolean filtersCached = false;    protected transient String filteredSubject = null;    protected transient String filteredBody = null;    protected transient Map filteredProperties = null;    private transient DbForumFactory factory;    /**     * Indicates if the object is ready to be saved or not. An object is not     * ready to be saved if it has just been created and has not yet been added     * to its container. For example, a message added to a thread, etc.     */    protected transient boolean readyToSave = false;    /**     * Creates a new DbForumMessage object. If the User object is null, we'll     * assume that the message has an anonymous author.     */    protected DbForumMessage(User user) {        if (user != null) {            this.userID = user.getID();        }        else {            this.userID = -1;        }        this.id = SequenceManager.nextID(JiveGlobals.MESSAGE);        long now = System.currentTimeMillis();        creationDate = new java.util.Date(now);        modifiedDate = new java.util.Date(now);        properties = new Hashtable();        factory = DbForumFactory.getInstance();    }    /**     * Loads the specified DbForumMessage by its message id.     */    protected DbForumMessage(long id) throws ForumMessageNotFoundException {        if (id < 0) {            throw new ForumMessageNotFoundException("ID " + id + " is not valid.");        }        this.id = id;        loadFromDb();        readyToSave = true;        factory = DbForumFactory.getInstance();    }    private void readObject(java.io.ObjectInputStream in)            throws IOException, ClassNotFoundException    {        in.defaultReadObject();        factory = DbForumFactory.getInstance();        readyToSave = true;    }    //FROM THE FORUMMESSAGE INTERFACE//    public long getID() {        return id;    }    public java.util.Date getCreationDate() {        return creationDate;    }    public void setCreationDate(java.util.Date creationDate)            throws UnauthorizedException    {        this.creationDate = creationDate;        // Only save to the db if the object is read        if (!readyToSave) {            return;        }        saveToDb();        // Re-add message to cache.        factory.cacheManager.messageCache.put(new Long(this.id), this);    }    public java.util.Date getModifiedDate() {        return modifiedDate;    }    public void setModifiedDate(java.util.Date modifiedDate)            throws UnauthorizedException    {        this.modifiedDate = modifiedDate;        // Only save to the db if the object is read        if (!readyToSave) {            return;        }        saveToDb();        // Re-add message to cache.        factory.cacheManager.messageCache.put(new Long(this.id), this);    }    public String getSubject() {        // Return cached filter value if it exists.        if (filteredSubject != null) {            return filteredSubject;        }        // Otherwise, return normal value.        else {            return subject;        }    }    public String getUnfilteredSubject() {        return subject;    }    public void setSubject(String subject) throws UnauthorizedException {        this.subject = subject;        // Only save to the db if the object is read        if (!readyToSave) {            return;        }        // Update modifiedDate to the current time.        modifiedDate.setTime(System.currentTimeMillis());        saveToDb();        // 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);    }    public String getBody() {        // Return cached filter value if it exists.        if (filteredBody != null) {            return filteredBody;        }        // Otherwise, return normal value.        else {            return body;        }    }    public String getUnfilteredBody() {        return body;    }    public void setBody(String body) throws UnauthorizedException {        this.body = body;        // Only save to the db if the object is read        if (!readyToSave) {            return;        }        // Update modifiedDate to the current time.        modifiedDate.setTime(System.currentTimeMillis());        saveToDb();        // 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);    }    public User getUser() {        if (userID == -1) {            return null;        }        User user = null;        try {            user = factory.getUserManager().getUser(userID);        }        catch (UserNotFoundException unfe) {            unfe.printStackTrace();        }        return user;    }    public Attachment createAttachment(String name, String contentType,            InputStream data) throws IllegalStateException, AttachmentException    {        // If the message has not yet been added to a thread, don't allow        // attachment to be created.        if (this.threadID == -1) {            throw new IllegalStateException("Cannot create an attachment before " +                "message is added to a thread.");        }        AttachmentManager attachmentManager = factory.getAttachmentManager();        // See if we're allowed to add another attachment.        if (getAttachmentCount() > attachmentManager.getMaxAttachmentsPerMessage()) {            throw new AttachmentException(AttachmentException.TOO_MANY_ATTACHMENTS);        }        DbAttachment attachment = new DbAttachment(id, name, contentType, data);        this.attachmentIDs = null;        // Re-add object to cache.        factory.cacheManager.messageCache.put(new Long(id), this);        return attachment;    }    public int getAttachmentCount() {        if (attachmentIDs == null) {            // load the attachment ID's            attachments();        }        return attachmentIDs.length;    }    public void deleteAttachment(Attachment attachment) throws AttachmentException {        // See if the attachment belongs to this message.        DbAttachment dbAttachment = (DbAttachment)attachment;        if (dbAttachment.getMessageID() != this.id) {            throw new IllegalArgumentException("Attachment does not belong " +                "to message.");        }        Connection con = null;        try {            con = ConnectionManager.getConnection();            dbAttachment.delete(con);            this.attachmentIDs = null;            // Re-add object to cache.            factory.cacheManager.messageCache.put(new Long(id), this);        }        catch (Exception e) {            throw new AttachmentException(AttachmentException.GENERAL_ERROR, e);        }        finally {            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    public Iterator attachments() {        if (attachmentIDs == null) {            LongList attachments = new LongList();            Connection con = null;            PreparedStatement pstmt = null;            try {                con = ConnectionManager.getConnection();                pstmt = con.prepareStatement(LOAD_ATTACHMENTS);                pstmt.setLong(1,  this.id);                ResultSet rs = pstmt.executeQuery();                while (rs.next()) {                    attachments.add(rs.getLong(1));                }                this.attachmentIDs = attachments.toArray();                // Re-add object to cache.                factory.cacheManager.messageCache.put(new Long(id), this);            }            catch(SQLException sqle) {                sqle.printStackTrace();            }            finally {                try {  pstmt.close();   }                catch (Exception e) { e.printStackTrace(); }

⌨️ 快捷键说明

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