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

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import jp.mosp.common.action.MosPWorkflowAction;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.common.MospUtility;
import jp.mosp.common.part.DatePart;
import jp.mosp.common.part.KintaiApprovalPart;
import jp.mosp.common.part.StringPart;

import jp.mosp.workflow.dto.CtOvertimeRequestDto;
import jp.mosp.workflow.dto.OverTimeRequestDto;
import jp.mosp.workflow.vo.OverTimeRequestListVo;

public class OverTimeRequestListAction extends MosPWorkflowAction {

	// コマンド
	private static final String CMD_LIST_SHOW 		= "K1410";
	private static final String CMD_LIST_SEARCH 		= "K1411";
	public  static final String CMD_LIST_RE_SEARCH 	= "K1412";
	private static final String CMD_LIST_SORT 		= "K1413";

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

	public void action() throws Exception {
		// VOの初期化
		OverTimeRequestListVo vo = (OverTimeRequestListVo) prepareVo();
		// プルダウンの設定
		setPulldownList(vo);
		// ユーザ情報の設定
		getInfo(user.getUserId());
		vo.setLblSection(getSectionName(mKihon.getSCode()));
		// コマンド毎の処理
		if (cmd.equals(CMD_LIST_SHOW)) {
			Date todayDate = DatePart.getSystemDate();
			vo.setPltYear(String.valueOf(MospUtility.getYear(todayDate)));
			vo.setPltMonth(String.valueOf(MospUtility.getMonth(todayDate)));
			// 検索
			search(vo);
		} else if (cmd.equals(CMD_LIST_SEARCH)) {
			vo.setParams(request);
			// 検索
			search(vo);
		} else if (cmd.equals(CMD_LIST_RE_SEARCH)) {
			// 再検索
			session.removeAttribute(MospConst.ATT_DTO);
			searchRequestList(vo);
			vo.setPageButton();
			sortList(vo);
		} else if (cmd.equals(CMD_LIST_SORT)) {
			// ソート
			sortList(key1, vo);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}

	protected OverTimeRequestListVo getSpecificVo() {
		return new OverTimeRequestListVo();
	}

	/**
	 * プルダウンの設定
	 * @param vo		対象VO
	 */
	private void setPulldownList(OverTimeRequestListVo vo) {
		vo.aryPltYear = StringPart.getYearArray(MospUtility.getYear(DatePart.getSystemDate()));
		vo.aryPltMonth = StringPart.getMonthArray();
	}

	/**
	 * 検索
	 * @param vo
	 * @throws Exception
	 */
	private void search(OverTimeRequestListVo vo) throws Exception {
		// 検索条件による一覧取得
		searchRequestList(vo);
		vo.setFormerKey(vo.KEY_WORK_DATE);
		vo.setIsAscending(true);
		vo.initListInfo();
	}

	/**
	 * 検索条件による一覧設定
	 * @param vo			対象VO
	 * @throws Exception
	 */
	private void searchRequestList(OverTimeRequestListVo vo) throws Exception {
		// 開始年月日
		Date startDate = DatePart.getFirstDateOfMonth(vo.getPltYear(), vo.getPltMonth());
		// 終了年月日
		Date endDate = DatePart.getLastDateOfMonth(vo.getPltYear(), vo.getPltMonth());
		// 申請一覧
		List<CtOvertimeRequestDto> overList = overRequestDao().findForCondition(
				mKihon.getKCode(),
				DatePart.convDateFormattedByHyphen(startDate),
				DatePart.convDateFormattedByHyphen(endDate)
		);
		// 表示一覧
		List<OverTimeRequestDto> listRequest = new ArrayList<OverTimeRequestDto>();
		for (Iterator iter = overList.iterator(); iter.hasNext();) {
			CtOvertimeRequestDto requestDto = (CtOvertimeRequestDto) iter.next();
			OverTimeRequestDto dto = new OverTimeRequestDto(requestDto);
			// 承認状況
			dto.setApprovalStatus(getApprovalName(toKintaiApprovalStatus(dto.getWorkflow())));
			// 承認者名
			String approverName = "";
			if (KintaiApprovalPart.is1stApproved(toKintaiApprovalStatus(dto.getWorkflow()))
					|| KintaiApprovalPart.is2ndApproved(toKintaiApprovalStatus(dto.getWorkflow()))
			) {
				approverName = getKName(getWorkflowDto(dto.getWorkflow()).getUpdateUser());
			}
			dto.setApproverName(approverName);
			listRequest.add(dto);
		}
		// VOにセット
		vo.setFields(listRequest);
		// 対象が無かった場合のメッセージ設定
		if (vo.getEscAryOverTime().length == 0) {
			vo.addErrMessage(MospUtility.getMessage(msg, MospConst.MSG_NO_DATA, ""));
		}
	}

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

	/**
	 * ソート(VO内のキー情報でソート)
	 * @param vo ソート対象Listを含むVO
	 * @throws MospException
	 */
	private void sortList(OverTimeRequestListVo 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, OverTimeRequestListVo vo) throws MospException {
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_WORK_DATE)) {
			comp = new CompWorkDate();
		} else if (key.equals(vo.KEY_REQUEST_DATE)) {
			comp = new CompRequestDate();
		} else if (key.equals(vo.KEY_STATUS)) {
			comp = new CompStatus();
		} else if (key.equals(vo.KEY_APPROVER)) {
			comp = new CompApprover();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}

	/**
	 * 勤務日による比較クラス
	 */
	protected static class CompWorkDate implements Comparator<Object>, Serializable {
		private static final long serialVersionUID = 9136870178985992803L;
		public int compare(Object obj1, Object obj2) {
			OverTimeRequestDto dto1 = (OverTimeRequestDto)obj1;
			OverTimeRequestDto dto2 = (OverTimeRequestDto)obj2;
			return dto1.getWorkDate().compareTo(dto2.getWorkDate());
		}
	}

	/**
	 * 申請日による比較クラス
	 */
	protected static class CompRequestDate implements Comparator<Object>, Serializable {
		private static final long serialVersionUID = 818289320529127227L;
		public int compare(Object obj1, Object obj2) {
			OverTimeRequestDto dto1 = (OverTimeRequestDto)obj1;
			OverTimeRequestDto dto2 = (OverTimeRequestDto)obj2;
			return dto1.getRequestDate().compareTo(dto2.getRequestDate());
		}
	}

	/**
	 * 承認状況による比較クラス
	 */
	protected static class CompStatus implements Comparator<Object>, Serializable {
		private static final long serialVersionUID = 7143311063774870712L;
		public int compare(Object obj1, Object obj2) {
			OverTimeRequestDto dto1 = (OverTimeRequestDto)obj1;
			OverTimeRequestDto dto2 = (OverTimeRequestDto)obj2;
			return dto1.getApprovalStatus().compareTo(dto2.getApprovalStatus());
		}
	}

	/**
	 * 承認状況による比較クラス
	 */
	protected static class CompApprover implements Comparator<Object>, Serializable {
		private static final long serialVersionUID = 1923632050645529241L;
		public int compare(Object obj1, Object obj2) {
			OverTimeRequestDto dto1 = (OverTimeRequestDto)obj1;
			OverTimeRequestDto dto2 = (OverTimeRequestDto)obj2;
			return dto1.getApproverName().compareTo(dto2.getApproverName());
		}
	}

}
