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

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

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.utils.DateUtil;
import jp.mosp.common.utils.StringUtil;
import jp.mosp.common.utils.VariousUtil;
import jp.mosp.mospcommon.system.LaborAgreementManager;
import jp.mosp.mospcommon.system.dto.CmAgreementOverTimeDto;
import jp.mosp.mospcommon.system.dto.CmAgreementWorkOnDto;
import jp.mosp.mospcommon.system.dto.CmLaborAgreementDto;
import jp.mosp.mospcommon.system.vo.LaborAgreementListVo;

/**
 * @author matsusita
 *
 */
public class LaborAgreementListAction extends MosPAction {
	
	// コマンド
	private static final String	CMD_AGREEMENT_LIST_SHOW			= "S2510";
	private static final String	CMD_AGREEMENT_LIST_SEARCH		= "S2511";
	/**
	 * 再検索
	 */
	public static final String	CMD_AGREEMENT_LIST_RE_SEARCH	= "S2512";
	private static final String	CMD_AGREEMENT_LIST_SORT			= "S2513";
	

	/**
	 * コンストラクタ
	 */
	public LaborAgreementListAction() {
		super();
	}
	
	public void action() throws Exception {
		// VOの取得
		prepareVo();
		// 初期処理
		init();
		// コマンド毎の処理
		if (cmd.equals(CMD_AGREEMENT_LIST_SHOW)) {
			// 表示処理
			show();
		} else if (cmd.equals(CMD_AGREEMENT_LIST_SEARCH)) {
			// 検索処理
			search();
		} else if (cmd.equals(CMD_AGREEMENT_LIST_RE_SEARCH)) {
			// 再検索
			research();
		} else if (cmd.equals(CMD_AGREEMENT_LIST_SORT)) {
			// ソート
			sortList(key1);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}
	
	@Override
	protected BaseVo getSpecificVo() {
		return new LaborAgreementListVo();
	}
	
	@Override
	protected void init() throws Exception {
		// DBコネクション取得
		getConnection();
		// SYSTEM権限確認
		confirmSystemAuth();
	}
	
	@Override
	protected void show() throws Exception {
		LaborAgreementListVo vo = (LaborAgreementListVo)getVo();
		vo.setPltDelete(MospConst.DELETE_FLAG_OFF);
		// 検索履歴を初期値にセット
		vo.setSearchmode(LaborAgreementListVo.SEARCH_OFF);
	}
	
	@Override
	protected void search() throws Exception {
		LaborAgreementListVo vo = (LaborAgreementListVo)getVo();
		vo.setParams(request);
		searchAgreementList();
		vo.setIsAscending(true);
		vo.initListInfo();
		// 検索履歴をありにセット
		vo.setSearchmode(LaborAgreementListVo.SEARCH_ON);
	}
	
	@Override
	protected void research() throws Exception {
		LaborAgreementListVo vo = (LaborAgreementListVo)getVo();
		if (vo.getSearchmode() == LaborAgreementListVo.SEARCH_ON) {
			removeSessionDto();
			searchAgreementList();
			vo.setPageButton();
			sortList();
		}
	}
	
	/**
	 * 協定リスト検索
	 * @throws Exception 例外処理が発生した場合
	 */
	private void searchAgreementList() throws Exception {
		LaborAgreementListVo vo = (LaborAgreementListVo)getVo();
		LaborAgreementManager laborAgreement = new LaborAgreementManager(cfg, cmd, aspUser, user, conn);
		// 検索
		List<CmLaborAgreementDto> list = laborAgreement.getAgreementDao().findForCondition(vo.getTxtAgreeCode(),
				LaborAgreementManager.K_CODE, vo.getPltDelete());
		String[] aryAgreeName = new String[list.size()];
		int i = 0;
		for (CmLaborAgreementDto dto : list) {
			if (LaborAgreementManager.K_CODE.equals(dto.getAgreeType())) {
				// 社員名を取得してセットする。
				aryAgreeName[i] = part().mospuser().getUserInfoForKCode(dto.getAgreeCode()).getUserName();
			} else {
				aryAgreeName[i] = dto.getAgreeCode();
			}
			// 時間外協定
			CmAgreementOverTimeDto overTimeDto = laborAgreement.getOverTimeAgreementInfo(dto.getOverTimeCode(),
					DateUtil.getSystemDate());
			if (VariousUtil.isNotNull(overTimeDto)) {
				// 業務の種類
				dto.setOverTimeCode(StringUtil.changeTerminal(overTimeDto.getWorkTypeName(), 21));
			}
			// 休出協定
			CmAgreementWorkOnDto workOnDto = laborAgreement.getWorkOnAgreementInfo(dto.getWorkOnCode(), DateUtil
				.getSystemDate());
			if (VariousUtil.isNotNull(workOnDto)) {
				// 業務の種類
				dto.setWorkOnCode(StringUtil.changeTerminal(workOnDto.getWorkTypeName(), 21));
			}
			i++;
		}
		vo.setFields(list);
		// 社員名をセットする。
		vo.setAryAgreeName(aryAgreeName);
		// 対象が無かった場合のメッセージ設定
		if (vo.getEscAryAgreeCode().length == 0) {
			vo.addErrMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		}
	}
	
	@Override
	protected void sortList(String key) throws MospException {
		LaborAgreementListVo vo = (LaborAgreementListVo)getVo();
		vo.sortList(key, getComparator(key));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected void sortList() throws MospException {
		LaborAgreementListVo vo = (LaborAgreementListVo)getVo();
		vo.sortList(getComparator(vo.getFormerKey()));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected Comparator<Object> getComparator(String key) throws MospException {
		LaborAgreementListVo vo = (LaborAgreementListVo)getVo();
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_AGREE_CODE)) {
			comp = new CompAgreeCode();
		} else if (key.equals(vo.KEY_OVER_TIME_CODE)) {
			comp = new CompOverTimeCode();
		} else if (key.equals(vo.KEY_WORK_ON_CODE)) {
			comp = new CompWorkOnCode();
		} else if (key.equals(vo.KEY_DELETE)) {
			comp = new CompDeleteFlag();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}
	

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

		public int compare(Object obj1, Object obj2) {
			CmLaborAgreementDto dto1 = (CmLaborAgreementDto)obj1;
			CmLaborAgreementDto dto2 = (CmLaborAgreementDto)obj2;
			return dto1.getAgreeCode().compareTo(dto2.getAgreeCode());
		}
	}
	
	/**
	 * 時間外協定コードによる比較クラス
	 */
	protected static class CompOverTimeCode implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -9102878735277786582L;
		

		public int compare(Object obj1, Object obj2) {
			CmLaborAgreementDto dto1 = (CmLaborAgreementDto)obj1;
			CmLaborAgreementDto dto2 = (CmLaborAgreementDto)obj2;
			return dto1.getOverTimeCode().compareTo(dto2.getOverTimeCode());
		}
	}
	
	/**
	 * 休出協定コードによる比較クラス
	 */
	protected static class CompWorkOnCode implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -146969641720157320L;
		

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

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