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

import jp.mosp.common.CommonConst;
import jp.mosp.common.action.MosPAction;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.system.dao.MCodeDao;
import jp.mosp.system.dto.MCodeDto;
import jp.mosp.system.vo.LeaveReasonCardVo;

public class LeaveReasonCardAction extends MosPAction {

	// コマンド
	private static final String CMD_LEAVE_REASON_CARD_SHOW   = "S2510";	// 表示
	private static final String CMD_LEAVE_REASON_CARD_UPDATE = "S2518";	// 更新

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

	/**
	 * アクション
	 */
	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// PAYROLL権限確認
		confirmSystemAuth();
		// VOの取得
		LeaveReasonCardVo vo = new LeaveReasonCardVo();
		vo.initVo(cfg, msg, cmd, aspUser, user);
		// VO及びフォワード先URLの設定
		setVo(vo);
		setUrl(vo.getRetUrl());
		// DAOの準備
		MCodeDao dao = new MCodeDao();
		dao.initDao(cfg, cmd, aspUser, user, conn);
		// コマンド毎の処理
		if (cmd.equals(CMD_LEAVE_REASON_CARD_SHOW)) {
			// 表示
			show(vo, dao);
			vo.setMode(LeaveReasonCardVo.MODE_UPDATE);
		} else if (cmd.equals(CMD_LEAVE_REASON_CARD_UPDATE)) {
			// 休暇理由更新
			for (int i = 0; i < 10; i++) {
				// 入力値取得
				vo.setParams(request, i);
				// パラメータの妥当性確認
				vo.validateLeaveReason(i);
				// 休暇理由更新
				updateLeave(vo, dao, i);
			}
			// コミット
			commit();
			// モード設定
			vo.setMode(LeaveReasonCardVo.MODE_UPDATE);
			// メッセージ設定
			vo.setMessage(MospUtility.getMessage(msg, MospConst.MSG_UPDATE, ""));
			show(vo, dao);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}

	/**
	 * 休暇理由検索
	 * @param vo 対象Voインスタンス
	 * @throws Exception
	 */
	private void show(LeaveReasonCardVo vo, MCodeDao dao) throws Exception {
		// 早退理由
		vo.setFieldsLeaveReason(dao.findForCondition(CommonConst.TID_LEAVE_REASON));
	}

	/**
	 * データ更新（休暇理由）
	 * @param vo 対象VO
	 * @param dto 対象DTO
	 * @throws Exception
	 */
	private void updateLeave(LeaveReasonCardVo vo, MCodeDao dao, int i) throws Exception {
		// DTOの準備
		MCodeDto dto = new MCodeDto();
		// DTOの準備
		vo.setDtoFieldLeaveReason(dto, i);
		// 排他確認
		chkExclusive(dao.findForKey(dto.getTId(), dto.getTCode()), dao.findForUpdate(dto.getTId(), dto.getTCode()), vo);
		// データの更新
		dao.update(dto);
		// DTOの再取得
		MCodeDto retDto = dao.findForKey(dto.getTId(), dto.getTCode());
		chkUpdate(retDto);
	}

}
