📄 dbforum.java
字号:
if (userPermCache != null) { anonyPermissions = (ForumPermissions)userPermCache.get(new Integer(-1)); } //Otherwise, do our own lookup. if (anonyPermissions == null) { anonyPermissions = factory.getUserPermissions(-1, id); //Add to cache so it will be there next time. if (userPermCache != null) { userPermCache.add(new Integer(-1), anonyPermissions); } } //Combine permissions finalPermissions = new ForumPermissions(finalPermissions, anonyPermissions); //If they are a valid user, figure out "any user" permissions. if (isUser) { ForumPermissions specialUserPermissions = null; //Check for cache if (userPermCache != null) { specialUserPermissions = (ForumPermissions)userPermCache.get(new Integer(0)); } //Otherwise, do our own lookup. if (specialUserPermissions == null) { specialUserPermissions = factory.getUserPermissions(0, id); //Add to cache so it will be there next time. if (userPermCache != null) { userPermCache.add(new Integer(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 (isUser && userPermCache != null) { userPermCache.add(new Integer(userID), finalPermissions); } return finalPermissions; } 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(name); //name size += CacheSizes.sizeOfString(description); //description size += CacheSizes.sizeOfDate(); //creation date size += CacheSizes.sizeOfDate(); //modified date size += CacheSizes.sizeOfInt(); //moderated size += filters.length * 8; //each filter is 8 bytes size += CacheSizes.sizeOfProperties(properties);//properties object size += CacheSizes.sizeOfObject(); //save lock return size; } //OTHER METHODS /** * Returns a String representation of the Forum object using the forum name. * * @return a String representation of the Forum object. */ public String toString() { return name; } public int hashCode() { return id; } public boolean equals(Object object) { if (this == object) { return true; } if (object != null && object instanceof DbForum) { return id == ((DbForum)object).getID(); } else { return false; } } /** * 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(saveLock) { 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(saveLock) { 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 (saveLock) { //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(); //See if we should load by forumID or by name if (id == -1) { pstmt = con.prepareStatement(LOAD_FORUM_BY_NAME); pstmt.setString(1,name); } else { pstmt = con.prepareStatement(LOAD_FORUM_BY_ID); pstmt.setInt(1, id); } ResultSet rs = pstmt.executeQuery(); if( !rs.next() ) { throw new ForumNotFoundException("Forum " + getID() + " could not be loaded from the database."); } id = rs.getInt("forumID"); name = rs.getString("name"); description = rs.getString("description"); this.creationDate = new java.util.Date(Long.parseLong(rs.getString("creationDate").trim())); this.modifiedDate = new java.util.Date(Long.parseLong(rs.getString("modifiedDate").trim())); 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, Long.toString(creationDate.getTime())); pstmt.setString(5, Long.toString(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, Long.toString(creationDate.getTime())); pstmt.setString(4, Long.toString(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 + -