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

import jp.mosp.common.CommonConst;
import jp.mosp.common.MosPMenuConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.dao.CmAuthorityDao;

import jp.mosp.common.dto.CmAuthorityDto;
import jp.mosp.common.dto.CmComponentDto;

/**
 * 権限に関するPartクラス
 * @author yoshida
 *	@version	0.0.1
 */
public class AuthorityPart extends BasePart {

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

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

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

	/**
	 * 権限情報の取得
	 * @param userAuth	対象権限コード
	 * @return			権限情報
	 * @throws Exception
	 */
	public CmAuthorityDto findForKey(String userAuth) throws Exception {
		return	getCmAuthorityDao().findForKey(userAuth);
	}

	/**
	 * 権限コードリスト取得(CM_AUTHORITY)
	 * @return コードリスト
	 * @throws NoSuchFieldException 
	 * @throws IllegalAccessException 
	 * @throws MospException 
	 * @throws ClassNotFoundException 
	 */
	public String[][] getArrangeAuthArray() throws Exception {
		// データの取得
		List<CmAuthorityDto> list = getCmAuthorityDao().findForAvailable();
		// データの整形
		String[][] array = new String[list.size()][2];
		for (int i = 0; i < list.size(); i++) {
			CmAuthorityDto dto = list.get(i);
			array[i][0] = dto.getAuthCode();
			array[i][1] = dto.getAuthName();
		}
		return array;
	}

	/**
	 * 全権限コードリスト取得(CM_AUTHORITY)
	 * @return コードリスト
	 * @throws Exception 
	 */
	public String[][] getArrangeAuthArrayAll() throws Exception {
		// データの取得
		List<CmAuthorityDto> list = getCmAuthorityDao().findAll();
		// データの整形
		String[][] array = new String[list.size()][2];
		for (int i = 0; i < list.size(); i++) {
			CmAuthorityDto dto = list.get(i);
			array[i][0] = dto.getAuthCode();
			array[i][1] = dto.getAuthName();
		}
		return array;
	}


	/**
	 * MANAGER権限確認
	 * @return TRAINER権限有無(true：権限有、false：権限無)
	 * @throws Exception
	 */
	public boolean hasManagerAuth() throws Exception {
		// MANAGER権限確認
		boolean hasAuth = false;
		String userAuth = user.getAuthority();
		CmAuthorityDto dto = findForKey(userAuth);
		// デフォルトコードの設定
		if (dto == null 
				&& (userAuth.equals(CommonConst.AUTHORITY_MANAGER) 
						|| userAuth.equals(CommonConst.AUTHORITY_SENIORS)
				)
		) {
			dto = new CmAuthorityDto();
			dto.setManager(CommonConst.CONF_ID_MANAGER);	
		}
		if (dto != null && !dto.getManager().equals("")) {
			hasAuth = true;
		}		
		return hasAuth;
	}

	/**
	 * PAYROLL権限確認
	 * @return PAYROLL権限有無(true：権限有、false：権限無)
	 * @throws Exception
	 */
	public boolean hasPayrollAuth() throws Exception {
		// PAYROLL権限確認
		boolean hasAuth = false;
		String userAuth = user.getAuthority();
		CmAuthorityDto dto = findForKey(userAuth);
		// デフォルトコードの設定
		if (dto == null
				&& (userAuth.equals(CommonConst.AUTHORITY_PAYROLL) 
						|| userAuth.equals(CommonConst.AUTHORITY_SENIORS)
						|| userAuth.equals(CommonConst.AUTHORITY_CALCULATE)
				)
		) {
			dto = new CmAuthorityDto();
			dto.setPayroll(CommonConst.CONF_ID_PAYROLL);	
		}
		if (dto != null && !dto.getPayroll().equals("")) {
			hasAuth = true;
		}		
		return hasAuth;
	}


	/**
	 * HUMAN権限確認
	 * @return PAYROLL権限有無(true：権限有、false：権限無)
	 * @throws Exception
	 */
	public boolean hasHumanAuth() throws Exception {
		// HUMAN権限確認
		boolean hasAuth = false;
		String userAuth = user.getAuthority();
		CmAuthorityDto dto = findForKey(userAuth);
		// デフォルトコードの設定
		if (dto == null 
				&& (userAuth.equals(CommonConst.AUTHORITY_HUMAN) 
						|| userAuth.equals(CommonConst.AUTHORITY_SENIORS)
				)
		) {
			dto = new CmAuthorityDto();
			dto.setHuman(CommonConst.CONF_ID_HUMAN);
		}
		if (dto != null && !dto.getHuman().equals("")) {
			hasAuth = true;
		}
		return hasAuth;
	}

	/**
	 * CALCULATE権限確認
	 * @return CALCULATE権限有無(true：権限有、false：権限無)
	 * @throws Exception
	 */
	public boolean hasCalculateAuth() throws Exception {
		// CALCULATE権限確認
		boolean hasAuth = false;
		String userAuth = user.getAuthority();
		CmComponentDto cmComponentDto = null;
		CmAuthorityDto cmAuthorityDto = findForKey(userAuth);
		if (cmAuthorityDto != null) {
			cmComponentDto = component().findForDto(cmAuthorityDto.getPayroll(), CommonConst.CONF_ID_PAYROLL);
		}
		if (userAuth.equals(CommonConst.AUTHORITY_CALCULATE)) {
			// デフォルトコードの設定
			if (cmAuthorityDto == null) {
				cmAuthorityDto = new CmAuthorityDto();
				cmAuthorityDto.setPayroll(CommonConst.CONF_ID_CALCULATE);	
				cmComponentDto = component().findForDto(cmAuthorityDto.getPayroll(), CommonConst.CONF_ID_PAYROLL);
			}
			// デフォルトコードの設定
			if (cmComponentDto == null) {
				cmComponentDto = new CmComponentDto();
				cmComponentDto.setComponent(MosPMenuConst.getCalcCmdArray()[1]);	
			}			
		}
		if (cmComponentDto != null) {
			String [] aryCmd = StringPart.divideByComma(cmComponentDto.getComponent());
			for (int i = 0; i < aryCmd.length ; i++) {
				if (aryCmd[i].equals(MosPMenuConst.getCalcCmdArray()[1])
						|| aryCmd[i].equals(MosPMenuConst.getCalcCmdArray()[5])
						|| aryCmd[i].equals(MosPMenuConst.getPayrollCmdArray()[7][0])
						|| aryCmd[i].equals(MosPMenuConst.getPayrollCmdArray()[8][0])		
				) {
					hasAuth = true;
					break;
				}			
			}
		}
		return hasAuth;
	}

	/**
	 * 所属コード取得(所属範囲により変化)(M_SYOZOKU)
	 * @return 所属コードリスト
	 * @throws Exception
	 */
	public String[][] getSectionScopeArrayForPayroll() throws Exception {
		CmComponentDto cmComponentDto = new CmComponentDto();
		String userAuth = user.getAuthority();
		CmAuthorityDto cmAuthorityDto = findForKey(userAuth);
		String [][] ary = null ;
		if (cmAuthorityDto != null) {
			cmComponentDto = component().findForKey(cmAuthorityDto.getPayroll());
		}
		// デフォルトの設定
		if (userAuth.equals(CommonConst.AUTHORITY_CALCULATE)) {
			cmComponentDto.setManage(VariousPart.getManageCode()[2]);
		} else if (userAuth.equals(CommonConst.AUTHORITY_PAYROLL)) {
			cmComponentDto.setManage(VariousPart.getManageCode()[0]);
		}
		// プルダウン設定
		if (cmComponentDto.getManage() == VariousPart.getManageCode()[0]) {
			ary = section().getSectionArrayForManager();			
		} else if (cmComponentDto.getManage() == VariousPart.getManageCode()[1]) {
			ary = section().getSectionArrayAll();
		} else {
			ary = StringPart.addArrayFirstRowValue(
					section().getSectionArrayAll(),
					CommonConst.NAM_COMMON_STRING_INITIAL_VALUE,
					CommonConst.NAM_ALL_SECTION);
		}
		return ary;
	}

	/**
	 * SYSTEM権限確認
	 * @return SYSTEM権限有無(true：権限有、false：権限無)
	 * @throws Exception 
	 */
	public boolean hasSystemAuth() throws Exception {
		// ADMIN権限確認
		return user.getAuthority().equals(CommonConst.AUTH_SYSTEM);
	}

}
