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

import java.sql.SQLException;
import java.util.Date;

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.payroll.base.PayrollConst;
import jp.mosp.payroll.common.vo.FirmBankingOutputVo;
import jp.mosp.payroll.common.vo.RetireDetailCardVo;
import jp.mosp.payroll.dao.CtRetireDetailDao;
import jp.mosp.payroll.dto.CtRetireDetailDto;

public class RetireDetailCardAction extends MosPAction {
	
	// コマンド
	
	private static final String	CMD_RETIRE_DETAIL_SHOW		= "P9901";
	private static final String	CMD_RETIRE_DETAIL_REGIST	= "P9902";
	

	/**
	 * アクション
	 */
	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// PAYROLL権限確認
		confirmPayrollAuth();
		// VOの取得及び設定
		// VOの取得
		RetireDetailCardVo vo = new RetireDetailCardVo();
		vo.initVo(cfg, msg, cmd, aspUser, user);
		vo.clearMessage();
		vo.clearErrField();
		setVo(vo);
		setUrl(vo.getRetUrl());
		// 和暦設定取得
		vo.listConf = getConfList(PayrollConst.CONF_ID_JP_IMP_CAL);
		// 異動有無配列
		vo.aryPltCityChanges = PayrollConst.getCityChangesArray();
		vo.aryEra = getCodeArray(PayrollConst.TID_JAPANESE);
		// リクエストの設定
		vo.setParams(request);
		// 退職明細年月設定
		setDetailMonth(vo);
		// コマンド毎の処理
		if (cmd.equals(CMD_RETIRE_DETAIL_SHOW)) {
			
			// VOの取得及び設定
			FirmBankingOutputVo vo1 = new FirmBankingOutputVo();
			vo1 = (FirmBankingOutputVo)getStoredVo(vo1.getClassName());
			vo1.setParams(request);
			show();
		} else if (cmd.equals(CMD_RETIRE_DETAIL_REGIST)) {
			regist(vo);
			
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	/**
	 * 表示
	 * @param vo
	 * @throws Exception
	 */
	@Override
	public void show() throws Exception {
		// 
	}
	
	/**
	 * 登録
	 * @param vo
	 * @throws MospException
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	private void regist(RetireDetailCardVo vo) throws MospException, SQLException, IllegalAccessException,
			NoSuchFieldException {
		// パラメータの妥当性確認
		vo.validate();
		// 対象年月の設定
		Date targetMonth = vo.getDate(vo.getPltSpecifyYear(), vo.getPltSpecifyMonth(), vo.getPltSpecifyDay());
		// DAO準備
		CtRetireDetailDao ctRetireDetailDao = new CtRetireDetailDao();
		ctRetireDetailDao.initDao(cfg, cmd, aspUser, user, conn);
		// 設定年月のデータ存在確認
		CtRetireDetailDto ctRetireDetailDto = ctRetireDetailDao.findForKey(vo.getTxtCityCode(), targetMonth);
		if (ctRetireDetailDto == null) {
			ctRetireDetailDto = new CtRetireDetailDto();
			// DTOに設定
			vo.setDtoFields(ctRetireDetailDto);
			ctRetireDetailDto.setDetailMonth(targetMonth);
			// 登録
			ctRetireDetailDao.insert(ctRetireDetailDto);
		} else {
			// DTOに設定
			vo.setDtoFields(ctRetireDetailDto);
			ctRetireDetailDto.setDetailMonth(targetMonth);
			// 更新
			ctRetireDetailDao.update(ctRetireDetailDto);
			
		}
		// コミット
		commit();
		// メッセージの表示
		vo.setMessage(MospUtility.getMessage(msg, MospConst.MSG_INSERT, ""));
		
	}
	
	/**
	 * 対象年月の設定
	 * @param vo
	 * @throws MospException
	 */
	private void setDetailMonth(RetireDetailCardVo vo) throws MospException {
		// 対象年月の設定
		Date targetMonth = vo.getDate(vo.getPltSpecifyYear(), vo.getPltSpecifyMonth(), vo.getPltSpecifyDay());
		// 和暦の設定
		vo.setJpDetailMonth(targetMonth);
	}
	
}
