📄 dbgroup.java
字号:
catch (Exception e) { e.printStackTrace(); }
}
// Add to cache
memberCache.add(user.getID(), bool);
}
return bool.getBoolean();
}
public int getAdministratorCount() {
int count = 0;
boolean answer = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(ADMIN_COUNT);
pstmt.setLong(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;
}
public int getMemberCount() {
int count = 0;
boolean answer = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(MEMBER_COUNT);
pstmt.setLong(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;
}
public Iterator members() {
Connection con = null;
PreparedStatement pstmt = null;
long [] users = new long[getMemberCount()];
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_USERS);
pstmt.setLong(1, id);
ResultSet rs = pstmt.executeQuery();
for(int i=0; i<users.length; i++) {
rs.next();
users[i] = 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.USER, users,
factory.getUserManager());
}
public Iterator administrators() {
Connection con = null;
PreparedStatement pstmt = null;
long [] admins = new long[getAdministratorCount()];
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_ADMINS);
pstmt.setLong(1, id);
ResultSet rs = pstmt.executeQuery();
for(int i=0; i<admins.length; i++) {
rs.next();
admins[i] = 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.USER, admins,
factory.getUserManager());
}
public ForumPermissions getPermissions(Authorization authorization) {
long userID = authorization.getUserID();
try {
User user = factory.getUserManager().getUser(userID);
if (isAdministrator(user)) {
return new ForumPermissions(false, false, false, false,
true, false, false, false, false);
}
}
catch (Exception e) { }
return ForumPermissions.none();
}
public boolean hasPermission(int type) {
return true;
}
//FROM THE CACHEABLE INTERFACE//
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.sizeOfLong(); // id
size += CacheSizes.sizeOfString(name); // name
size += CacheSizes.sizeOfString(description); // description
size += CacheSizes.sizeOfMap(properties); // properties
size += CacheSizes.sizeOfObject(); // forum factory ref.
size += memberCache.getMaxSize(); // member cache
return size;
}
//OTHER METHODS
/**
* Returns a String representation of the Group object using the group name.
*
* @return a String representation of the Group object.
*/
public String toString() {
return name;
}
public int hashCode() {
return (int)id;
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object != null && object instanceof DbGroup) {
return id == ((DbGroup)object).getID();
}
else {
return false;
}
}
/**
* Updates the modified date. It accepts a Connection so that it can
* participate in trasactions.
*/
protected void updateModifiedDate(long date, Connection con)
throws SQLException
{
this.modifiedDate.setTime(date);
PreparedStatement pstmt = null;
try {
pstmt = con.prepareStatement(UPDATE_MODIFIED_DATE);
pstmt.setString(1, StringUtils.dateToMillis(modifiedDate));
pstmt.setLong(2, id);
pstmt.executeUpdate();
}
finally {
try { pstmt.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 = ConnectionManager.getConnection();
pstmt = con.prepareStatement(query);
if (name == null) {
pstmt.setLong(1, id);
}
else {
pstmt.setString(1, name);
}
ResultSet rs = pstmt.executeQuery();
if (!rs.next()) {
throw new GroupNotFoundException();
}
this.id = rs.getLong("groupID");
this.name = rs.getString("name");
this.description = rs.getString("description");
// We trim() the dates before trying to parse them because some
// databases pad with extra characters when returning the data.
this.creationDate =
new java.util.Date(Long.parseLong(rs.getString("creationDate").trim()));
this.modifiedDate =
new java.util.Date(Long.parseLong(rs.getString("modifiedDate").trim()));
pstmt.close();
// Load any extended message properties.
properties = new Hashtable();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setLong(1, id);
rs = pstmt.executeQuery();
while(rs.next()) {
// Add in name, value as a new property.
properties.put(rs.getString(1), rs.getString(2));
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
throw new GroupNotFoundException();
}
catch (NumberFormatException nfe) {
System.err.println("WARNING: There was an error parsing the dates " +
"returned from the database. Ensure that they're being stored " +
"correctly.");
throw new GroupNotFoundException( "Group with id "
+ id + " could not be loaded from the database."
);
}
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() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_GROUP);
pstmt.setString(1, name);
pstmt.setString(2, description);
pstmt.setLong(3, id);
pstmt.setString(4, StringUtils.dateToMillis(creationDate));
pstmt.setString(5, StringUtils.dateToMillis(modifiedDate));
pstmt.executeUpdate();
}
catch( SQLException 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 = ConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_GROUP);
pstmt.setString(1, name);
pstmt.setString(2, description);
pstmt.setString(3, StringUtils.dateToMillis(creationDate));
pstmt.setString(4, StringUtils.dateToMillis(modifiedDate));
pstmt.setLong(5, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Saves properties to the database.
*/
private synchronized void savePropertiesToDb() {
boolean abortTransaction = false;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getTransactionConnection();
// Delete all old values.
pstmt = con.prepareStatement(DELETE_PROPERTIES);
pstmt.setLong(1, id);
pstmt.execute();
pstmt.close();
// Now insert new values.
pstmt = con.prepareStatement(INSERT_PROPERTY);
Iterator iter = properties.keySet().iterator();
while (iter.hasNext()) {
String name = (String)iter.next();
String value = (String)properties.get(name);
pstmt.setLong(1, id);
pstmt.setString(2, name);
pstmt.setString(3, value);
pstmt.executeUpdate();
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
abortTransaction = true;
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
ConnectionManager.closeTransactionConnection(con, abortTransaction);
}
}
/**
* Deletes a property from the db.
*/
private synchronized void deletePropertyFromDb(String name) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_PROPERTY);
pstmt.setLong(1, id);
pstmt.setString(2, name);
pstmt.execute();
}
catch( SQLException 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 + -