📄 dbforum.java
字号:
}
//Otherwise, do our own lookup.
else {
anonyPermissions = factory.getUserPermissions(-1, id);
}
//Combine permissions
finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions);
//If they are a valid user, figure out "any user" permissions.
if (isUser) {
ForumPermissions specialUserPermissions;
//Check for cache
if (cacheEnabled && userPermissionsCache.containsKey(0)) {
specialUserPermissions = (ForumPermissions)userPermissionsCache.get(0);
}
//Otherwise, do our own lookup.
else {
specialUserPermissions = factory.getUserPermissions(0, id);
//Add to cache so it will be there next time.
if (cacheEnabled) {
userPermissionsCache.add(0, specialUserPermissions);
}
}
//Combine permissions
finalPermissions = new ForumPermissions(finalPermissions, specialUserPermissions);
}
//Step 2 -- get Permissions for all groups the user is in.
int [] groups = ((DbProfileManager)factory.getProfileManager()).getUserGroups(userID);
for (int i=0; i<groups.length; i++) {
ForumPermissions groupPermissions = factory.getGroupPermissions(groups[i], id);
finalPermissions = new ForumPermissions(finalPermissions, groupPermissions);
}
//Finally, add user to cache so it will be there next time.
if (cacheEnabled) {
userPermissionsCache.add(userID, finalPermissions);
}
return finalPermissions;
}
public boolean hasPermission(int type) {
return true;
}
public String toString() {
return name;
}
/**
* Updates the modified date but doesn't require a security check since
* it is a protected method.
*/
protected void updateModifiedDate(java.util.Date modifiedDate) {
this.modifiedDate = modifiedDate;
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(UPDATE_FORUM_MODIFIED_DATE);
pstmt.setString(1, ""+modifiedDate.getTime());
pstmt.setInt(2, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum:updateModifiedDate()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Loads forum properties from the database.
*/
private void loadProperties() {
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 DbForum: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 forum properties to the database.
*/
private void saveProperties() {
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(); }
}
}
}
/**
* Loads filters from the database.
*/
private void loadFiltersFromDb() {
ArrayList newFilters = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_FILTERS);
pstmt.setInt(1,id);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
try {
ObjectInputStream in = new ObjectInputStream(rs.getBinaryStream("filterObject"));
newFilters.add(in.readObject());
}
catch (ClassCastException cce) {
//ignore for now since the filter might be updated. we
//need a solution for this. probably custom class loading
//of filter classes to protect against failure like this.
}
catch (Exception e) {
e.printStackTrace();
}
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
filters = new ForumMessageFilter[newFilters.size()];
for (int i=0; i<filters.length; i++) {
filters[i] = (ForumMessageFilter)newFilters.get(i);
}
//Finally, save filters back to Db. Effectively, this deletes filters
//from the database that failed to load. See note above.
saveFiltersToDb();
}
/**
* Saves filters to the database. Filter saving works by serializing
* each filter to a byte stream and then inserting that stream into
* the database.
*/
protected void saveFiltersToDb() {
synchronized (filterLock) {
//This should really be done as a transaction if the database
//supports them. Perhaps, we can have code in the future which
//will use transactions if they're supported.
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(DELETE_FILTERS);
pstmt.setInt(1,id);
pstmt.execute();
//Now insert new list of filters.
pstmt.close();
pstmt = con.prepareStatement(ADD_FILTER);
for (int i=0; i<filters.length; i++) {
try {
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(byteOut);
out.writeObject(filters[i]);
pstmt.setInt(1,id);
pstmt.setInt(2,i);
pstmt.setBytes(3,byteOut.toByteArray());
pstmt.execute();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
}
/**
* Loads forum data from the database.
*/
private void loadFromDb() throws ForumNotFoundException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_FORUM);
pstmt.setInt(1,id);
ResultSet rs = pstmt.executeQuery();
if( !rs.next() ) {
throw new ForumNotFoundException("Forum " + getID() +
" could not be loaded from the database.");
}
name = rs.getString("name");
description = rs.getString("description");
this.creationDate =
new java.util.Date(Long.parseLong(rs.getString("creationDate")));
this.modifiedDate =
new java.util.Date(Long.parseLong(rs.getString("modifiedDate")));
moderated = rs.getInt("moderated");
}
catch( SQLException sqle ) {
sqle.printStackTrace();
throw new ForumNotFoundException("Forum " + getID() +
" could not be loaded from the database.");
}
catch (NumberFormatException nfe) {
System.err.println("WARNING: In DbForum.loadFromDb() -- there " +
"was an error parsing the dates returned from the database. Ensure " +
"that they're being stored correctly.");
}
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 = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ADD_FORUM);
pstmt.setInt(1,id);
pstmt.setString(2,name);
pstmt.setString(3,description);
pstmt.setString(4, ""+creationDate.getTime());
pstmt.setString(5, ""+modifiedDate.getTime());
pstmt.setInt(6, moderated);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum:insertIntoDb()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
/**
* Saves forum data to the database.
*/
private synchronized void saveToDb() {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(SAVE_FORUM);
pstmt.setString(1, name);
pstmt.setString(2, description);
pstmt.setString(3, ""+creationDate.getTime());
pstmt.setString(4, ""+modifiedDate.getTime());
pstmt.setInt(5, moderated);
pstmt.setInt(6, id);
pstmt.executeUpdate();
}
catch( SQLException sqle ) {
System.err.println("Error in DbForum: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 + -