/**
 * ÀÌ ¼Ò½º´Â Spring ÇÁ·¹ÀÓ¿öÅ© ¿öÅ©ºÏ¿¡¼­ »ç¿ëÇÑ ¿¹Á¦ ¼Ò½ºÀÔ´Ï´Ù. 
 * ÀÌ ¼Ò½º´Â ¸ðµç °³¹ßÀÚµéÀÌ ÀÚÀ¯·Ó°Ô ¼öÁ¤ ¹× ¹èÆ÷ÇÒ ¼ö ÀÖ½À´Ï´Ù. 
 * ´Ü, ÀÌ ¼Ò½º¸¦ ±â¹ÝÀ¸·Î »õ·Î¿î ¾ÖÇÃ¸®ÄÉÀÌ¼ÇÀ» °³¹ßÇÒ °æ¿ì ÃâÃ³¸¦ ¸í½ÃÇØ ÁÖ½Ã¸é µË´Ï´Ù. 
 */
package net.javajigi.board.dao;

import java.util.Iterator;
import java.util.List;

import net.javajigi.board.model.BoardFile;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class BoardFileDAOHibernate extends HibernateDaoSupport implements
		BoardFileDAO {

	public int insert(int boardNo, List boardFileList)
			throws DataAccessException {
		Iterator boardFileIter = boardFileList.iterator();
		while (boardFileIter.hasNext()) {
			BoardFile boardFile = (BoardFile) boardFileIter.next();
			boardFile.setBoardNo(boardNo);

			getHibernateTemplate().save(boardFile);
		}

		return boardFileList.size();
	}

	public int deleteByBoardNo(int boardNo) throws DataAccessException {
		List boardFileList = findBoardFileList(boardNo);
		getHibernateTemplate().deleteAll(boardFileList);

		return boardFileList.size();
	}

	public int deleteByBoardFileNo(int boardFileNo) throws DataAccessException {
		BoardFile boardFile = findBoardFile(boardFileNo);
		if( boardFile != null ) {
			getHibernateTemplate().delete(findBoardFile(boardFileNo));
			
			return 1;
		} else {
			return 0;
		}
	}

	public BoardFile findBoardFile(int boardFileNo) throws DataAccessException {
		BoardFile boardFile = (BoardFile) getHibernateTemplate().get(
				BoardFile.class, new Integer(boardFileNo));

		return boardFile;
	}

	public List findBoardFileList(int boardNo) throws DataAccessException {
		return getHibernateTemplate().find(
				"from BoardFile as boardFile where boardFile.boardNo = ?",
				new Object[] { new Integer(boardNo) });
	}
}
