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

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.workflow.dto.AmUnitDto;
import jp.mosp.workflow.vo.ApprovalUnitListVo;

public class ApprovalUnitListAction extends MosPWorkflowAction {

	// コマンド
	private static final String CMD_UNIT_LIST_SHOW      = "S2810";
	private static final String CMD_UNIT_LIST_SEARCH    = "S2811";
	private static final String CMD_UNIT_LIST_RE_SEARCH = "S2812";
	private static final String CMD_UNIT_LIST_SORT      = "S2813";

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

	/**
	 * アクション
	 */
	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// SYSTEM権限確認
		confirmSystemAuth();
		// VOの取得
		ApprovalUnitListVo vo;
		Object obj = session.getAttribute(MospConst.ATT_FORMER_VO);
		if (obj instanceof ApprovalUnitListVo) {
			vo = (ApprovalUnitListVo)obj;
			vo.clearMessage();
			vo.clearErrField();
		} else {
			vo = new ApprovalUnitListVo();
		}
		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.aryPltUser = getBasisArrayAll();
		// コマンド毎の処理
		if (cmd.equals(CMD_UNIT_LIST_SHOW)) {
			// 検索履歴を初期値にセット
			vo.setSearchmode(ApprovalUnitListVo.SEARCH_OFF);
		} else if (cmd.equals(CMD_UNIT_LIST_SEARCH)) {
			// 検索
			vo.setParams(request);
			searchUnitList(vo);
			vo.setFormerKey(vo.KEY_UNIT_CODE);
			vo.setIsAscending(true);
			vo.initListInfo();
		} else if (cmd.equals(CMD_UNIT_LIST_RE_SEARCH)) {
			// 再検索
			if (vo.getSearchmode() == ApprovalUnitListVo.SEARCH_ON) {
				session.removeAttribute(MospConst.ATT_DTO);
				searchUnitList(vo);
				vo.setPageButton();
				sortList(vo);
			}
		} else if (cmd.equals(CMD_UNIT_LIST_SORT)) {
			// ソート
			sortList(key1, vo);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}

	/**
	 * ユニットリスト検索
	 * @param vo UserListVoインスタンス
	 * @throws Exception
	 */
	private void searchUnitList(ApprovalUnitListVo vo) throws Exception {
		// DBコネクションの取得
		if (conn == null) {
			getConnection();
		}
		// 検索
		prepareAmUnitDao();
		vo.setFields(unitDao.findForCondition(
				vo.getTxtUnitCode(), vo.getTxtKCode(), vo.getTxtKName())
		);
		// 対象が無かった場合のメッセージ設定
		if (vo.getEscAryUnit().length == 0) {
			vo.addErrMessage(MospUtility.getMessage(msg, MospConst.MSG_NO_DATA, ""));
		}
		// モード設定
		vo.setSearchmode(ApprovalUnitListVo.SEARCH_ON);			
	}

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

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

	/**
	 * 比較クラス取得
	 * @param key ソートキー
	 * @param vo ソート対象Listを含むVO
	 * @throws MospException
	 */
	private Comparator getComparator(String key, ApprovalUnitListVo vo) throws MospException {
		Comparator comp = null;
		if (key.equals(vo.KEY_UNIT_CODE)) {
			comp = new CompUnitCode();
		} else if (key.equals(vo.KEY_MAIN)) {
			comp = new CompMain();
		} else if (key.equals(vo.KEY_AGENT)) {
			comp = new CompAgent();
		} else if (key.equals(vo.KEY_START_DATE)) {
			comp = new CompStartDate();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}

	/**
	 * ユニットコードによる比較クラス
	 */
	protected static class CompUnitCode implements Comparator, Serializable {
		private static final long serialVersionUID = 8882101817659803340L;
		public int compare(Object obj1, Object obj2) {
			AmUnitDto dto1 = (AmUnitDto)obj1;
			AmUnitDto dto2 = (AmUnitDto)obj2;
			return dto1.getUnitCode().compareTo(dto2.getUnitCode());
		}
	}

	/**
	 * 承認者による比較クラス
	 */
	protected static class CompMain implements Comparator, Serializable {
		private static final long serialVersionUID = -1958254792298208211L;
		public int compare(Object obj1, Object obj2) {
			AmUnitDto dto1 = (AmUnitDto)obj1;
			AmUnitDto dto2 = (AmUnitDto)obj2;
			return dto1.getMain().compareTo(dto2.getMain());
		}
	}

	/**
	 * 代理による比較クラス
	 */
	protected static class CompAgent implements Comparator, Serializable {
		private static final long serialVersionUID = 1656557705586257941L;
		public int compare(Object obj1, Object obj2) {
			AmUnitDto dto1 = (AmUnitDto)obj1;
			AmUnitDto dto2 = (AmUnitDto)obj2;
			return dto1.getAgent().compareTo(dto2.getAgent());
		}
	}

	/**
	 * 開始日による比較クラス
	 */
	protected static class CompStartDate implements Comparator, Serializable {
		private static final long serialVersionUID = 3154045096780718675L;
		public int compare(Object obj1, Object obj2) {
			AmUnitDto dto1 = (AmUnitDto)obj1;
			AmUnitDto dto2 = (AmUnitDto)obj2;
			return dto1.getStartDate().compareTo(dto2.getStartDate());
		}
	}

}
