dbuser.java

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

JAVA
760
字号
/** * $RCSfile: DbUser.java,v $ * $Revision: 1.9 $ * $Date: 2002/07/16 13:17:05 $ * * Copyright (C) 1999-2001 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 com.jivesoftware.forum.*;import com.jivesoftware.util.*;import java.util.*;import java.sql.*;import java.io.IOException;/** * Database implementation of the User interface. Additionally, it filters all * HTML tags from fields before returning them for security purposes.<p> * * The DbUser keeps track of a user's reward points. Optionally, you * can specify a initial number of reward points to assign a newly created user * by setting the Jive property <tt>rewards.initialPoints</tt>. If no value is * set, the default will be 0 points. */public class DbUser implements User, Cacheable {    /** DATABASE QUERIES **/    private static final String LOAD_PROPERTIES =        "SELECT name, propValue FROM jiveUserProp WHERE userID=?";    private static final String DELETE_PROPERTY =        "DELETE FROM jiveUserProp WHERE userID=? AND name=?";    private static final String UPDATE_PROPERTY =        "UPDATE jiveUserProp SET propValue=? WHERE name=? AND userID=?";    private static final String INSERT_PROPERTY =        "INSERT INTO jiveUserProp(userID,name,propValue) VALUES(?,?,?)";    private static final String LOAD_USER_BY_USERNAME =        "SELECT userID,username,passwordHash,name,nameVisible,email,emailVisible,"+        "rewardPoints,creationDate,modifiedDate FROM jiveUser WHERE username=?";    private static final String LOAD_USER_BY_ID =        "SELECT userID,username,passwordHash,name,nameVisible,email,emailVisible,"+        "rewardPoints,creationDate,modifiedDate FROM jiveUser WHERE userID=?";    private static final String INSERT_USER =        "INSERT INTO jiveUser(userID,username,passwordHash,name,nameVisible," +        "email,emailVisible,rewardPoints,creationDate,modifiedDate) " +        "VALUES(?,?,?,?,?,?,?,?,?,?)";    private static final String SAVE_USER =        "UPDATE jiveUser SET passwordHash=?,name=?,nameVisible=?,email=?," +        "emailVisible=?,rewardPoints=?,creationDate=?,modifiedDate=? WHERE " +        "userID=?";    private static final String SAVE_REWARD_POINTS =        "UPDATE jiveUser SET rewardPoints=? WHERE userID=?";    /**     * 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.     */    private static final boolean LAZY_PROP_LOADING = true;    private static final ForumPermissions USER_ADMIN_PERMS =            new ForumPermissions(false,false,false,true,false,false,false,false,                    false,false, false, false);    private static int initialRewardPoints;    static {        // Load the default reward points to assign new users. Default is 0.        initialRewardPoints = 0;        // Load a user defined value if specified.        try {            String points = JiveGlobals.getJiveProperty("rewards.initialPoints");            if (points != null) {                initialRewardPoints = Integer.parseInt(points);            }        }        catch(Exception e) { }    }    /**     * User id of -2 means no user id has been set yet. -1 is reserved for     * "anonymous user" and 0 is reserved for "all users".     */    private long id = -2;    private String username = null;    private String passwordHash;    private String name = "";    private boolean nameVisible = true;    private String email;    private boolean emailVisible = true;    private int rewardPoints;    private java.util.Date creationDate;    private java.util.Date modifiedDate;    private Map properties;    private transient DbForumFactory factory;    /**     * Create a new DbUser with all required fields.     *     * @param username the username for the user.     * @param password a password for the user.     * @param email the email address for the user.     */    protected DbUser(String username, String password, String email) {        this.id = SequenceManager.nextID(JiveGlobals.USER);        this.username = username;        // Compute hash of password.        this.passwordHash = StringUtils.hash(password);        this.email = email;        // Set the reward points to the default.        this.rewardPoints = initialRewardPoints;        long now = System.currentTimeMillis();        creationDate = new java.util.Date(now);        modifiedDate = new java.util.Date(now);        properties = new Hashtable();        insertIntoDb();        init();    }    /**     * Create a new DbUser with all required AND optional fields. Using this     * constructor can result in significantly fewer database operations than     * calling the other create constructor and then using setter methods.     *     * @param username the username for the user.     * @param password a password for the user.     * @param email the email address for the user.     */    protected DbUser(String username, String password, String name, String email,            boolean nameVisible, boolean emailVisible, Map properties)    {        this.id = SequenceManager.nextID(JiveGlobals.USER);        this.username = username;        // Compute hash of password.        this.passwordHash = StringUtils.hash(password);        this.email = email;        if (name != null) {            this.name = name;        }        this.nameVisible = nameVisible;        this.emailVisible = emailVisible;        this.rewardPoints = initialRewardPoints;        long now = System.currentTimeMillis();        creationDate = new java.util.Date(now);        modifiedDate = new java.util.Date(now);        if (properties != null) {            // Make sure that the none of the properties are null values.            Iterator iter = properties.keySet().iterator();            while (iter.hasNext()) {                String pName = (String)iter.next();                if (pName == null || "".equals(pName)) {                    throw new NullPointerException(                            "Cannot set property with empty or null value."                    );                }            }            iter = properties.values().iterator();            while (iter.hasNext()) {                String pValue = (String)iter.next();                if (pValue == null || "".equals(pValue)) {                    throw new NullPointerException(                            "Cannot set property with empty or null value."                    );                }            }            // No null found, set the properties.            this.properties = properties;        }        else {            this.properties = new Hashtable();        }        insertIntoDb();        init();    }    /**     * Load a DbUser object specified by userID.     *     * @param userID the userID of the user to load.     */    protected DbUser(long userID) throws UserNotFoundException {        if (userID < 1) {            throw new UserNotFoundException();        }        this.id = userID;        loadFromDb();        init();    }    private void init() {        factory = DbForumFactory.getInstance();    }    private void readObject(java.io.ObjectInputStream in)            throws IOException, ClassNotFoundException    {        in.defaultReadObject();        init();    }    /**     * Load a DbUser object specified by username.     *     * @param username the username of the user to load.     */    protected DbUser(String username, DbForumFactory factory)            throws UserNotFoundException    {        if (username == null) {            throw new UserNotFoundException();        }        this.username = username;        this.factory = factory;        loadFromDb();    }    //FROM THE USER INTERFACE//    public long getID() {        return id;    }    public String getUsername() {        return StringUtils.escapeHTMLTags(username);    }    public String getName() {        return StringUtils.escapeHTMLTags(name);    }    public void setName(String name) throws UnauthorizedException {        this.name = name;        // Update modified date        modifiedDate.setTime(System.currentTimeMillis());        saveToDb();        // Re-add user to cache.        factory.cacheManager.userCache.put(new Long(this.id), this);    }    public boolean isNameVisible() {        return nameVisible;    }    public void setNameVisible(boolean visible) throws UnauthorizedException {        this.nameVisible = visible;        // Update modified date        modifiedDate.setTime(System.currentTimeMillis());        saveToDb();        // Re-add user to cache.        factory.cacheManager.userCache.put(new Long(this.id), this);    }    public void setPassword(String password) throws UnauthorizedException {        // Compute hash of password.        this.passwordHash = StringUtils.hash(password);        // Update modified date        modifiedDate.setTime(System.currentTimeMillis());        saveToDb();        // Re-add user to cache.        factory.cacheManager.userCache.put(new Long(this.id), this);    }    public String getPasswordHash() throws UnauthorizedException {        return passwordHash;    }    public void setPasswordHash(String passwordHash) {        this.passwordHash = passwordHash;        // Update modified date        modifiedDate.setTime(System.currentTimeMillis());        saveToDb();        // Re-add user to cache.        factory.cacheManager.userCache.put(new Long(this.id), this);    }    public String getEmail() {        return StringUtils.escapeHTMLTags(email);    }    public void setEmail(String email) throws UnauthorizedException {        this.email = email;        // Update modified date        modifiedDate.setTime(System.currentTimeMillis());        saveToDb();        // Re-add user to cache.        factory.cacheManager.userCache.put(new Long(this.id), this);    }    public boolean isEmailVisible() {        return emailVisible;    }    public void setEmailVisible(boolean visible) throws UnauthorizedException {        this.emailVisible = visible;        // Update modified date        modifiedDate.setTime(System.currentTimeMillis());        saveToDb();        // Re-add user to cache.        factory.cacheManager.userCache.put(new Long(this.id), this);    }    public java.util.Date getCreationDate() {        return creationDate;    }    public void setCreationDate(java.util.Date creationDate)            throws UnauthorizedException    {        this.creationDate = creationDate;        saveToDb();        // Re-add user to cache.        factory.cacheManager.userCache.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;        saveToDb();        // Re-add user to cache.        factory.cacheManager.userCache.put(new Long(this.id), this);    }    public String getProperty(String name) {        if (LAZY_PROP_LOADING && properties == null) {            loadPropertiesFromDb();        }        return StringUtils.escapeHTMLTags((String)properties.get(name));    }    public synchronized 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);                updatePropertyInDb(name, value);                // Re-add user to cache.                factory.cacheManager.userCache.put(new Long(this.id), this);            }        }        else {            properties.put(name, value);            insertPropertyIntoDb(name, value);            // Re-add user to cache.            factory.cacheManager.userCache.put(new Long(this.id), this);        }    }    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);

⌨️ 快捷键说明

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