📄 dbprofilemanager.java~4~
字号:
/*
* DbProfileManager.java
*
* Created on 2001年7月4日, 上午8:54
*/
package com.gs.db.dbimp;
import com.gs.db.*;
import com.gs.util.*;
import java.util.*;
import java.sql.*;
/**
* Manages Users and Groups & Organization Structure Units --- ()
*/
public class DbProfileManager
implements ProfileManager {
/** DATABASE QUERIES **/
private static final String USER_GROUPS =
"SELECT groupid from gsGroupUser WHERE userid=?";
private static final String USER_COUNT =
"SELECT count(*) FROM gsUser where status<>-1";
private static final String DELETE_USER_PERMS =
"DELETE FROM gsUserMenu WHERE userid=?";
private static final String DELETE_USER_GROUPS =
"DELETE FROM gsGroupUser WHERE userid=?";
private static final String DELETE_USER =
"DELETE FROM gsUser WHERE userid=?";
private static final String GROUP_COUNT =
"SELECT count(*) FROM gsGroup";
private static final String DELETE_GROUP_USERS =
"DELETE FROM gsGroupUser WHERE groupid=?";
private static final String DELETE_GROUP =
"DELETE FROM gsGroup WHERE groupid=?";
private static final String INSERT_SUB_UNIT =
"INSERT INTO u_djjg(djjg, djjgmc, lxdh, lxr,sjjg,inside) VALUES(?,?,?,?,?,?)";
private static final String DELETE_UNIT =
"DELETE FROM u_djjg WHERE djjg=?";
private static final String DELETE_GROUP_ALLPERM =
"DELETE FROM gsGroupMenu WHERE groupid=?";
private static final String GET_UNIT_FREE_GROUPS =
"SELECT groupid FROM gsGroup WHERE djjg='-1'";
private static final String GET_GROUPS_USERID =
"SELECT distinct groupID FROM gsGroupUser WHERE userID = ?";
//////////////////////////////////////////////////////////////////////////////////
private static final String FIND_USER =
"SELECT myname FROM gsUser WHERE userid=?";
private static final String SELECT_ALL_UNITS =
"select djjg from u_djjg";
private static final String GET_TOPLEVEL_UNIT_AND_PRI_BY_USER =
"select min(gp.priority) as pri,gp.djjg from gsGroup as gp,gsGroupUser as gUser"
+" where gp.groupID = gUser.groupID and gUser.userID=? and gp.djjg!='-1'"
+" group by gp.djjg order by pri";
private static final String GET_OUTSIDE_UNITS =
"select djjg,djjgmc from u_djjg"
+" where inside='0' and djjg!='0'";
//////////////////////////////////////////////////////////////////////////////////////
/*李军增加的函数*/
private static final String USER_WDJLB=
"SELECT djbh,tsrq,tslx,lrr,tsrmc,btsdw FROM U_DJNRB WHERE cldw=? and clbz='0' and tslx IN('1','2','3')";
/*李军增加的函数结束*/
private User anonymousUser = null;
private User specialUser = null;
private DbIofficeFactory factory;
/**
* Creates a new ProfileManager.
*/
public DbProfileManager(DbIofficeFactory factory) {
this.factory = factory;
try {
anonymousUser = getUser( -1);
specialUser = getUser(0);
}
catch (UserNotFoundException unfe) {}
}
public synchronized User createUser(String username, String password,
String email) throws
UserAlreadyExistsException {
User newUser = null;
try {
User existingUser = getUser(username);
//The user already exists since now exception, so:
if (!existingUser.isDelete()) {
throw new UserAlreadyExistsException();
}
else {
newUser = new DbUser(username, password, email);
}
}
catch (UserNotFoundException unfe) {
//The user doesn't already exist so we can create a new user
newUser = new DbUser(username, password, email);
}
return newUser;
}
//sj: jive oringinal copy, donnot need to modify
public User getUser(int userID) throws UserNotFoundException {
DbCacheManager cacheManager = factory.getCacheManager();
//If cache is not enabled, do a new lookup of object
if (!cacheManager.isCacheEnabled()) {
return new DbUser(userID);
}
//Cache is enabled.
Integer userIDInteger = new Integer(userID);
DbUser user = (DbUser) cacheManager.get(
DbCacheManager.USER_CACHE,
userIDInteger
);
if (user == null) {
user = new DbUser(userID);
cacheManager.add(DbCacheManager.USER_CACHE, userIDInteger, user);
}
return user;
}
//sj: jive oringinal copy, donnot need to modify
public User getUser(String username) throws UserNotFoundException {
DbCacheManager cacheManager = factory.getCacheManager();
//If cache is not enabled, do a new lookup of object
if (!cacheManager.isCacheEnabled()) {
User user = new DbUser(username);
return getUser(user.getID());
}
//Cache is enabled.
CacheableInteger userIDInteger = (CacheableInteger) cacheManager.get(
DbCacheManager.USER_ID_CACHE,
username
);
//if id wan't found in cache, load it up and put it there.
if (userIDInteger == null) {
User user = new DbUser(username);
userIDInteger = new CacheableInteger(new Integer(user.getID()));
cacheManager.add(DbCacheManager.USER_ID_CACHE, username, userIDInteger);
}
return getUser(userIDInteger.getInteger().intValue());
}
//To Everyone's Attention:
//PrepareUnlinkUserOwnedData() is called by DeleteUser.
//Some data need to be gone with the User.
// Here, you must not delete the User-owned data immediately
// because the User after is not deleted yet. What you should
// do is to keep temporary pointers (such as all IDs of User's Data)
// in the class's data members, which will be used in UnlinkUserOwnedData()
// to delete the data from DB. After that all user definition will be
// delete form DB.
// And then, we have to do with Cached User-owned data object if necessary.
// The task should completed in "RemoveCachedUserOwnedData()".
// Now I cannot decide what kinds of data object to delete when the IOFFICE
// system grows, so I give the functions mentioned above as a framework
// for developers to fill in. Let me repeat in summary:
//
// PrepareUnlinkUserOwnedData(User user)
//then, UnlinkUserOwnedData(User user)
// ..... some system tasks in DeleteUser(User user)
//time to cope with cache,
// RemoveCachedUserOwnedData(User user);
//
// ---- SUNJIAN July 4, 2001
private void PrepareUnlinkUserOwnedData(int userid) {
}
private void unlinkUserOwnedData(int userid) {
int i;
DbComponentManager cpMgr = (DbComponentManager) factory.getComponentManager();
Iterator comps = cpMgr.getComponents();
DbIofficeComponent cmp;
while (comps.hasNext()) {
cmp = (DbIofficeComponent) comps.next();
IofficeComponentAgent agent = (IofficeComponentAgent) cmp.
getAgentInstance();
try {
agent.onDeletingUser(userid);
}
catch (Exception e) {
}
}
}
protected void afterDeleteUser(int userid) {
int i;
DbComponentManager cpMgr = (DbComponentManager) factory.getComponentManager();
Iterator comps = cpMgr.getComponents();
DbIofficeComponent cmp;
while (comps.hasNext()) {
cmp = (DbIofficeComponent) comps.next();
IofficeComponentAgent agent = (IofficeComponentAgent) cmp.
getAgentInstance();
try {
agent.onUserDeleted(userid);
}
catch (Exception e) {
}
}
}
public synchronized void deleteUser(User user) throws UnauthorizedException {
int userID = user.getID();
Connection con = null;
PreparedStatement pstmt = null;
//check whether the user has already been deleted by another thread
try {
con = DbConnectionManager.getConnection();
//remove all permissions given to user
pstmt = con.prepareStatement(FIND_USER);
pstmt.setInt(1, userID);
ResultSet rs = pstmt.executeQuery();
if (!rs.next()) {
//already not exists in the user table, just return
try {
rs.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
return;
}
}
catch (SQLException sqle) {
sqle.printStackTrace();
return;
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
if (user.getUsername().equals("root"))throw new UnauthorizedException(
"You cannot delete default adminstrator");
//sj: now we donnot need to care about User's Data & the cache.
// after all , we have to reserve a insertion point to perform such task.
// so I offer a protected function "PrepareUnlinkUserOwnedData()"
//PrepareUnlinkUserOwnedData(userid);
// unlinkUserOwnedData(userID);
try {
con = DbConnectionManager.getConnection();
//remove all permissions given to user
pstmt = con.prepareStatement(DELETE_USER_PERMS);
pstmt.setInt(1, userID);
pstmt.execute();
pstmt.close();
//remove user from all groups
pstmt = con.prepareStatement(DELETE_USER_GROUPS);
pstmt.setInt(1, userID);
pstmt.execute();
pstmt.close();
//delete the actual user entry
pstmt = con.prepareStatement(DELETE_USER);
pstmt.setInt(1, 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();
}
}
afterDeleteUser(userID);
//user cache
DbCacheManager cacheManager = factory.getCacheManager();
cacheManager.remove(DbCacheManager.USER_ID_CACHE, user.getUsername());
cacheManager.remove(DbCacheManager.USER_CACHE, new Integer(userID));
}
//sj: jive oringinal copy, donnot need to modify
public User getAnonymousUser() {
return anonymousUser;
}
//sj: jive oringinal copy, donnot need to modify
public User getSpecialUser() {
return specialUser;
}
public int getUserCount() {
int count = 0;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(USER_COUNT);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
count = rs.getInt(1);
}
}
catch (SQLException sqle) {
sqle.printStackTrace();
}
finally {
try {
pstmt.close();
}
catch (Exception e) {
e.printStackTrace();
}
try {
con.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
return count;
}
public Iterator users() {
return new DbUserIterator(this);
}
public Iterator users(int startIndex, int numResults) {
return new DbUserIterator(this, startIndex, numResults);
}
//**************************************************
//group management functions
public synchronized Group createGroup(String name) throws
UnauthorizedException,
GroupAlreadyExistsException {
Group newGroup = null;
try {
Group existingGroup = getGroup(name);
//The group already exists since now exception, so:
throw new GroupAlreadyExistsException();
}
catch (GroupNotFoundException unfe) {
//The group doesn't already exist so we can create a new group
newGroup = new DbGroup(name, factory);
}
return newGroup;
}
public Group createGroup(String name, Unit unit, int priority) throws
UnauthorizedException,
// public Group createGroup(String name, int unitid, int priority) throws UnauthorizedException,
GroupAlreadyExistsException {
Group newGroup = null;
try {
Group existingGroup = getGroup(name);
//The group already exists since now exception, so:
throw new GroupAlreadyExistsException();
}
catch (GroupNotFoundException unfe) {
//The group doesn't already exist so we can create a new group
newGroup = new DbGroup(name, factory, unit, priority);
// newGroup = new DbGroup(name, factory,unitid, priority);
}
return newGroup;
}
public Group getGroup(int groupID) throws GroupNotFoundException {
DbCacheManager cacheManager = factory.getCacheManager();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -