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

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.mosp.common.CommonConst;
import jp.mosp.common.dto.HumanBasisDtoInterface;

import jp.mosp.system.dao.MYakusyokuDao;
import jp.mosp.system.dto.MYakusyokuDto;

/**
 * 役職に関するPartクラス
 * @author yoshida
 *	@version	0.0.2
 */
public class PositionPart extends BasePart {

	/**
	 * コンストラクタ
	 */
	public PositionPart() {
		super();
	}

	/**
	 * コンストラクタ
	 * @param partInfo
	 */
	public PositionPart(PartInfo partInfo) {
		super(partInfo);
	}

	/**
	 * M_YAKUSYOKUテーブル用のDAO取得
	 * @return	初期化済みMYakusyokuDaoインスタンス
	 * @throws Exception
	 */
	public MYakusyokuDao getMYakusyokuDao() throws Exception {
		return (MYakusyokuDao)getPreparedDao(MYakusyokuDao.class.getCanonicalName());
	}

	/**
	 * 役職コード取得(M_YAKUSYOKU)
	 * @return 役職コードリスト
	 * @throws Exception 
	 */
	public String[][] getPositionArrayAll() throws Exception {
		// 情報の取得
		List<MYakusyokuDto> listPosition = getMYakusyokuDao().findAll();
		// 配列の作成
		String[][] aryPosition = new String[listPosition.size()][3];
		for (int i = 0; i < listPosition.size(); i++) {
			aryPosition[i][0] = listPosition.get(i).getYCode();
			aryPosition[i][1] = listPosition.get(i).getYName();
			aryPosition[i][2] = listPosition.get(i).getTokyu();
		}
		return aryPosition;
	}

	/**
	 * 役職コード取得(等級有りのみ)(M_YAKUSYOKU)
	 * @return 役職コードリスト
	 * @throws Exception 
	 */
	public String[][] getPositionArray() throws Exception {
		// 情報の取得
		List<MYakusyokuDto> listPosition = getMYakusyokuDao().findAvailable();
		// 配列の作成
		String[][] aryPosition = new String[listPosition.size()][3];
		for (int i = 0; i < listPosition.size(); i++) {
			aryPosition[i][0] = listPosition.get(i).getYCode();
			aryPosition[i][1] = listPosition.get(i).getYName();
			aryPosition[i][2] = listPosition.get(i).getTokyu();
		}
		return aryPosition;
	}

	/**
	 * 役職コード取得(等級無しのみ)(M_YAKUSYOKU)
	 * @return 役職コードリスト
	 * @throws Exception 
	 */
	public String[][] getPositionTitleArray() throws Exception {
		// 情報の取得
		List<MYakusyokuDto> listPosition = getMYakusyokuDao().findForTitle();
		// 配列の作成
		String[][] aryPosition = new String[listPosition.size()][3];
		for (int i = 0; i < listPosition.size(); i++) {
			aryPosition[i][0] = listPosition.get(i).getYCode();
			aryPosition[i][1] = listPosition.get(i).getYName();
		}
		return aryPosition;
	}

	/**
	 * 役職コード取得(プルダウン用)(M_YAKUSYOKU)
	 * @return 役職コードリスト
	 * @throws Exception 
	 */
	public String[][] getPositionPltArray() throws Exception {
		// 情報の取得
		List<MYakusyokuDto> listPosition = getMYakusyokuDao().findAvailable();
		// 配列の作成
		String[][] aryPosition = new String[listPosition.size()][2];
		for (int i = 0; i < listPosition.size(); i++) {
			aryPosition[i][0] = listPosition.get(i).getYCode() + CommonConst.STR_TIME_SEPARATOR + listPosition.get(i).getTokyu();
			aryPosition[i][1] = listPosition.get(i).getYName();
		}
		return aryPosition;
	}

	/**
	 * 役職リスト取得(M_YAKUSYOKU)
	 * @return 役職リスト
	 * @throws Exception 
	 */
	public Map<String, MYakusyokuDto> getPositionMap() throws Exception {
		// 情報の取得
		List<MYakusyokuDto> listPosition = getMYakusyokuDao().findAll();
		// Mapの作成
		HashMap<String, MYakusyokuDto> map = new HashMap<String, MYakusyokuDto>();
		for (int i = 0; i < listPosition.size(); i++) {
			map.put(listPosition.get(i).getYCode(), listPosition.get(i));
		}
		return map;
	}

	/**
	 * 役職情報取得(M_YAKUSYOKU)
	 * @param kCode		対象社員コード
	 * @return 			対象社員役職情報
	 * @throws Exception 
	 */
	public MYakusyokuDto getHoldingPositionInfo(
			String kCode
	) throws Exception {
		String positionCode = "";
		String grade = "0";
		// 人事基本情報取得
		HumanBasisDtoInterface dto = human().getBasisInfo(kCode);
		if (VariousPart.chkExistObject(dto)) {
			// 役職情報取得
			positionCode = dto.getYCode();
			grade = dto.getTokyu();
		}
		return getMYakusyokuDao().findForKey(positionCode, grade);
	}


	/**
	 * 役職名称取得(等級付き)
	 * @param kCode		対象社員コード
	 * @return	対象社員役職名称
	 * @throws Exception 
	 */
	public String getPositionName(
			String kCode
	) throws Exception {
		String name = "";
		HumanBasisDtoInterface dto = human().getBasisInfo(kCode);
		if (dto != null) {
			StringBuffer position = new StringBuffer();
			position.append(dto.getYCode());
			position.append(CommonConst.STR_TIME_SEPARATOR);
			position.append(dto.getTokyu());
			name = StringPart.getCodeName(position.toString(), getPositionPltArray());
			if (name.equals(CommonConst.STR_TIME_SEPARATOR)) {
				name = "";
			}
		}
		return name;
	}

	/**
	 * 皆勤手当利用可否
	 * @since	0.0.2
	 * @param kCode		対象社員コード
	 * @return
	 * <p>
	 * 皆勤手当有りの場合true、そうでない場合false
	 * </p>
	 * @throws Exception
	 */
	public boolean isApplyAttendanceAllowance(String kCode) throws Exception {
		MYakusyokuDto dto = getHoldingPositionInfo(kCode);
		// 皆勤手当
		if (VariousPart.chkExistDto(dto) && CommonConst.NAM_COMMON_FLAG_ON.equals(dto.getKaikin())) {
			return true;
		}
		return false;
	}

}
