/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2008 MIND Co., Ltd.  http://www.e-mind.co.jp/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package jp.mosp.common.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import jp.mosp.common.dto.CtHolidayRequestDto;
import jp.mosp.common.common.BaseDao;
import jp.mosp.common.common.MospException;

public class CtHolidayRequestDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE 	= "CT_HOLIDAYREQUEST"	;
	public static final String KEY_1 	= "VOUCHER"				;	//休暇申請伝票番号

	// 列名
	public static final String COL_VOUCHER				= "VOUCHER"			;	//休暇申請伝票番号
	public static final String COL_WORKFLOW			= "WORKFLOW"		;	//ワークフロー番号
	public static final String COL_K_CODE				= "KCODE"			;	//社員コード
	public static final String COL_REQUEST_DATE		= "REQUESTDATE"		;	//申請日
	public static final String COL_REASON				= "REASON"			;	//申請理由
	public static final String COL_SUBSTITUTE_DATE		= "SUBSTITUTEDATE"	;	//充当日
	public static final String COL_SUBSTITUTE_WORK		= "SUBSTITUTEWORK"	;	//充当勤務形態
	public static final String COL_DELETE_FLAG			= "DELETEFLAG"		;	//削除フラグ

	/**
	 * コンストラクタ
	 */
	public CtHolidayRequestDao() {
		super();
	}

	/**
	 * 検索結果取得(DTOのList)
	 * @return List 検索リスト
	 * @throws SQLException
	 */
	private List<CtHolidayRequestDto> mappingAll() throws SQLException {
		List<CtHolidayRequestDto> all = new ArrayList<CtHolidayRequestDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}

	/**
	 * 検索結果取得(DTO)
	 * @return CtHolidayrequestDto ct_holidayrequestレコード
	 * @throws SQLException
	 */
	private CtHolidayRequestDto mapping() throws SQLException {
		CtHolidayRequestDto dto = new CtHolidayRequestDto();
		dto.setVoucher			(rs.getInt		(COL_VOUCHER)			);		//休暇申請伝票番号
		dto.setWorkflow			(rs.getLong		(COL_WORKFLOW)			);		//ワークフロー番号
		dto.setKCode			(rs.getString	(COL_K_CODE)			);		//社員コード
		dto.setRequestDate		(rs.getString	(COL_REQUEST_DATE)		);		//申請日
		dto.setReason			(rs.getString	(COL_REASON)			);		//申請理由
		dto.setSubstituteDate	(rs.getString	(COL_SUBSTITUTE_DATE)	);		//充当日
		dto.setSubstituteWork	(rs.getString	(COL_SUBSTITUTE_WORK)	);		//充当勤務形態
		dto.setDeleteFlag		(rs.getInt		(COL_DELETE_FLAG)		);		//削除フラグ
		return dto;
	}

	/**
	 * 全データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<CtHolidayRequestDto> findAll() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			prepareStatement(getSelectQuery(getClass()) + getOrderForKey(getClass()));
			executeQuery();
			List<CtHolidayRequestDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによるデータ取得
	 * @param voucher 休暇申請伝票番号(検索キー)
	 * @return CtHolidayrequestDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtHolidayRequestDto findForKey(
			int     voucher
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, voucher);
			executeQuery();
			CtHolidayRequestDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param voucher 休暇申請伝票番号(検索キー)
	 * @return CtHolidayrequestDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CtHolidayRequestDto findForUpdate(
			String voucher
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, voucher);
			executeQuery();
			CtHolidayRequestDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param voucher 検索休暇申請伝票番号
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			String voucher
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_VOUCHER);
		sb.append(" = ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   voucher  );
			executeQuery();
			List list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	public List<CtHolidayRequestDto> findForKCode(
			String kCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_K_CODE);
		sb.append(" = ? ");
		sb.append(" AND ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = 0 ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   kCode  );
			executeQuery();
			List<CtHolidayRequestDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	public CtHolidayRequestDto findForWorkFlow(
			long     workFlow
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append(" WHERE ");
			sb.append(COL_WORKFLOW);
			sb.append(" = ? ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, workFlow);
			executeQuery();
			CtHolidayRequestDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * max値取得
	 * @return String　最大連番
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public String findForMax() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			String maxEno = "";
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT MAX(");
			sb.append(COL_VOUCHER);
			sb.append(") FROM ");
			sb.append(TABLE);
			prepareStatement(sb.toString());
			executeQuery();
			if (rs.next()) {
				maxEno = String.valueOf(rs.getInt(1));
			}
			return maxEno;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(CtHolidayRequestDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getInsertQuery(getClass()));
			setParams(dto, true);
			executeUpdate();
			chkInsert(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ更新
	 * @param dto 更新対象DTO
	 * @return int 更新結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int update(CtHolidayRequestDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getVoucher());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * データ削除
	 * @param dto 更新対象DTO
	 * @return int 削除結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int delete(CtHolidayRequestDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getVoucher());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(CtHolidayRequestDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getVoucher());		//休暇申請伝票番号
		setParam(index++, dto.getWorkflow());		//ワークフロー番号
		setParam(index++, dto.getKCode());			//社員コード
		setParam(index++, dto.getRequestDate());	//申請日
		setParam(index++, dto.getReason());			//申請理由
		setParam(index++, dto.getSubstituteDate());	//充当日
		setParam(index++, dto.getSubstituteWork());	//充当勤務形態
		setParam(index++, dto.getDeleteFlag());		//削除フラグ
		setCommonParams(isInsert);
	}

}
