boardservicebycacheimp.java

来自「一个jsp写的bbs」· Java 代码 · 共 853 行 · 第 1/2 页

JAVA
853
字号
package com.laoer.bbscs.service.imp;

import java.io.*;
import java.util.*;

import org.apache.commons.io.*;
import org.apache.commons.logging.*;
import com.laoer.bbscs.bean.*;
import com.laoer.bbscs.comm.*;
import com.laoer.bbscs.dao.*;
import com.laoer.bbscs.exception.*;
import com.laoer.bbscs.service.*;
import com.laoer.bbscs.service.singleton.*;

/**
 * <p>Title: Tianyi BBS</p>
 *
 * <p>Description: BBSCS</p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: Laoer.com</p>
 *
 * @author Gong Tianyi
 * @version 7.0
 */
public class BoardServiceByCacheImp
    implements BoardService {

  private static final Log logger = LogFactory.getLog(BoardServiceByCacheImp.class);

  private BoardDAO boardDAO;

  private BoardPermissionDAO boardPermissionDAO;

  private UserGroupDAO userGroupDAO;

  private PermissionDAO permissionDAO;

  private RoleDAO roleDAO;

  private Cache boardCache;

  private ForumDAO forumDAO;

  private ForumHistoryDAO forumHistoryDAO;

  private BoardSingleton boardSingleton;

  private SysStatService sysStatService;

  private Cache userPermissionCache;

  public BoardServiceByCacheImp() {
  }

  /**
   * 保存或更新Board对象
   *
   * @param board Board
   * @return Board
   * @throws BbscsException
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public Board saveBoard(Board board) throws BbscsException {
    try {
      board = this.getBoardDAO().saveBoard(board);
      this.getBoardCache().remove(board.getId()); //从Cache中清除
      return board;
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BbscsException(ex);
    }
  }

  /**
   *
   * @param board Board
   * @return Board
   * @throws BbscsException
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public Board createBoard(Board board) throws BbscsException {
    try {
      Board pboard = this.getBoardDAO().getBoardByID(board.getParentID()); //取得父级版区
      if (pboard != null) { //父级版区存在
        List pboards = new ArrayList();
        //pboards = pboard.getParentIDs();
        pboards.addAll(pboard.getParentIDs());
        //System.out.println("pboards size:" + pboards.size());
        pboards.add(pboard.getId());
        board.setParentIDs(pboards); //设置父级版区列表字段
        board.setLevel(pboard.getLevel() + 1); //设置级别
      }
      board = this.getBoardDAO().saveBoard(board);

      if (pboard != null) {
        //List pcboards = this.getBoardDAO().findBoardsByParentID(board.getParentID(), 1, 0,
        //    Constant.FIND_BOARDS_BY_ORDER); //取得父级半区的所有子版区列表
        List pcboards = this.getBoardDAO().findBoardsByParentID(board.getParentID(), 1, -1,
            Constant.FIND_BOARDS_BY_ORDER); //取得父级半区的所有子版区列表
        //System.out.println("pcboards size:" + pcboards.size());
        //System.out.println("pboard.getParentIDs().size():" + pboard.getParentIDs().size());
        List cids = this.getBoardIDs(pcboards);
        pboard.setChildIDs(cids); //设置父级版区的所有子版区列表字段
        this.getBoardDAO().saveBoard(pboard);
        this.getBoardCache().remove(pboard.getId());
      }

      //为版区增加用户组版区权限
      List gl = this.getUserGroupDAO().findUserGroupsAll(); //取得用户组列表
      BoardPermission bp;
      for (int i = 0; i < gl.size(); i++) {
        UserGroup ug = (UserGroup) gl.get(i);
        bp = new BoardPermission();
        bp.setBoardID(board.getId().longValue());
        bp.setGroupID(ug.getId().intValue());

        if (ug.getId().intValue() == 1) {
          bp.setPermissions(Constant.BOARD_PERMISSION_GROUP_LIST_1);
        }
        else if (ug.getId().intValue() == 2) {
          bp.setPermissions(Constant.BOARD_PERMISSION_GROUP_LIST_2);
        }
        else if (ug.getId().intValue() == 3) {
          bp.setPermissions(Constant.BOARD_PERMISSION_GROUP_LIST_3);
        }
        else if (ug.getId().intValue() == 4) {
          bp.setPermissions(Constant.BOARD_PERMISSION_GROUP_LIST_4);
        }
        else if (ug.getId().intValue() == 5) {
          bp.setPermissions(Constant.BOARD_PERMISSION_GROUP_LIST_5);
        }
        else if (ug.getId().intValue() == 6) {
          bp.setPermissions(Constant.BOARD_PERMISSION_GROUP_LIST_6);
        }
        else {
          bp.setPermissions(Constant.BOARD_PERMISSION_GROUP_LIST_1);
        }
        this.getBoardPermissionDAO().saveBoardPermission(bp);
      }
      this.getBoardSingleton().init(); //重新载入版区列表 add at 2006.10.26
      return board;
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BbscsException(ex);
    }
  }

  /**
   * 更新Board对象
   *
   * @param board Board
   * @param oldParentID long
   * @return Board
   * @throws BbscsException
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  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 = pboard.getParentIDs();
        pboards.addAll(pboard.getParentIDs());
        //System.out.println("pboards size:" + pboards.size());
        pboards.add(pboard.getId());
        board.setParentIDs(pboards);
        board.setLevel(pboard.getLevel() + 1);
      }
      else {
        board.setParentIDs(new ArrayList());
        board.setLevel(0);
      }
      board = this.getBoardDAO().saveBoard(board);

      if (pboard != null) {
        List pcboards = this.getBoardDAO().findBoardsByParentID(board.getParentID(), 1, -1,
            Constant.FIND_BOARDS_BY_ORDER);
        //System.out.println("pcboards size:" + pcboards.size());
        //System.out.println("pboard.getParentIDs().size():" + pboard.getParentIDs().size());
        List cids = this.getBoardIDs(pcboards);
        pboard.setChildIDs(cids);
        this.getBoardDAO().saveBoard(pboard);
        this.getBoardCache().remove(pboard.getId());
      }
      if (oldParentID != -1) { //父级版区改变。修正父级版区数据
        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.getBoardCache().remove(board.getId()); //从Cache中清除
      this.getBoardSingleton().init(); //重新载入版区列表 add at 2006.10.26
      return board;
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BbscsException(ex);
    }
  }

  /**
   * 根据ID取得Board对象
   *
   * @param id long
   * @return Board
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  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) {
        //Map m = board.getBoardMaster();
        //m.size();
        //board.getBoardMaster().size();
        //System.out.println(m.size());
        this.getBoardCache().add(board.getId(), board);
      }
    }
    return board;
  }

  /**
   * 根据parentID取得Board对象列表
   *
   * @param pid long
   * @return List
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public List findBoardsByParentID(long pid) {
    return this.getBoardDAO().findBoardsByParentID(pid);
  }

  /**
   * 根据parentID取得Board对象列表
   *
   * @param pid long
   * @param useStat int
   * @param hidden int
   * @param orderType int
   * @return List
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public List findBoardsByParentID(long pid, int useStat, int hidden, int orderType) {
    return this.getBoardDAO().findBoardsByParentID(pid, useStat, hidden, orderType);
  }

  /**
   * 生成版区树
   *
   * @param pid int
   * @param topList List
   * @return List
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public List findBoardsAllTree(long pid, List topList) {
    List l = this.getBoardDAO().findBoardsByParentID(0);
    for (int i = 0; i < l.size(); i++) {
      Board b = (Board) l.get(i);
      topList.add(b);
      //this.findBoardsAllTree(b.getId().longValue(), topList);
      List cl = this.getBoardDAO().findBoardsByParentID(b.getId().longValue());
      for (int j = 0; j < cl.size(); j++) {
        b = (Board) cl.get(j);
        topList.add(b);
      }
    }
    return topList;
  }

  public List findBoardsAllTree(long pid, List topList, int useStat, int hidden, int orderType) {
    List l = this.getBoardDAO().findBoardsByParentID(pid, useStat, hidden, orderType);
    //topList.addAll(l);
    for (int i = 0; i < l.size(); i++) {
      Board b = (Board) l.get(i);
      topList.add(b);
      this.findBoardsAllTree(b.getId().longValue(), topList);
    }
    return topList;

  }

  /**
   * 根据parentID预取得Board序列
   *
   * @param pid long
   * @return int
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public int getNextOrder(long pid) {
    return this.getBoardDAO().getNextOrder(pid);
  }

  /**
   * 取得帖子总数(主帖或全部)
   *
   * @param mainorall int
   * @param useStat int
   * @param hidden int
   * @return int
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public int getPostSumNum(int mainorall, int useStat, int hidden) {
    return this.getBoardDAO().getPostSumNum(mainorall, useStat, hidden);
  }

  /**
   * 删除Board对象
   *
   * @param board Board
   * @throws BbscsException
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public void removeBoard(Board board) throws BbscsException {
    try {
      Long lbid = board.getId();
      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);
        List cids = this.getBoardIDs(pcboards);
        pboard.setChildIDs(cids);
        this.getBoardDAO().saveBoard(pboard);
      }

      this.getBoardCache().remove(lbid);
    }
    catch (Exception ex) {
      logger.error(ex);
      throw new BbscsException(ex);
    }
  }

  /**
   *
   * @param boards List
   * @return List
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public List getBoardIDs(List boards) {
    List l = new ArrayList();
    for (int i = 0; i < boards.size(); i++) {
      Board b = (Board) boards.get(i);
      l.add(b.getId());
    }
    return l;
  }

  /**
   *
   * @param bid long
   * @param groupID int
   * @return Map[]
   * @todo Implement this com.laoer.bbscs.service.BoardService method
   */
  public Map[] getBoardPermission(long bid, int groupID) {
    /*
         Map[] mapPermission = {new HashMap(), new HashMap()};
         BoardPermission bp = this.getBoardPermissionDAO().findBoardPermissionByBidGid(bid, groupID);
         List permissions = bp.getPermissions(); //取得权限ID列表
         if (permissions != null && !permissions.isEmpty()) {
      List permissionList = this.getPermissionDAO().findPermissionnIDs(permissions); //取得权限列表
      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;
     */
    if (Constant.USE_PERMISSION_CACHE) {
      Map[] mapPermission = (Map[])this.getUserPermissionCache().get("BG_" + String.valueOf(bid) + "_" +
          String.valueOf(groupID));
      if (mapPermission == null) {
        mapPermission = new Map[2];
        mapPermission[0] = new HashMap();
        mapPermission[1] = new HashMap();
        BoardPermission bp = this.getBoardPermissionDAO().findBoardPermissionByBidGid(bid, groupID);
        List permissions = bp.getPermissions(); //取得权限ID列表
        if (permissions != null && !permissions.isEmpty()) {
          List permissionList = this.getPermissionDAO().findPermissionnIDs(permissions); //取得权限列表
          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);
            }
          }
        }
        this.getUserPermissionCache().add("BG_" + String.valueOf(bid) + "_" + String.valueOf(groupID),
                                          mapPermission);
      }
      return mapPermission;
    }
    else {
      Map[] mapPermission = {new HashMap(), new HashMap()};
      BoardPermission bp = this.getBoardPermissionDAO().findBoardPermissionByBidGid(bid, groupID);
      List permissions = bp.getPermissions(); //取得权限ID列表
      if (permissions != null && !permissions.isEmpty()) {
        List permissionList = this.getPermissionDAO().findPermissionnIDs(permissions); //取得权限列表
        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);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?