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

import java.sql.SQLException;

import java.util.Date;
import java.util.Map;

import jp.mosp.common.CommonConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.common.dao.MosPUserDao;
import jp.mosp.common.dto.MosPUserDto;
import jp.mosp.common.vo.MosPVo;

import jp.mosp.human.action.HumanKihonCardAction;
import jp.mosp.human.dao.MEmergencyDao;
import jp.mosp.human.dao.MKihonDao;
import jp.mosp.human.dao.MKojinDao;
import jp.mosp.human.dto.MEmergencyDto;
import jp.mosp.human.dto.MKihonDto;
import jp.mosp.human.dto.MKojinDto;
import jp.mosp.human.vo.HumanCardVo;
import jp.mosp.human.vo.HumanKihonCardVo;

import jp.mosp.system.dto.MYakusyokuDto;

public abstract class MosPHumanAction extends MosPAction {

	// フィールド
	protected Map<String, MYakusyokuDto> 	mapPosition;
	protected MYakusyokuDto				mPositionDto;
	protected MKihonDao					mKihonDao;
	protected MKihonDto					mKihonDto;
	protected MKihonDto					storedmKihonDto;

	protected MKojinDao					mKojinDao;	
	protected MKojinDto					mKojinDto;

	protected MEmergencyDao				mEmergencyDao;
	protected MEmergencyDto				mEmergencyDto;	

	/**
	 * 人事基本情報用DAO準備
	 */
	protected void prepareHumanBasisDao() {
		// DAO準備
		if (mKihonDao != null) {
			return;
		}
		mKihonDao = new MKihonDao();
		mKihonDao.initDao(cfg, cmd, aspUser, user, conn);
	}

	/**
	 *個人情報用DAO準備
	 */
	protected void prepareKojinBasisDao() {
		// DAO準備
		if (mKojinDao != null) {
			return;
		}
		mKojinDao = new MKojinDao();
		mKojinDao.initDao(cfg, cmd, aspUser, user, conn);
	}

	/**
	 * 緊急情報用DAO準備
	 */
	protected void prepareEmergencyBasisDao() {
		// DAO準備
		if (mEmergencyDao != null) {
			return;
		}
		mEmergencyDao = new MEmergencyDao();
		mEmergencyDao.initDao(cfg, cmd, aspUser, user, conn);		
	}

	/**
	 * 人事基本情報取得
	 * @param kCode       社員コード
	 * @param isForUpdate アップデート用フラグ
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	protected void findHumanBasisInfo(
			String kCode,
			boolean isForUpdate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		// 人事基本情報用DAO準備
		prepareHumanBasisDao();
		// 現在の情報を取得
		if (isForUpdate) {
			mKihonDto      = mKihonDao.findForUpdate(kCode);
		} else {
			mKihonDto      = mKihonDao.findForKey(kCode);
		}
	}

	/**
	 * 個人情報取得
	 * @param kCode       社員コード
	 * @param isForUpdate アップデート用フラグ
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	protected void findHumanPersonnelInfo(
			String kCode,
			boolean isForUpdate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		// 個人情報用DAO準備
		if (mKojinDao != null) {
			return;
		}
		mKojinDao  = new MKojinDao();
		mKojinDao.initDao(cfg, cmd, aspUser, user, conn);
		// 現在の情報を取得
		if (isForUpdate) {
			mKojinDto = mKojinDao.findForUpdate(kCode);
		} else {
			mKojinDto = mKojinDao.findForKey(kCode);
		}
	}

	/**
	 * 緊急連絡先情報取得
	 * @param kCode       社員コード
	 * @param isForUpdate アップデート用フラグ
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	protected void  findHumanEmergencyInfo(
			String kCode,
			boolean isForUpdate
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		// 緊急連絡先情報用DAO準備
		// DAO準備
		if (mEmergencyDao != null) {
			return;
		}
		mEmergencyDao = new MEmergencyDao();
		mEmergencyDao.initDao(cfg, cmd, aspUser, user, conn);
		mEmergencyDto = new MEmergencyDto();

		String maxEno =	mEmergencyDao.findForMax(kCode);		

		// 現在の情報を取得
		if (isForUpdate) {			
			mEmergencyDto = mEmergencyDao.findForConditionUp(maxEno,kCode);
		} else {
			mEmergencyDto = mEmergencyDao.findForCondition(maxEno,kCode);
		}

	}	

	/**
	 * 変更内容取得
	 * @param kCode 社員コード
	 * @return 変更内容文字列
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	protected String getChanging(
			String kCode
	) throws SQLException, IllegalAccessException, NoSuchFieldException {
		// 人事基本情報取得
		findHumanBasisInfo(kCode, false);
		// 変更内容取得
		StringBuffer sb = new StringBuffer();
		if (mKihonDto == null) {
			sb.append(CommonConst.NAM_MAKE_NEW_DATA);
		} else {
			if (sb.length() > 0) {
				return sb.substring(0, sb.length() - 1);
			}
		}
		return sb.toString();
	}

	/**
	 * ユーザ情報適用
	 * @param kCode        社員コード
	 * @param isBatchApply 一括適用フラグ(true：一括適用、false：個別適用)
	 * @return 処理件数
	 * @throws Exception
	 */
	protected int applyHistory(String kCode, boolean isBatchApply, MosPVo vo) throws Exception {
		// 人事基本情報取得
		findHumanBasisInfo(kCode, false);
		// 排他確認
		if (!isBatchApply && storedmKihonDto != null) {
			chkExclusive(storedmKihonDto, mKihonDto, vo);
		} else {
			// 人事基本情報取得
			storedmKihonDto = mKihonDao.findForKey(kCode);
		}
		// ユーザー情報取得
		MosPUserDao mospuserDao = new MosPUserDao();
		mospuserDao.initDao(cfg, cmd, aspUser, user, conn);
		MosPUserDto mospuserDto = mospuserDao.findForUpdate(kCode);
		// 職位リスト取得
		if (mapPosition == null) {
			mapPosition = getPositionMap();
		}
		mPositionDto = mapPosition.get(mKihonDto.getYCode());
		chkExist(mPositionDto);
		// ユーザ処理
		if (mospuserDto == null) {
			// ユーザーの作成
			mospuserDto = new MosPUserDto();
			mospuserDto.setUserId(kCode);
			mospuserDto.setPassword(MospUtility.getDigest(MospUtility.getDigest(kCode)));
			mospuserDto.setKCode(kCode);
			mospuserDto.setUserName(mKihonDto.getSimei());
			mospuserDto.setAuthority(CommonConst.AUTH_DEFAULT_USER);
			mospuserDto.setChangePass(new Date());
			mospuserDto.setDataLevel(CommonConst.DATA_DEFAULT_LEVEL);
			mospuserDto.setDeleteFlag(mKihonDto.getTsDate() == 0 ? CommonConst.DEFAULT_FLAG_OFF : CommonConst.DEFAULT_FLAG_ON);
			mospuserDao.insert(mospuserDto);
		} else {
			// ユーザの更新
			mospuserDto.setUserName(mKihonDto.getSimei());
			mospuserDto.setDeleteFlag(mKihonDto.getTsDate() == 0 ? CommonConst.DEFAULT_FLAG_OFF : CommonConst.DEFAULT_FLAG_ON);
			mospuserDao.update(mospuserDto, true);
		}
		return 1;
	}

	/**
	 * 選択社員人事基本情報取得
	 * @param vo 対象VO
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 * @throws MospException
	 */
	protected void getSelectedEmployeeBasis(
			HumanCardVo vo
	) throws SQLException, IllegalAccessException, NoSuchFieldException, MospException {
		// 選択社員コード取得
		String kCode = "";
		if (vo.getTxtChangeKCode() != null && !vo.getTxtChangeKCode().equals("") ) {
			kCode = vo.getTxtChangeKCode();
		} else if (key1 != null && !key1.equals("")) {
			kCode = key1;
		} else {
			HumanKihonCardVo kihonVo = (HumanKihonCardVo)getStoredVo(CommonConst.VO_HUMAN_KIHON);
			if (kihonVo == null) {
				// 基本情報画面へ戻る(基本情報が無い場合)
				throwNoBasisException();
			} else {
				kCode = kihonVo.getTxtKCode() == null ? "" : kihonVo.getTxtKCode();
			}
		}
		// 人事基本情報取得
		findHumanBasisInfo(kCode, false);
		if (mKihonDto == null) {
			// 基本情報画面へ戻る(基本情報が無い場合)
			throwNoBasisException();
		}
		// 社員氏名取得
		if (mKihonDto != null) {
			vo.setLblKCode(mKihonDto.getKCode());
			vo.setLblKName(mKihonDto.getSimei());
			vo.setLblKKana(mKihonDto.getFurigana());
		}
	}

	/**
	 * 人事基本情報取得不能時処理
	 * @throws MospException
	 */
	protected void throwNoBasisException() throws MospException {
		String errMessage = MospUtility.getMessage(msg, CommonConst.MSG_NO_HUMAN_DATA, "");
		addErrMessage(errMessage);
		setNextCmd(HumanKihonCardAction.CMD_HUMAN_BASIS_SHOW);
		throw new MospException(CommonConst.EX_NO_BASIS, errMessage);
	}

	/**
	 * セッション保持DTO取得
	 */
	protected void getStoredDto() {
		storedmKihonDto = (MKihonDto)session.getAttribute(CommonConst.ATT_MKIHON_DTO);
	}

	/**
	 * セッション保持DTO設定
	 */
	protected void setStoredDto() {
		session.setAttribute(CommonConst.ATT_MKIHON_DTO, storedmKihonDto);
	}

	/**
	 * ボタン表示設定
	 * @param vo	対象VO
	 */
	protected void setListButton(HumanCardVo vo) {
		vo.setCmdListButton1("H1012");
		vo.setNamListButton1(CommonConst.NAM_TO_HUMAN_LIST);
	}

}
