/*
 * 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.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.common.part.StringPart;


import jp.mosp.workflow.dto.AttendanceRouteDto;
import jp.mosp.workflow.vo.AttendanceRouteListVo;

public class AttendanceRouteListAction extends MosPWorkflowAction {

	// コマンド
	private static final String CMD_ROUTE_LIST_SHOW      = "S2830";
	private static final String CMD_ROUTE_LIST_SEARCH    = "S2831";
	private static final String CMD_ROUTE_LIST_RE_SEARCH = "S2832";
	private static final String CMD_ROUTE_LIST_SORT      = "S2833";

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

	/**
	 * アクション
	 */
	public void action() throws Exception {
		// DBコネクション取得
		getConnection();
		// SYSTEM権限確認
		confirmSystemAuth();
		// VOの取得
		AttendanceRouteListVo vo;
		Object obj = session.getAttribute(MospConst.ATT_FORMER_VO);
		if (obj instanceof AttendanceRouteListVo) {
			vo = (AttendanceRouteListVo)obj;
		} else {
			vo = new AttendanceRouteListVo();
		}
		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.aryPltUnit = getCurrentUnitArray();
		// 所属コード
		vo.aryPltSection = StringPart.addArrayFirstRowSpace(getSectionArrayAll());
		// 社員コード
		vo.aryPltUser = getBasisArrayAll();
		// 事業所コード
		vo.aryPltOffice = StringPart.addArrayFirstRowSpace(getOfficeArrayAll());
		// コマンド毎の処理
		if (cmd.equals(CMD_ROUTE_LIST_SHOW)) {
			// 検索履歴を初期値にセット
			vo.setSearchmode(AttendanceRouteListVo.SEARCH_OFF);
		} else if (cmd.equals(CMD_ROUTE_LIST_SEARCH)) {
			// 検索
			vo.setParams(request);
			searchRouteList(vo);
			vo.setFormerKey(vo.KEY_SECTION);
			vo.setIsAscending(true);
			vo.initListInfo();
			sortList(vo);
		} else if (cmd.equals(CMD_ROUTE_LIST_RE_SEARCH)) {
			// 再検索
			if (vo.getSearchmode() == AttendanceRouteListVo.SEARCH_ON) {
				session.removeAttribute(MospConst.ATT_DTO);
				searchRouteList(vo);
				vo.setPageButton();
				sortList(vo);
			}
		} else if (cmd.equals(CMD_ROUTE_LIST_SORT)) {
			// ソート
			sortList(key1, vo);
		} else {
			throw new MospException(MospConst.EX_CMD_INVALID);
		}
	}

	/**
	 * ルートリスト検索
	 * @param vo UserListVoインスタンス
	 * @throws Exception
	 */
	private void searchRouteList(AttendanceRouteListVo vo) throws Exception {
		// 検索
		vo.setFields(atRouteDao().findForCondition(
				vo.getPltSection(),
				vo.getTxtKCode(),
				vo.getTxtKName(),
				vo.getTxtRouteCode(),
				vo.getPltOffice())
		);
		// 対象が無かった場合のメッセージ設定
		if (vo.getEscAryAttendanceRoute().length == 0) {
			vo.addErrMessage(MospUtility.getMessage(msg, MospConst.MSG_NO_DATA, ""));
		}
		vo.setSearchmode(AttendanceRouteListVo.SEARCH_ON);
	}

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

	/**
	 * ソート(VO内のキー情報でソート)
	 * @param vo ソート対象Listを含むVO
	 * @throws MospException
	 */
	private void sortList(AttendanceRouteListVo 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, AttendanceRouteListVo vo) throws MospException {
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_SECTION)) {
			comp = new CompSection();
		} else if (key.equals(vo.KEY_K_CODE)) {
			comp = new CompKCode();
		} else if (key.equals(vo.KEY_OFFICE)) {
			comp = new CompOffice();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}

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

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

	/**
	 * 事業所コードによる比較クラス
	 */
	protected static class CompOffice implements Comparator<Object>, Serializable {
		private static final long serialVersionUID = -6507035343563191493L;
		public int compare(Object obj1, Object obj2) {
			AttendanceRouteDto dto1 = (AttendanceRouteDto)obj1;
			AttendanceRouteDto dto2 = (AttendanceRouteDto)obj2;
			return dto1.getJCode().compareTo(dto2.getJCode());
		}
	}

}
