/*
 * 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.system.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.KintaiCommonAction;
import jp.mosp.kintai.common.dto.MSyozokuDto;
import jp.mosp.system.vo.SyozokuMasterListVo;

/**
 * @author yoshida
 *
 */
public class SyozokuMasterListAction extends KintaiCommonAction {
	
	// コマンド
	private static final String	CMD_SYOZOKU_LIST_SHOW		= "S2100";	// 表示
	private static final String	CMD_SYOZOKU_LIST_SEARCH		= "S2101";	// 検索
	private static final String	CMD_SYOZOKU_LIST_RE_SEARCH	= "S2102";	// 再検索
	private static final String	CMD_SYOZOKU_TYPE_LIST_SORT	= "S2103";	// ソート
																		
	
	/**
	 * コンストラクタ
	 */
	public SyozokuMasterListAction() {
		super();
	}
	
	public void action() throws Exception {
		// VOの取得
		prepareVo();
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_SYOZOKU_LIST_SHOW)) {
			show();
		} else if (cmd.equals(CMD_SYOZOKU_LIST_SEARCH)) {
			// 検索処理
			search();
		} else if (cmd.equals(CMD_SYOZOKU_LIST_RE_SEARCH)) {
			// 再検索
			research();
		} else if (cmd.equals(CMD_SYOZOKU_TYPE_LIST_SORT)) {
			// ソート
			sortList(key1);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new SyozokuMasterListVo();
	}
	
	@Override
	protected void init() throws Exception {
		// DBコネクション取得
		getConnection();
		// SYSTEM権限確認
		confirmSystemAuth();
	}
	
	@Override
	protected void show() throws Exception {
		SyozokuMasterListVo vo = (SyozokuMasterListVo)getVo();
		// 検索履歴を初期値にセット	
		vo.setSearchmode(SyozokuMasterListVo.SEARCH_OFF);
	}
	
	@Override
	protected void search() throws Exception {
		SyozokuMasterListVo vo = (SyozokuMasterListVo)getVo();
		vo.setParams(request);
		searchSyozokuMasterList();
		vo.setFormerKey(vo.KEY_S_CODE);
		vo.setIsAscending(true);
		vo.initListInfo();
		// 検索履歴をありにセット
		vo.setSearchmode(SyozokuMasterListVo.SEARCH_ON);
	}
	
	@Override
	protected void research() throws Exception {
		SyozokuMasterListVo vo = (SyozokuMasterListVo)getVo();
		if (vo.getSearchmode() == SyozokuMasterListVo.SEARCH_ON) {
			removeSessionDto();
			searchSyozokuMasterList();
			vo.setPageButton();
			sortList();
		}
	}
	
	/**
	 * 所属リスト検索
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void searchSyozokuMasterList() throws Exception {
		SyozokuMasterListVo vo = (SyozokuMasterListVo)getVo();
		vo.setFields(part().section().getMSyozokuDao().findForCondition(vo.getTxtSCode(), vo.getTxtSName()));
		// 対象がなかった場合のメッセージ設定
		if (vo.getArySCode().length == 0) {
			vo.addErrMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		}
	}
	
	@Override
	protected void sortList(String key) throws MospException {
		SyozokuMasterListVo vo = (SyozokuMasterListVo)getVo();
		vo.sortList(key, getComparator(key));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected void sortList() throws MospException {
		SyozokuMasterListVo vo = (SyozokuMasterListVo)getVo();
		vo.sortList(getComparator(vo.getFormerKey()));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected Comparator<Object> getComparator(String key) throws MospException {
		SyozokuMasterListVo vo = (SyozokuMasterListVo)getVo();
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_S_CODE)) {
			comp = new CompSyozokuCode();
		} else if (key.equals(vo.KEY_S_NAME)) {
			comp = new CompSyozokuName();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}
	

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

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

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