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

import java.io.Serializable;
import java.util.Comparator;

import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.common.dto.HumanBasisDtoInterface;
import jp.mosp.common.utils.DateUtil;
import jp.mosp.common.utils.StringUtil;
import jp.mosp.kintai.common.action.MosPWorkflowAction;
import jp.mosp.kintai.vo.EmployeeApprovalListVo;

/**
 * @author yoshida
 *
 */
public class EmployeeApprovalListAction extends MosPWorkflowAction {
	
	// コマンド
	private static final String	CMD_APPROVAL_LIST_SHOW		= "K2110";
	private static final String	CMD_APPROVAL_LIST_RE_SEARCH	= "K2112";
	private static final String	CMD_APPROVAL_LIST_SORT		= "K2113";
	

	/**
	 * コンストラクタ
	 */
	public EmployeeApprovalListAction() {
		super();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得
		prepareVo();
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_APPROVAL_LIST_SHOW)) {
			// 検索
			search();
		} else if (cmd.equals(CMD_APPROVAL_LIST_RE_SEARCH)) {
			// 再検索
			research();
		} else if (cmd.equals(CMD_APPROVAL_LIST_SORT)) {
			// ソート
			sortList(key1);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new EmployeeApprovalListVo();
	}
	
	@Override
	protected void init() throws Exception {
		// MANAGER権限確認
		confirmManagerAuth();
		EmployeeApprovalListVo vo = (EmployeeApprovalListVo)getVo();
		// ユーザー情報取得
		getInfo(user.getUserId());
		// プルダウン取得
		vo.arySectionAll = getSectionArrayAll();
		vo.aryPositionAll = getPositionArray();
		vo.aryPltYear = StringUtil.getYearArray(MospUtility.getYear(getCalcMonth()));
		vo.aryPltMonth = StringUtil.getMonthArray();
		// 対象年月日取得
		vo.setParams(request);
		if (StringUtil.isNull(vo.getPltSelectYear())) {
			vo.setPltSelectYear(String.valueOf(MospUtility.getYear(getCalcMonth())));
			vo.setPltSelectMonth(String.valueOf(MospUtility.getMonth(getCalcMonth())));
		}
		// 人事基本情報設定
		setLabelBasisInfo(userBasis);
	}
	
	@Override
	protected void search() throws Exception {
		EmployeeApprovalListVo vo = (EmployeeApprovalListVo)getVo();
		serachEmployeeList();
		vo.setFormerKey(vo.KEY_K_CODE);
		vo.setIsAscending(true);
		vo.initListInfo();
		sortList();
	}
	
	@Override
	protected void research() throws Exception {
		EmployeeApprovalListVo vo = (EmployeeApprovalListVo)getVo();
		serachEmployeeList();
		vo.setPageButton();
		sortList();
	}
	
	/**
	 * 社員リスト検索
	 * @throws Exception	例外処理が発生した場合
	 */
	private void serachEmployeeList() throws Exception {
		EmployeeApprovalListVo vo = (EmployeeApprovalListVo)getVo();
		// VO設定
		vo.setFields(getApprovalEmployeeList(DateUtil
			.getFirstDateOfMonth(vo.getPltSelectYear(), vo.getPltSelectMonth())));
		// 対象が無かった場合のメッセージ設定
		if (vo.getAryKCode().length == 0) {
			addErrMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		}
	}
	
	@Override
	protected Comparator<Object> getComparator(String key) throws MospException {
		EmployeeApprovalListVo vo = (EmployeeApprovalListVo)getVo();
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_K_CODE)) {
			comp = new CompKCode();
		} else if (key.equals(vo.KEY_K_NAME)) {
			comp = new CompKName();
		} else if (key.equals(vo.KEY_SECTION)) {
			comp = new CompSection();
		} else if (key.equals(vo.KEY_POSITION)) {
			comp = new CompPosition();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}
	

	/**
	 * 社員コードによる比較クラス
	 */
	protected static class CompKCode implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 1321988381196408314L;
		

		public int compare(Object obj1, Object obj2) {
			HumanBasisDtoInterface dto1 = (HumanBasisDtoInterface)obj1;
			HumanBasisDtoInterface dto2 = (HumanBasisDtoInterface)obj2;
			return dto1.getKCode().compareTo(dto2.getKCode());
		}
	}
	
	/**
	 * 社員名による比較クラス
	 */
	protected static class CompKName implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -2913545521784316945L;
		

		public int compare(Object obj1, Object obj2) {
			HumanBasisDtoInterface dto1 = (HumanBasisDtoInterface)obj1;
			HumanBasisDtoInterface dto2 = (HumanBasisDtoInterface)obj2;
			return dto1.getSimei().compareTo(dto2.getSimei());
		}
	}
	
	/**
	 * 所属による比較クラス
	 */
	protected static class CompSection implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 3158913862750258976L;
		

		public int compare(Object obj1, Object obj2) {
			HumanBasisDtoInterface dto1 = (HumanBasisDtoInterface)obj1;
			HumanBasisDtoInterface dto2 = (HumanBasisDtoInterface)obj2;
			return dto1.getSCode().compareTo(dto2.getSCode());
		}
	}
	
	/**
	 * 職位による比較クラス
	 */
	protected static class CompPosition implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 3947836481650506034L;
		

		public int compare(Object obj1, Object obj2) {
			HumanBasisDtoInterface dto1 = (HumanBasisDtoInterface)obj1;
			HumanBasisDtoInterface dto2 = (HumanBasisDtoInterface)obj2;
			//役職が等しい場合、等級で比較
			if (dto1.getYCode().equals(dto2.getYCode())) {
				return dto1.getTokyu().compareTo(dto2.getTokyu());
			} else {
				return dto1.getYCode().compareTo(dto2.getYCode());
			}
		}
	}
	
}
