📄 dbprofilemanager.java
字号:
} //Finally, expire all relevant caches //all of users's messages for (int i=0; i<messages.length; i++) { factory.messageCache.remove(messages[i]); } //user cache userIDMap.remove(user.getUsername()); userCache.remove(userID); } /** * Creates a Group. * * @param name the new and unique name for the group. * * @throws UnauthorizedException if does not have ADMIN permissions. */ public Group createGroup(String name) throws UnauthorizedException, GroupAlreadyExistsException { Group newGroup = null; try { Group existingGroup = new DbGroup(name, factory, null); //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; } /** * Gets a Group by ID. * * throws GroupNotFoundException if the group does not exist. */ public Group getGroup(int groupID) throws GroupNotFoundException { Group group; if(!groupCache.containsKey(groupID)) { group = new DbGroup(groupID, factory); groupCache.add(groupID, group); } else { group = (Group)groupCache.get(groupID); } return group; } /** * Gets a Group by name. * * throws GroupNotFoundException if the group does not exist. */ public Group getGroup(String name) throws GroupNotFoundException { Group group; if (!groupIDMap.containsKey(name)) { group = new DbGroup(name, null, factory); Integer id = new Integer(group.getID()); groupIDMap.put(name, id); } else { int id = ((Integer)groupIDMap.get(name)).intValue(); group = getGroup(id); } return group; } /** * Deletes a Group. * * @throws UnauthorizedException */ public void deleteGroup(Group group) throws UnauthorizedException { int groupID = group.getID(); int [] users = new int[group.getUserCount()]; Iterator iter = group.users(); for (int i=0; i<users.length;i++) { User user = (User)iter.next(); users[i] = user.getID(); } Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); //mark all message by user as anonymous pstmt = con.prepareStatement(DELETE_GROUP_USERS); pstmt.setInt(1,groupID); pstmt.execute(); pstmt.close(); //remove all permissions given to user pstmt = con.prepareStatement(DELETE_GROUP); pstmt.setInt(1,groupID); pstmt.execute(); pstmt.close(); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } //Finally, expire all relevant caches groupIDMap.remove(group.getName()); groupCache.remove(groupID); //Removing a group can change the permissions of all the users in the //group. Therefore, remove the users from the permissions cache. for (int i=0; i<users.length; i++) { factory.userPermissionsCache.remove(users[i]); } } /** * Returns the numer of users in the system. */ 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; } /** * Returns the number of groups in the system. */ public int getGroupCount() { int count = 0; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(GROUP_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; } /** * Retruns an iterator for all users. */ public Iterator users() { return new DbUserIterator(this); } /** * Retruns an iterator for all users. */ public Iterator users(int startIndex, int numResults) { return new DbUserIterator(this, startIndex, numResults); } /** * Returns an iterator for all groups. */ public Iterator groups() { return new DbGroupIterator(this); } /** * Returns an iterator for all groups. */ public Iterator groups(int startIndex, int numResults) { return new DbGroupIterator(this, startIndex, numResults); } /** * Returns the number of messages a user has posted in a particular forum. */ public int userMessageCount(User user, Forum forum) { int count = 0; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(USER_MESSAGE_COUNT); pstmt.setInt(1, user.getID()); pstmt.setInt(2, forum.getID()); 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; } /** * Returns an iterator for all the messages that a user posted in a * particular forum. */ public Iterator userMessages(User user, Forum forum) { return new DbUserMessagesIterator(factory, user, forum); } /** * Returns an array of all the groups that the user belongs to. */ protected int[] getUserGroups(int userID) { Connection con = null; PreparedStatement pstmt = null; int [] groups = new int[0]; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(USER_GROUPS); pstmt.setInt(1,userID); ResultSet rs = pstmt.executeQuery(); ArrayList groupList = new ArrayList(); while (rs.next()) { groupList.add(new Integer(rs.getInt("groupID"))); } groups = new int[groupList.size()]; for (int i=0; i<groups.length; i++) { groups[i] = ((Integer)groupList.get(i)).intValue(); } } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } return groups; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -