📄 dbforum.java
字号:
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.
userPermissionsCache.remove(user.getID());
}
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.
userPermissionsCache.remove(user.getID());
}
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 permissions for all users in this group.
Iterator iter = group.users();
while (iter.hasNext()) {
User user = (User)iter.next();
userPermissionsCache.remove(user.getID());
}
}
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 permissions for all users in this group.
Iterator iter = group.users();
while (iter.hasNext()) {
User user = (User)iter.next();
userPermissionsCache.remove(user.getID());
}
}
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();
boolean cacheEnabled = factory.isCacheEnabled();
//Simple case: if cache is turned on and the user is already cached,
//we can simply return the cached permissions.
if (cacheEnabled && userPermissionsCache.containsKey(userID)) {
return (ForumPermissions)userPermissionsCache.get(userID);
}
//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;
if (cacheEnabled && userPermissionsCache.containsKey(-1)) {
anonyPermissions = (ForumPermissions)userPermissionsCache.get(-1);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -