📄 dbgroup.java
字号:
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();
}
/**
* Returns the permissions for the group that correspond to the
* passed-in Authorization.
*
* @param authorization the auth token to lookup permissions for.
*/
public IofficePermissions getPermissions(Authorization authorization)
{
int userID = authorization.getUserID();
try {
User user = profileManager.getUser(userID);
if (isAdministrator(user)) {
return IofficePermissions.groupAdminDefault();
}
}
catch (Exception e) { }
return IofficePermissions.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.
*
* @param type a permission type.
* @return true if the specified permission is valid.
* @see IofficePermissions
*/
public boolean hasPermission(int type) {
return true;
}
/**
* Returns true if the group is associated with a unit.
*
* @return true if the group is associated with a unit..
* @see Unit
*/
public boolean hasAssociateUnit() {
return ( djjg.trim().equals("-1") );
}
/**
* Returns the unit object that group is associated with
*
* @return the Unit object that group is associated with
* @see Unit
*/
public Unit getAssociateUnit() // return null if not linked with Unit
{
if ( Integer.parseInt(djjg.trim()) >=0 )
{
try {
//return profileManager.getUnit( unitid );
return profileManager.getUnit( djjg );
}
catch( UnitNotFoundException e ){
return null;
}
}
return null;
}
/**
* Returns the priority level of the group in terms of being
* associated with a unit. Now this function is the synonym of
* <code >getPriority() </code>
* @return priority level ( 0 max, 1,2, .. lower )
*/
public int getPriorityInUnit() //return -1 if not linked with Unit
{
return priority;
}
/**
* Grants administrator privileges of the group to a user.<p>
*
* This method is restricted to those with group administration permission.
*
* @param user the User to grant adminstrative privileges to.
* @throws UnauthorizedException if does not have group admin permissions.
*/
public void addAdministrator(User user) throws UnauthorizedException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ADD_ADMIN);
pstmt.setInt(1, id);
pstmt.setInt(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.
DbCacheManager cacheManager = factory.getCacheManager();
cacheManager.removeUserPerm(new Integer(user.getID()));
}
/**
* Revokes administrator privileges of the group to a user.<p>
*
* This method is restricted to those with group administration permission.
*
* @param user the User to grant adminstrative privileges to.
* @throws UnauthorizedException if does not have group admin permissions.
*/
public void removeAdministrator(User user) throws UnauthorizedException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(REMOVE_ADMIN);
pstmt.setInt(1, id);
pstmt.setInt(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.
DbCacheManager cacheManager = factory.getCacheManager();
cacheManager.removeUserPerm(new Integer(user.getID()));
}
/**
* Returns true if the User has group administrator permissions. Group
* administrators are also considered to be members.
*
* @return true if the User is an administrator of the group.
*/
public boolean isAdministrator(User user) {
boolean answer = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ADMIN_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.
*
* @return 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;
}
/**
* An iterator for all the users that are administrators of the group.
*
* @return an Iterator for all administrators of the group.
*/
public Iterator administrators() {
ArrayList admins = new ArrayList();
//Load list of group admins from db.
Connection con = null;
PreparedStatement pstmt = null;
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 = profileManager.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();
}
//private functions
/**
* 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.setString(3, djjg);
pstmt.setInt(4, priority);
pstmt.setInt(5, 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(); }
}
}
/**
* Load the group data from the database.
*/
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("myname");
this.description = rs.getString("description");
this.djjg = rs.getString("djjg");
if(djjg!=null)
djjg = djjg.trim();
this.priority = rs.getInt("priority");
}
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.setString(4, djjg);
pstmt.setInt(5, priority);
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(); }
}
}
/**
* Returns the approximate size of the Object in bytes. The size should be
* considered to be a best estimate of how much memory the Object occupies
* and may be based on empirical trials or dynamic calculations.<p>
*
* @return the size of the Object in bytes.
*/
public int getSize() {
//Approximate the size of the object in bytes by calculating the size
//of each field.
int size = 0;
size += CacheSizes.sizeOfObject(); //overhead of object
size += CacheSizes.sizeOfInt(); //id
size += CacheSizes.sizeOfString(name); //name
size += CacheSizes.sizeOfString(description); //description
size += CacheSizes.sizeOfObject(); //profile manager ref.
size += CacheSizes.sizeOfObject(); //forum factory ref.
return size;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -