📄 dbuser.java
字号:
public String getProperty(String name) {
return StringUtils.escapeHTMLTags((String)properties.get(name));
}
public Enumeration propertyNames() {
return properties.propertyNames();
}
public void setProperty(String name, String value) {
properties.put(name, value);
saveProperties();
}
public ForumPermissions getPermissions(Authorization authorization) {
if (authorization.getUserID() == id || id==-1 || id==0) {
return new ForumPermissions(false,false,false,true,false,false,false,false);
}
else {
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.sizeOfInt(); //id
size += CacheSizes.sizeOfString(username); //username
size += CacheSizes.sizeOfString(passwordHash); //password
size += CacheSizes.sizeOfString(name); //name
size += CacheSizes.sizeOfString(email); //email
size += CacheSizes.sizeOfBoolean(); //nameVisible
size += CacheSizes.sizeOfBoolean(); //emailVisible
size += CacheSizes.sizeOfObject(); //property lock
size += CacheSizes.sizeOfProperties(properties);//properties object
return size;
}
//OTHER METHODS
/**
* Returns a String representation of the User object using the username.
*
* @return a String representation of the User object.
*/
public String toString() {
return username;
}
public int hashCode() {
return id;
}
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object != null && object instanceof DbUser) {
return id == ((DbUser)object).getID();
}
else {
return false;
}
}
/**
* Loads user properties from the database.
*/
private void loadProperties() {
//If "anonymous" or "all users", do nothing.
if (id == -1 || id == 0) {
properties = new Properties();
return;
}
//Acquire a lock so that no other property loading or saving can be
//performed at the same time.
synchronized(propertyLock) {
Properties newProps = new Properties();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_PROPERTIES);
pstmt.setInt(1, id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
String name = rs.getString("name");
String value = rs.getString("propValue");
newProps.put(name, value);
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbUser:loadProperties():" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
this.properties = newProps;
}
}
/**
* Saves user properties to the database.
*/
private void saveProperties() {
//If "anonymous" or "all users", do nothing.
if (id == -1 || id == 0) {
return;
}
//Acquire a lock so that no other property loading or saving can be
//performed at the same time.
synchronized(propertyLock) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
//Delete all old values.
pstmt = con.prepareStatement(DELETE_PROPERTIES);
pstmt.setInt(1, id);
pstmt.execute();
pstmt.close();
//Now insert new values.
pstmt = con.prepareStatement(INSERT_PROPERTY);
Enumeration enum = properties.keys();
while (enum.hasMoreElements()) {
String name = (String)enum.nextElement();
String value = (String)properties.get(name);
pstmt.setInt(1, id);
pstmt.setString(2, name);
pstmt.setString(3, value);
pstmt.executeUpdate();
}
}
catch( SQLException sqle ) {
System.err.println(sqle);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
/**
* Load the user data from the database.
*/
private void loadFromDb() throws UserNotFoundException {
//If the user is anonymous or "all users", do nothing.
if (id == -1 || id == 0) {
return;
}
// Otherwise, select user data from User table and fill in relevant fields.
String query;
//We may want to do a username lookup.
if (username != null) {
query = LOAD_USER_BY_USERNAME;
}
//Otherwise, a lookup by id
else {
query = LOAD_USER_BY_ID;
}
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(query);
if (username != null) {
pstmt.setString(1, username);
}
else {
pstmt.setInt(1, id);
}
ResultSet rs = pstmt.executeQuery();
if (!rs.next()) {
throw new UserNotFoundException(
"Failed to read user " + id + " from database."
);
}
this.id = rs.getInt("userID");
this.username = rs.getString("username");
this.passwordHash = rs.getString("passwordHash");
this.name = rs.getString("name");
this.nameVisible = (rs.getInt("nameVisible") == 1);
this.email = rs.getString("email");
this.emailVisible = (rs.getInt("emailVisible") == 1);
}
catch( SQLException sqle ) {
throw new UserNotFoundException(
"Failed to read user " + id + " from database.", sqle
);
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Inserts a new user record into the database.
*/
private void insertIntoDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(INSERT_USER);
pstmt.setInt(1, id);
pstmt.setString(2, username);
pstmt.setString(3, passwordHash);
pstmt.setString(4, email);
pstmt.setInt(5, emailVisible?1:0);
pstmt.setInt(6, nameVisible?1:0);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbUser:insertIntoDb()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Save the user data to the database.
*/
private void saveToDb() {
if ( id == -1 || id == 0 ) {
//"anonymous" or "all users", do nothing
return;
}
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_USER);
pstmt.setString(1, passwordHash);
pstmt.setString(2, email);
pstmt.setInt(3, emailVisible?1:0);
pstmt.setString(4, name);
pstmt.setInt(5, nameVisible?1:0);
pstmt.setInt(6, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println( "SQLException in DbUser.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 + -