package com.ntsky.bbs.dao.hibernate;

import java.util.List;

import org.springframework.dao.DataAccessException;

import com.ntsky.bbs.domain.Admin;
import com.ntsky.bbs.domain.Forum;
import com.ntsky.bbs.dao.ForumDAO;
import com.ntsky.bbs.exception.DAOException;

/**
 * 论坛信息Hibernate数据处理实现 
 * 
 * @author ntsky
 * @link www.ntsky.com
 * 
 * @version $Revision: 1.12 $ $Date: 2007/04/01 17:18:24 $
 */
public class ForumDAOHibernateImpl extends BaseDAOHibernateImpl implements ForumDAO {
	
	/**
	 * 查找全部的论坛信息
	 * <p>
	 * 	以树行结构显示整个论坛树
	 * </p>
	 * @return List 论坛集合
	 */
	public List findForums () throws DAOException {
		try{
			return super.find("from Forum as forum order by forum.branchId ,forum.displayOrder asc");
		}
		catch(DAOException daoException){
			throw new DAOException("取得整个论坛类别树发生错误...");
		}
	}
	
	/**
	 * 根据父类编号取得论坛列表
	 * @param parentId 父类编号
	 * @return 论坛版块列表
	 * @throws ServiceException
	 */
	public List findForums(int parentId) throws DAOException {
		try{
			return super.find("from Forum as forum where forum.parentId='"+parentId+"'");
		}
		catch(DAOException daoException){
			throw new DAOException("根据parentId : ["+parentId+"]取得整个论坛版块发生错误...");
		}
	}	
	
	/**
	 * 根据论坛编号查找论坛数据
	 * @param forumId 论坛标示
	 * @return Forum 论坛数据
	 */	
	public Forum findForum(int forumId) throws DAOException {
		try{
			return (Forum) super.get(Forum.class,new Long(forumId));
		}
		catch(DAOException daoException){
			throw new DAOException("根据["+forumId+"]取得论坛信息失败.");
		}
	}
	
	/**
	 * 取得最大的分支编号
	 * @return 最大分支编号
	 */
	public int findMaxBranchId() throws DAOException {
		try{			
			Object funValue = super.findByAggregate("select max(forum.branchId) from Forum as forum");
			if(funValue==null){
				return 0;
			}
			else{
				return ((Integer)funValue).intValue();
			}
		}
		catch(DAOException daoException){
			throw new DAOException("取得最大的分支编号错误");
		}
	}	
	
	/**
	 * 取得子层中最大的分支编号
	 * 
	 * @param parentId 父节点编号
	 * @int 取得节点的排序编号
	 */
	public int findChildMaxOrder(int parentId) throws DAOException{
		try{
			Object funValue = super.findByAggregate("select max(forum.displayOrder) from Forum as forum where forum.parentId='"+parentId+"'");
			if(funValue==null){
				return 0;
			}
			else{
				return ((Integer)funValue).intValue();
			}
		}
		catch(DAOException daoException){
			throw new DAOException("取得最大的分支编号错误");
		}
	}
	
	/**
	 * 批量更新显示次序
	 * @param branchId 分支编号
	 * @param displayOrder 显示次序
	 * @throws DAOException 数据处理异常
	 */
	public void batchUpdateDisplayOrder(int branchId,int displayOrder) throws DAOException{
		try{
			super.executeHsql("update Forum set displayOrder=displayOrder+1 where branchId='"+branchId+"' and displayOrder>'"+displayOrder+"'");
		}
		catch(DAOException daoException){
			throw new DAOException("批量更新显示次序发生错误");
		}
	}	
	
	/**
	 * 更新论坛版块序号
	 * 
	 * @param oldBranchId 旧分支编号
	 * @param newbranchId 新分支编号
	 * @throws DAOException
	 */
	public void updateForumBranch(int oldBranchId,int newbranchId) throws DAOException{
		try{
			super.executeHsql("update Forum set branchId='"+newbranchId+"' where branchId='"+oldBranchId+"'");
		}
		catch(DAOException daoException){
			throw new DAOException("更新论坛版块branchId ["+newbranchId+"] 编号发生错误");
		}
	}	

	/**
	 * 查找分支是否存在
	 * @param branchId 分支编号
	 * @throws DAOException
	 */
	public boolean isExistBranch(int branchId) throws DAOException{
		List list = null;
		try{
			if(logger.isDebugEnabled()){
				logger.debug("检索分支["+branchId+"]是否存在.");
			}
			list = super.find("from Forum as forum where forum.branchId=?",new Integer(branchId));
			if(list!=null&&list.size()>0){
				return true;
			}
			else{
				return false;
			}
		}
		catch(DAOException daoException){
			throw new DAOException("判断分支是否存在发生错误.");
		}
	}
	
	/**
	 * 根据父类集合取得子论坛列表
	 * @return 子论坛集合
	 * @throws DAOException
	 */
	public List findForumsByParentEnum(String parentEnum) throws DAOException{
		List list = null;
		try{
			if(logger.isDebugEnabled()){
				logger.debug("检索条件parentEnum为 : "+parentEnum+".");
			}
			list = super.find("from Forum as forum where forum.parentEnum like '"+parentEnum+"%'");
		}
		catch(DAOException daoException){
			throw new DAOException("根据父类集合取得子论坛列表发生错误.");
		}
		return list;
	}
	
}