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

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

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

import jp.mosp.payroll.dto.MKintaiKeisanDto;

public class AttendanceCalculateStartCardVo extends AttendanceCalculateMenuVo {

	private static final long serialVersionUID = 930283603344677026L;

	// URL、JSファイル、CSSファイル
	private static final String RETURN_URL = "/jsp/payroll/attendanceCalculateStartCard.jsp";
	private static final String EXTRA_JS   = "/pub/payroll/js/attendanceCalculateStartCard.js";
	private static final String EXTRA_CSS  = "/pub/payroll/css/attendanceCalculateStartCard.css";

	// 画面サブタイトル
	public final String SUB_TITLE = CommonConst.TTL_ATT_CALCULATE_START;

	// 定数
	public static final int MODE_UPDATE = 1;

	// パラメータ名
	public static final String PRM_PLT_KTNEN 		= "pltKTnen";
	public static final String PRM_PLT_KTTUKI 		= "pltKTtuki";

	// フィールド
	private String	pltKTnen;
	private String	pltKTtuki;

	// プルダウンリスト
	public String[][] aryPltKTnen;
	public String[][] aryPltKTtuki;

	// モード(1：更新)
	private int mode;

	// フィールド名及び制限
	public static final String NAM_KT_YYMM_NAME	= CommonConst.NAM_CONFIRM_ATTENDANCE + CommonConst.NAM_CALCULATE_TARGET + CommonConst.NAM_YEAR + CommonConst.NAM_MONTH;
	public static final String NAM_KT_YEAR		= CommonConst.NAM_CONFIRM_ATTENDANCE + CommonConst.NAM_CALCULATE_TARGET + CommonConst.NAM_YEAR;
	public static final String NAM_KT_MONTH		= CommonConst.NAM_CONFIRM_ATTENDANCE + CommonConst.NAM_CALCULATE_TARGET + CommonConst.NAM_MONTH;

	public static final int	LEN_NEN		= CommonConst.LEN_YEAR;
	public static final int	LEN_TUKI	= CommonConst.LEN_MONTH_DAY;

	/**
	 * コンストラクタ
	 */
	public AttendanceCalculateStartCardVo() {
		super();
		setExtraCss(EXTRA_CSS);
		addExtraJs(EXTRA_JS);
		setRetUrl(RETURN_URL);
		mode = MODE_UPDATE;
	}

	/**
	 * 初期値設定
	 */	
	public void setDefaultValues(){
		setPltKTnen("");
		setPltKTtuki("");
	}

	/**
	 * パラメータ設定
	 */
	public void setParams(HttpServletRequest request) {
		pltKTnen  = request.getParameter(PRM_PLT_KTNEN);
		pltKTtuki = request.getParameter(PRM_PLT_KTTUKI);
	}

	/**
	 * フィールド設定
	 * @param dto ユーザーリスト
	 */
	public void setFields(MKintaiKeisanDto dto) {
		pltKTnen 	= String.valueOf(dto.getKTNen());
		pltKTtuki 	= String.valueOf(dto.getKTTuki());
	}

	/**
	 * フィールド設定(DTOに対するVO情報のセット)
	 * @param dto 設定対象DTO
	 */
	public void setDtoFields(MKintaiKeisanDto dto) {
		dto.setKTNen	(Integer.parseInt(pltKTnen)		);
		dto.setKTTuki	(Integer.parseInt(pltKTtuki)	);
		dto.setKKanryou	(CommonConst.NUM_ATTENDANCE_CALCULATE_FLAG_OFF);
	}

	/**
	 * フィールドの妥当性確認
	 * @param dto 対象DTO
	 * @param current 集計年月
	 * @param change 入力年月
	 * @throws MospException
	 */
	public void validate(MKintaiKeisanDto dto, Date current, Date change) throws MospException {
		// 妥当性確認開始時処理
		startValidation();
		// メッセージ用計算年月
		String msgdate = MospUtility.getYear(current) + CommonConst.STR_UNDER_SEPARATOR + MospUtility.getMonth(current);
		// 未更新翌月指定チェック
		if ((dto.getKKanryou().equals(CommonConst.NUM_ATTENDANCE_CALCULATE_FLAG_OFF))) {
			// 例）dto 200807 < vo 200808
			// 指定年月の計算が終わっていないのに翌月以降の計算を開始しようとした為
			if (current.compareTo(change) < 0) {
				String errMsg = MospUtility.getMessage(msg, CommonConst.MSG_CANNOT_FUTURE_MONTH, msgdate);
				addErrMessage(errMsg);
				isValid = false;
			}
		} else {
			// 計算完了チェック
			if (current.compareTo(change) == 0) {
				String errMsg = MospUtility.getMessage(msg, CommonConst.MSG_ALREADY_CALCULATE, msgdate);
				addErrMessage(errMsg);
				isValid = false;
			}
		}		
		// 過去への戻りチェック
		if (current.compareTo(change) > 0) {
			String errMsg = MospUtility.getMessage(msg, CommonConst.MSG_CANNOT_PAST_MONTH, msgdate);
			addErrMessage(errMsg);
			isValid = false;			
		}
		// 妥当性確認終了時処理
		endValidation();
	}

	/**
	 * JavaScript設定
	 * @param cls 情報取得クラス
	 * @throws IllegalAccessException
	 */
	@Override
	public String getDirectJs(Class cls) throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDirectJs(cls));
		sb.append("var MODE = ");
		sb.append(mode);
		sb.append(";");
		return sb.toString();
	}

	// アクセサメソッド
	public String getPltKTnen() 		{ return pltKTnen		; }
	public String getPltKTtuki() 	    { return pltKTtuki  	; }

	public String getEscTxtKTnen() 		{ return escapeHTML(pltKTnen	); }
	public String getEscTxtKTtuki() 	{ return escapeHTML(pltKTtuki	); }

	public void setPltKTnen	(String pltKTnen	) 	{ this.pltKTnen 	= pltKTnen	; }
	public void setPltKTtuki	(String pltKTtuki	)   { this.pltKTtuki 	= pltKTtuki	; }

	public void setMode(int mode) { this.mode = mode; }
	public int getMode() { return mode; }

}
