dbgroupmanager.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 256 行
JAVA
256 行
/** * $RCSfile: DbGroupManager.java,v $ * $Revision: 1.6 $ * $Date: 2002/06/28 19:49:09 $ * * 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 java.util.Iterator;import java.sql.*;import com.jivesoftware.forum.*;import com.jivesoftware.util.*;/** * Database implementation of the GroupManager interface. */public class DbGroupManager implements GroupManager { /** DATABASE QUERIES **/ private static final String USER_GROUPS = "SELECT groupID FROM jiveGroupUser WHERE userID=? AND administrator=0"; private static final String GROUP_COUNT = "SELECT count(1) FROM jiveGroup"; private static final String DELETE_GROUP_USERS = "DELETE FROM jiveGroupUser WHERE groupID=?"; private static final String DELETE_GROUP = "DELETE FROM jiveGroup WHERE groupID=?"; private static final String ALL_GROUPS = "SELECT groupID FROM jiveGroup"; private DbForumFactory factory; /** * Creates a new GroupManager. */ public DbGroupManager() { this.factory = DbForumFactory.getInstance(); } //FROM THE GROUPMANAGER INTERFACE// public Group createGroup(String name) throws UnauthorizedException, GroupAlreadyExistsException { Group newGroup = null; try { 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); } return newGroup; } public Group getGroup(long groupID) throws GroupNotFoundException { if (!factory.cacheManager.isCacheEnabled()) { return new DbGroup(groupID); } // Cache is enabled. DbGroup group = (DbGroup)factory.cacheManager.groupCache.get(new Long(groupID)); if (group == null) { group = new DbGroup(groupID); factory.cacheManager.groupCache.put(new Long(groupID), group); } return group; } public Group getGroup(String name) throws GroupNotFoundException { if (!factory.cacheManager.isCacheEnabled()) { return new DbGroup(name, null); } // Cache is enabled. Long groupIDLong = (Long)factory.cacheManager.groupIDCache.get(name); // If ID wan't found in cache, load it up and put it there. if (groupIDLong == null) { Group group = new DbGroup(name, null); groupIDLong = new Long(group.getID()); factory.cacheManager.groupIDCache.put(name, groupIDLong); } return getGroup(groupIDLong.longValue()); } public void deleteGroup(Group group) throws UnauthorizedException { long groupID = group.getID(); long [] members = new long[group.getMemberCount()]; Iterator iter = group.members(); for (int i=0; i<members.length; i++) { User user = (User)iter.next(); members[i] = user.getID(); } Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); // Remove all users in the group. pstmt = con.prepareStatement(DELETE_GROUP_USERS); pstmt.setLong(1,groupID); pstmt.execute(); pstmt.close(); // Remove the group entry. pstmt = con.prepareStatement(DELETE_GROUP); pstmt.setLong(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 DatabaseCacheManager cacheManager = factory.getCacheManager(); cacheManager.groupCache.remove(new Long(groupID)); // Remove entries in the group membership cache for all members of the // group. for (int i=0; i<members.length; i++) { factory.cacheManager.groupMemberCache.remove("userGroups-" + members[i]); } // Removing a group can change the permissions of all the users in that // group. Therefore, expire permissions cache. cacheManager.userPermsCache.clear(); } public int getGroupCount() { int count = 0; Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.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; } public Iterator groups() { LongList groups = new LongList(); Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(ALL_GROUPS); ResultSet rs = pstmt.executeQuery(); while (rs.next()) { groups.add(rs.getLong(1)); } } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } return new DatabaseObjectIterator(JiveGlobals.GROUP, groups.toArray(), this); } public Iterator groups(int startIndex, int numResults) { LongList groups = new LongList(); Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(ALL_GROUPS); ResultSet rs = pstmt.executeQuery(); // Move to start of index for (int i=0; i<startIndex; i++) { rs.next(); } // Now read in desired number of results for (int i=0; i<numResults; i++) { if (rs.next()) { groups.add(rs.getLong(1)); } else { break; } } } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } return new DatabaseObjectIterator(JiveGlobals.GROUP, groups.toArray(), this); } public Iterator userGroups(User user) { String key = "userGroups-" + user.getID(); // Look in the group membership cache for the value. long [] groups = (long [])factory.cacheManager.groupMemberCache.get(key); if (groups == null) { long userID = user.getID(); Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getConnection(); pstmt = con.prepareStatement(USER_GROUPS); pstmt.setLong(1,userID); ResultSet rs = pstmt.executeQuery(); LongList groupList = new LongList(); while (rs.next()) { groupList.add(rs.getLong(1)); } groups = groupList.toArray(); // Add to cache. factory.cacheManager.groupMemberCache.put(key, groups); } catch( SQLException sqle ) { sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } return new DatabaseObjectIterator(JiveGlobals.GROUP, groups, this); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?