/*
 * 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.sql.SQLException;
import java.util.List;

import jp.mosp.common.CommonConst;
import jp.mosp.common.common.MospException;

import jp.mosp.system.dao.MCodeDao;
import jp.mosp.system.dto.MCodeDto;

/**
 * MosP偺僐乕僪偵娭偡傞Part僋儔僗
 * @author yoshida
 *	@version	0.0.2
 */
public class MosPCodePart extends BasePart {

	/**
	 * 僐儞僗僩儔僋僞
	 */
	public MosPCodePart() {
		super();
	}

	/**
	 * 僐儞僗僩儔僋僞
	 * @param partInfo	
	 */
	public MosPCodePart(PartInfo partInfo) {
		super(partInfo);
	}

	/**
	 * MCodeDao庢摼
	 * @return			弶婜壔嵪MCodeDao僀儞僗僞儞僗
	 * @throws Exception
	 */
	public MCodeDao getMCodeDao() throws Exception {
		return (MCodeDao)getPreparedDao(MCodeDao.class.getCanonicalName());
	}

	/**
	 * @param tid
	 * @param tcode
	 * @return
	 * @throws Exception
	 */
	public MCodeDto findForKey(String tid, String tcode) throws Exception {
		return getMCodeDao().findForKey(tid, tcode);
	}

	/**
	 * @param tid
	 * @param tcode
	 * @return
	 * @throws Exception
	 */
	public MCodeDto findForDto(String tid) throws Exception {
		return getMCodeDao().findForDto(tid);
	}

	/**
	 * @param tid
	 * @param tcode
	 * @return
	 * @throws Exception
	 */
	public List<MCodeDto> findForCondition(String tid, String tcode) throws Exception {
		return getMCodeDao().findForCondition(tid, tcode);
	}

	/**
	 * @param tid
	 * @param tcode
	 * @return
	 * @throws Exception
	 */
	public String findTchange(String tid, String tcode) throws Exception {
		MCodeDto dto = findForKey(tid, tcode);
		if (VariousPart.chkExistDto(dto)) {
			return dto.getTChange();
		}
		return "";
	}

	/**
	 * @param tid
	 * @param tcode
	 * @return
	 * @throws Exception
	 */
	public String findTchange(String tid) throws Exception {
		MCodeDto dto = findForDto(tid);
		if (VariousPart.chkExistDto(dto)) {
			return dto.getTChange();
		}
		return "";
	}

	/**
	 * 僐乕僪嬫暘庢摼(M_CODE)
	 * @return 僐乕僪嬫暘儕僗僩
	 * @throws MospException 
	 * @throws ClassNotFoundException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws SQLException 
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 */
	public String[][] getMosPCodeArray() throws Exception {
		// 僐乕僪嬫暘偺庢摼(distinct僨乕僞)
		List<MCodeDto> list = getMCodeDao().findAllTId();     
		String[][] aryCode = new String[list.size()][2];
		// Sting偺攝楍偵奿擺
		for (int i = 0; i < list.size(); i++) {
			String tId = list.get(i).getTId();
			aryCode[i][0] = tId;
			aryCode[i][1] = tId;
		}
		return aryCode;
	}

	/**
	 * 僐乕僪嬫暘庢摼(M_CODE)
	 * @return 僐乕僪攝楍[][]<br>
	 * 			[0]tcode<br>
	 * 			[1]tname<br>
	 * @param tid 僐乕僪ID
	 * @throws MospException 
	 * @throws ClassNotFoundException 
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws SQLException 
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 */
	public String[][] getMosPCodeArray(String tid) throws Exception {
		// 僐乕僪偺庢摼
		List<MCodeDto> list = getMCodeDao().findForCondition(tid);     
		String[][] aryCode = new String[list.size()][2];
		// Sting偺攝楍偵奿擺
		for (int i = 0; i < list.size(); i++) {
			aryCode[i][0] = list.get(i).getTCode();
			aryCode[i][1] = list.get(i).getTName();
		}
		return aryCode;
	}

	/**
	 * 僐乕僪嬫暘庢摼(M_CODE)
	 * @return 僐乕僪攝楍[][]<br>
	 * 			[0]tcode<br>
	 * 			[1]tname<br>
	 * 			[2]tchange
	 * @param tid 僐乕僪ID
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 */
	public String[][] getMosPCodeArray(String tid, String tcode) throws Exception {
		// 僐乕僪偺庢摼
		List<MCodeDto> list = findForCondition(tid, tcode);     
		String[][] aryCode = new String[list.size()][3];
		// Sting偺攝楍偵奿擺
		for (int i = 0; i < list.size(); i++) {
			aryCode[i][0] = list.get(i).getTCode();
			aryCode[i][1] = list.get(i).getTName();
			aryCode[i][2] = list.get(i).getTChange();
		}
		return aryCode;
	}

	/**
	 * 僐乕僪嬫暘庢摼(M_CODE)
	 * @return 僐乕僪攝楍[][]<br>
	 * 			[0]tcode<br>
	 * 			[1]tname<br>
	 * 			[2]tchange
	 * @param tid 僐乕僪ID
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 */
	public String[] getMosPCode(String tid, String tcode) throws Exception {
		// 僐乕僪偺庢摼
		MCodeDto dto = findForKey(tid, tcode);     
		String[] aryCode = new String[3];
		// Sting偺攝楍偵奿擺
		if (dto != null) {
			for (int i = 0; i < 3; i++) {
				aryCode[0] = dto.getTCode();
				aryCode[1] = dto.getTName();
				aryCode[2] = dto.getTChange();
			}
		}
		return aryCode;
	}

	/**
	 * 棟桼嬫暘庢摼(M_CODE)
	 * @param tid 僐乕僪ID
	 * @return 僐乕僪攝楍[][]<br>
	 * 			[0]tcode<br>
	 * 			[1]tname<br>
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 */
	public String[][] getReasonCodeArray(String tid) throws Exception {
		// 僐乕僪偺庢摼
		List<MCodeDto> list = getMCodeDao().findForReason(tid);     
		String[][] aryCode = new String[list.size()][2];
		// Sting偺攝楍偵奿擺
		for (int i = 0; i < list.size(); i++) {
			aryCode[i][0] = list.get(i).getTCode();
			aryCode[i][1] = list.get(i).getTName();
		}
		return aryCode;
	}


	/**
	 * 僐乕僪嬫暘庢摼(M_CODE)
	 * @return 僐乕僪攝楍[][]<br>
	 * 			[0]tcode<br>
	 * 			[1]tname<br>
	 * 			[2]tchange
	 * @param tid 僐乕僪ID
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws MospException 
	 * @throws ClassNotFoundException 
	 */
	public String[] getMosPCode(String tid) throws Exception {
		// 僐乕僪偺庢摼
		MCodeDto dto = findForDto(tid);     
		String[] aryCode = new String[3];
		// Sting偺攝楍偵奿擺
		for (int i = 0; i < 3; i++) {
			aryCode[0] = dto.getTCode();
			aryCode[1] = dto.getTName();
			aryCode[2] = dto.getTChange();
		}
		return aryCode;
	}

	/**
	 * 桳媼媥壣巟媼擔悢
	 * @return [0]乧嬑懕擭悢丄[1]乧巟媼擔悢
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public String[][] getPaidHolidayArray() throws Exception {
		List<MCodeDto> listCode = getPaidHolidayList();
		String[][] aryPaidHolidayDays = new String[listCode.size()][2];
		for (int i = 0; i < aryPaidHolidayDays.length; i++) {
			aryPaidHolidayDays[i][0] = listCode.get(i).getTCode();
			aryPaidHolidayDays[i][1] = listCode.get(i).getTChange();
		}
		return aryPaidHolidayDays;
	}

	/**
	 * 桳媼媥壣巟媼擔悢忣曬儕僗僩庢摼
	 * @return 桳媼媥壣巟媼擔悢忣曬儕僗僩
	 * @throws SQLException
	 * @throws IllegalAccessException
	 * @throws NoSuchFieldException
	 */
	public List<MCodeDto> getPaidHolidayList() throws Exception {
		return getMCodeDao().findForHoliday(CommonConst.NAM_PAID_HOLIDAY + CommonConst.NAM_SUPPLY);
	}

	/**
	 * 嬑懹彸擣婡擻忬嫷庢摼(M_CODE)
	 * @return	boolean 	嬑懹彸擣婡擻偺桳岠柍岠
	 * @throws SQLException
	 * @throws NoSuchFieldException
	 * @throws IllegalAccessException
	 * @throws MospException 
	 * @throws ClassNotFoundException 
	 */
	public boolean isAttendanceApprovalFunction() throws Exception {
		// 僐乕僪偺庢摼
		MCodeDto dto = findForKey(CommonConst.TID_APPROVAL_FUNCTION, CommonConst.TCD_ATTENDANCE_APPROVAL);     
		if (dto == null) {
			return false;
		} else {
			if (dto.getTName().equals(CommonConst.NAM_COMMON_FLAG_ON)) {
				return false;
			} else if (dto.getTName().equals(CommonConst.NAM_COMMON_FLAG_OFF)) {
				return true;
			} else {
				return false;
			}
		}
	}

	/**
	 * 僷僗儚乕僪桳岠婜尷擔悢
	 * @return	int		僷僗儚乕僪桳岠婜尷擔悢
	 * @throws Exception
	 */
	public int getPasswordTerm() throws Exception {
		int days = 0;
		// 僐乕僪偺庢摼
		MCodeDto dto = findForDto(CommonConst.TID_PASSWORD_TERM);
		if (dto != null) {
			days = Integer.parseInt(dto.getTChange());
		}
		return days;
	}

	/**
	 * 桳媼孞墇忣曬
	 * @return	String[]	桳媼孞墇忣曬
	 * <p>
	 * [0]...孞墇寧丄
	 * [1]...孞墇擔悢丄
	 * [2]...孞墇嬫暘
	 * </p>
	 * @throws Exception 
	 */
	public String[] getPaidHolidayInfo() throws Exception {
		String[] aryInfo = new String[3];
		// 孞墇寧
		aryInfo[0] = String.valueOf(getCarriedForwardMonth());
		// 孞墇擔悢
		aryInfo[1] = String.valueOf(getCarriedForwardDays());
		// 孞墇嬫暘
		aryInfo[2] = getCarriedForwardType();
		return aryInfo;
	}

	/**
	 * 孞墇寧
	 * @return	孞墇寧
	 * @throws Exception
	 */
	public int getCarriedForwardMonth() throws Exception {
		return NumberPart.getIntParam(findTchange(
				CommonConst.NAM_PAID_HOLIDAY,
				CommonConst.NAM_CARRIED_FORWARD + CommonConst.NAM_MONTH)
		);
	}

	/**
	 * 孞墇擔悢
	 * @return	孞墇擔悢
	 * @throws Exception
	 */
	public int getCarriedForwardDays() throws Exception {
		return NumberPart.getIntParam(findTchange(
				CommonConst.NAM_PAID_HOLIDAY,
				CommonConst.NAM_CARRIED_FORWARD + CommonConst.NAM_NUMBER_OF_DAYS)
		);
	}

	/**
	 * 孞墇嬫暘
	 * @return	孞墇嬫暘
	 * @throws Exception
	 */
	public String getCarriedForwardType() throws Exception {
		return findTchange(
				CommonConst.NAM_PAID_HOLIDAY,
				CommonConst.NAM_CARRIED_FORWARD + CommonConst.NAM_COMMON_TYPE
		);
	}

	/**
	 * 媥壣棟桼攝楍庢摼
	 * @since	0.0.2
	 * @return	媥壣棟桼攝楍
	 * @throws Exception
	 */
	public String[][] getHolidayReasonArray() throws Exception {
		return getReasonCodeArray(CommonConst.TID_LEAVE_REASON);
	}

}
