📄 dbforum.java
字号:
} finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } DbCacheManager cacheManager = factory.getCacheManager(); SearchIndexer indexer = factory.getSearchIndexer(); //Remove both forums from cache. Integer key = new Integer(this.id); cacheManager.remove(DbCacheManager.FORUM_CACHE, key); key = new Integer(forum.getID()); cacheManager.remove(DbCacheManager.FORUM_CACHE, key); //Remove thread from cache. key = new Integer(thread.getID()); cacheManager.remove(DbCacheManager.THREAD_CACHE, key); //Loop through all messages in thread Iterator messages = thread.messages(); while (messages.hasNext()) { ForumMessage message = (ForumMessage)messages.next(); //Remove each message from cache. key = new Integer(message.getID()); cacheManager.remove(DbCacheManager.MESSAGE_CACHE, key); //Remove and re-add every message to the search index. indexer.removeFromIndex(message); indexer.addToIndex(message); } // Update the modified date of thread Date now = new Date(); thread.setModifiedDate(now); // Update the modified date of forum thread is now in forum.setModifiedDate(now); } public Iterator threads() { return new DbForumIterator(this, factory); } public Iterator threads(int startIndex, int numResults) { return new DbForumIterator(this, factory, startIndex, numResults); } public int getThreadCount() { int threadCount = 0; // Based on the id in the object, get the thread data from the database: Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(THREAD_COUNT); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); threadCount = rs.getInt(1 /*"threadCount"*/); } catch( SQLException sqle ) { System.err.println("DbForum:getThreadCount() failed: " + sqle); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } return threadCount; } public int getMessageCount() { int messageCount = 0; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(MESSAGE_COUNT); pstmt.setInt(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); messageCount = rs.getInt(1 /*"messageCount"*/); } catch( SQLException sqle ) { System.err.println("DbForum:getMessageCount() failed: " + sqle); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } return messageCount; } public Query createQuery() { return new DbQuery(this, factory); } public void addUserPermission(User user, int permissionType) throws UnauthorizedException { Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(ADD_USER_PERM); pstmt.setInt(1,id); pstmt.setInt(2,user.getID()); pstmt.setInt(3,permissionType); pstmt.execute(); //Remove user permissions from cache since they've changed. factory.getCacheManager().removeUserPerm( new Integer(user.getID()), new Integer(id) ); } catch( SQLException sqle ) { System.err.println("Error in DbForum.java:" + sqle); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } public void removeUserPermission(User user, int permissionType) throws UnauthorizedException { Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(REMOVE_USER_PERM); pstmt.setInt(1,id); pstmt.setInt(2,user.getID()); pstmt.setInt(3,permissionType); pstmt.execute(); //Remove user permissions from cache since they've changed. factory.getCacheManager().removeUserPerm( new Integer(user.getID()), new Integer(id) ); } catch( SQLException sqle ) { System.err.println("Error in DbForum.java:" + sqle); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } public int[] usersWithPermission(int permissionType) throws UnauthorizedException { int [] users = new int[0]; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(USERS_WITH_PERM); pstmt.setInt(1,id); pstmt.setInt(2,permissionType); ResultSet rs = pstmt.executeQuery(); ArrayList userList = new ArrayList(); while (rs.next()) { userList.add(new Integer(rs.getInt("userID"))); } users = new int[userList.size()]; for (int i=0; i<users.length; i++) { users[i] = ((Integer)userList.get(i)).intValue(); } } catch( SQLException sqle ) { System.err.println("Error in DbForum.java:" + sqle); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } return users; } public void addGroupPermission(Group group, int permissionType) throws UnauthorizedException { Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(ADD_GROUP_PERM); pstmt.setInt(1,id); pstmt.setInt(2,group.getID()); pstmt.setInt(3,permissionType); pstmt.execute(); //Remove user permissions from cache since they've changed. Because //of the way that user perm cache is handled, it is easiest to //simply remove all the user perm cache for the forum. This is ok //since happens infrequently. factory.getCacheManager().remove( DbCacheManager.USER_PERMS_CACHE, new Integer(id) ); } catch( SQLException sqle ) { System.err.println("Error in DbForum.java:" + sqle); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } public void removeGroupPermission(Group group, int permissionType) throws UnauthorizedException { Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(REMOVE_GROUP_PERM); pstmt.setInt(1,id); pstmt.setInt(2,group.getID()); pstmt.setInt(3,permissionType); pstmt.execute(); //Remove user permissions from cache since they've changed. Because //of the way that user perm cache is handled, it is easiest to //simply remove all the user perm cache for the forum. This is ok //since happens infrequently. factory.getCacheManager().remove( DbCacheManager.USER_PERMS_CACHE, new Integer(id) ); } catch( SQLException sqle ) { System.err.println("Error in DbForum.java:" + sqle); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } } public int[] groupsWithPermission(int permissionType) throws UnauthorizedException { int [] groups = new int[0]; Connection con = null; PreparedStatement pstmt = null; try { con = DbConnectionManager.getConnection(); pstmt = con.prepareStatement(GROUPS_WITH_PERM); pstmt.setInt(1,id); pstmt.setInt(2,permissionType); ResultSet rs = pstmt.executeQuery(); ArrayList groupList = new ArrayList(); while (rs.next()) { groupList.add(new Integer(rs.getInt("groupID"))); } groups = new int[groupList.size()]; for (int i=0; i<groups.length; i++) { groups[i] = ((Integer)groupList.get(i)).intValue(); } } catch( SQLException sqle ) { System.err.println("Error in DbForum.groupsWithPermission:" + sqle); sqle.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } try { con.close(); } catch (Exception e) { e.printStackTrace(); } } return groups; } public ForumMessage applyFilters(ForumMessage message) { //Loop through filters and apply them for (int i=0; i < filters.length; i++) { message = filters[i].clone(message); } return message; } public ForumMessageFilter[] getForumMessageFilters() throws UnauthorizedException { ForumMessageFilter [] dbFilters = new ForumMessageFilter[filters.length]; for (int i=0; i<filters.length; i++) { dbFilters[i] = new DbForumMessageFilter((ForumMessage)filters[i], this); } return dbFilters; } public void addForumMessageFilter(ForumMessageFilter filter) throws UnauthorizedException { ArrayList newFilters = new ArrayList(filters.length+1); for (int i=0; i<filters.length; i++) { newFilters.add(filters[i]); } newFilters.add(filter); ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()]; for (int i=0; i<newArray.length; i++) { newArray[i] = (ForumMessageFilter)newFilters.get(i); } //Finally, overwrite filters with the new array filters = newArray; saveFiltersToDb(); } public void addForumMessageFilter(ForumMessageFilter filter, int index) throws UnauthorizedException { ArrayList newFilters = new ArrayList(filters.length+1); for (int i=0; i<filters.length; i++) { newFilters.add(filters[i]); } newFilters.add(index, filter); ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()]; for (int i=0; i<newArray.length; i++) { newArray[i] = (ForumMessageFilter)newFilters.get(i); } //Finally, overwrite filters with the new array filters = newArray; saveFiltersToDb(); } public void removeForumMessageFilter(int index) throws UnauthorizedException { ArrayList newFilters = new ArrayList(filters.length); for (int i=0; i<filters.length; i++) { newFilters.add(filters[i]); } newFilters.remove(index); ForumMessageFilter[] newArray = new ForumMessageFilter[newFilters.size()]; for (int i=0; i<newArray.length; i++) { newArray[i] = (ForumMessageFilter)newFilters.get(i); } //Finally, overwrite filters with the new array filters = newArray; saveFiltersToDb(); } public ForumPermissions getPermissions(Authorization authorization) { int userID = authorization.getUserID(); //Get the user perm cache for this forum Cache userPermCache = (Cache)factory.getCacheManager().get( DbCacheManager.USER_PERMS_CACHE, new Integer(id) ); //Simple case: if cache is turned on and the user is already cached, //we can simply return the cached permissions. if (userPermCache != null) { ForumPermissions permissions = (ForumPermissions)userPermCache.get(new Integer(userID)); if (permissions != null) { return permissions; } } //Not so simple case: cache is not turned on or the user permissions //have not been cached yet. boolean isAnonymous = (userID == -1); boolean isUser = !isAnonymous; ForumPermissions finalPermissions = ForumPermissions.none(); //Step 1 - Get permissions for the User. This includes anonymous //perms, "special user" perms, and the specific perms for the user. if (isUser) { ForumPermissions userPermissions = factory.getUserPermissions(userID, id); //Combine permissions finalPermissions = new ForumPermissions(finalPermissions, userPermissions); } //Add in anonymous perms. ForumPermissions anonyPermissions = null;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -