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

import jp.mosp.common.CommonConst;
import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.utils.StringUtil;
import jp.mosp.kintai.common.action.MosPCodeAction;
import jp.mosp.kintai.system.vo.YukyuInformationCardVo;

/**
 * @author yoshida
 *
 */
public class YukyuInformationCardAction extends MosPCodeAction {
	
	// コマンド
	private static final String	CMD_YUKYU_INFO_SHOW		= "S2700";	//表示
	private static final String	CMD_YUKYU_INFO_UPDATE	= "S2708";	//更新
																	
	
	/**
	 * コンストラクタ
	 */
	public YukyuInformationCardAction() {
		super();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得及び設定
		prepareVo();
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_YUKYU_INFO_SHOW)) {
			// 表示
			show();
		} else if (cmd.equals(CMD_YUKYU_INFO_UPDATE)) {
			// 繰越更新、条件更新
			regist();
			// 表示
			show();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new YukyuInformationCardVo();
	}
	
	@Override
	protected void init() throws Exception {
		// DBコネクション取得
		getConnection();
		// システム権限確認
		confirmSystemAuth();
		YukyuInformationCardVo vo = (YukyuInformationCardVo)getVo();
		// プルダウンリスト作成
		vo.aryPltCFMonth = StringUtil.getMonthArray();
		// 繰越区分
		vo.aryPltCFKbn = new String[][]{ { CommonConst.NAM_COMMON_FLAG_OFF, CommonConst.NAM_CARRY_OVER_THIS_YEAR },
			{ CommonConst.NAM_COMMON_FLAG_ON, CommonConst.NAM_CARRY_OVER_TOTAL } };
		// 支給方法
		vo.aryPltSupplyModel = new String[][]{ { CommonConst.NAM_COMMON_FLAG_OFF, CommonConst.NAM_STANDARD_MONTH },
			{ CommonConst.NAM_COMMON_FLAG_ON, CommonConst.NAM_EMPLOY_MONTH } };
	}
	
	@Override
	protected void show() throws Exception {
		YukyuInformationCardVo vo = (YukyuInformationCardVo)getVo();
		// 有給繰越情報 
		String[] aryPaidHolidayInfo = getPaidHolidayInfo();
		// 繰越月
		vo.setPltCFMonth(aryPaidHolidayInfo[0]);
		// 繰越日数
		vo.setTxtCFDays(aryPaidHolidayInfo[1]);
		// 繰越区分
		vo.setPltCFKbn(aryPaidHolidayInfo[2]);
		// 支給方法
		vo.setPltSupplyModel(findTchange(CommonConst.TID_PAID_HOLIDAY, CommonConst.TCD_SUPPLY_MODEL));
		// 有給休暇支給	
		vo.setDefaultValues();
		vo.setFields(part().mospcode().getPaidHolidayList());
	}
	
	/**
	 * データ更新（繰越項目）
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void regist() throws Exception {
		YukyuInformationCardVo vo = (YukyuInformationCardVo)getVo();
		vo.setParams(request);
		// パラメータの妥当性確認
		vo.validate();
		// 繰越月
		regist(CommonConst.TID_PAID_HOLIDAY, CommonConst.NAM_CARRIED_FORWARD + CommonConst.NAM_MONTH, "", vo
			.getPltCFMonth());
		// 繰越日数
		regist(CommonConst.TID_PAID_HOLIDAY, CommonConst.TCD_CARRIED_FORWARD_DAYS, "", vo.getTxtCFDays());
		// 繰越区分
		regist(CommonConst.TID_PAID_HOLIDAY, CommonConst.TCD_CARRIED_FORWARD_TYPE, CommonConst.TNM_CARRY_OVER, vo
			.getPltCFKbn());
		// 支給方法
		regist(CommonConst.TID_PAID_HOLIDAY, CommonConst.TCD_SUPPLY_MODEL, "", vo.getPltSupplyModel());
		// データ更新(支給条件)
		// テーブルロック
		part().mospcode().getMCodeDao().tableLock(CommonConst.TID_PAID_HOLIDAY_SUPPLY);
		// 削除
		part().mospcode().getMCodeDao().delete(CommonConst.TID_PAID_HOLIDAY_SUPPLY);
		String[] aryStandard = vo.getAryTxtStandard();
		String[] aryInfo = vo.getAryTxtInfo();
		String[] aryDays = vo.getAryTxtDays();
		for (int i = 0; i < aryStandard.length; i++) {
			if (StringUtil.isNotNull(aryStandard[i])) {
				// 更新処理
				regist(CommonConst.TID_PAID_HOLIDAY_SUPPLY, StringUtil.addZero(aryStandard[i]), aryInfo[i], aryDays[i]);
			}
		}
		// コミット
		commit();
		// メッセージ設定
		vo.setMessage(getMessage(MospConst.MSG_UPDATE, ""));
	}
	
}
