/*
 * 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.MosPAction;
import jp.mosp.common.common.BaseVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.dto.CmCodeDto;
import jp.mosp.kintai.system.vo.CmCodeListVo;

/**
 * @author yoshida
 *
 */
public class CmCodeListAction extends MosPAction {
	
	// コマンド
	private static final String	CMD_CODE_CARD_SHOW		= "S2000";	// 表示
	private static final String	CMD_CODE_CARD_SELECT	= "S2001";	// 検索
	private static final String	CMD_CODE_LIST_RE_SEARCH	= "S2002";	// 再検索
	private static final String	CMD_CODE_CARD_SORT		= "S2003";	// ソート
																	
	
	/**
	 * コンストラクタ
	 */
	public CmCodeListAction() {
		super();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// VOの取得
		prepareVo();
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_CODE_CARD_SHOW)) {
			show();
		} else if (cmd.equals(CMD_CODE_CARD_SELECT)) {
			// 検索処理
			search();
		} else if (cmd.equals(CMD_CODE_LIST_RE_SEARCH)) {
			// 再検索
			research();
		} else if (cmd.equals(CMD_CODE_CARD_SORT)) {
			// ソート
			sortList(key1);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new CmCodeListVo();
	}
	
	@Override
	protected void init() throws Exception {
		// DBコネクション取得
		getConnection();
		// SYSTEM権限確認
		confirmSystemAuth();
		CmCodeListVo vo = (CmCodeListVo)getVo();
		// コード区分プルダウンリスト作成
		vo.aryPltTId = getCodeArray();
	}
	
	@Override
	protected void show() throws Exception {
		CmCodeListVo vo = (CmCodeListVo)getVo();
		// 削除フラグのプルダウンリスト作成
		vo.setPltDelete(String.valueOf(MospConst.DELETE_FLAG_OFF));
		// 検索履歴を初期値にセット
		vo.setSearchmode(0);
	}
	
	@Override
	protected void search() throws Exception {
		CmCodeListVo vo = (CmCodeListVo)getVo();
		vo.setParams(request);
		searchCodeList();
		vo.setIsAscending(true);
		vo.initListInfo();
		// 検索履歴をありにセット
		vo.setSearchmode(1);
	}
	
	@Override
	protected void research() throws Exception {
		CmCodeListVo vo = (CmCodeListVo)getVo();
		if (vo.getSearchmode() == 1) {
			removeSessionDto();
			searchCodeList();
			vo.setPageButton();
			sortList();
		}
	}
	
	/**
	 * コードリスト検索
	 * @throws Exception 	例外処理が発生した場合
	 */
	private void searchCodeList() throws Exception {
		CmCodeListVo vo = (CmCodeListVo)getVo();
		// 検索
		vo.setFields(part().cmcode().getCmCodeDao().findForSearch(vo.getPltTId(), vo.getTxtTCode(), vo.getTxtTName(),
				vo.getPltDelete()));
		setCodeTypeName();
		// 対象が無かった場合のメッセージ設定
		if (vo.getAryTCode().length == 0) {
			addErrMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		}
	}
	
	/**
	 * コード区分名称設定
	 * @throws Exception  	例外処理が発生した場合
	 */
	private void setCodeTypeName() throws Exception {
		CmCodeListVo vo = (CmCodeListVo)getVo();
		String[] aryCodeTypeName = new String[vo.getList().size()];
		for (int i = 0; i < vo.getList().size(); i++) {
			CmCodeDto dto = (CmCodeDto)vo.getList().get(i);
			aryCodeTypeName[i] = getCodeTypeName(dto.getTId());
		}
		vo.setAryTIdName(aryCodeTypeName);
	}
	
	@Override
	protected Comparator<Object> getComparator(String key) throws MospException {
		CmCodeListVo vo = (CmCodeListVo)getVo();
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_T_ID)) {
			comp = new CompTId();
		} else if (key.equals(vo.KEY_T_CODE)) {
			comp = new CompTCode();
		} else if (key.equals(vo.KEY_T_NAME)) {
			comp = new CompTName();
		} else if (key.equals(vo.KEY_VIEWINDEX)) {
			comp = new CompViewIndex();
		} else if (key.equals(vo.KEY_DELETE)) {
			comp = new CompDeleteFlag();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}
	

	/**
	 * コード区分による比較クラス
	 */
	protected static class CompTId implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 7210303772053153377L;
		

		public int compare(Object obj1, Object obj2) {
			CmCodeDto dto1 = (CmCodeDto)obj1;
			CmCodeDto dto2 = (CmCodeDto)obj2;
			return dto1.getTId().compareTo(dto2.getTId());
		}
	}
	
	/**
	 * コードによる比較クラス
	 */
	protected static class CompTCode implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 5672044129524635759L;
		

		public int compare(Object obj1, Object obj2) {
			CmCodeDto dto1 = (CmCodeDto)obj1;
			CmCodeDto dto2 = (CmCodeDto)obj2;
			return dto1.getTCode().compareTo(dto2.getTCode());
		}
	}
	
	/**
	 * コード名称による比較クラス
	 */
	protected static class CompTName implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -8615320106870669507L;
		

		public int compare(Object obj1, Object obj2) {
			CmCodeDto dto1 = (CmCodeDto)obj1;
			CmCodeDto dto2 = (CmCodeDto)obj2;
			return dto1.getTName().compareTo(dto2.getTName());
		}
	}
	
	/**
	 * 表示順による比較クラス
	 */
	protected static class CompViewIndex implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 2787828366453888987L;
		

		public int compare(Object obj1, Object obj2) {
			CmCodeDto dto1 = (CmCodeDto)obj1;
			CmCodeDto dto2 = (CmCodeDto)obj2;
			return dto1.getViewIndex() - (dto2.getViewIndex());
		}
	}
	
	/**
	 * 削除フラグによる比較クラス
	 */
	static class CompDeleteFlag implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -1824284390861851592L;
		

		public int compare(Object obj1, Object obj2) {
			CmCodeDto dto1 = (CmCodeDto)obj1;
			CmCodeDto dto2 = (CmCodeDto)obj2;
			return dto2.getDeleteFlag() - dto1.getDeleteFlag();
		}
	}
	
}
