📄 详细分析一.txt
字号:
return l;
}
return bl;
}
与其相反的方法是:
public List getBoardIDs(List boards) {
List<Long> l = new ArrayList<Long>();
for (int i = 0; i < boards.size(); i++) {
Board b = (Board) boards.get(i);
l.add(b.getId());
}
return l;
}
注意以上都有this.getBoardByID(),它是带Cache的哦!从BoardCache中取!
public Board getBoardByID(long id) {
Board board = (Board) this.getBoardCache().get(new Long(id));
if (board == null) {
board = this.getBoardDAO().getBoardByID(id);
if (board != null) {
this.getBoardCache().add(board.getId(), board);
}
}
return board;
}
另外,对于Permission,有以下几个方法:
public Map[] getBoardMasterPermission(int roleID)
public Map[] getBoardPermission(long bid, int groupID)
private Map[] getPermissionMaps(long bid, int groupID)
private Map[] getPermissionMaps(int roleID)
前面2个是公开的方法,后2个是被调用的..
public Map[] getBoardMasterPermission(int roleID) {
if (Constant.USE_PERMISSION_CACHE) {
Map[] mapPermission = (Map[]) this.getUserPermissionCache().get("R_" + String.valueOf(roleID));
if (mapPermission == null) {
mapPermission = this.getPermissionMaps(roleID);
this.getUserPermissionCache().add("R_" + String.valueOf(roleID), mapPermission);
}
return mapPermission;
} else {
return this.getPermissionMaps(roleID);
}
}
private Map[] getPermissionMaps(int roleID) {
Map[] mapPermission = { new HashMap(), new HashMap() }; //MAP数组,555
Role role = this.getRoleDAO().findRoleByID(roleID);//得到id为roleID的角色对象
List permissions = role.getPermissions(); // 取得角色的权限ID列表(ID的List)
if (permissions != null && !permissions.isEmpty()) {
List permissionList = this.getPermissionDAO().findPermissionnIDs(permissions); // 取得权限列表Permission对象的List
for (int i = 0; i < permissionList.size(); i++) {
Permission permission = (Permission) permissionList.get(i);
if (permission.getTypeID() == 2) {
mapPermission[0].put(permission.getResource() + "," + permission.getAction(), permission);
}
if (permission.getTypeID() == 3) {
mapPermission[1].put(permission.getId(), permission);
} //这段需理解!!!
}
}
return mapPermission;
}
另外,除了版主外,还有版区权限:
public Map[] getBoardPermission(long bid, int groupID) {
if (Constant.USE_PERMISSION_CACHE) {
Map[] mapPermission = (Map[]) this.getUserPermissionCache().get(
"BG_" + String.valueOf(bid) + "_" + String.valueOf(groupID));
if (mapPermission == null) {
mapPermission = this.getPermissionMaps(bid, groupID);
this.getUserPermissionCache().add("BG_" + String.valueOf(bid) + "_" + String.valueOf(groupID),
mapPermission);
}
return mapPermission;
} else {
return this.getPermissionMaps(bid, groupID);
}
}
同样,它用了类似的重载方法:
private Map[] getPermissionMaps(long bid, int groupID) {
Map[] boardPermission = { new HashMap(), new HashMap() };
BoardPermission bp = this.getBoardPermissionDAO().findBoardPermissionByBidGid(bid, groupID);//用的是BoardPermissionDAO
List permissions = bp.getPermissions(); // 取得权限ID列表
if (permissions != null && !permissions.isEmpty()) {
List permissionList = this.getPermissionDAO().findPermissionnIDs(permissions); // 取得权限列表Permission对象的List
for (int i = 0; i < permissionList.size(); i++) {
Permission permission = (Permission) permissionList.get(i);
if (permission.getTypeID() == 2) {
boardPermission[0].put(permission.getResource() + "," + permission.getAction(), permission);
}
if (permission.getTypeID() == 3) {
boardPermission[1].put(permission.getId(), permission);
}
}
}
return boardPermission;
}
接下来,看看getNextOrder(long pid),getPostSumNum(int mainorall,int usStat,int hidden),它们完全由DAO去查询数据库!我们看下remove系列:
public void removeBoard(Board board) throws BbscsException {
try {
Long lbid = board.getId();
long pbid = board.getParentID();
Board pboard = this.getBoardDAO().getBoardByID(board.getParentID()); // 取得父版区
this.getBoardDAO().removeBoard(board); // 删除版区
this.getBoardPermissionDAO().removeBoardPermissionsByBid(board.getId().longValue());//删除对应版区的权限
if (pboard != null) { // 父版区存在,对ChildIDs字段做矫正
List pcboards = this.getBoardDAO().findBoardsByParentID(pboard.getId().longValue(), 1, 0,
Constant.FIND_BOARDS_BY_ORDER);//pcboards是子对象
List cids = this.getBoardIDs(pcboards);//cids是子IDs
pboard.setChildIDs(cids);
this.getBoardDAO().saveBoard(pboard);
}
this.getBoardCache().remove(lbid);
this.clearBoradListSysListCache(pbid);
//清理本id的BoardCache,清理父id的SysListObjectCache,而userPermission中出现过去2类:R_ BG_的..都不好清理!
} catch (Exception ex) {
logger.error(ex);
throw new BbscsException(ex);
}
}
public void removeBoardTag(Board board, String tagID) throws BbscsException {
BoardTag bt = null;
Iterator it = board.getBoardTag().iterator(); //由Iterator遍历查找,找到后remove掉
while (it.hasNext()) {
bt = (BoardTag) it.next();
if (bt.getId().equals(tagID)) {
board.getBoardTag().remove(bt);
break;
}
}
try {
board = this.getBoardDAO().saveBoard(board); //保存修改结果
this.getForumDAO().updateForumsTag(tagID, "0", "");//帖子TAG
this.getForumHistoryDAO().updateForumsTag(tagID, "0", "");//历史贴TAG
this.getBoardCache().remove(board.getId());//BoardCache清理一次
} catch (Exception e) {
logger.error(e);
throw new BbscsException(e);
}
}
最后再看两个方法:
public Board updateBoard(Board board, long oldParentID) throws BbscsException {
try {
Board pboard = this.getBoardDAO().getBoardByID(board.getParentID());
if (pboard != null) {
List pboards = new ArrayList();
pboards.addAll(pboard.getParentIDs());
pboards.add(pboard.getId());
board.setParentIDs(pboards);
board.setLevel(pboard.getLevel() + 1);
} else {
board.setParentIDs(new ArrayList());//hbm.xml决定需List对象
board.setLevel(0);
}
/**
数据库中的数据id /parentID/ParentIDs/ChildIDs
1/ 0/ /2,3
2/ 1/ 1/
3/ 1/ 1/
*/
board = this.getBoardDAO().saveBoard(board);
if (pboard != null) {
List pcboards = this.getBoardDAO().findBoardsByParentID(board.getParentID(), 1, -1,
Constant.FIND_BOARDS_BY_ORDER);
List cids = this.getBoardIDs(pcboards);
pboard.setChildIDs(cids);
this.getBoardDAO().saveBoard(pboard);
this.getBoardCache().remove(pboard.getId());
}
this.clearBoradListSysListCache(board.getParentID());
if (oldParentID != -1) { // 父级版区改变。修正父级版区数据 关键点,由传入的oldParentID决定是否改变ParentID
Board pboardOld = this.getBoardDAO().getBoardByID(oldParentID);
if (pboardOld != null) {
List pcboards = this.getBoardDAO().findBoardsByParentID(pboardOld.getId().longValue(), 1, -1,
Constant.FIND_BOARDS_BY_ORDER);
List cids = this.getBoardIDs(pcboards);
pboardOld.setChildIDs(cids);
this.getBoardDAO().saveBoard(pboardOld);
this.getBoardCache().remove(pboardOld.getId());
this.clearBoradListSysListCache(oldParentID);
}
}
this.getBoardCache().remove(board.getId()); // 从Cache中清除
return board;
} catch (Exception ex) {
logger.error(ex);
throw new BbscsException(ex);
}
}
最后一个方法了:
public void saveBoardsPostNumCount() throws BbscsException {
long totalNum = 0;
long totalMainNum = 0;
List bl = findBoardsByParentID(0, 1, -1, Constant.FIND_BOARDS_BY_ORDER);//从0继承的版区有显示主题数和贴子数的需求
for (int i = 0; i < bl.size(); i++) {
Board b = (Board) bl.get(i);
if (b.getBoardType() == 3) { //可发贴的版区吧!
b.setMainPostNum(this.getForumDAO().getForumNum(b.getId(), 1, 0, 0, -1)
+ this.getForumHistoryDAO().getForumNum(b.getId(), 1, 0, 0, -1));
b.setPostNum(this.getForumDAO().getForumNum(b.getId(), -1, 0, 0, -1)
+ this.getForumHistoryDAO().getForumNum(b.getId(), -1, 0, 0, -1));
//isNew=-1表示非主题贴,而1则相反!
try {
b = this.getBoardDAO().saveBoard(b);
totalNum = totalNum + b.getPostNum();
totalMainNum = totalMainNum + b.getMainPostNum();
// if (Constant.USE_CLUSTER) {
this.getBoardCache().remove(b.getId()); // 从Cache中清除
// } else {
// this.getBoardCache().add(b.getId(), b);
// }
} catch (Exception ex1) {
logger.error(ex1);
throw new BbscsException(ex1);
}
}
List bl2 = findBoardsByParentID(b.getId(), 1, -1, Constant.FIND_BOARDS_BY_ORDER); //子论坛的更新
if (!bl2.isEmpty()) {
for (int j = 0; j < bl2.size(); j++) {
Board b2 = (Board) bl2.get(j);
if (b2.getBoardType() == 3) {
b2.setMainPostNum(this.getForumDAO().getForumNum(b2.getId(), 1, 0, 0, -1)
+ this.getForumHistoryDAO().getForumNum(b2.getId(), 1, 0, 0, -1));
b2.setPostNum(this.getForumDAO().getForumNum(b2.getId(), -1, 0, 0, -1)
+ this.getForumHistoryDAO().getForumNum(b2.getId(), -1, 0, 0, -1));
try {
b2 = this.getBoardDAO().saveBoard(b2);
totalNum = totalNum + b2.getPostNum();
totalMainNum = totalMainNum + b2.getMainPostNum();
// if (Constant.USE_CLUSTER) {
this.getBoardCache().remove(b2.getId()); // 从Cache中清除
// } else {
// this.getBoardCache().add(b2.getId(), b2);
// }
} catch (Exception ex1) {
logger.error(ex1);
throw new BbscsException(ex1);
}
}
}
}
}
logger.info("postMainNum:" + totalMainNum + " postNum:" + totalNum);//打印
this.getSysStatService().savePostNum(totalMainNum, totalNum);//SysStatService服务终于用上了!
}
OK!我们直接进入DAO层:先看接口,发现与service接口层的方法并不相同,主要是由于service层有些方法是业务处理用,而不是用于数据处理,如createBoard,updateBoard,findBoardsAllTree,getBoardPermission,getBoardMasterPermission,isBoardMaster,当然DAO接口层也有一些方法是没有的:findBoardsNeedCount,findBoardsInIDsfindBoardsIdByParentIDInUse等等,我们看其实现吧:
进入com.laoer.bbscs.BoardHibernateDAO中首先是一些字符常量:
private static final String LOADS_BY_PARENTID_BY_ORDER = "from Board where parentID = ? order by orders";
private static final String LOADS_ALL = "from Board";
public static final String[] FIND_BOARDS = new String[3];
public static final String LOAD_NEXT_ORDER = "select max(orders) from Board where parentID = ?";
private static final String LOAD_IDS_IN_USE = "select id from Board where parentID = ? and useStat = 1";
我们看几个重点的实现方法:
public List findBoardsByParentID(long pid) {
return this.getHibernateTemplate().find(LOADS_BY_PARENTID_BY_ORDER, new Long(pid));
}
其重载方法:
public List findBoardsByParentID(final long pid, final int useStat, final int hidden, final int orderType) {
return getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(Session s) throws HibernateException {
Criteria c = s.createCriteria(Board.class);
c.add(Restrictions.eq("parentID", new Long(pid)));
if (useStat != -1) {
c.add(Restrictions.eq("useStat", new Integer(useStat)));
}
if (hidden != -1) {
c.add(Restrictions.eq("isHidden", new Integer(hidden)));
}
if (orderType != -1) {
if (orderType == Constant.FIND_BOARDS_BY_ORDER) {
c.addOrder(Order.asc("orders"));
}
if (orderType == Constant.FIND_BOARDS_BY_MAINPOSTNUM) {
c.addOrder(Order.desc("mainPostNum"));
}
if (orderType == Constant.FIND_BOARDS_BY_POSTNUM) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -