/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 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.human.dao;

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

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

import jp.mosp.human.dto.MKojinDto;

public class MKojinDao  extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE  = "M_KOJIN"	;
	public static final String KEY_1 	= "KCODE"	;	//社員コード

	// 列名
	public static final String COL_KCODE  		= "KCODE"		;	//社員コード
	public static final String COL_SEINENBI  		= "SEINENBI"	;	//生年月日
	public static final String COL_SEIBETU  		= "SEIBETU"		;	//性別
	public static final String COL_YUBINBAN  		= "YUBINBAN"	;	//郵便番号
	public static final String COL_PREFECTURES  	= "PREFECTURES"	;	//都道府県コード
	public static final String COL_JYUUSYO1  		= "JYUUSYO1"	;	//住所1
	public static final String COL_JYUUSYO2  		= "JYUUSYO2"	;	//住所2
	public static final String COL_JYUUSYO3  		= "JYUUSYO3"	;	//住所3
	public static final String COL_TEL  			= "TEL"			;	//電話番号
	public static final String COL_FAX  			= "FAX"			;	//FAX番号
	public static final String COL_KEITAI  		= "KEITAI"		;	//携帯番号
	public static final String COL_KETUEKI  		= "KETUEKI"		;	//血液型
	public static final String COL_SIKAKU  		= "SIKAKU"		;	//資格等
	public static final String COL_KENKOU  		= "KENKOU"		;	//健康状態
	public static final String COL_TEKIYOU  		= "TEKIYOU"		;	//摘要

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return m_kojinDto m_kojinレコード
	 * @throws SQLException
	 */
	private MKojinDto mapping() throws SQLException {
		MKojinDto dto = new MKojinDto();
		dto.setKCode 		(rs.getString (COL_KCODE  		));	//社員コード
		dto.setSeinenbi 	(rs.getInt    (COL_SEINENBI  	));	//生年月日
		dto.setSeibetu 		(rs.getString (COL_SEIBETU  	));	//性別
		dto.setYubinban 	(rs.getString (COL_YUBINBAN  	));	//郵便番号
		dto.setPrefectures 	(rs.getInt    (COL_PREFECTURES	));	//都道府県コード
		dto.setJyuusyo1 	(rs.getString (COL_JYUUSYO1  	));	//住所1
		dto.setJyuusyo2 	(rs.getString (COL_JYUUSYO2  	));	//住所2
		dto.setJyuusyo3 	(rs.getString (COL_JYUUSYO3  	));	//住所3
		dto.setTel 			(rs.getString (COL_TEL  		));	//電話番号
		dto.setFax 			(rs.getString (COL_FAX  		));	//Fax番号
		dto.setKeitai 		(rs.getString (COL_KEITAI  		));	//携帯番号
		dto.setKetueki 		(rs.getString (COL_KETUEKI  	));	//血液型
		dto.setSikaku 		(rs.getString (COL_SIKAKU  		));	//資格等
		dto.setKenkou 		(rs.getString (COL_KENKOU  		));	//健康状態
		dto.setTekiyou 		(rs.getString (COL_TEKIYOU  	));	//摘要
		return dto;
	}

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

	/**
	 * キーによるデータ取得
	 * @param kcode 社員コード(検索キー)
	 * @return m_kojinDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MKojinDto findForKey(
			String kcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kcode);
			executeQuery();
			MKojinDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param kcode 社員コード(検索キー)
	 * @return m_kojinDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MKojinDto findForUpdate(
			String kcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kcode);
			executeQuery();
			MKojinDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

	/**
	 * データ挿入
	 * @param dto 挿入対象DTO
	 * @return int 挿入結果(件数)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 * @throws MospException
	 */
	public int insert(MKojinDto 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(MKojinDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto,false);
			setParam(index++, dto.getKCode());
			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(MKojinDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getDeleteQuery(getClass()));
			setParam(index++, dto.getKCode());
			executeUpdate();
			chkDelete(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(MKojinDto dto,boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode());			//社員コード
		setParam(index++, dto.getSeinenbi());		//生年月日
		setParam(index++, dto.getSeibetu());		//性別
		setParam(index++, dto.getYubinban());		//郵便番号
		setParam(index++, dto.getPrefectures());	//都道府県コード
		setParam(index++, dto.getJyuusyo1());		//住所1
		setParam(index++, dto.getJyuusyo2());		//住所2
		setParam(index++, dto.getJyuusyo3());		//住所3
		setParam(index++, dto.getTel());			//電話番号
		setParam(index++, dto.getFax());			//Fax番号
		setParam(index++, dto.getKeitai());			//携帯番号
		setParam(index++, dto.getKetueki());		//血液型
		setParam(index++, dto.getSikaku());			//資格等
		setParam(index++, dto.getKenkou());			//健康状態
		setParam(index++, dto.getTekiyou());		//摘要
		setCommonParams(isInsert);
	}

}
