dbgroup.java

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

JAVA
824
字号
/** * $RCSfile: DbGroup.java,v $ * $Revision: 1.18 $ * $Date: 2002/07/14 03:57:23 $ * * 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 com.jivesoftware.forum.*;import com.jivesoftware.util.*;import java.util.*;import java.sql.*;import java.io.IOException;/** * Database implementation of the Group interface. Most operations on a group * require trips to the database, with the exception of the isMember(User) * method, which uses a cache to make the test fast. * * @see Group * @author Matt Tucker */public class DbGroup implements Group, Cacheable {    /** DATABASE QUERIES **/    private static final String ADD_ADMIN =        "INSERT INTO jiveGroupUser(groupID,userID,administrator) VALUES(?,?,1)";    private static final String REMOVE_ADMIN =        "DELETE FROM jiveGroupUser WHERE groupID=? AND userID=? AND administrator=1";    private static final String ADD_USER =        "INSERT INTO jiveGroupUser(groupID,userID,administrator) VALUES(?,?,0)";    private static final String REMOVE_USER =        "DELETE FROM jiveGroupUser WHERE groupID=? AND userID=? AND administrator=0";    private static final String ADMIN_TEST =        "SELECT userID FROM jiveGroupUser WHERE groupID=? AND userID=? AND " +        "administrator=1";    private static final String MEMBER_TEST =        "SELECT userID FROM jiveGroupUser WHERE groupID=? AND userID=? " +        "AND administrator=0";    private static final String LOAD_ADMINS =        "SELECT userID FROM jiveGroupUser WHERE administrator=1 AND groupID=?";    private static final String LOAD_MEMBERS =        "SELECT userID FROM jiveGroupUser WHERE administrator=0 AND groupID=?";    private static final String LOAD_GROUP_BY_ID =        "SELECT * FROM jiveGroup WHERE groupID=?";    private static final String LOAD_GROUP_BY_NAME =        "SELECT * FROM jiveGroup WHERE name=?";    private static final String INSERT_GROUP =        "INSERT INTO jiveGroup(name,description,groupID,creationDate," +        "modifiedDate) VALUES(?,?,?,?,?)";    private static final String SAVE_GROUP =        "UPDATE jiveGroup SET name=?, description=?, creationDate=?, " +        "modifiedDate=? WHERE groupID=?";    private static final String UPDATE_MODIFIED_DATE =        "UPDATE jiveGroup SET modifiedDate=? WHERE groupID=?";    private static final String LOAD_PROPERTIES =        "SELECT name, propValue FROM jiveGroupProp WHERE groupID=?";    private static final String DELETE_PROPERTY =        "DELETE FROM jiveGroupProp WHERE groupID=? AND name=?";    private static final String UPDATE_PROPERTY =        "UPDATE jiveGroupProp SET propValue=? WHERE name=? AND groupID=?";    private static final String INSERT_PROPERTY =        "INSERT INTO jiveGroupProp(groupID,name,propValue) VALUES(?,?,?)";    private long id;    private String name = null;    private String description = "";    private java.util.Date creationDate;    private java.util.Date modifiedDate;    private Map properties;    private long [] memberList;    private long [] adminList;    private transient DbForumFactory factory;    /**     * Creates a new group.     *     * @param name the name of the group.     */    protected DbGroup(String name) {        this.name = name;        this.id = SequenceManager.nextID(JiveGlobals.GROUP);        long now = System.currentTimeMillis();        creationDate = new java.util.Date(now);        modifiedDate = new java.util.Date(now);        properties = new Hashtable();        insertIntoDb();        init();    }    /**     * Loads a group from the database based on its id.     *     * @param id the id of the group to load.     */    protected DbGroup(long id)            throws GroupNotFoundException    {        this.id = id;        loadFromDb();        init();    }    /**     * Loads a group from the database based on its name. The implementation     * of this method is rather hackish since it includes a fake parameter just     * so that it can have a different method signature than the first     * constructor. Even so, this methodology makes this class behave more like     * our other classes, so we're gleefully leaving it this way. :)     *     * @param name the name of the group to load.     * @param fake a fake paramater that can always be null.     */    protected DbGroup(String name, Object fake)            throws GroupNotFoundException    {        this.name = name;        loadFromDb();        init();    }    private void init() {        factory = DbForumFactory.getInstance();    }    private void readObject(java.io.ObjectInputStream in)            throws IOException, ClassNotFoundException    {        in.defaultReadObject();        init();    }    // FROM THE USER INTERFACE //    public long getID() {        return id;    }    public String getName() {        return name;    }    public void setName(String name) throws UnauthorizedException {        this.name = name;        saveToDb();    }    public String getDescription() {        return description;    }    public void setDescription(String description)            throws UnauthorizedException    {        this.description = description;        saveToDb();    }    public java.util.Date getCreationDate() {        return creationDate;    }    public void setCreationDate(java.util.Date creationDate)            throws UnauthorizedException    {        this.creationDate = creationDate;        saveToDb();    }    public java.util.Date getModifiedDate() {        return modifiedDate;    }    public void setModifiedDate(java.util.Date modifiedDate)            throws UnauthorizedException    {        this.modifiedDate = modifiedDate;        saveToDb();    }    public String getProperty(String name) {        return (String)properties.get(name);    }    public void setProperty(String name, String value) {        // 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.groupCache.put(new Long(this.id), this);            }        }        else {            properties.put(name, value);            insertPropertyIntoDb(name, value);            // Re-add group to cache.            factory.cacheManager.groupCache.put(new Long(this.id), this);        }    }    public void deleteProperty(String name) {        // Only delete the property if it exists.        if (properties.containsKey(name)) {            properties.remove(name);            deletePropertyFromDb(name);            // Re-add group to cache.            factory.cacheManager.groupCache.put(new Long(this.id), this);        }    }    public Iterator propertyNames() {        return Collections.unmodifiableSet(properties.keySet()).iterator();    }    public void addAdministrator(User user) throws UnauthorizedException {        // If the user is already an administrator, do nothing.        if (isAdministrator(user)) {            return;        }        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            // Update the modified date.            updateModifiedDate(System.currentTimeMillis(), con);            // Add the administrator.            pstmt = con.prepareStatement(ADD_ADMIN);            pstmt.setLong(1, id);            pstmt.setLong(2, user.getID());            pstmt.execute();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        // Changed admins, so reset admin list        this.adminList = null;        factory.cacheManager.groupCache.put(new Long(id), this);        // Remove user from admin test cache        factory.cacheManager.groupMemberCache.remove(id + ",admin," + user.getID());        // Now, clear the permissions cache.        factory.cacheManager.userPermsCache.clear();    }    public void removeAdministrator(User user) throws UnauthorizedException {        // If the user is not an administrator, do nothing.        if (!isAdministrator(user)) {            return;        }        long userID = user.getID();        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            // Update the modified date.            updateModifiedDate(System.currentTimeMillis(), con);            // Remove the admin.            pstmt = con.prepareStatement(REMOVE_ADMIN);            pstmt.setLong(1, id);            pstmt.setLong(2, userID);            pstmt.execute();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        // Changed admins, so reset admin list        this.adminList = null;        factory.cacheManager.groupCache.put(new Long(id), this);        // Remove user from admin cache        factory.cacheManager.groupMemberCache.remove(id + ",admin," + userID);        // Now, clear the permissions cache.        factory.cacheManager.userPermsCache.clear();    }    public void addMember(User user) throws UnauthorizedException {        // Don't do anything if the user is already a member of the group.        if (isMember(user)) {            return;        }        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            // Update the modified date.            updateModifiedDate(System.currentTimeMillis(), con);            // Add the user.            pstmt = con.prepareStatement(ADD_USER);            pstmt.setLong(1, id);            pstmt.setLong(2, user.getID());            pstmt.execute();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        // Changed membership, so reset member list        this.memberList = null;        factory.cacheManager.groupCache.put(new Long(id), this);        // Remove user from member cache        factory.cacheManager.groupMemberCache.remove(id + ",member," + user.getID());        // Remove the user's entry for groups they belong in.        factory.cacheManager.groupMemberCache.remove("userGroups-" + user.getID());        // Now, clear the permissions cache.        factory.cacheManager.userPermsCache.clear();    }    public void removeMember(User user) throws UnauthorizedException {        // Don't do anything if the user isn't a member of the group.        if (!isMember(user)) {            return;        }        long userID = user.getID();        Connection con = null;        PreparedStatement pstmt = null;        try {            con = ConnectionManager.getConnection();            // Update the modified date.            updateModifiedDate(System.currentTimeMillis(), con);            // Remove the user.            pstmt = con.prepareStatement(REMOVE_USER);            pstmt.setLong(1, id);            pstmt.setLong(2, userID);            pstmt.execute();        }        catch( SQLException sqle ) {            sqle.printStackTrace();        }        finally {            try {  pstmt.close();   }            catch (Exception e) { e.printStackTrace(); }            try {  con.close();   }            catch (Exception e) { e.printStackTrace(); }        }        // Changed membership, so reset member list        this.memberList = null;        factory.cacheManager.groupCache.put(new Long(id), this);        // Remove user from member cache        factory.cacheManager.groupMemberCache.remove(id + ",member," + userID);        // Remove the user's entry for groups they belong in.        factory.cacheManager.groupMemberCache.remove("userGroups-" + user.getID());        // Now, clear the permissions cache.        factory.cacheManager.userPermsCache.clear();    }    public boolean isAdministrator(User user) {        long userID = user.getID();        Boolean bool = null;        bool = (Boolean)factory.cacheManager.groupMemberCache.get(                id + ",admin," + userID);        if (bool == null ) {            bool = new Boolean(false);            Connection con = null;            PreparedStatement pstmt = null;            try {                con = ConnectionManager.getConnection();                pstmt = con.prepareStatement(ADMIN_TEST);                pstmt.setLong(1, id);                pstmt.setLong(2, userID);                ResultSet rs = pstmt.executeQuery();                // If there is a result, then the user is an admin of the group.                if (rs.next()) {                    bool = new Boolean(true);                }            }

⌨️ 快捷键说明

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