/*
 * MosP - Mind Open Source Project         http://www.mosp.jp/
 * Copyright (C) 1987-2009 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.action;

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


import jp.mosp.common.MosPAction;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;

import jp.mosp.kintai.dao.CmRotationDao;
import jp.mosp.kintai.dto.CmRotationDto;
import jp.mosp.kintai.vo.ScheduleRotationListVo;

public class ScheduleRotationListAction extends MosPAction {

	//コマンド
	private static final String CMD_ROTATION_LIST_SHOW      	= "K1210";	// 表示
	private static final String CMD_ROTATION_LIST_SEARCH    	= "K1211";	// 検索
	private static final String CMD_ROTATION_LIST__RE_SEARCH 	= "K1212";	// 再検索
	private static final String CMD_ROTATION_LIST_SORT      	= "K1213";	// ソート

	/**
	 * コンストラクタ
	 */
	public ScheduleRotationListAction() {
		super();
	}

	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// VOの取得
		ScheduleRotationListVo vo;
		Object obj = session.getAttribute(MospConst.ATT_FORMER_VO);
		if (obj instanceof ScheduleRotationListVo) {
			vo = (ScheduleRotationListVo) obj;
			vo.clearMessage();
			vo.clearErrField();
		} else {
			vo = new ScheduleRotationListVo();
		}
		vo.clearMessage();
		vo.clearErrField();
		vo.initVo(cfg, msg, cmd, aspUser, user);
		//VO及びフォワード先URLの設定
		setVo(vo);
		setUrl(vo.getRetUrl());	
		session.setAttribute(MospConst.ATT_FORMER_VO, vo);
		// コード区分プルダウンリスト作成
		vo.aryPltWorkType = getWorkTypeArrayWithWeekHoliday();
		vo.aryPltHoliday  = getApplicableFlagArray();
		// ユーザー情報取得
		getInfo(user.getUserId());
		//コマンド毎の処理
		if (cmd.equals(CMD_ROTATION_LIST_SHOW)){
			//検索履歴を初期値にリセット
			vo.setSearchmode(ScheduleRotationListVo.SEARCH_OFF);			
		} else if (cmd.equals(CMD_ROTATION_LIST_SEARCH)) {
			//検索処理
			vo.setParams(request);
			searchFmRotationList(vo);
			vo.setFormerKey(vo.KEY_RT_CODE);
			vo.setIsAscending(true);
			vo.initListInfo();
			//検索履歴をありにセット
			vo.setSearchmode(ScheduleRotationListVo.SEARCH_ON);			
		} else if (cmd.equals(CMD_ROTATION_LIST__RE_SEARCH)) {
			//再検索
			if(vo.getSearchmode() == ScheduleRotationListVo.SEARCH_ON){
				session.removeAttribute(MospConst.ATT_DTO);
				searchFmRotationList(vo);
				vo.setPageButton();
				sortList(vo);		
			}			
		} else if (cmd.equals(CMD_ROTATION_LIST_SORT)){
			//ソート
			sortList(key1, vo);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}

	/**
	 * 輪番パターンリスト検索
	 * @param vo Voインスタンス
	 * @throws Exception
	 */
	private void searchFmRotationList(ScheduleRotationListVo vo) throws Exception {
		// 検索
		CmRotationDao dao = new CmRotationDao();
		dao.initDao(cfg, cmd, aspUser, user, conn);
		vo.setFields(dao.findForCondition(mKihon.getKCode(), vo.getTxtRTCode(), vo.getTxtRTName()));
		// 対象が無かった場合のメッセージ設定
		if (vo.getEscAryRTCode().length == 0) {
			vo.addErrMessage(MospUtility.getMessage(msg, MospConst.MSG_NO_DATA, ""));
		}
	}

	/**
	 * ソート
	 * @param key ソートキー
	 * @param vo ソート対象Listを含むVO
	 * @throws MospException
	 */
	private void sortList(String key, ScheduleRotationListVo vo) throws MospException {
		vo.sortList(key, getComparator(key, vo));
		vo.setFields(vo.getList());
	}

	/**
	 * ソート(VO内のキー情報でソート)
	 * @param vo ソート対象Listを含むVO
	 * @throws MospException
	 */
	private void sortList(ScheduleRotationListVo vo) throws MospException {
		vo.sortList(getComparator(vo.getFormerKey(), vo));
		vo.setFields(vo.getList());
	}

	/**
	 * 比較クラス取得
	 * @param key ソートキー
	 * @param vo ソート対象Listを含むVO
	 * @throws MospException
	 */
	private Comparator<Object> getComparator(String key, ScheduleRotationListVo vo) throws MospException {
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_RT_CODE)) {
			comp = new CompFmRotationCode();
		} else if (key.equals(vo.KEY_RT_NAME)) {
			comp = new CompFmRotationName();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}

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

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

}
