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

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

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

import jp.mosp.kintai.dto.CmRotationDto;

public class CmRotationDao extends BaseDao {

	// テーブル名及びキー列名
	public static final String TABLE	= "CM_ROTATION";
	public static final String KEY_1	= "KCODE";			//社員コード
	public static final String KEY_2	= "RTCODE";			//輪番パターンコード

	// 列名
	public static final String COL_KCODE 			= "KCODE";		//社員コード
	public static final String COL_RTCODE 		= "RTCODE";		//輪番パターンコード
	public static final String COL_RTNAME  		= "RTNAME";		//輪番パターン名称
	public static final String COL_HOLIDAYFLAG  	= "HOLIDAYFLAG";//祝祭日フラグ
	public static final String COL_DAY1  			= "DAY1";		//1日目
	public static final String COL_DAY2  			= "DAY2";		//2日目
	public static final String COL_DAY3  			= "DAY3";		//3日目
	public static final String COL_DAY4  			= "DAY4";		//4日目
	public static final String COL_DAY5  			= "DAY5";		//5日目
	public static final String COL_DAY6  			= "DAY6";		//6日目
	public static final String COL_DAY7  			= "DAY7";		//7日目
	public static final String COL_DAY8  			= "DAY8";		//8日目
	public static final String COL_DAY9  			= "DAY9";		//9日目
	public static final String COL_DAY10  		= "DAY10";		//10日目
	public static final String COL_DELETEFLAG 	= "DELETEFLAG";	//削除フラグ

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

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

	/**
	 * 検索結果取得(DTO)
	 * @return fm_rotationDto fm_rotationレコード
	 * @throws SQLException
	 */
	private CmRotationDto mapping() throws SQLException {
		CmRotationDto dto = new CmRotationDto();
		dto.setKCode		(rs.getString (COL_KCODE));
		dto.setRtCode 		(rs.getString (COL_RTCODE  ));		//輪番パターンコード
		dto.setRtName 		(rs.getString (COL_RTNAME  ));		//輪番パターン名称
		dto.setHolidayFlag 	(rs.getInt    (COL_HOLIDAYFLAG  ));	//祝祭日フラグ
		dto.setDay1 		(rs.getString (COL_DAY1  ));		//1日目
		dto.setDay2 		(rs.getString (COL_DAY2  ));		//2日目
		dto.setDay3		 	(rs.getString (COL_DAY3  ));		//3日目
		dto.setDay4 		(rs.getString (COL_DAY4  ));		//4日目
		dto.setDay5 		(rs.getString (COL_DAY5  ));		//5日目
		dto.setDay6 		(rs.getString (COL_DAY6  ));		//6日目
		dto.setDay7 		(rs.getString (COL_DAY7  ));		//7日目
		dto.setDay8 		(rs.getString (COL_DAY8  ));		//8日目
		dto.setDay9 		(rs.getString (COL_DAY9  ));		//9日目
		dto.setDay10 		(rs.getString (COL_DAY10  ));		//10日目
		dto.setDeleteFlag 	(rs.getInt    (COL_DELETEFLAG  ));	//削除フラグ
		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 rtcode 輪番パターンコード(検索キー)
	 * @return fm_rotationDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmRotationDto findForKey(
			String kcode,
			String rtcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()));
			setParam(index++, kcode);
			setParam(index++, rtcode);
			executeQuery();
			CmRotationDto dto = null;
			if (rs.next()) {
				dto = mapping();
				mappingCommonInfo(dto);
			}
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * キーによる行ロック取得
	 * @param rtcode 輪番パターンコード(検索キー)
	 * @return fm_rotationDto 検索結果(DTO)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public CmRotationDto findForUpdate(
			String kcode,
			String rtcode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			prepareStatement(getSelectQuery(getClass()) + getConditionForKey(getClass()) + getForUpdate());
			setParam(index++, kcode);
			setParam(index++, rtcode);
			executeQuery();
			CmRotationDto 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<CmRotationDto> findForCondition(
			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_DELETEFLAG);
		sb.append(" = " + CommonConst.NAM_COMMON_INTEGER_INITIAL_VALUE);
		sb.append(" ");
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++,   kcode);
			executeQuery();
			List<CmRotationDto> list = mappingAll();
			return list;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * 条件文によるデータ取得
	 * @param kcode		検索社員コード
	 * @param rtcode	検索ルートコード
	 * @param rtname	検索ルート名称
	 * @return list 検索結果(DTOのList)
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List findForCondition(
			String kcode,
			String rtcode,
			String rtname
	) 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_DELETEFLAG);
		sb.append(" = " + CommonConst.NAM_COMMON_INTEGER_INITIAL_VALUE);
		sb.append(" ");
		if (!rtcode.equals("")) {
			sb.append(" AND ");
			sb.append(COL_RTCODE);
			sb.append(" LIKE ? ");
		}
		if (!rtname.equals("")) {
			sb.append(" AND ");
			sb.append(COL_RTNAME);
			sb.append(" LIKE ? ");
		}
		try {
			prepareStatement(getSelectQuery(getClass()) + sb.toString() + getOrderForKey(getClass()));
			setParam(index++, kcode);
			if (!rtcode.equals("")) {
				setParam(index++, rtcode + "%");
			}
			if (!rtname.equals("")) {
				setParam(index++, "%" + rtname + "%");
			}
			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(CmRotationDto 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(CmRotationDto dto) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		try {							
			index = 1;
			prepareStatement(getUpdateQuery(getClass()));
			setParams(dto,false);
			setParam(index++, dto.getKCode());
			setParam(index++, dto.getRtCode());
			executeUpdate();
			chkUpdate(1);
			return cnt;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}

	/**
	 * パラメータ設定
	 * @param dto 更新対象DTO
	 * @throws SQLException
	 */
	private void setParams(CmRotationDto dto,boolean isInsert) throws SQLException {
		setParam(index++, dto.getKCode());		//社員コード		
		setParam(index++, dto.getRtCode());		//輪番パターンコード
		setParam(index++, dto.getRtName());		//輪番パターン名称
		setParam(index++, dto.getHolidayFlag());//祝祭日フラグ
		setParam(index++, dto.getDay1());		//1日目
		setParam(index++, dto.getDay2());		//2日目
		setParam(index++, dto.getDay3());		//3日目
		setParam(index++, dto.getDay4());		//4日目
		setParam(index++, dto.getDay5());		//5日目
		setParam(index++, dto.getDay6());		//6日目
		setParam(index++, dto.getDay7());		//7日目
		setParam(index++, dto.getDay8());		//8日目
		setParam(index++, dto.getDay9());		//9日目
		setParam(index++, dto.getDay10());		//10日目
		setParam(index++, dto.getDeleteFlag());	//削除フラグ
		setCommonParams(isInsert);
	}

}
