/**
 * ÀÌ ¼Ò½º´Â Spring ÇÁ·¹ÀÓ¿öÅ© ¿öÅ©ºÏ¿¡¼­ »ç¿ëÇÑ ¿¹Á¦ ¼Ò½ºÀÔ´Ï´Ù. 
 * ÀÌ ¼Ò½º´Â ¸ðµç °³¹ßÀÚµéÀÌ ÀÚÀ¯·Ó°Ô ¼öÁ¤ ¹× ¹èÆ÷ÇÒ ¼ö ÀÖ½À´Ï´Ù. 
 * ´Ü, ÀÌ ¼Ò½º¸¦ ±â¹ÝÀ¸·Î »õ·Î¿î ¾ÖÇÃ¸®ÄÉÀÌ¼ÇÀ» °³¹ßÇÒ °æ¿ì ÃâÃ³¸¦ ¸í½ÃÇØ ÁÖ½Ã¸é µË´Ï´Ù. 
 */
package net.javajigi.board.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import net.javajigi.board.model.BoardFile;
import net.javajigi.common.dao.MyJdbcDaoSupport;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;

public class SpringJDBCWithTemplateBoardFileDAO extends MyJdbcDaoSupport
		implements BoardFileDAO {

	public int insert(final int boardNo, final List boardFileList)
			throws DataAccessException {
		String sql = getMessageSourceAccessor().getMessage(
				"boardfile.sql.insert");

		BatchPreparedStatementSetter bpsSetter = new BatchPreparedStatementSetter() {
			public void setValues(PreparedStatement ps, int i)
					throws SQLException {
				BoardFile boardFile = (BoardFile) boardFileList.get(i);
				int boardFileNo = getIncrementer().nextIntValue();
				ps.setInt(1, boardFileNo);
				ps.setInt(2, boardNo);
				ps.setInt(3, boardFile.getFileSize());
				ps.setString(4, boardFile.getFileName());
				ps.setString(5, boardFile.getContentType());
				ps.setString(6, boardFile.getTempFileName());
			}

			public int getBatchSize() {
				return boardFileList.size();
			}
		};

		int result[] = getJdbcTemplate().batchUpdate(sql, bpsSetter);

		int insertResult = 0;
		for (int i = 0; i < result.length; i++) {
			insertResult += result[i];
		}
		return insertResult;
	}

	public int deleteByBoardNo(int boardNo) throws DataAccessException {
		String sql = getMessageSourceAccessor().getMessage(
				"boardfile.sql.delete.byboardno");

		return getJdbcTemplate().update(sql,
				new Object[] { new Integer(boardNo) });
	}

	public int deleteByBoardFileNo(int boardFileNo) throws DataAccessException {
		String sql = getMessageSourceAccessor().getMessage(
				"boardfile.sql.delete.byboardfileno");

		return getJdbcTemplate().update(sql,
				new Object[] { new Integer(boardFileNo) });
	}

	public BoardFile findBoardFile(int boardFileNo) throws DataAccessException {
		String sql = getMessageSourceAccessor().getMessage(
				"boardfile.sql.select.byboardfileno");

		RowMapper rowMapper = new RowMapper() {
			public Object mapRow(ResultSet rs, int rownum) throws SQLException {
				BoardFile boardFile = new BoardFile();
				boardFile.setFileNo(rs.getInt("fileNo"));
				boardFile.setFileSize(rs.getInt("fileSize"));
				boardFile.setFileName(rs.getString("fileName"));
				boardFile.setContentType(rs.getString("contentType"));
				boardFile.setTempFileName(rs.getString("tempFileName"));

				return boardFile;
			}
		};

		BoardFile boardFile = (BoardFile) getJdbcTemplate().queryForObject(sql,
				new Object[] { new Integer(boardFileNo) }, rowMapper);

		return boardFile;
	}

	public List findBoardFileList(int boardNo) throws DataAccessException {
		String sql = getMessageSourceAccessor().getMessage(
				"boardfile.sql.select.byboardno");

		RowMapper rowMapper = new RowMapper() {
			public Object mapRow(ResultSet rs, int rownum) throws SQLException {
				BoardFile boardFile = new BoardFile();
				boardFile.setFileNo(rs.getInt("fileNo"));
				boardFile.setFileSize(rs.getInt("fileSize"));
				boardFile.setFileName(rs.getString("fileName"));

				return boardFile;
			}
		};

		List boardFileList = getJdbcTemplate().query(sql,
				new Object[] { new Integer(boardNo) }, rowMapper);

		return boardFileList;
	}
}
