dbuser.java

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

JAVA
760
字号
            deletePropertyFromDb(name);            // Re-add user to cache.            factory.cacheManager.userCache.put(new Long(this.id), this);        }    }    public Iterator propertyNames() {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        return Collections.unmodifiableSet(properties.keySet()).iterator();    }    public int getRewardPoints() {        return rewardPoints;    }    public void setRewardPoints(int rewardPoints) throws SQLException {        // Save old point value in case something goes wrong.        int oldPoints = this.rewardPoints;        this.rewardPoints = rewardPoints;        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(SAVE_REWARD_POINTS);            pstmt.setInt(1, rewardPoints);            pstmt.setLong(2, this.id);            pstmt.executeUpdate();        }        catch (SQLException sqle) {            // Switch back to old point value.            this.rewardPoints = oldPoints;            // Throw an exception so that the caller knows that the update            // operation failed.            throw sqle;        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        // Re-add user to cache.        factory.cacheManager.userCache.put(new Long(this.id), this);    }    public ForumPermissions getPermissions(Authorization authorization) {        if (authorization.getUserID() == id) {            return USER_ADMIN_PERMS;        }        else {            return ForumPermissions.none();        }    }    public boolean hasPermission(int type) {        return true;    }    //FROM THE CACHEABLE INTERFACE//    public int getCachedSize() {        // 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.sizeOfLong();                // id        size += CacheSizes.sizeOfString(username);      // username        size += CacheSizes.sizeOfString(passwordHash);  // password        size += CacheSizes.sizeOfString(name);          // name        size += CacheSizes.sizeOfString(email);         // email        size += CacheSizes.sizeOfBoolean();             // nameVisible        size += CacheSizes.sizeOfBoolean();             // emailVisible        size += CacheSizes.sizeOfMap(properties);       // properties        size += CacheSizes.sizeOfDate();                // creationDate        size += CacheSizes.sizeOfDate();                // modifiedDate        size += CacheSizes.sizeOfInt();                 // reward points        size += CacheSizes.sizeOfObject();              // factory        return size;    }    //OTHER METHODS    /**     * Returns a String representation of the User object using the username.     *     * @return a String representation of the User object.     */    public String toString() {        return username;    }    public int hashCode() {        return (int)id;    }    public boolean equals(Object object) {        if (this == object) {            return true;        }        if (object != null && object instanceof User) {            return id == ((User)object).getID();        }        else {            return false;        }    }    /**     * 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 = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            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(); }            try {  con.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 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(); }        }    }    /**     * Load the user data from the database.     */    private void loadFromDb() throws UserNotFoundException {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            if (username != null) {                pstmt = con.prepareStatement(LOAD_USER_BY_USERNAME);                pstmt.setString(1, username);            }            else {                pstmt = con.prepareStatement(LOAD_USER_BY_ID);                pstmt.setLong(1, id);            }            ResultSet rs = pstmt.executeQuery();            if (!rs.next()) {                if (username != null) {                    throw new UserNotFoundException(                        "Failed to read user " + username + " from database."                    );                }                else {                    throw new UserNotFoundException(                        "Failed to read user with ID " + id + " from database."                    );                }            }            this.id = rs.getLong(1);            this.username = rs.getString(2);            this.passwordHash = rs.getString(3);            this.name = rs.getString(4);            this.nameVisible = (rs.getInt(5) == 1);            this.email = rs.getString(6);            this.emailVisible = (rs.getInt(7) == 1);            this.rewardPoints = rs.getInt(8);            // 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(9).trim()));            this.modifiedDate =                new java.util.Date(Long.parseLong(rs.getString(10).trim()));            // Now, load any extended message 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 ) {            throw new UserNotFoundException(                "Failed to read user " + id + " from database.", sqle            );        }        catch (NumberFormatException nfe) {            System.err.println("WARNING: There was an error parsing the dates " +                "returned from the database. Ensure that they're being stored " +                "correctly.");            throw new UserNotFoundException("User with id "                    + id + " could not be loaded from the database."            );        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Inserts a new user record into the database.     */    private void insertIntoDb() {        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            pstmt = con.prepareStatement(INSERT_USER);            pstmt.setLong(1, id);            pstmt.setString(2, username);            pstmt.setString(3, passwordHash);            pstmt.setString(4, name);            pstmt.setInt(5, nameVisible?1:0);            pstmt.setString(6, email);            pstmt.setInt(7, emailVisible?1:0);            pstmt.setInt(8, rewardPoints);            pstmt.setString(9, StringUtils.dateToMillis(creationDate));            pstmt.setString(10, StringUtils.dateToMillis(modifiedDate));            pstmt.executeUpdate();            // Now, insert properties if there are any.            if (properties.size() > 0) {                // Close the previously opened statement.                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();                }            }        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }    /**     * Save the user data to the database.     */    private 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(); }        }    }    /**     * Implementation of saveToDb that takes a Connection. This is useful for     * participating in transactions.     */    private void saveToDb(Connection con) throws SQLException {        PreparedStatement pstmt = null;        try {            pstmt = con.prepareStatement(SAVE_USER);            pstmt.setString(1, passwordHash);            pstmt.setString(2, name);            pstmt.setInt(3, nameVisible?1:0);            pstmt.setString(4, email);            pstmt.setInt(5, emailVisible?1:0);            pstmt.setInt(6, rewardPoints);            pstmt.setString(7, StringUtils.dateToMillis(creationDate));            pstmt.setString(8, StringUtils.dateToMillis(modifiedDate));            pstmt.setLong(9, id);            pstmt.executeUpdate();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }        }    }}

⌨️ 快捷键说明

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