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

public class LeaveEarlyReasonCardAction extends MosPAction {

	// コマンド
	private static final String CMD_LEAVE_EARLY_REASON_CARD_SHOW   = "S2530";	// 表示
	private static final String CMD_LEAVE_EARLY_REASON_CARD_UPDATE = "S2538";	// 更新

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

	/**
	 * アクション
	 */
	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// PAYROLL権限確認
		confirmSystemAuth();
		// VOの取得
		LeaveEarlyReasonCardVo vo = new LeaveEarlyReasonCardVo();
		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_EARLY_REASON_CARD_SHOW)) {
			// 表示
			show(vo, dao);
			// モード設定
			vo.setMode(LeaveEarlyReasonCardVo.MODE_UPDATE);
		} else if (cmd.equals(CMD_LEAVE_EARLY_REASON_CARD_UPDATE)) {
			// 早退理由更新
			for (int i = 0; i < 10; i++) {
				// 入力値取得
				vo.setParams(request, i);
				// パラメータの妥当性確認
				vo.validateLeaveEarlyReason(i);
				// 早退理由更新
				updateLeaveEarly(vo, dao, i);
			}
			// コミット
			commit();
			// モード設定
			vo.setMode(LeaveEarlyReasonCardVo.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(LeaveEarlyReasonCardVo vo, MCodeDao dao) throws Exception {
		// 早退理由
		vo.setFieldsLeaveEarlyReason(dao.findForCondition(CommonConst.TID_LEAVE_EARLY_REASON));
	}

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

}
