/*
 * 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.common.part;

import java.util.ArrayList;
import java.util.List;

import jp.mosp.common.CommonConst;
import jp.mosp.common.MosPMenuConst;
import jp.mosp.common.dto.CmAuthorityDto;
import jp.mosp.common.dto.CmComponentDto;
import jp.mosp.common.part.AuthorityPart;
import jp.mosp.common.part.MosPMenuPart;
import jp.mosp.common.part.PartInfo;
import jp.mosp.common.utils.VariousUtil;
import jp.mosp.payroll.common.PayrollMenuConst;

/**
 * @author koike
 *
 */
public class PayrollMosPMenuPart extends MosPMenuPart {
	
	/**
	 * コンストラクタ
	 */
	public PayrollMosPMenuPart() {
		super();
	}
	
	/**
	 * コンストラクタ
	 * @param partInfo Partを利用する際に必要な情報
	 */
	public PayrollMosPMenuPart(PartInfo partInfo) {
		super(partInfo);
	}
	
	/**
	 * デフォルトDTO作成（CmComponent）
	 * <p>メニュー番号</p>
	 * 勤怠入力…{@link CommonConst#AUTH_EMPLOYEE_MENU}<br>
	 * 勤怠管理…{@link CommonConst#AUTH_MANAGER_MENU}<br>
	 * 人事管理…{@link CommonConst#AUTH_HUMAN_MENU}<br>
	 * 給与管理…{@link CommonConst#AUTH_PAYROLL_MENU}<br>
	 * 計算権限…{@link CommonConst#AUTH_CALCULATE_MENU}<br>
	 * @param menuNo		メニュー番号
	 * @return 対象メニューデフォルトDTO
	 */
	public static CmComponentDto getDefaultCompoDto(int menuNo) {
		CmComponentDto dto = new CmComponentDto();
		String[] aryDefault = getDefaultCompArray()[menuNo];
		dto.setDivision(aryDefault[0]);
		dto.setCompCode(aryDefault[0]);
		dto.setCompName(aryDefault[1]);
		dto.setManage(Integer.parseInt(aryDefault[2]));
		dto.setComponent(aryDefault[3]);
		return dto;
	}
	
	/**
	 * 全コマンドリスト生成
	 * @return	全コマンドリスト
	 */
	public static List<String[][]> getAllCmdArrayList() {
		List<String[][]> list = new ArrayList<String[][]>();
		list.add(PayrollMenuConst.getEmployeeCmdArray());
		list.add(MosPMenuConst.getCmdManagerArray());
		list.add(MosPMenuConst.getCmdHumanArray());
		list.add(PayrollMenuConst.getPayrollCmdArray());
		list.add(MosPMenuConst.getCmdSystemArray());
		return list;
	}
	
	/**
	 * 各デフォルト基本権限の配列作成 {division/code,name,manage,component}
	 * @return array
	 */
	public static String[][] getDefaultCompArray() {
		return new String[][]{
			{ CommonConst.CONF_ID_EMPLOYEE, CommonConst.NAM_AUTH_EMPLOYEE, CommonConst.NUM_BELONGING_SECTION,
				convAryToComponent(PayrollMenuConst.getEmployeeCmdArray()) },
			{ CommonConst.CONF_ID_MANAGER, CommonConst.NAM_AUTH_MANAGER, CommonConst.NUM_SELECT_SECTION,
				convAryToComponent(MosPMenuConst.getCmdManagerArray()) },
			{ CommonConst.CONF_ID_HUMAN, CommonConst.NAM_AUTH_HUMAN, CommonConst.NUM_ALL_SECTION,
				convAryToComponent(MosPMenuConst.getCmdHumanArray()) },
			{ CommonConst.CONF_ID_PAYROLL, CommonConst.NAM_AUTH_PAYROLL, CommonConst.NUM_ALL_SECTION,
				convAryToComponent(MosPMenuConst.getPayrollRestrictCalcArray()) },
			{ CommonConst.CONF_ID_CALCULATE, CommonConst.NAM_AUTH_CALCULATE, CommonConst.NUM_ALL_SECTION,
				convAryToComponent(PayrollMenuConst.getPayrollCmdArray()) }, };
	}
	
	/**
	 * 従業員コマンド情報取得
	 * @return 従業員コマンド情報
	 * @throws Exception	例外処理が発生した場合
	 */
	@Override
	public CmComponentDto getEmployeeComponentInfo() throws Exception {
		CmComponentDto componentDto = null;
		CmAuthorityDto dto = part().authority().getUserAuthDto();
		if (VariousUtil.isNotNull(dto)) {
			componentDto = part().component().findForKey(dto.getEmployee());
		}
		// デフォルトコードの設定
		if (VariousUtil.isNull(componentDto)) {
			componentDto = getDefaultCompoDto(CommonConst.AUTH_EMPLOYEE_MENU);
		}
		return componentDto;
	}
	
	/**
	 * 給与コマンド情報取得
	 * @return	給与コマンド情報
	 * @throws Exception	例外処理が発生した場合
	 */
	@Override
	public CmComponentDto getPayrollComponentInfo() throws Exception {
		CmComponentDto componentDto = null;
		CmAuthorityDto dto = part().authority().getUserAuthDto();
		if (VariousUtil.isNotNull(dto)) {
			componentDto = part().component().findForKey(dto.getPayroll());
		}
		// デフォルトコードの設定
		if (VariousUtil.isNull(componentDto)) {
			if (AuthorityPart.isCalculateAuth(user.getAuthority())) {
				componentDto = getDefaultCompoDto(CommonConst.AUTH_CALCULATE_MENU);
			} else {
				componentDto = getDefaultCompoDto(CommonConst.AUTH_PAYROLL_MENU);
			}
		}
		return componentDto;
	}
	
}
