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

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

import jp.mosp.common.CommonConst;
import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.utils.StringUtil;
import jp.mosp.kintai.common.action.KintaiCommonAction;
import jp.mosp.kintai.common.dto.MYakusyokuDto;
import jp.mosp.kintai.system.vo.YakusyokuMasterListVo;

/**
 * @author yoshida
 *
 */
public class YakusyokuMasterListAction extends KintaiCommonAction {
	
	private static final String	CMD_YAKUSYOKU_MASTER_LIST_SHOW		= "S2200";
	private static final String	CMD_YAKUSYOKU_MASTER_LIST_SEARCH	= "S2201";
	private static final String	CMD_YAKUSYOKU_MASTER_LIST_RE_SEARCH	= "S2202";
	private static final String	CMD_YAKUSYOKU_MASTER_LIST_SORT		= "S2203";
	

	/**
	 * コンストラクタ
	 */
	public YakusyokuMasterListAction() {
		super();
	}
	
	public void action() throws Exception {
		// VOの取得
		prepareVo();
		// 初期処理 
		init();
		// コマンド毎の処理		
		if (cmd.equals(CMD_YAKUSYOKU_MASTER_LIST_SHOW)) {
			show();
		} else if (cmd.equals(CMD_YAKUSYOKU_MASTER_LIST_SEARCH)) {
			// 検索処理
			search();
		} else if (cmd.equals(CMD_YAKUSYOKU_MASTER_LIST_RE_SEARCH)) {
			// 再検索
			research();
		} else if (cmd.equals(CMD_YAKUSYOKU_MASTER_LIST_SORT)) {
			// ソート
			sortList(key1);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new YakusyokuMasterListVo();
	}
	
	@Override
	protected void init() throws Exception {
		// DBコネクション取得
		getConnection();
		// PAYROLL権限確認
		confirmSystemAuth();
		YakusyokuMasterListVo vo = (YakusyokuMasterListVo)getVo();
		// 等級区分プルダウンリスト作成
		vo.aryPltTokyu = StringUtil.addArrayFirstRowSpace(getMosPCodeArray(CommonConst.TID_GRADE));
	}
	
	@Override
	protected void show() throws Exception {
		YakusyokuMasterListVo vo = (YakusyokuMasterListVo)getVo();
		// 検索履歴を初期値にセット	
		vo.setSearchmode(YakusyokuMasterListVo.SEARCH_OFF);
	}
	
	@Override
	protected void search() throws Exception {
		YakusyokuMasterListVo vo = (YakusyokuMasterListVo)getVo();
		vo.setParams(request);
		searchPositionList();
		vo.setFormerKey(vo.KEY_YCODE);
		vo.setIsAscending(true);
		vo.initListInfo();
		// 検索履歴をありにセット
		vo.setSearchmode(YakusyokuMasterListVo.SEARCH_ON);
	}
	
	@Override
	protected void research() throws Exception {
		YakusyokuMasterListVo vo = (YakusyokuMasterListVo)getVo();
		if (vo.getSearchmode() == YakusyokuMasterListVo.SEARCH_ON) {
			removeSessionDto();
			searchPositionList();
			vo.setPageButton();
			sortList();
		}
	}
	
	/**
	 * 役職リスト検索
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void searchPositionList() throws Exception {
		YakusyokuMasterListVo vo = (YakusyokuMasterListVo)getVo();
		vo.setFields(part().position().getMYakusyokuDao().findForCondition(vo.getTxtYCode(), vo.getPltTokyu(),
				vo.getTxtYName()));
		// 対象が無かった場合のメッセージ設定
		if (vo.getAryYCode().length == 0) {
			vo.addErrMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		}
	}
	
	@Override
	protected void sortList(String key) throws MospException {
		YakusyokuMasterListVo vo = (YakusyokuMasterListVo)getVo();
		vo.sortList(key, getComparator(key));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected void sortList() throws MospException {
		YakusyokuMasterListVo vo = (YakusyokuMasterListVo)getVo();
		vo.sortList(getComparator(vo.getFormerKey()));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected Comparator<Object> getComparator(String key) throws MospException {
		YakusyokuMasterListVo vo = (YakusyokuMasterListVo)getVo();
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_YCODE)) {
			comp = new CompYCode();
		} else if (key.equals(vo.KEY_YNAME)) {
			comp = new CompYName();
		} else if (key.equals(vo.KEY_TOKYU)) {
			comp = new CompTokyu();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}
	

	/**
	 * 役職コードによる比較クラス
	 */
	protected static class CompYCode implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -5729849389393964036L;
		

		public int compare(Object obj1, Object obj2) {
			MYakusyokuDto dto1 = (MYakusyokuDto)obj1;
			MYakusyokuDto dto2 = (MYakusyokuDto)obj2;
			return dto1.getYcode().compareTo(dto2.getYcode());
		}
	}
	
	/**
	 * 役職名称による比較クラス
	 */
	protected static class CompYName implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 1765058312562701593L;
		

		public int compare(Object obj1, Object obj2) {
			MYakusyokuDto dto1 = (MYakusyokuDto)obj1;
			MYakusyokuDto dto2 = (MYakusyokuDto)obj2;
			return dto1.getYname().compareTo(dto2.getYname());
		}
	}
	
	/**
	 * 等級による比較クラス
	 */
	protected static class CompTokyu implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -3586842444641043500L;
		

		public int compare(Object obj1, Object obj2) {
			MYakusyokuDto dto1 = (MYakusyokuDto)obj1;
			MYakusyokuDto dto2 = (MYakusyokuDto)obj2;
			return dto1.getTokyu().compareTo(dto2.getTokyu());
		}
	}
	
}
