/*
 * 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.common.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.dao.CmComponentDao;
import jp.mosp.common.dto.CmComponentDto;
import jp.mosp.common.utils.VariousUtil;
import jp.mosp.common.vo.MosPMenuArrangeVo;

/**
 * @author yoshida
 *
 */
public abstract class MosPMenuArrangeAction extends MosPAction {
	
	/**
	 * メニュー構成DTO
	 */
	protected CmComponentDto	componentDto;
	

	/**
	 * メニュー構成用DAOの取得
	 * @return	CmComponentDao
	 * @throws Exception	例外処理が発生した場合
	 */
	protected CmComponentDao componentDao() throws Exception {
		return part().component().getCmComponentDao();
	}
	
	/**
	 * 初期処理
	 * @param targetCmdArray		対象コマンド配列
	 * @param division				対象メニュー
	 * @throws Exception	例外処理が発生した場合
	 */
	protected void initCardAction(String[][] targetCmdArray, String division) throws Exception {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		vo.setCardParams(request);
		vo.aryPltManage = VariousUtil.getManageArray();
		// 項目設定
		vo.aryPltCmdItem = targetCmdArray;
		vo.setHidDivision(division);
	}
	
	/**
	 * 初期処理
	 * @param targetCmdArray		対象コマンド配列
	 * @param division				対象メニュー
	 */
	protected void initListAction(String[][] targetCmdArray, String division) {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		vo.aryPltManage = VariousUtil.getManageArray();
		vo.aryPltCmdItem = targetCmdArray;
		vo.setHidDivision(division);
	}
	
	/**
	 * 表示
	 */
	protected void showCard() {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		vo.arySelectedCmdItem = new String[0][2];
		vo.setMode(MosPMenuArrangeVo.MODE_INSERT);
		// セッションにDTOをセット
		setSessionDto(componentDto);
	}
	
	/**
	 * 選択
	 * @throws Exception	例外処理が発生した場合
	 */
	protected void selectCard() throws Exception {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		componentDto = part().component().findForDto(key1, vo.getHidDivision());
		chkExist(componentDto);
		vo.setFields(componentDto);
		// モード設定
		vo.setMode(MosPMenuArrangeVo.MODE_UPDATE);
		// セッションにDTOをセット
		setSessionDto(componentDto);
	}
	
	/**
	 * 検索モード設定
	 */
	protected void setSearchModeOff() {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		vo.setSearchmode(MosPMenuArrangeVo.SEARCH_OFF);
	}
	
	@Override
	protected void search() throws Exception {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		vo.setListParams(request);
		searchList();
		vo.setIsAscending(true);
		vo.initListInfo();
		// 検索履歴をありにセット
		vo.setSearchmode(MosPMenuArrangeVo.SEARCH_ON);
	}
	
	@Override
	protected void research() throws Exception {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		if (vo.getSearchmode() == MosPMenuArrangeVo.SEARCH_ON) {
			removeSessionDto();
			searchList();
			vo.setPageButton();
			sortList();
		}
	}
	
	/**
	 * リスト検索
	 * @throws Exception	例外処理が発生した場合
	 */
	private void searchList() throws Exception {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		// 検索
		vo.setFields(componentDao().findForCondition(vo.getTxtCompCode(), vo.getHidDivision(), vo.getTxtCompName(),
				vo.getPltManage(), vo.getPltDelete()));
		// 対象が無かった場合のメッセージ設定
		if (vo.getAryCompCode().length == 0) {
			vo.addErrMessage(getMessage(MospConst.MSG_NO_DATA, ""));
		}
	}
	
	@Override
	protected void sortList(String key) throws MospException {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		vo.sortList(key, getComparator(key));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected void sortList() throws MospException {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		vo.sortList(getComparator(vo.getFormerKey()));
		vo.setFields(vo.getList());
	}
	
	@Override
	protected Comparator<Object> getComparator(String key) throws MospException {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		Comparator<Object> comp = null;
		if (key.equals(vo.KEY_COMP_CODE)) {
			comp = new CmComponentCode();
		} else if (key.equals(vo.KEY_COMP_NAME)) {
			comp = new CmComponentName();
		} else if (key.equals(vo.KEY_MANAGE)) {
			comp = new CmComponentManage();
		} else if (key.equals(vo.KEY_COMPONENT)) {
			comp = new CmComponent();
		} else if (key.equals(vo.KEY_DELETE)) {
			comp = new CompOutPutDelete();
		} else {
			throw new MospException(MospConst.EX_SORTKEY_INVALID);
		}
		return comp;
	}
	

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

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

		public int compare(Object obj1, Object obj2) {
			CmComponentDto dto1 = (CmComponentDto)obj1;
			CmComponentDto dto2 = (CmComponentDto)obj2;
			return dto1.getCompName().compareTo(dto2.getCompName());
		}
	}
	
	/**
	 * 閲覧権限による比較クラス
	 */
	protected static class CmComponentManage implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= -1445454210063675079L;
		

		public int compare(Object obj1, Object obj2) {
			CmComponentDto dto1 = (CmComponentDto)obj1;
			CmComponentDto dto2 = (CmComponentDto)obj2;
			return String.valueOf(dto1.getManage()).compareTo(String.valueOf(dto2.getManage()));
		}
	}
	
	/**
	 * 閲覧可能機能による比較クラス
	 */
	protected static class CmComponent implements Comparator<Object>, Serializable {
		
		private static final long	serialVersionUID	= 4168257683510439452L;
		

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

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

	/**
	 * 選択出力項目配列
	 */
	protected void setSelectedItemArray() {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		String[] array = vo.getPltSelected();
		String[][] arySelectedItem = new String[1][2];
		arySelectedItem[0][0] = "";
		arySelectedItem[0][1] = "";
		if (chkExistObject(array)) {
			arySelectedItem = new String[array.length][2];
			for (int i = 0; i < arySelectedItem.length; i++) {
				arySelectedItem[i][0] = String.valueOf(i);
				arySelectedItem[i][1] = array[i];
			}
		}
		vo.arySelectedCmdItem = arySelectedItem;
	}
	
	/**
	 * データ挿入
	 * @throws Exception	例外処理が発生した場合
	 */
	protected void insertCard() throws Exception {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		// DTOの準備
		CmComponentDto dto = new CmComponentDto();
		vo.setPltDelete(String.valueOf(MospConst.DELETE_FLAG_OFF));
		// パラメータの妥当性確認
		vo.validate();
		// 重複確認
		chkDuplicate(componentDao().findForKey(vo.getEscTxtCompCode()));
		// DTOの準備
		vo.setDtoFields(dto);
		// データの更新
		componentDao().insert(dto);
		// DTOの再取得
		CmComponentDto retDto = componentDao().findForDto(dto.getCompCode(), dto.getDivision());
		chkInsert(retDto);
		// コミット
		commit();
		// モード設定
		vo.setMode(MosPMenuArrangeVo.MODE_UPDATE);
		// メッセージ設定
		vo.setMessage(getMessage(MospConst.MSG_INSERT, ""));
		// セッションにDTOをセット
		setSessionDto(retDto);
	}
	
	/**
	 * データ更新
	 * @throws Exception	例外処理が発生した場合
	 */
	protected void updateCard() throws Exception {
		MosPMenuArrangeVo vo = (MosPMenuArrangeVo)getVo();
		// DTOの取得
		componentDto = (CmComponentDto)getSessionDto();
		// パラメータの妥当性確認
		vo.validate();
		// 排他確認
		chkExclusive(componentDto, componentDao().findForUpdate(vo.getEscTxtCompCode()));
		// DTOの準備
		vo.setDtoFields(componentDto);
		// データの更新
		componentDao().update(componentDto);
		// DTOの再取得
		CmComponentDto retDto = componentDao().findForDto(componentDto.getCompCode(), componentDto.getDivision());
		chkUpdate(retDto);
		// コミット
		commit();
		// モード設定
		vo.setMode(MosPMenuArrangeVo.MODE_UPDATE);
		// メッセージ設定
		vo.setMessage(getMessage(MospConst.MSG_UPDATE, ""));
		// セッションにDTOをセット
		setSessionDto(retDto);
	}
	
}
