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

import java.sql.SQLException;

import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.payroll.base.PayrollAction;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.bonus.dao.PmDetailBaDao;
import jp.mosp.payroll.bonus.dao.PmDetailBdDao;
import jp.mosp.payroll.bonus.dao.PmDetailBsDao;
import jp.mosp.payroll.bonus.dto.PmDetailBaDto;
import jp.mosp.payroll.bonus.dto.PmDetailBdDto;
import jp.mosp.payroll.bonus.dto.PmDetailBsDto;
import jp.mosp.payroll.dao.PmDetailPaDao;
import jp.mosp.payroll.dao.PmDetailPbDao;
import jp.mosp.payroll.dao.PmDetailPdDao;
import jp.mosp.payroll.dao.PmDetailPsDao;
import jp.mosp.payroll.dao.PmDetailPtDao;
import jp.mosp.payroll.dto.DetailDto;
import jp.mosp.payroll.dto.DetailMoneyDto;
import jp.mosp.payroll.dto.DetailPaDto;
import jp.mosp.payroll.dto.DetailPbDto;
import jp.mosp.payroll.dto.DetailPdDto;
import jp.mosp.payroll.dto.DetailTimeDto;
import jp.mosp.payroll.system.vo.DetailCardVo;

/**
 * 明細設定一覧画面における処理を行う。<br><br>
 * 以下のコマンドを扱う。
 * <ul><li>
 * {@link #CMD_SHOW}
 * </li><li>
 * {@link #CMD_SELECT}
 * </li><li>
 * {@link #CMD_REGIST}
 * </li></ul>
 */
public class DetailCardAction extends PayrollAction {
	
	/**
	 * 画面表示コマンド。<br><br>
	 * 明細設定編集画面を表示する。<br>
	 */
	public static final String	CMD_SHOW	= "P7015";
	
	/**
	 * 検索コマンド。<br><br>
	 * 指定された条件で明細設定を選択し、表示する。<br>
	 */
	public static final String	CMD_SELECT	= "P7016";
	
	/**
	 * 登録コマンド。<br><br>
	 * 編集された内容を、登録する。<br>
	 */
	public static final String	CMD_REGIST	= "P7017";
	

	/**
	 * Actionの初期化を行う。<br><br>
	 * 以下の処理を行う。
	 * <ul><li>
	 * {@link PayrollAction#PayrollAction()}
	 * </li></ul>
	 */
	public DetailCardAction() {
		super();
	}
	
	/* (non-Javadoc)
	 * @see jp.mosp.common.common.BaseAction#getSpecificVo()
	 */
	@Override
	protected BaseVo getSpecificVo() {
		return new DetailCardVo();
	}
	
	/**
	 * アクション。<br>
	 * 詳細は、クラス説明を参照。<br>
	 */
	public void action() throws Exception {
		// VO取得及び設定
		prepareVo();
		if (cmd.equals(CMD_SHOW)) {
			// 画面表示
			show();
		} else if (cmd.equals(CMD_SELECT)) {
			// 選択処理
			select();
		} else if (cmd.equals(CMD_REGIST)) {
			// 登録処理
			regist();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	/**
	 * 画面表示処理を行う。<br><br>
	 * @throws Exception 
	 */
	@Override
	public void show() throws Exception {
		// コネクション取得
		getConnection();
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// VOフィールド設定
		vo.setPltType(String.valueOf(PayrollConst.DETAIL_TYPE_PT));
		vo.setTxtCode("");
		vo.setTxtName("");
		vo.setTxtAbbr("");
		vo.setPltDelete(String.valueOf(MospConst.DELETE_FLAG_OFF));
	}
	
	/**
	 * 選択処理を行う。
	 * @throws Exception 
	 */
	@Override
	public void select() throws Exception {
		// コネクション取得
		getConnection();
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// 検索キー存在確認
		if (key1 == null || key1.equals("") || key2 == null || key2.equals("")) {
			addErrMessage(MospUtility.getMessage(msg, MospConst.MSG_NO_DATA, ""));
			setNextCmd(DetailListAction.CMD_RE_SEARCH);
			return;
		}
		// 明細区分毎情報取得
		DetailDto dto = null;
		if (key1.equals(PayrollConst.DETAIL_TYPE_PT)) {
			// 給与勤怠
			dto = getDetailPt();
			vo.setPltType(PayrollConst.DETAIL_TYPE_PT);
		} else if (key1.equals(PayrollConst.DETAIL_TYPE_PA)) {
			// 給与支給
			dto = getDetailPa();
			vo.setPltType(PayrollConst.DETAIL_TYPE_PA);
		} else if (key1.equals(PayrollConst.DETAIL_TYPE_PD)) {
			// 給与控除
			dto = getDetailPd();
			vo.setPltType(PayrollConst.DETAIL_TYPE_PD);
		} else if (key1.equals(PayrollConst.DETAIL_TYPE_PB)) {
			// 給与内訳
			dto = getDetailPb();
			vo.setPltType(PayrollConst.DETAIL_TYPE_PB);
		} else if (key1.equals(PayrollConst.DETAIL_TYPE_PS)) {
			// 給与合計
			dto = getDetailPs();
			vo.setPltType(PayrollConst.DETAIL_TYPE_PS);
		} else if (key1.equals(PayrollConst.DETAIL_TYPE_BA)) {
			// 賞与支給
			dto = getDetailBa();
			vo.setPltType(PayrollConst.DETAIL_TYPE_BA);
		} else if (key1.equals(PayrollConst.DETAIL_TYPE_BD)) {
			// 賞与控除
			dto = getDetailBd();
			vo.setPltType(PayrollConst.DETAIL_TYPE_BD);
		} else if (key1.equals(PayrollConst.DETAIL_TYPE_BS)) {
			// 賞与合計
			dto = getDetailBs();
			vo.setPltType(PayrollConst.DETAIL_TYPE_BS);
		}
		
		// データ存在確認
		if (dto == null) {
			addErrMessage(MospUtility.getMessage(msg, MospConst.MSG_NO_DATA, ""));
			setNextCmd(DetailListAction.CMD_RE_SEARCH);
			return;
		}
		// VOに設定
		vo.setFields(dto);
		// 編集可否フラグ確認
		if (dto.getEditFlag() == PayrollConst.DETAIL_EDIT_NG) {
			vo.setMode(2);
		} else {
			vo.setMode(1);
		}
	}
	
	/**
	 * 登録処理を行う。
	 * @throws Exception 
	 */
	private void regist() throws Exception {
		// コネクション取得
		getConnection();
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// パラメータ取得
		vo.setParams(request);
		// 明細区分毎リスト取得
		if (vo.getPltType().equals(PayrollConst.DETAIL_TYPE_PT)) {
			// 給与勤怠
			registDetailPt();
		} else if (vo.getPltType().equals(PayrollConst.DETAIL_TYPE_PA)) {
			// 給与支給
			registDetailPa();
		} else if (vo.getPltType().equals(PayrollConst.DETAIL_TYPE_PD)) {
			// 給与控除
			registDetailPd();
		} else if (vo.getPltType().equals(PayrollConst.DETAIL_TYPE_PB)) {
			// 給与内訳
			registDetailPb();
		} else if (vo.getPltType().equals(PayrollConst.DETAIL_TYPE_PS)) {
			// 給与合計
			registDetailPs();
		} else if (vo.getPltType().equals(PayrollConst.DETAIL_TYPE_BA)) {
			// 賞与支給
			registDetailBa();
		} else if (vo.getPltType().equals(PayrollConst.DETAIL_TYPE_BD)) {
			// 賞与控除
			registDetailBd();
		} else if (vo.getPltType().equals(PayrollConst.DETAIL_TYPE_BS)) {
			// 賞与合計
			registDetailBs();
		}
		// コミット
		commit();
		// メッセージ設定
		setMessage(MospUtility.getMessage(msg, "IC0001", ""));
		// 画面表示処理
		key1 = vo.getPltType();
		key2 = vo.getTxtCode();
		select();
	}
	
	/**
	 * 給与勤怠明細設定を取得する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	private DetailDto getDetailPt() throws SQLException, IllegalAccessException, NoSuchFieldException {
		return new PmDetailPtDao(cfg, cmd, aspUser, user, conn).findForKey(key2);
	}
	
	/**
	 * 給与支給明細設定を取得する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	private DetailDto getDetailPa() throws SQLException, IllegalAccessException, NoSuchFieldException {
		return new PmDetailPaDao(cfg, cmd, aspUser, user, conn).findForKey(key2);
	}
	
	/**
	 * 給与控除明細設定を取得する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	private DetailDto getDetailPd() throws SQLException, IllegalAccessException, NoSuchFieldException {
		return new PmDetailPdDao(cfg, cmd, aspUser, user, conn).findForKey(key2);
	}
	
	/**
	 * 給与内訳明細設定を取得する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	private DetailDto getDetailPb() throws SQLException, IllegalAccessException, NoSuchFieldException {
		return new PmDetailPbDao(cfg, cmd, aspUser, user, conn).findForKey(key2);
	}
	
	/**
	 * 給与合計明細設定を取得する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	private DetailDto getDetailPs() throws SQLException, IllegalAccessException, NoSuchFieldException {
		return new PmDetailPsDao(cfg, cmd, aspUser, user, conn).findForKey(key2);
	}
	
	/**
	 * 賞与支給明細設定を取得する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	private DetailDto getDetailBa() throws SQLException, IllegalAccessException, NoSuchFieldException {
		return new PmDetailBaDao(cfg, cmd, aspUser, user, conn).findForKey(key2);
	}
	
	/**
	 * 賞与控除明細設定を取得する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	private DetailDto getDetailBd() throws SQLException, IllegalAccessException, NoSuchFieldException {
		return new PmDetailBdDao(cfg, cmd, aspUser, user, conn).findForKey(key2);
	}
	
	/**
	 * 賞与合計明細設定を取得する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 */
	private DetailDto getDetailBs() throws SQLException, IllegalAccessException, NoSuchFieldException {
		return new PmDetailBsDao(cfg, cmd, aspUser, user, conn).findForKey(key2);
	}
	
	/**
	 * 給与勤怠明細設定を登録する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws MospException 
	 */
	private void registDetailPt() throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// DAO準備
		PmDetailPtDao dao = new PmDetailPtDao(cfg, cmd, aspUser, user, conn);
		// 情報取得
		DetailTimeDto dto = dao.findForUpdate(vo.getTxtCode());
		if (dto == null) {
			// 新規登録
			dto = new DetailTimeDto();
			dto.setDetailCode(vo.getTxtCode());
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.insert(dto, true);
		} else {
			// 更新
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.update(dto, true);
		}
	}
	
	/**
	 * 給与支給明細設定を登録する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws MospException 
	 */
	private void registDetailPa() throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// DAO準備
		PmDetailPaDao dao = new PmDetailPaDao(cfg, cmd, aspUser, user, conn);
		// 情報取得
		DetailPaDto dto = dao.findForUpdate(vo.getTxtCode());
		if (dto == null) {
			// 新規登録
			dto = new DetailPaDto();
			dto.setDetailCode(vo.getTxtCode());
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.insert(dto, true);
		} else {
			// 更新
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.update(dto, true);
		}
	}
	
	/**
	 * 給与控除明細設定を登録する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws MospException 
	 */
	private void registDetailPd() throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// DAO準備
		PmDetailPdDao dao = new PmDetailPdDao(cfg, cmd, aspUser, user, conn);
		// 情報取得
		DetailPdDto dto = dao.findForUpdate(vo.getTxtCode());
		if (dto == null) {
			// 新規登録
			dto = new DetailPdDto();
			dto.setDetailCode(vo.getTxtCode());
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.insert(dto, true);
		} else {
			// 更新
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.update(dto, true);
		}
	}
	
	/**
	 * 給与内訳明細設定を登録する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws MospException 
	 */
	private void registDetailPb() throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// DAO準備
		PmDetailPbDao dao = new PmDetailPbDao(cfg, cmd, aspUser, user, conn);
		// 情報取得
		DetailPbDto dto = dao.findForUpdate(vo.getTxtCode());
		if (dto == null) {
			// 新規登録
			dto = new DetailPbDto();
			dto.setDetailCode(vo.getTxtCode());
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.insert(dto, true);
		} else {
			// 更新
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.update(dto, true);
		}
	}
	
	/**
	 * 給与合計明細設定を登録する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws MospException 
	 */
	private void registDetailPs() throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// DAO準備
		PmDetailPsDao dao = new PmDetailPsDao(cfg, cmd, aspUser, user, conn);
		// 情報取得
		DetailMoneyDto dto = dao.findForUpdate(vo.getTxtCode());
		if (dto == null) {
			// 新規登録
			dto = new DetailMoneyDto();
			dto.setDetailCode(vo.getTxtCode());
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.insert(dto, true);
		} else {
			// 更新
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.update(dto, true);
		}
	}
	
	/**
	 * 賞与支給明細設定を登録する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws MospException 
	 */
	private void registDetailBa() throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// DAO準備
		PmDetailBaDao dao = new PmDetailBaDao(cfg, cmd, aspUser, user, conn);
		// 情報取得
		PmDetailBaDto dto = dao.findForUpdate(vo.getTxtCode());
		if (dto == null) {
			// 新規登録
			dto = new PmDetailBaDto();
			dto.setDetailCode(vo.getTxtCode());
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.insert(dto, true);
		} else {
			// 更新
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.update(dto, true);
		}
	}
	
	/**
	 * 賞与控除明細設定を登録する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws MospException 
	 */
	private void registDetailBd() throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// DAO準備
		PmDetailBdDao dao = new PmDetailBdDao(cfg, cmd, aspUser, user, conn);
		// 情報取得
		PmDetailBdDto dto = dao.findForUpdate(vo.getTxtCode());
		if (dto == null) {
			// 新規登録
			dto = new PmDetailBdDto();
			dto.setDetailCode(vo.getTxtCode());
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.insert(dto, true);
		} else {
			// 更新
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.update(dto, true);
		}
	}
	
	/**
	 * 賞与合計明細設定を登録する。
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws NoSuchFieldException 
	 * @throws MospException 
	 */
	private void registDetailBs() throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// VO取得
		DetailCardVo vo = (DetailCardVo)getVo();
		// DAO準備
		PmDetailBsDao dao = new PmDetailBsDao(cfg, cmd, aspUser, user, conn);
		// 情報取得
		PmDetailBsDto dto = dao.findForUpdate(vo.getTxtCode());
		if (dto == null) {
			// 新規登録
			dto = new PmDetailBsDto();
			dto.setDetailCode(vo.getTxtCode());
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.insert(dto, true);
		} else {
			// 更新
			dto.setDetailName(vo.getTxtName());
			dto.setDetailAbbr(vo.getTxtAbbr());
			dto.setDeleteFlag(Byte.parseByte(vo.getPltDelete()));
			dao.update(dto, true);
		}
	}
	
}
