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

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

import jp.mosp.common.common.MospConst;
import jp.mosp.kintai.payroll.dto.KdSubstituteHolidayDto;

/**
 * @author koike
 *
 */
public class KdSubstituteHolidayDao extends jp.mosp.kintai.payroll.dao.KdSubstituteHolidayDao {
	
	/**
	 * 検索結果取得(DTOのList)。
	 * @return 検索結果リスト
	 * @throws SQLException 	SQL例外が発生した場合
	 */
	@SuppressWarnings("unused")
	private List<KdSubstituteHolidayDto> mappingAll() throws SQLException {
		List<KdSubstituteHolidayDto> all = new ArrayList<KdSubstituteHolidayDto>();
		while (rs.next()) {
			all.add(mapping());
		}
		return all;
	}
	
	/**
	 * 検索結果取得(DTO)。
	 * @return KD_SUBSTITUTE_HOLIDAYレコード
	 * @throws SQLException 	SQL例外が発生した場合
	 */
	private KdSubstituteHolidayDto mapping() throws SQLException {
		KdSubstituteHolidayDto dto = new KdSubstituteHolidayDto();
		dto.setSubstituteId(rs.getLong(COL_SUBSTITUTE_ID));
		dto.setKCode(rs.getString(COL_K_CODE));
		dto.setOccurYear(rs.getInt(COL_OCCUR_YEAR));
		dto.setOccurMonth(rs.getInt(COL_OCCUR_MONTH));
		dto.setCloseDate(rs.getDate(COL_CLOSE_DATE));
		dto.setLimitStartDate(rs.getDate(COL_LIMIT_START_DATE));
		dto.setLimitEndDate(rs.getDate(COL_LIMIT_END_DATE));
		dto.setAttitudeStatus(rs.getString(COL_ATTITUDE_STATUS));
		dto.setPaymentAcquireTime(rs.getInt(COL_PAYMENT_ACQUIRE_TIME));
		dto.setPaymentAcquireHoliday(rs.getInt(COL_PAYMENT_ACQUIRE_HOLIDAY));
		dto.setPaymentQuitTime(rs.getInt(COL_PAYMENT_QUIT_TIME));
		dto.setUseHolidayTime(rs.getInt(COL_USE_HOLIDAY_TIME));
		dto.setAcquisitionStatus(rs.getString(COL_ACQUISITION_STATUS));
		dto.setLiquidationOverTime(rs.getInt(COL_LIQUIDATION_OVER_TIME));
		dto.setLiquidationWeekHoliday(rs.getInt(COL_LIQUIDATION_WEEK_HOLIDAY));
		dto.setDecisionStatus(rs.getString(COL_DECISION_STATUS));
		dto.setDeleteFlag(rs.getInt(COL_DELETE_FLAG));
		mappingCommonInfo(dto);
		return dto;
	}
	
	/**
	 * キーによるデータ取得。
	 * @param kCode 
	 * @param year 
	 * @param month 
	 * @return 検索結果(DTO)
	 * @throws SQLException 	SQL例外が発生した場合
	 * @throws IllegalAccessException 	メソッドにアクセスできない場合
	 * @throws NoSuchFieldException 	フィールドが見つからなかった場合
	 */
	public KdSubstituteHolidayDto findForCondition(String kCode, int year, int month, String status)
			throws SQLException, IllegalAccessException, NoSuchFieldException {
		try {
			index = 1;
			StringBuffer sb = new StringBuffer();
			sb.append("WHERE ");
			sb.append(COL_K_CODE);
			sb.append(" = ?");
			sb.append(" AND ");
			sb.append(COL_OCCUR_YEAR);
			sb.append(" = ? ");
			sb.append(" AND ");
			sb.append(COL_OCCUR_MONTH);
			sb.append(" = ? ");
			sb.append(" AND ");
			sb.append(COL_DECISION_STATUS);
			sb.append(" = ? ");
			sb.append(" AND ");
			sb.append(COL_DELETE_FLAG);
			sb.append(" = ");
			sb.append(MospConst.DELETE_FLAG_OFF);
			
			prepareStatement(getSelectQuery(getClass()) + sb.toString());
			setParam(index++, kCode);
			setParam(index++, year);
			setParam(index++, month);
			setParam(index++, status);
			executeQuery();
			
			KdSubstituteHolidayDto dto = null;
			if (rs.next()) {
				dto = mapping();
			}
			
			return dto;
		} catch (SQLException e) {
			throw e;
		} finally {
			releaseResultSet();
			releasePreparedStatement();
		}
	}
}
