/*
 * 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.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.MEmergencyDto;

public class MEmergencyDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE  = "M_EMERGENCY"	;
	public static final String KEY_1 	= "EMCODE"		;	//連番

	// 列名
	public static final String COL_EMCODE  		= "EMCODE"		;	//連番
	public static final String COL_KCODE  		= "KCODE"		;	//社員コード
	public static final String COL_NAME  			= "NAME"		;	//連絡先氏名
	public static final String COL_PHONE  		= "PHONE"		;	//電話番号
	public static final String COL_CELL  			= "CELL"		;	//携帯番号
	public static final String COL_POSTALCODE  	= "POSTALCODE"	;	//郵便番号
	public static final String COL_PREFECTURES  	= "PREFECTURES"	;	//都道府県コード
	public static final String COL_JYUUSYO1  		= "JYUUSYO1"	;	//住所１
	public static final String COL_JYUUSYO2  		= "JYUUSYO2"	;	//住所２
	public static final String COL_JYUUSYO3  		= "JYUUSYO3"	;	//住所３
	public static final String COL_DELETE_FLAG  	= "DELETEFLAG"	;	//削除フラグ

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return m_emergencyDto m_emergencyレコード
	 * @throws SQLException
	 */
	private MEmergencyDto mapping() throws SQLException {
		MEmergencyDto dto = new MEmergencyDto();
		dto.setEmCode 		(rs.getInt    (COL_EMCODE		));	//連番
		dto.setKCode 		(rs.getString (COL_KCODE		));	//社員コード
		dto.setName 		(rs.getString (COL_NAME			));	//連絡先氏名
		dto.setPhone 		(rs.getString (COL_PHONE		));	//電話番号
		dto.setCell 		(rs.getString (COL_CELL			));	//携帯番号
		dto.setPostalCode 	(rs.getString (COL_POSTALCODE	));	//郵便番号
		dto.setPrefectures 	(rs.getInt    (COL_PREFECTURES	));	//都道府県コード
		dto.setJyuusyo1 	(rs.getString (COL_JYUUSYO1		));	//住所１
		dto.setJyuusyo2 	(rs.getString (COL_JYUUSYO2		));	//住所２
		dto.setJyuusyo3 	(rs.getString (COL_JYUUSYO3		));	//住所３
		dto.setDeleteFlag 	(rs.getInt    (COL_DELETE_FLAG	));	//削除フラグ
		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 emcode 連番(検索キー)
	 * @return m_emergencyDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MEmergencyDto findForKey(
			String emcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, emcode);
			executeQuery();
			MEmergencyDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

	/**
	 * 条件文による行ロック取得
	 * @param EmCode	コード
	 * @param KCode		社員コード
	 * @return dto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public MEmergencyDto findForConditionUp(
			String EmCode,
			String KCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_EMCODE);
		sb.append(" LIKE ? AND ");
		sb.append(COL_KCODE);
		sb.append(" LIKE ?  ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass())+ getForUpdate());
			setParam(index++, EmCode + "%");
			setParam(index++, "%" + KCode + "%" );
			executeQuery();
			MEmergencyDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}		
	}

	/**
	 * 条件文によるデータ取得
	 * @param EmCode	コード
	 * @param KCode		社員コード
	 * @return dto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public MEmergencyDto findForCondition(
			String EmCode,
			String KCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_EMCODE);
		sb.append(" = ? AND ");
		sb.append(COL_KCODE);
		sb.append(" LIKE ?  ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, Integer.parseInt(EmCode));
			setParam(index++, "%" + KCode + "%" );
			executeQuery();
			MEmergencyDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}		
	}

	/**
	 * 条件文によるデータ取得
	 * @param KCode		社員コード
	 * @return dto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 */
	public MEmergencyDto findForConditionUpdate(
			String KCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_KCODE);
		sb.append(" = ?  ");
		sb.append(" AND ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = 0 ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getForUpdate());
			setParam(index++, KCode);
			executeQuery();
			MEmergencyDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}		
	}

	/**
	 * 条件文によるデータ取得
	 * @param emcode 検索連番
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			int emcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append("WHERE ");
		sb.append(COL_EMCODE);
		sb.append(" LIKE ? ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   emcode  );
			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(MEmergencyDto 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(MEmergencyDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto,false);
			setParam(index++, dto.getEmCode());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * max値取得
	 * @param  Kcode 社員コード(検索キー)
	 * @return String　最大連番
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public String findForMax(String Kcode) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			String maxEno = "";
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("SELECT MAX(");
			sb.append(COL_EMCODE);
			sb.append(") FROM ");
			sb.append(TABLE);
			sb.append(" WHERE ");
			sb.append(COL_KCODE);
			sb.append(" = ? ");
			prepareStatement(sb.toString());
			setParam(index++, Kcode);
			executeQuery();
			if (rs.next()) {
				maxEno = String.valueOf(rs.getInt(1));
			}
			return maxEno;
		} 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_EMCODE);
			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();
		}
	}

	/**
	 * データ取得
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findAllDelflag() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {

			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = 0 ");
			prepareStatement(getSelectQuery(getClass()) + sb + getOrderForKey(getClass()));
			executeQuery();
			List all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(MEmergencyDto dto,boolean isInsert) throws SQLException {
		setParam(index++, dto.getEmCode		());	//連番
		setParam(index++, dto.getKCode		());	//社員コード
		setParam(index++, dto.getName		());	//連絡先氏名
		setParam(index++, dto.getPhone		());	//電話番号
		setParam(index++, dto.getCell		());	//携帯番号
		setParam(index++, dto.getPostalCode	());	//郵便番号
		setParam(index++, dto.getPrefectures());	//都道府県コード
		setParam(index++, dto.getJyuusyo1	());	//住所１
		setParam(index++, dto.getJyuusyo2	());	//住所２
		setParam(index++, dto.getJyuusyo3	());	//住所３
		setParam(index++, dto.getDeleteFlag	());	//削除フラグ
		setCommonParams(isInsert);
	}

}
