/*
 * 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.payroll.common.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.payroll.base.PayrollAction;
import jp.mosp.payroll.base.PayrollConst;
import jp.mosp.payroll.common.vo.ImportMasterListVo;
import jp.mosp.payroll.dao.MImportDao;
import jp.mosp.payroll.dto.MImportDto;

public class ImportMasterListAction extends PayrollAction {
	
	// コマンド
	private static final String	CMD_IMPORT_MASTER_LIST_SHOW			= "P9020";	// 表示
	private static final String	CMD_IMPORT_MASTER_LIST_SEARCH		= "P9021";	// 検索
	private static final String	CMD_IMPORT_MASTER_LIST_RE_SEARCH	= "P9022";	// 再検索
	private static final String	CMD_IMPORT_MASTER_LIST_SORT			= "P9023";	// ソート
																				
	
	/**
	 * コンストラクタ
	 */
	public ImportMasterListAction() {
		super();
		setNeedProcSeq(false);
		
	}
	
	/* (non-Javadoc)
	 * @see jp.mosp.common.common.BaseAction#getSpecificVo()
	 */
	@Override
	protected BaseVo getSpecificVo() {
		return new ImportMasterListVo();
	}
	
	/**
	 * アクション
	 */
	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// VO取得及び設定
		prepareVo();
		// プルダウンテーブル設定
		setPulldown();
		
		// コマンド毎の処理
		if (cmd.equals(CMD_IMPORT_MASTER_LIST_SHOW)) {
			// 表示
			show();
		} else if (cmd.equals(CMD_IMPORT_MASTER_LIST_SEARCH)) {
			// 検索
			search();
		} else if (cmd.equals(CMD_IMPORT_MASTER_LIST_RE_SEARCH)) {
			// 再検索
			reSearch();
		} else if (cmd.equals(CMD_IMPORT_MASTER_LIST_SORT)) {
			// ソート
			sort();
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	/**
	 * 表示
	 */
	@Override
	public void show() {
		// VO取得
		ImportMasterListVo vo = (ImportMasterListVo)getVo();
		// 検索履歴を初期値にセット
		vo.setSearchmode(ImportMasterListVo.SEARCH_OFF);
	}
	
	/**
	 * 検索
	 * @throws Exception
	 */
	@Override
	public void search() throws Exception {
		// VO取得
		ImportMasterListVo vo = (ImportMasterListVo)getVo();
		// パラメータ取得
		vo.setParams(request);
		// 検索
		searchImportMasterList();
		vo.setFormerKey(vo.KEY_I_CODE);
		vo.setIsAscending(true);
		vo.initListInfo();
		// 検索履歴をありにセット
		vo.setSearchmode(ImportMasterListVo.SEARCH_ON);
	}
	
	/**
	 * 再検索
	 * @throws Exception
	 */
	private void reSearch() throws Exception {
		// VO取得
		ImportMasterListVo vo = (ImportMasterListVo)getVo();
		if (vo.getSearchmode() == ImportMasterListVo.SEARCH_ON) {
			session.removeAttribute(MospConst.ATT_DTO);
			searchImportMasterList();
			vo.setPageButton();
			sortList(vo);
		}
	}
	
	/**
	 * ソート
	 * @throws Exception
	 */
	private void sort() throws Exception {
		/* Makeing by y.mikami  */
	}
	
	/**
	 * プルダウン設定
	 */
	private void setPulldown() {
		// VO取得
		ImportMasterListVo vo = (ImportMasterListVo)getVo();
		// プルダウンの設定
		vo.aryPltITable = getImportTableArray();
	}
	
	/**
	 * インポートテーブルプルダウン取得
	 * @return インポートテーブルプルダウン用文字列配列
	 */
	protected String[][] getImportTableArray() {
		String[][] aryImportTable = new String[3][2];
		aryImportTable[0][0] = PayrollConst.CODE_IMPORT_TABLE_PI_PAYMENT;
		aryImportTable[0][1] = PayrollConst.NAM_IMPORT_TABLE_PI_PAYMENT;
		aryImportTable[1][0] = PayrollConst.CODE_IMPORT_TABLE_M_SYUKKINBO;
		aryImportTable[1][1] = PayrollConst.NAM_IMPORT_TABLE_M_SYUKKINBO;
		aryImportTable[2][0] = PayrollConst.CODE_IMPORT_TABLCT_TIMECARD;
		aryImportTable[2][1] = PayrollConst.NAM_IMPORT_TABLCT_TIMECARD;
		return aryImportTable;
	}
	
	/**
	 * インポートリスト検索
	 * @throws Exception
	 */
	private void searchImportMasterList() throws Exception {
		// VO取得
		ImportMasterListVo vo = (ImportMasterListVo)getVo();
		// DAOの準備
		MImportDao dao = new MImportDao();
		dao.initDao(cfg, cmd, aspUser, user, conn);
		// VOに設定
		vo.setFields(dao.findForCondition(vo.getTxtICode(), vo.getTxtIName(), vo.getPltITable()));
		// 対象が無かった場合のメッセージ設定
		if (vo.getEscAryICode().length == 0) {
			vo.addErrMessage(MospUtility.getMessage(msg, MospConst.MSG_NO_DATA, ""));
		}
	}
	
	/**
	 * ソート(VO内のキー情報でソート)
	 * @param vo ソート対象Listを含むVO
	 * @throws MospException
	 */
	private void sortList(ImportMasterListVo vo) throws MospException {
		vo.sortList(getComparator(vo.getFormerKey(), vo));
		vo.setFields(vo.getList());
	}
	
	/**
	 * 比較クラス取得
	 * @param key ソートキー
	 * @param vo ソート対象Listを含むVO
	 * @throws MospException 
	 * @throws MospException
	 */
	private Comparator<Object> getComparator(String key, ImportMasterListVo vo) throws MospException {
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_I_CODE)) {
			comp = new CompICode();
		} else if (key.equals(vo.KEY_I_NAME)) {
			comp = new CompIName();
		} else if (key.equals(vo.KEY_I_TABLE)) {
			comp = new CompITable();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}
	

	/**
	 * インポートコードによる比較クラス
	 */
	protected static class CompICode implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -4919768827859268739L;
		

		public int compare(Object obj1, Object obj2) {
			MImportDto dto1 = (MImportDto)obj1;
			MImportDto dto2 = (MImportDto)obj2;
			return dto1.getICode().compareTo(dto2.getICode());
		}
	}
	
	/**
	 * インポート名称による比較クラス
	 */
	protected static class CompIName implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -5780195486773585945L;
		

		public int compare(Object obj1, Object obj2) {
			MImportDto dto1 = (MImportDto)obj1;
			MImportDto dto2 = (MImportDto)obj2;
			return dto1.getIName().compareTo(dto2.getIName());
		}
	}
	
	/**
	 * インポート名称による比較クラス
	 */
	protected static class CompITable implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 2237491082499752665L;
		

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