dbforumcategory.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,544 行 · 第 1/4 页
JAVA
1,544 行
public void moveForum(Forum forum, ForumCategory destinationCategory) { Connection con = null; PreparedStatement pstmt = null; boolean abortTransaction = false; try { con = ConnectionManager.getTransactionConnection(); // First, find the old index value for the forum. pstmt = con.prepareStatement(FIND_FORUM_INDEX); pstmt.setLong(1, forum.getID()); pstmt.setLong(2, this.id); ResultSet rs = pstmt.executeQuery(); // If we didn't find a row, that means the forum does not belong // to the category. In that case, throw an exception. if (!rs.next()) { throw new IllegalArgumentException("Forum " + forum.getID() + " is not in category " + this.id); } int oldIndex = rs.getInt(1); pstmt.close(); // Find the max index for the forum. pstmt = con.prepareStatement(FORUM_IN_CATEGORY_COUNT); pstmt.setLong(1, this.id); rs = pstmt.executeQuery(); rs.next(); int count = rs.getInt(1); int newIndex = count==0?0:count-1; pstmt.close(); // Now shift the values of other forums. if (newIndex < oldIndex) { pstmt = con.prepareStatement(SHIFT_FORUM_INDEX_1); } else { pstmt = con.prepareStatement(SHIFT_FORUM_INDEX_2); } pstmt.setLong(1, this.id); pstmt.setInt(2, newIndex); pstmt.setInt(3, oldIndex); pstmt.execute(); pstmt.close(); // Find the correct index for the destination category. pstmt = con.prepareStatement(FORUM_IN_CATEGORY_COUNT); pstmt.setLong(1, destinationCategory.getID()); rs = pstmt.executeQuery(); rs.next(); int destinationIndex = rs.getInt(1); pstmt.close(); // Change the category. pstmt = con.prepareStatement(MOVE_FORUM); pstmt.setLong(1, destinationCategory.getID()); pstmt.setInt(2, destinationIndex); pstmt.setLong(3, forum.getID()); pstmt.executeUpdate(); } catch (SQLException sqle) { sqle.printStackTrace(); abortTransaction = true; } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } ConnectionManager.closeTransactionConnection(con, abortTransaction); } // Expire caches. try { factory.cacheManager.getForum(forum.getID()).clearCache(); } catch (Exception e) { e.printStackTrace(); } factory.cacheManager.forumCache.remove(new Long(forum.getID())); this.clearCache(); try { factory.cacheManager.getForumCategory( destinationCategory.getID()).clearCache(); } catch (Exception e) { e.printStackTrace(); } // Also, we have to wipe out the permissions cache since the permission // for reading the forum or displaying the category could have changed. factory.cacheManager.userPermsCache.clear(); } public ForumCategory getParentCategory() { ForumCategory parent = null; try { // As long as this isn't the root category, find the parent. if (id != 1) { parent = factory.getForumCategory(categoryTree.getParent(id)); } } catch (ForumCategoryNotFoundException fcnfe) { fcnfe.printStackTrace(); } return parent; } public int getCategoryCount() { return categoryTree.getChildCount(id); } public Iterator categories() { long [] children = categoryTree.getChildren(id); return new DatabaseObjectIterator(JiveGlobals.FORUM_CATEGORY, children, factory); } public int getRecursiveCategoryCount() { return categoryTree.getRecursiveChildren(id).length; } public Iterator recursiveCategories() { long [] children = categoryTree.getRecursiveChildren(id); return new DatabaseObjectIterator(JiveGlobals.FORUM_CATEGORY, children, factory); } public int getCategoryDepth() { return categoryTree.getDepth(this.id); } public void setCategoryIndex(ForumCategory category, int newIndex) { synchronized(lock) { long categoryID = category.getID(); if (categoryID == 1 || category.getParentCategory().getID() != this.id) { throw new IllegalArgumentException("Invalid category argument."); } if (newIndex < 0 || newIndex >= getCategoryCount()) { throw new IllegalArgumentException("Invalid new index: " + newIndex); } Connection con = null; PreparedStatement pstmt = null; boolean abortTransaction = false; try { con = ConnectionManager.getTransactionConnection(); // Find lft, rgt of the category. pstmt = con.prepareStatement(FIND_LFT_RGT); pstmt.setLong(1, categoryID); ResultSet rs = pstmt.executeQuery(); rs.next(); int currentLft = rs.getInt(1); int currentRgt = rs.getInt(2); // Figure out the categoryID of the index we want to move to // and its lft and rgt values. long replaceID = categoryTree.getChildren(this.id)[newIndex]; pstmt.setLong(1, replaceID); rs = pstmt.executeQuery(); rs.next(); int replaceLft = rs.getInt(1); int replaceRgt = rs.getInt(2); pstmt.close(); // Find out the numeric gap between the values. int indexDelta; if (replaceRgt < currentRgt) { indexDelta = (currentLft - replaceLft); } else { indexDelta = Math.abs(currentRgt - replaceRgt); } int delta = currentRgt - currentLft; // Move the other index values to make room. if (replaceRgt < currentRgt) { pstmt = con.prepareStatement(SHIFT_CATEGORY_INDEX_1); pstmt.setInt(1, delta+1); pstmt.setInt(2, delta+1); pstmt.setInt(3, replaceLft); pstmt.setInt(4, currentLft); } else { pstmt = con.prepareStatement(SHIFT_CATEGORY_INDEX_2); pstmt.setInt(1, 0-(delta+1)); pstmt.setInt(2, 0-(delta+1)); pstmt.setInt(3, currentLft); pstmt.setInt(4, currentRgt); pstmt.setInt(5, replaceRgt); } pstmt.execute(); pstmt.close(); // Set the new index value for this category and its children. StringBuffer sql = new StringBuffer(); if (replaceRgt < currentRgt) { sql.append("UPDATE jiveCategory SET lft=lft-").append(indexDelta); sql.append(", rgt=rgt-").append(indexDelta); } else { sql.append("UPDATE jiveCategory SET lft=lft+").append(indexDelta); sql.append(", rgt=rgt+").append(indexDelta); } sql.append(" WHERE categoryID IN(").append(categoryID); long [] childCats = categoryTree.getRecursiveChildren(categoryID); for (int i=0; i<childCats.length; i++) { sql.append(",").append(childCats[i]); } sql.append(")"); pstmt = con.prepareStatement(sql.toString()); pstmt.execute(); } catch (Exception e) { e.printStackTrace(); abortTransaction = true; } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } ConnectionManager.closeTransactionConnection(con, abortTransaction); } refreshCategoryTree(); } } public void moveCategory(ForumCategory category, ForumCategory destinationCategory) { synchronized(lock) { // Moving a parent category to a child or sub-child is not allowed. ForumCategory parent = destinationCategory.getParentCategory(); while (parent != null) { if (parent.getID() == category.getID()) { throw new IllegalArgumentException("You cannot move a parent " + "category into a child."); } parent = parent.getParentCategory(); } Connection con = null; PreparedStatement pstmt = null; boolean abortTransaction = false; try { con = ConnectionManager.getTransactionConnection(); // First, load lft, rgt values of category. pstmt = con.prepareStatement(FIND_LFT_RGT); pstmt.setLong(1, category.getID()); ResultSet rs = pstmt.executeQuery(); rs.next(); int lft = rs.getInt(1); int rgt = rs.getInt(2); int delta = rgt - lft + 1; pstmt.close(); // Remove category from current place in tree. long [] childCats = categoryTree.getRecursiveChildren(category.getID()); StringBuffer sql = new StringBuffer(); sql.append(CHANGE_LFT); // Don't modify the sub-tree that we're going to // be moving. sql.append(" AND categoryID NOT IN(").append(category.getID()); for (int i=0; i<childCats.length; i++) { sql.append(",").append(childCats[i]); } sql.append(")"); pstmt = con.prepareStatement(sql.toString()); pstmt.setInt(1, -delta); pstmt.setInt(2, lft); pstmt.setInt(3, rgt); pstmt.execute(); pstmt.close(); sql = new StringBuffer(); sql.append(CHANGE_RGT); // Don't modify the sub-tree that we're going to // be moving. sql.append(" AND categoryID NOT IN(").append(category.getID()); for (int i=0; i<childCats.length; i++) { sql.append(",").append(childCats[i]); } sql.append(")"); pstmt = con.prepareStatement(sql.toString()); pstmt.setInt(1, -delta); pstmt.setInt(2, rgt); pstmt.execute(); pstmt.close(); // Find the the parent category's lft and rgt values. pstmt = con.prepareStatement(FIND_LFT_RGT); pstmt.setLong(1, destinationCategory.getID()); rs = pstmt.executeQuery(); rs.next(); int parentLft = rs.getInt(1); int parentRgt = rs.getInt(2); pstmt.close(); // Prepare tree for newly inserted value. sql = new StringBuffer(); sql.append(CHANGE_LFT); // Don't modify the sub-tree that we're going to // be moving. sql.append(" AND categoryID NOT IN(").append(category.getID()); for (int i=0; i<childCats.length; i++) { sql.append(",").append(childCats[i]); } sql.append(")"); pstmt = con.prepareStatement(sql.toString()); pstmt.setInt(1, delta); pstmt.setInt(2, parentLft); pstmt.setInt(3, parentRgt); pstmt.execute(); pstmt.close(); sql = new StringBuffer(); sql.append(CHANGE_RGT); // Don't modify the sub-tree that we're going to // be moving. sql.append(" AND categoryID NOT IN(").append(category.getID()); for (int i=0; i<childCats.length; i++) { sql.append(",").append(childCats[i]); } sql.append(")"); pstmt = con.prepareStatement(sql.toString()); pstmt.setInt(1, delta); pstmt.setInt(2, parentRgt); pstmt.execute(); pstmt.close(); // Finally, move the sub-tree. sql = new StringBuffer(); sql.append("UPDATE jiveCategory SET lft=lft+?, rgt=rgt+? WHERE "); sql.append(" categoryID IN(").append(category.getID()); for (int i=0; i<childCats.length; i++) { sql.append(",").append(childCats[i]); } sql.append(")"); pstmt = con.prepareStatement(sql.toString()); pstmt.setInt(1, parentRgt - lft); pstmt.setInt(2, parentRgt - lft); pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); abortTransaction = true; } finally { try { pstmt.close(); } catch (Exception e) { e.printStackTrace(); } ConnectionManager.closeTransactionConnection(con, abortTransaction); } refreshCategoryTree(); } // Finally, we have to wipe out the permissions cache since the permission // for reading the category or displaying the category could have changed. factory.cacheManager.userPermsCache.clear(); } public ForumCategory createCategory(String name, String description) { synchronized (lock) { ForumCategory category = new DbForumCategory(name, description, this); // Refresh the category tree. refreshCategoryTree(); return category; } } public void deleteCategory(ForumCategory forumCategory) { synchronized (lock) { if (forumCategory.getID() == 1 || forumCategory.getParentCategory().getID() != this.id) { throw new IllegalArgumentException(); } boolean abortTransaction = false; Connection con = null; PreparedStatement pstmt = null; try { con = ConnectionManager.getTransactionConnection(); DbForumCategory category = factory.cacheManager.getForumCategory( forumCategory.getID()); // First, load lft, rgt values. pstmt = con.prepareStatement(FIND_LFT_RGT); pstmt.setLong(1, category.getID()); ResultSet rs = pstmt.executeQuery(); rs.next(); int lft = rs.getInt(1); int rgt = rs.getInt(2); int delta = 0 - (rgt - lft + 1); pstmt.close();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?