/*
 * 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.workflow.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.kintai.common.action.MosPWorkflowAction;
import jp.mosp.workflow.dto.AmUnitDto;
import jp.mosp.workflow.vo.ApprovalUnitListVo;

/**
 * @author yoshida
 *
 */
public class ApprovalUnitListAction extends MosPWorkflowAction {
	
	// コマンド
	private static final String	CMD_UNIT_LIST_SHOW		= "S2810";
	private static final String	CMD_UNIT_LIST_SEARCH	= "S2811";
	private static final String	CMD_UNIT_LIST_RE_SEARCH	= "S2812";
	private static final String	CMD_UNIT_LIST_SORT		= "S2813";
	

	/**
	 * コンストラクタ
	 */
	public ApprovalUnitListAction() {
		super();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得
		prepareVo();
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_UNIT_LIST_SHOW)) {
			show();
		} else if (cmd.equals(CMD_UNIT_LIST_SEARCH)) {
			// 検索
			search();
		} else if (cmd.equals(CMD_UNIT_LIST_RE_SEARCH)) {
			// 再検索
			research();
		} else if (cmd.equals(CMD_UNIT_LIST_SORT)) {
			// ソート
			sortList(key1);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new ApprovalUnitListVo();
	}
	
	@Override
	protected void init() throws Exception {
		// DBコネクション取得
		getConnection();
		// SYSTEM権限確認
		confirmSystemAuth();
		ApprovalUnitListVo vo = (ApprovalUnitListVo)getVo();
		// 人事基本情報取得
		vo.aryPltUser = part().human().getBasisArrayAll();
	}
	
	@Override
	protected void show() throws Exception {
		ApprovalUnitListVo vo = (ApprovalUnitListVo)getVo();
		// 検索履歴を初期値にセット
		vo.setSearchmode(ApprovalUnitListVo.SEARCH_OFF);
	}
	
	@Override
	protected void search() throws Exception {
		ApprovalUnitListVo vo = (ApprovalUnitListVo)getVo();
		vo.setParams(request);
		searchUnitList();
		vo.setFormerKey(vo.KEY_UNIT_CODE);
		vo.setIsAscending(true);
		vo.initListInfo();
	}
	
	@Override
	protected void research() throws Exception {
		ApprovalUnitListVo vo = (ApprovalUnitListVo)getVo();
		if (vo.getSearchmode() == ApprovalUnitListVo.SEARCH_ON) {
			removeSessionDto();
			searchUnitList();
			vo.setPageButton();
			sortList();
		}
	}
	
	/**
	 * ユニットリスト検索
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void searchUnitList() throws Exception {
		ApprovalUnitListVo vo = (ApprovalUnitListVo)getVo();
		// 検索
		vo.setFields(unitDao().findForCondition(vo.getTxtUnitCode(), vo.getTxtKCode(), vo.getTxtKName()));
		// 対象が無かった場合のメッセージ設定
		if (vo.getEscAryUnit().length == 0) {
			vo.addErrMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		}
		// モード設定
		vo.setSearchmode(ApprovalUnitListVo.SEARCH_ON);
	}
	
	@Override
	protected void sortList(String key) throws MospException {
		ApprovalUnitListVo vo = (ApprovalUnitListVo)getVo();
		vo.sortList(key, getComparator(key));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected void sortList() throws MospException {
		ApprovalUnitListVo vo = (ApprovalUnitListVo)getVo();
		vo.sortList(getComparator(vo.getFormerKey()));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected Comparator<Object> getComparator(String key) throws MospException {
		ApprovalUnitListVo vo = (ApprovalUnitListVo)getVo();
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_UNIT_CODE)) {
			comp = new CompUnitCode();
		} else if (key.equals(vo.KEY_MAIN)) {
			comp = new CompMain();
		} else if (key.equals(vo.KEY_AGENT)) {
			comp = new CompAgent();
		} else if (key.equals(vo.KEY_START_DATE)) {
			comp = new CompStartDate();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}
	

	/**
	 * ユニットコードによる比較クラス
	 */
	protected static class CompUnitCode implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 8882101817659803340L;
		

		public int compare(Object obj1, Object obj2) {
			AmUnitDto dto1 = (AmUnitDto)obj1;
			AmUnitDto dto2 = (AmUnitDto)obj2;
			return dto1.getUnitCode().compareTo(dto2.getUnitCode());
		}
	}
	
	/**
	 * 承認者による比較クラス
	 */
	protected static class CompMain implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -1958254792298208211L;
		

		public int compare(Object obj1, Object obj2) {
			AmUnitDto dto1 = (AmUnitDto)obj1;
			AmUnitDto dto2 = (AmUnitDto)obj2;
			return dto1.getMain().compareTo(dto2.getMain());
		}
	}
	
	/**
	 * 代理による比較クラス
	 */
	protected static class CompAgent implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 1656557705586257941L;
		

		public int compare(Object obj1, Object obj2) {
			AmUnitDto dto1 = (AmUnitDto)obj1;
			AmUnitDto dto2 = (AmUnitDto)obj2;
			return dto1.getAgent().compareTo(dto2.getAgent());
		}
	}
	
	/**
	 * 開始日による比較クラス
	 */
	protected static class CompStartDate implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 3154045096780718675L;
		

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