📄 dbgroup.java
字号:
/**
* $RCSfile: DbGroup.java,v $
* $Revision: 1.1.1.1 $
* $Date: 2002/09/09 13:50:56 $
*
* New Jive from Jdon.com.
*
* 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.*;
/**
* 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
*/
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=?";
private static final String ADMIN_COUNT =
"SELECT count(*) FROM jiveGroupUser WHERE groupID=? " +
"AND administrator=1";
private static final String MEMBER_COUNT =
"SELECT DISTINCT count(userID) FROM jiveGroupUser " +
"WHERE groupID=?";
private static final String LOAD_ADMINS =
"SELECT userID FROM jiveGroupUser WHERE administrator=1 AND groupID=?";
private static final String LOAD_USERS =
"SELECT userID FROM jiveGroupUser WHERE 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 DELETE_PROPERTIES =
"DELETE FROM jiveGroupProp WHERE groupID=?";
private static final String INSERT_PROPERTY =
"INSERT INTO jiveGroupProp(userID,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 DbForumFactory factory;
/**
* Cache to make the isMember(User) test fast.
*/
private LongCache memberCache = new LongCache(4*1024, 6*JiveGlobals.HOUR);
/**
* Creates a new group.
*
* @param the name of the group.
* @param factory a ForumFactory that can be used to get user and group
* managers.
*/
protected DbGroup(String name, DbForumFactory factory) {
this.name = name;
this.factory = factory;
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();
}
/**
* Loads a group from the database based on its id.
*
* @param id the id of the group to load.
* @param factory a ForumFactory that can be used to get user and group
* managers.
*/
protected DbGroup(long id, DbForumFactory factory)
throws GroupNotFoundException
{
this.id = id;
this.factory = factory;
loadFromDb();
}
/**
* 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.
* @param factory a ForumFactory that can be used to get user and group
* managers.
*/
protected DbGroup(String name, Object fake, DbForumFactory factory)
throws GroupNotFoundException
{
this.name = name;
this.factory = factory;
loadFromDb();
}
// 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) {
properties.put(name, value);
savePropertiesToDb();
}
public void deleteProperty(String name) {
properties.remove(name);
deletePropertyFromDb(name);
}
public Iterator propertyNames() {
return Collections.unmodifiableSet(properties.keySet()).iterator();
}
public void addAdministrator(User user) throws UnauthorizedException {
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(); }
}
// Now, remove the user from the user perm cache since being in the
// group could affect their permissions.
factory.cacheManager.userPermsCache.removeUserPerm(user.getID());
}
public void removeAdministrator(User user) throws UnauthorizedException {
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(); }
}
// Remove user from member cache
memberCache.remove(userID);
// Now, remove the user from the USER_PERM_CACHE since being in the
// group could affect their permissions.
factory.cacheManager.userPermsCache.removeUserPerm(userID);
}
public void addMember(User user) throws UnauthorizedException {
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(); }
}
memberCache.add(user.getID(), new CacheableBoolean(true));
// Now, remove the user from the USER_PERM_CACHE since being in the
// group could affect their permissions.
factory.cacheManager.userPermsCache.removeUserPerm(user.getID());
}
public void removeMember(User user) throws UnauthorizedException {
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(); }
}
// Remove user from member cache
memberCache.remove(userID);
// Now, remove the user from the USER_PERM_CACHE since being in the
// group could affect their permissions.
factory.cacheManager.userPermsCache.removeUserPerm(userID);
}
public boolean isAdministrator(User user) {
boolean answer = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(ADMIN_TEST);
pstmt.setLong(1, id);
pstmt.setLong(2, user.getID());
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
answer = true;
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return answer;
}
public boolean isMember(User user) {
CacheableBoolean bool = null;
bool = (CacheableBoolean)memberCache.get(user.getID());
if (bool == null ) {
bool = new CacheableBoolean(false);
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(MEMBER_TEST);
pstmt.setLong(1, id);
pstmt.setLong(2, user.getID());
ResultSet rs = pstmt.executeQuery();
// If there is a result, then the user is a member of the group.
if (rs.next()) {
bool = new CacheableBoolean(true);
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -