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

import java.util.Date;
import java.util.List;

import jp.mosp.common.CommonConst;
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.common.utils.VariousUtil;
import jp.mosp.kintai.common.action.KintaiCommonAction;
import jp.mosp.kintai.common.dto.MKihonDto;
import jp.mosp.mospcommon.system.LaborAgreementManager;
import jp.mosp.mospcommon.system.action.LaborAgreementListAction;
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 KintaiCommonAction {
	
	// コマンド
	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 = getOfficeArrayAll();
		// 所属コード
		vo.aryPltSCode = getSectionArrayAll();
		// 社員コード
		if (VariousUtil.isNotNull(vo.aryPltJCode) && vo.aryPltJCode.length > 0 && VariousUtil.isNotNull(vo.aryPltSCode)
				&& vo.aryPltSCode.length > 0) {
			vo.aryPltKCode = getKCodeArray(vo.aryPltJCode[0][0], vo.aryPltSCode[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.setPltJCode(part().office().getBelongOfficeCode(dto.getAgreeCode()));
		vo.setPltSCode(part().section().getBelongSectionCode(dto.getAgreeCode()));
		vo.aryPltKCode = new String[][]{ { dto.getAgreeCode(), getKName(dto.getAgreeCode()) } };
		// モード設定
		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();
		// 登録件数
		int number = 0;
		String[] aryAgreeCode = vo.getAryPltAgreeCode();
		for (int i = 0; i < aryAgreeCode.length; i++) {
			vo.setDtoFields(dto, i);
			if (VariousUtil.isNull(dao.findForKey(aryAgreeCode[i], vo.getHidAgreeType()))) {
				// データの挿入
				dao.insert(dto);
				// DTOの再取得
				CmLaborAgreementDto retDto = dao.findForKey(dto.getAgreeCode(), dto.getAgreeType());
				chkInsert(retDto);
				number++;
			} else {
				addErrMessage(getKName(aryAgreeCode[i]) + "は既に登録されています");
			}
		}
		// コミット
		commit();
		// メッセージ設定
		setMessage(getMessage(CommonConst.MSG_NUMBER_OF_UPDATES, String.valueOf(number)));
		// 一覧へ遷移
		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.setPltJCode(part().office().getBelongOfficeCode(dto.getAgreeCode()));
		vo.setPltSCode(part().section().getBelongSectionCode(dto.getAgreeCode()));
		vo.setPltAgreeCode(vo.getAryPltAgreeCode()[0]);
		vo.aryPltKCode = new String[][]{ { dto.getAgreeCode(), getKName(dto.getAgreeCode()) } };
		// パラメータの妥当性確認
		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() throws Exception {
		LaborAgreementCardVo vo = (LaborAgreementCardVo)getVo();
		vo.setParams(request);
		vo.aryPltKCode = getKCodeArray(vo.getPltJCode(), vo.getPltSCode());
	}
	
	private String[][] getKCodeArray(String jCode, String sCode) throws Exception {
		// 情報の取得
		List<MKihonDto> list = part().human().getMKihonDao().findForCondition(jCode, sCode);
		// 配列の作成
		String[][] aryKCode = new String[list.size()][2];
		for (int i = 0; i < aryKCode.length; i++) {
			aryKCode[i][0] = list.get(i).getKCode();
			aryKCode[i][1] = list.get(i).getSimei();
		}
		return aryKCode;
	}
	
}
