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

import java.util.Date;

import jp.mosp.common.MosPAction;
import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.utils.DateUtil;
import jp.mosp.mospcommon.system.LaborAgreementManager;
import jp.mosp.mospcommon.system.dao.CmLaborAgreementDao;
import jp.mosp.mospcommon.system.dto.CmLaborAgreementDto;
import jp.mosp.mospcommon.system.vo.LaborAgreementCardVo;

/**
 * @author matsusita
 *
 */
public class LaborAgreementCardAction extends MosPAction {
	
	// コマンド
	private static final String	CMD_AGREEMENT_CARD_SHOW		= "S2515";
	private static final String	CMD_AGREEMENT_CARD_SELECT	= "S2516";
	private static final String	CMD_AGREEMENT_CARD_INSERT	= "S2517";
	private static final String	CMD_AGREEMENT_CARD_UPDATE	= "S2518";
	private static final String	CMD_AGREEMENT_CARD_SEARCH	= "S2519";
	

	/**
	 * コンストラクタ
	 */
	public LaborAgreementCardAction() {
		super();
	}
	
	public void action() throws Exception {
		// VOの取得及び設定
		prepareVo(false);
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_AGREEMENT_CARD_SHOW)) {
			// 表示（新規登録）
			show();
		} else if (cmd.equals(CMD_AGREEMENT_CARD_SELECT)) {
			// 表示(キーで情報を検索)
			select();
		} else if (cmd.equals(CMD_AGREEMENT_CARD_INSERT)) {
			// 新規登録
			insert();
		} else if (cmd.equals(CMD_AGREEMENT_CARD_UPDATE)) {
			// 更新
			update();
		} else if (cmd.equals(CMD_AGREEMENT_CARD_SEARCH)) {
			// 検索
			search();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new LaborAgreementCardVo();
	}
	
	@Override
	protected void init() throws Exception {
		// DBコネクション取得
		getConnection();
		// SYSTEM権限確認
		confirmSystemAuth();
		LaborAgreementCardVo vo = (LaborAgreementCardVo)getVo();
		LaborAgreementManager laborAgreement = new LaborAgreementManager(cfg, cmd, aspUser, user, conn);
		Date date = DateUtil.getSystemDate();
		// 事業所コード
		vo.aryPltJCode = new String[0][0];
		// 所属コード
		vo.aryPltSCode = new String[0][0];
		// 社員コード
		vo.aryPltKCode = new String[0][0];
		// 時間外協定コード
		vo.aryPltOverTimeCode = laborAgreement.getOverTimeArray(date);
		// 休出協定コード
		vo.aryPltWorkOnCode = laborAgreement.getWorkOnArray(date);
		// コードが一件も登録されていない場合
		if (vo.aryPltOverTimeCode.length == 0 || vo.aryPltWorkOnCode.length == 0) {
			// メッセージ設定
			setMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		}
	}
	
	@Override
	protected void show() {
		LaborAgreementCardVo vo = (LaborAgreementCardVo)getVo();
		vo.setHidAgreeType(LaborAgreementManager.K_CODE);
		// モード設定
		vo.setMode(LaborAgreementCardVo.MODE_INSERT);
	}
	
	@Override
	protected void select() throws Exception {
		LaborAgreementCardVo vo = (LaborAgreementCardVo)getVo();
		CmLaborAgreementDao dao = new CmLaborAgreementDao();
		dao.initDao(cfg, cmd, aspUser, user, conn);
		// データの取得
		CmLaborAgreementDto dto = dao.findForKey(key1, key2);
		chkExist(dto);
		// VOに情報をセット
		vo.setFields(dto);
		// モード設定
		vo.setMode(LaborAgreementCardVo.MODE_UPDATE);
		// セッションにDTOをセット
		setSessionDto(dto);
	}
	
	@Override
	protected void insert() throws Exception {
		LaborAgreementCardVo vo = (LaborAgreementCardVo)getVo();
		vo.setParams(request);
		vo.setPltDelete(String.valueOf(MospConst.DELETE_FLAG_OFF));
		// パラメータの妥当性確認
		vo.validate();
		// DAOの準備
		CmLaborAgreementDao dao = new CmLaborAgreementDao();
		dao.initDao(cfg, cmd, aspUser, user, conn);
		// DTOの準備
		CmLaborAgreementDto dto = new CmLaborAgreementDto();
		String[] aryAgreeCode = vo.getAryPltAgreeCode();
		for (int i = 0; i < aryAgreeCode.length; i++) {
			vo.setDtoFields(dto, i);
			// 重複チェック
			chkDuplicate(dao.findForKey(aryAgreeCode[i], vo.getHidAgreeType()));
			// データの挿入
			dao.insert(dto);
			// DTOの再取得
			CmLaborAgreementDto retDto = dao.findForKey(dto.getAgreeCode(), dto.getAgreeType());
			chkInsert(retDto);
		}
		// コミット
		commit();
		// メッセージ設定
		setMessage(getMessage(MospConst.MSG_INSERT, ""));
		// 一覧へ遷移
		setNextCmd(LaborAgreementListAction.CMD_AGREEMENT_LIST_RE_SEARCH);
	}
	
	@Override
	protected void update() throws Exception {
		// DTOの取得
		CmLaborAgreementDto dto = (CmLaborAgreementDto)getSessionDto();
		LaborAgreementCardVo vo = (LaborAgreementCardVo)getVo();
		vo.setParams(request);
		// パラメータの妥当性確認
		vo.validate();
		// DTOの準備
		vo.setDtoFields(dto, 0);
		// DAOの準備
		CmLaborAgreementDao dao = new CmLaborAgreementDao();
		dao.initDao(cfg, cmd, aspUser, user, conn);
		// 排他確認
		chkExclusive(dto, dao.findForUpdate(vo.getAryPltAgreeCode()[0], vo.getHidAgreeType()));
		// データの更新
		dao.update(dto);
		// DTOの再取得
		CmLaborAgreementDto retDto = dao.findForKey(dto.getAgreeCode(), dto.getAgreeType());
		chkUpdate(retDto);
		// コミット
		commit();
		// モード設定
		vo.setMode(LaborAgreementCardVo.MODE_UPDATE);
		// メッセージ設定
		setMessage(getMessage(MospConst.MSG_UPDATE, ""));
		// セッションにDTOをセット
		setSessionDto(retDto);
	}
	
	@Override
	protected void search() {
		LaborAgreementCardVo vo = (LaborAgreementCardVo)getVo();
		vo.setParams(request);
		vo.aryPltKCode = new String[0][0];
	}
	
}
