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

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

import jp.mosp.system.dto.MWorkTypeDto;

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

public class MWorkTypeDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE	= "CM_WORKTYPE"	;
	public static final String KEY_1	= "SHIFTCODE"	;	//勤務形態コード

	// 列名
	public static final String COL_SHIFTCODE		= "SHIFTCODE"	;	//勤務形態コード
	public static final String COL_SHIFTNAME		= "SHIFTNAME"	;	//勤務形態名称
	public static final String COL_STARTTIME		= "STARTTIME"	;	//出勤時刻
	public static final String COL_QUITTIME		= "QUITTIME"	;	//退社時刻
	public static final String COL_WORKTIME		= "WORKTIME"	;	//勤務時間
	public static final String COL_BREAKTIME 		= "BREAKTIME"	;	//休憩時間
	public static final String COL_OVERSTART		= "OVERSTART"	;	//残業開始時刻
	public static final String COL_OVERBREAK		= "OVERBREAK"	;	//残業休憩時間
	public static final String COL_OPTION1		= "OPTION1"		;	//オプション1
	public static final String COL_OPTION2		= "OPTION2"		;	//オプション2
	public static final String COL_OPTION3		= "OPTION3"		;	//オプション3
	public static final String COL_OPTION4		= "OPTION4"		;	//オプション4
	public static final String COL_OPTION5		= "OPTION5"		;	//オプション5
	public static final String COL_DELETE_FLAG	= "DELETEFLAG"	;	//削除フラグ

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return cm_worktypeDto cm_worktypeレコード
	 * @throws SQLException
	 */
	private MWorkTypeDto mapping() throws SQLException {
		MWorkTypeDto dto = new MWorkTypeDto();
		dto.setShiftCode	(rs.getString	(COL_SHIFTCODE)	);	//勤務形態コード
		dto.setShiftName	(rs.getString	(COL_SHIFTNAME)	);	//勤務形態名称
		dto.setStartTime	(rs.getInt		(COL_STARTTIME)	);	//出勤時刻
		dto.setQuitTime		(rs.getInt		(COL_QUITTIME)	);	//退社時刻
		dto.setWorkTime		(rs.getInt		(COL_WORKTIME)	);	//勤務時間
		dto.setBreakTime	(rs.getInt		(COL_BREAKTIME)	);	//休憩時間
		dto.setOverStart	(rs.getInt		(COL_OVERSTART)	);	//残業開始時刻
		dto.setOverBreak	(rs.getInt		(COL_OVERBREAK)	);	//残業休憩時間
		dto.setOption1		(rs.getString	(COL_OPTION1)	);	//オプション1
		dto.setOption2		(rs.getString	(COL_OPTION2)	);	//オプション2
		dto.setOption3		(rs.getString	(COL_OPTION3)	);	//オプション3
		dto.setOption4		(rs.getString	(COL_OPTION4)	);	//オプション4
		dto.setOption5		(rs.getString	(COL_OPTION5)	);	//オプション5
		dto.setDeleteFlag	(rs.getInt		(COL_DELETE_FLAG));	//削除フラグ
		return dto;
	}

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

	/**
	 * 全データ取得（有効のみ）
	 * @return List 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MWorkTypeDto> findAvailable() throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			StringBuffer sb = new StringBuffer();
			sb.append(" WHERE ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = " + MospConst.DELETE_FLAG_OFF + " ");
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			executeQuery();
			List<MWorkTypeDto> all = mappingAll();
			return all;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

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

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

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

	/**
	 * 条件文によるデータ取得
	 * @param shiftCode 検索勤務形態コード
	 * @param shiftName 検索勤務形態名称
	 * @return list 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			String shiftCode,
			String shiftName
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_DELETE_FLAG);
		sb.append(" = " + MospConst.DELETE_FLAG_OFF + " ");
		if (!shiftCode.equals("")) {
			sb.append(" AND ");
			sb.append(COL_SHIFTCODE);
			sb.append(" LIKE ? ");
		}
		if (!shiftName.equals("")) {
			sb.append(" AND ");
			sb.append(COL_SHIFTNAME);
			sb.append(" LIKE ? ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			if (!shiftCode.equals("")) {
				setParam(index++, shiftCode + "%");
			}
			if (!shiftName.equals("")) {
				setParam(index++, "%" + shiftName + "%");
			}
			executeQuery();
			List list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param shiftCode 	検索勤務形態コード
	 * @param shiftName		検索勤務形態名称
	 * @param deleteFlag	検索削除フラグ
	 * @return list 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			String shiftCode,
			String shiftName,
			String deleteFlag
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		index = 1;
		StringBuffer sb = new StringBuffer();
		sb.append(" WHERE ");
		sb.append(COL_SHIFTCODE);
		sb.append(" LIKE ? ");
		sb.append(" AND ");
		sb.append(COL_SHIFTNAME);
		sb.append(" LIKE ? ");
		if (!deleteFlag.equals("")) {
			sb.append(" AND ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = ? ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, shiftCode + "%");
			setParam(index++, "%" + shiftName + "%");
			if (!deleteFlag.equals("")) {
				setParam(index++, Integer.parseInt(deleteFlag));
			}
			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(MWorkTypeDto 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(MWorkTypeDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto, false);
			setParam(index++, dto.getShiftCode());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(MWorkTypeDto dto, boolean isInsert) throws SQLException {
		setParam(index++, dto.getShiftCode());	//勤務形態コード
		setParam(index++, dto.getShiftName());	//勤務形態名称
		setParam(index++, dto.getStartTime());	//出勤時刻
		setParam(index++, dto.getQuitTime());	//退社時刻
		setParam(index++, dto.getWorkTime());	//勤務時間
		setParam(index++, dto.getBreakTime());	//休憩時間
		setParam(index++, dto.getOverStart());	//残業開始時刻
		setParam(index++, dto.getOverBreak());	//残業休憩時間
		setParam(index++, dto.getOption1());	//オプション1
		setParam(index++, dto.getOption2());	//オプション2
		setParam(index++, dto.getOption3());	//オプション3
		setParam(index++, dto.getOption4());	//オプション4
		setParam(index++, dto.getOption5());	//オプション5
		setParam(index++, dto.getDeleteFlag());	//削除フラグ
		setCommonParams(isInsert);
	}

}
