📄 dbgroup.java
字号:
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; } /** * Returns true if if the User is a member of the group. */ public boolean isMember(User user) { boolean answer = false; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(MEMBER_TEST); pstmt.setInt(1, id); pstmt.setInt(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; } /** * Returns the number of group administrators. */ public int getAdministratorCount() { int count = 0; boolean answer = false; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(ADMIN_COUNT); pstmt.setInt(1, id); 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 group members. */ public int getUserCount() { int count = 0; boolean answer = false; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(MEMBER_COUNT); pstmt.setInt(1, id); 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; } /** * An iterator for all the users that are members of the group. */ public Iterator users() { ArrayList admins = new ArrayList(); //Load list of group admins from db.
Connection con = null;
PreparedStatement pstmt = null;
ProfileManager manager = factory.getProfileManager();
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_USERS);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
User user = null;
while (rs.next()) {
try {
user = manager.getUser(rs.getInt("userID"));
}
catch (UserNotFoundException unfe) {
unfe.printStackTrace(System.out);
}
admins.add(user);
}
}
catch( SQLException sqle ) {
System.err.println( "SQLException in DbGroup.java:" + "users():reading group data " + sqle ); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } }
return admins.iterator(); } /** * An iterator for all the users that are administrators of the group. */ public Iterator administrators() { ArrayList admins = new ArrayList(); //Load list of group admins from db.
Connection con = null;
PreparedStatement pstmt = null;
ProfileManager manager = factory.getProfileManager();
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_ADMINS);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
User user = null;
while (rs.next()) {
try {
user = manager.getUser(rs.getInt("userID"));
}
catch (UserNotFoundException unfe) {
unfe.printStackTrace(System.out);
}
admins.add(user);
}
}
catch( SQLException sqle ) {
System.err.println( "SQLException in DbGroup.java:" + "administrators():reading group data " + sqle ); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } }
return admins.iterator();
} /** * Returns the permissions for the group that correspond to the * passed-in Authorization. * * @param authorization the auth token to lookup permissions for. */ public ForumPermissions getPermissions(Authorization authorization) { int userID = authorization.getUserID(); try { User user = factory.getProfileManager().getUser(userID); if (isAdministrator(user)) { return new ForumPermissions(false, false, false, false, true, false, false, false); } } catch (Exception e) { } return ForumPermissions.none(); } /** * Returns true if the handle on the object has the permission specified. * A list of possible permissions can be found in the ForumPermissions * class. Certain methods of this class are restricted to certain * permissions as specified in the method comments. * * @see ForumPermissions */ public boolean hasPermission(int type) { return true; } /** * Converts the object to a String by returning the name of the group. * This functionality is primarily for Java applications that might be * accessing CoolForum objects through a GUI. */ public String toString() { return name; } private synchronized void loadFromDb() throws GroupNotFoundException { String query; if (name == null) { query = LOAD_GROUP_BY_ID; } else { query = LOAD_GROUP_BY_NAME; } Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(query); if (name == null) { pstmt.setInt(1, id); } else { pstmt.setString(1, name); } ResultSet rs = pstmt.executeQuery(); if (!rs.next()) { throw new GroupNotFoundException(); } this.id = rs.getInt("groupID"); this.name = rs.getString("name"); this.description = rs.getString("description"); } catch( SQLException sqle ) { System.err.println( "SQLException in DbGroup.java:" + "loadFromDb():reading group data " + sqle ); throw new GroupNotFoundException(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Inserts a new record into the database. */ private void insertIntoDb() { StringBuffer insert = new StringBuffer(); Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(INSERT_GROUP); pstmt.setString(1, name); pstmt.setString(2, description); pstmt.setInt(3, id); pstmt.executeUpdate(); } catch( SQLException sqle ) { System.err.println("Error in DbGroup:insertIntoDb()-" + sqle); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * Saves group data to the db. */ private synchronized void saveToDb() { Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(SAVE_GROUP); pstmt.setString(1, name); pstmt.setString(2, description); pstmt.setInt(3, id); pstmt.executeUpdate(); } catch( SQLException sqle ) { System.err.println( "SQLException in DbGroup.java:saveToDb(): " + sqle ); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -