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

📄 dbforummessage.java

📁 Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    public boolean isAnonymous() {
        return (userID == -1);
    }

    public ForumThread getForumThread() {
        if (thread != null) {
            return thread;
        }
        //Load the thread since this is the first time the method has been
        //called.
        else {
            //First, we need a handle on the parent Forum object based
            //on the threadID.
            int forumID = -1;
            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = DbConnectionManager.getConnection();
                pstmt = con.prepareStatement(GET_FORUM_BY_THREAD);
                pstmt.setInt(1, threadID);
                ResultSet rs = pstmt.executeQuery();
                if (rs.next()) {
                    forumID = rs.getInt("forumID");
                }
             }
            catch( SQLException sqle ) {
                sqle.printStackTrace();
            }
            finally {
                try {  pstmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            //If the forumID for the message is less than 1, we have problems.
            //Print a warning and return null
            if (forumID < 1) {
                System.err.println("WARNING: forumID of " + forumID +
                    " found for message " + id + " in DbForumMessage.getForumThread()." +
                    " You may wish to delete the message from your database."
                );
                return null;
            }

            Forum forum = null;
            ForumThread thread = null;
            try {
                forum = factory.getForum(forumID);
                //Now, get the thread
                thread = forum.getThread(threadID);
            }
            catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            this.thread = thread;
            return thread;
        }
    }

    public boolean hasPermission(int type) {
        return true;
    }

    //FROM CACHEABLE INTERFACE//

    public int getSize() {
        //Approximate the size of the object in bytes by calculating the size
        //of each field.
        int size = 0;
        size += CacheSizes.sizeOfObject();              //overhead of object
        size += CacheSizes.sizeOfInt();                 //id
        size += CacheSizes.sizeOfString(subject);       //subject
        size += CacheSizes.sizeOfString(body);          //body
        size += CacheSizes.sizeOfDate();                //creation date
        size += CacheSizes.sizeOfDate();                //modified date
        size += CacheSizes.sizeOfInt();                 //userID
        size += CacheSizes.sizeOfInt();                 //threadID
        size += CacheSizes.sizeOfMap(properties);       //map object
        size += CacheSizes.sizeOfObject();              //property lock
        size += CacheSizes.sizeOfObject();              //ref to factory

        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 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;
        }
    }

    /**
     * Loads message properties from the database.
     */
    private void loadProperties() {
        synchronized(propertyLock) {
            Properties newProps = new Properties();
            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = DbConnectionManager.getConnection();
                pstmt = con.prepareStatement(LOAD_PROPERTIES);
                pstmt.setInt(1, id);
                ResultSet rs = pstmt.executeQuery();
                while(rs.next()) {
                    String name = rs.getString("name");
                    String value = rs.getString("propValue");
                    newProps.put(name, value);
                }
            }
            catch( SQLException sqle ) {
                System.err.println("Error in DbForumMessage:loadProperties():" + sqle);
                sqle.printStackTrace();
            }
            finally {
                try {  pstmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            this.properties = newProps;
        }
    }

    /**
     * Saves message properties to the database.
     */
    private void saveProperties() {
        synchronized(propertyLock) {
            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = DbConnectionManager.getConnection();
                //Delete all old values.
                pstmt = con.prepareStatement(DELETE_PROPERTIES);
                pstmt.setInt(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.setInt(1, id);
                    pstmt.setString(2, name);
                    pstmt.setString(3, value);
                    pstmt.executeUpdate();
                }
            }
            catch( SQLException sqle ) {
                System.err.println(sqle);
            }
            finally {
                try {  pstmt.close(); }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
        }
    }

    /**
     *  Loads message and user data from the database.
     */
    private void loadFromDb() throws ForumMessageNotFoundException {
        // Based on the id in the object, get the message data from the database.
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_MESSAGE);
            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            if( !rs.next() ) {
                throw new ForumMessageNotFoundException("Message " + id +
                    " could not be loaded from the database.");
            }
            //Get the query results. We use int indexes into the ResultSet
            //because it is slightly faster. Care should be taken so that the
            //SQL query is not modified without modifying these indexes.
            this.userID = rs.getInt(1);
            //We trim() the dates before trying to parse them because some
            //databases pad with extra characters when returning the data.
            this.creationDate =
                new java.util.Date(Long.parseLong(rs.getString(2).trim()));
            this.modifiedDate =
                new java.util.Date(Long.parseLong(rs.getString(3).trim()));
            this.subject = rs.getString(4);
            this.body = rs.getString(5);
            this.threadID = rs.getInt(6);
         }
        catch( SQLException sqle ) {
            throw new ForumMessageNotFoundException( "Message of id "
                    + id + " was not found in the database."
            );
        }
        catch (NumberFormatException nfe) {
            System.err.println("WARNING: In DbForumMessage.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 message 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.
     * @param thread the ForumThread the message is being added to.
     */
    public void insertIntoDb(Connection con, ForumThread thread)
            throws SQLException
    {
        //Set the message threadID to the thread that the message is being
        //added to.
        this.threadID = thread.getID();
        PreparedStatement pstmt = con.prepareStatement(INSERT_MESSAGE);
        pstmt.setInt(1, id);
        pstmt.setInt(2, threadID);
        pstmt.setString(3, Long.toString(creationDate.getTime()));
        pstmt.setString(4, Long.toString(modifiedDate.getTime()));
        pstmt.setInt(5, userID);
        pstmt.setString(6, subject);
        pstmt.setString(7, body);
        pstmt.executeUpdate();
        pstmt.close();

        //We're done inserting the message, so now save any extended
        //properties to the database.
        saveProperties();

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

    /**
     *  Saves message data to the database.
     */
    private synchronized void saveToDb() {
        Connection con = null;
        PreparedStatement pstmt = null;
        try {
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(SAVE_MESSAGE);
            pstmt.setInt(1, userID);
            pstmt.setString(2, subject);
            pstmt.setString(3, body);
            pstmt.setString(4, Long.toString(creationDate.getTime()));
            pstmt.setString(5, Long.toString(modifiedDate.getTime()));
            pstmt.setInt(6, id);
            pstmt.executeUpdate();
        }
        catch( SQLException sqle ) {
            System.err.println( "SQLException in DbForumMessage:saveToDb()- " + 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 + -