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

import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import jp.mosp.common.CommonConst;
import jp.mosp.common.MosPVo;
import jp.mosp.common.common.MospConst;
import jp.mosp.common.common.MospException;
import jp.mosp.common.dto.CmComponentDto;

public abstract class MosPMenuArrangeVo extends MosPVo {

	// 定数
	public static final int SEARCH_OFF = 0;
	public static final int SEARCH_ON  = 1;

	public static final int MODE_INSERT = 0;
	public static final int MODE_UPDATE = 1;

	// パラメーター名
	public static final String PRM_TXT_COMP_CODE  = "txtCompCode"	;
	public static final String PRM_TXT_COMP_NAME  = "txtCompName"	;
	public static final String PRM_PLT_MANAGE  	= "pltManage"	;
	public static final String PRM_PLT_SELECTED	= "pltSelected"	;
	public static final String PRM_PLT_DELETE		= "pltDelete"	;

	// フィールド
	private String		txtCompCode		;
	private String   	txtCompName		;
	private String   	hidDivision		;
	private String   	pltManage  		;
	private	 String[]	pltSelected		;
	private String		pltDelete		;

	private String[]	aryCompCode	;
	private String[]	aryCompName	;
	private String[]	aryManage	;
	private String[]	aryComponent;
	private String[]	aryDelete	;

	// モード(0：新規登録、1：更新)
	private int mode;

	// モード(0：検索なし、1：検索あり)
	private int searchmode;

	// フィールド名及び制限
	public static final String NAM_COMP_CODE 		= CommonConst.NAM_COMMON_CODE		;
	public static final String NAM_COMP_NAME 		= CommonConst.NAM_COMMON_MEISYO		;
	public static final String NAM_MANAGE			= CommonConst.NAM_MANAGE			;
	public static final String NAM_COMPONENT		= CommonConst.NAM_COMPONENT			;
	public static final String NAM_PLT_SELECTED	= CommonConst.NAM_PLT_SELECTED		;
	public static final String NAM_DELETE			= CommonConst.NAM_DELETE_FLAG		;

	public static final String NAM_EMPLOYEE_CMD	= CommonConst.NAM_EMPLOYEE_CMD		;
	public static final String NAM_MANAGER_CMD		= CommonConst.NAM_MANAGER_CMD		;
	public static final String NAM_PAYROLL_CMD		= CommonConst.NAM_PAYROLL_CMD		;
	public static final String NAM_HUMAN_CMD		= CommonConst.NAM_HUMAN_CMD			;

	public static final int LEN_CODE		= 10;
	public static final int LEN_NAME		= 10;
	public static final String	 REG_DELETE = CommonConst.REG_DELETE;

	// プルダウンリスト
	public String[][] aryPltManage;
	public String[][] aryPltCmdItem;
	public String[][] arySelectedCmdItem;

	// ソートキー
	public final String KEY_COMP_CODE 	= "keyCompCode";
	public final String KEY_COMP_NAME 	= "keyCompName";
	public final String KEY_MANAGE 	= "keyManage";
	public final String KEY_COMPONENT 	= "keyComponent";
	public final String KEY_DELETE 	= "keyDelete";

	public MosPMenuArrangeVo() {
		super();
		this.txtCompCode = "";
		this.txtCompName = "";
		this.hidDivision = "";
		this.pltManage = "";
		this.pltSelected = new String[0];
		this.pltDelete = "";
		this.aryCompCode = new String[0];
		this.aryCompName = new String[0];
		this.aryManage = new String[0];
		this.aryComponent = new String[0];
		this.aryDelete = new String[0];
		this.mode = MODE_INSERT;
		this.searchmode = SEARCH_OFF;
	}

	/**
	 * パラメータ設定
	 * @param request リクエスト
	 */
	public void setCardParams(HttpServletRequest request) {
		txtCompCode		= request.getParameter(PRM_TXT_COMP_CODE);
		txtCompName    	= request.getParameter(PRM_TXT_COMP_NAME);
		pltManage		= request.getParameter(PRM_PLT_MANAGE);
		pltSelected 	= request.getParameterValues(PRM_PLT_SELECTED);
		pltDelete 		= request.getParameter(PRM_PLT_DELETE);
	}

	/**
	 * パラメータ設定
	 * @param request リクエスト
	 */
	public void setListParams(HttpServletRequest request) {
		txtCompCode 	= request.getParameter(PRM_TXT_COMP_CODE);
		txtCompName  	= request.getParameter(PRM_TXT_COMP_NAME);
		pltManage 		= request.getParameter(PRM_PLT_MANAGE);
		pltDelete  		= request.getParameter(PRM_PLT_DELETE);	
	}

	/**
	 * フィールド設定
	 * @param dto ユーザーリスト
	 */
	public void setFields(CmComponentDto dto) {
		txtCompCode		= dto.getCompCode();
		txtCompName		= dto.getCompName();
		pltManage		= String.valueOf(dto.getManage());
		pltSelected		= util.divideByComma(dto.getComponent());
		pltDelete		= String.valueOf(dto.getDeleteFlag());		
	}

	/**
	 * フィールド設定
	 * @param listHumanComp	人事メニューリスト
	 */
	public void setFields(List list) {
		// リストの設定
		this.list = list;
		// データ総数の取得
		count = list.size();
		// 配列の初期化
		aryCompCode		= new String[count];
		aryCompName		= new String[count];
		aryManage		= new String[count];
		aryComponent	= new String[count];
		aryDelete		= new String[count];
		// データの設定
		int i = 0;
		Iterator it = list.iterator();
		while (it.hasNext()) {
			CmComponentDto dto = (CmComponentDto)it.next();
			aryCompCode[i]	= escapeHTML(dto.getCompCode());
			aryCompName[i]	= escapeHTML(dto.getCompName());
			aryManage[i]	= escapeHTML(getCodeName(String.valueOf(dto.getManage()), aryPltManage));
			aryComponent[i]	= escapeHTML(getCmdNameList(dto.getComponent(), aryPltCmdItem));
			if (dto.getDeleteFlag() == MospConst.DELETE_FLAG_OFF) {
				aryDelete[i] = escapeHTML(MospConst.DELETE_FLAG_OFF_NAME);
			} else if (dto.getDeleteFlag() == MospConst.DELETE_FLAG_ON) {
				aryDelete[i] = escapeHTML(MospConst.DELETE_FLAG_ON_NAME);
			}
			i++;
		}
	}

	/**
	 * フィールド設定(DTOに対するVO情報のセット)
	 * @param dto 設定対象DTO
	 */
	public void setDtoFields(CmComponentDto dto) {
		dto.setDivision		(hidDivision);
		dto.setCompCode		(txtCompCode);
		dto.setCompName		(txtCompName);
		dto.setManage		(Integer.parseInt(pltManage));
		dto.setComponent	(util.combineByComma(pltSelected));
		dto.setDeleteFlag	(Integer.parseInt(pltDelete));
	}

	/**
	 * フィールドの妥当性確認
	 * @throws MospException
	 */
	public void validate() throws MospException {
		// 妥当性確認開始時処理
		startValidation();
		// 必須確認
		checkRequired(txtCompCode	, NAM_COMP_CODE	, PRM_TXT_COMP_CODE);
		checkRequired(txtCompName	, NAM_COMP_NAME	, PRM_TXT_COMP_NAME);
		// 文字列長確認
		checkLength(txtCompCode	, LEN_CODE	, NAM_COMP_CODE	, PRM_TXT_COMP_CODE);
		checkLength(txtCompName	, LEN_NAME	, NAM_COMP_NAME	, PRM_TXT_COMP_NAME);
		// 妥当性確認終了時処理
		endValidation();
	}

	/**
	 * JavaScript設定
	 * @param cls 情報取得クラス
	 * @throws IllegalAccessException
	 */
	@Override
	public String getDirectJs(Class cls) throws IllegalAccessException {
		StringBuffer sb = new StringBuffer();
		sb.append(super.getDirectJs(cls));
		sb.append("var MODE = ");
		sb.append(mode);
		sb.append(";");
		sb.append("var DEFAULT_CODE = \"");
		sb.append(hidDivision);
		sb.append("\";");
		if (mode == MODE_UPDATE) {
			sb.append(getArrayJs("ARY_SELECTED", arySelectedCmdItem));			
		}
		return sb.toString();
	}

	/**
	 * コマンド名称一覧
	 * @param component			対象コマンド一覧
	 * @param aryPltCmdItem		対象コマンド配列
	 * @return
	 */
	protected String getCmdNameList(String component, String[][] aryPltCmdItem) {
		String [] aryCmd = util.divideByComma(component);
		StringBuffer sb = new StringBuffer();
		for (int j = 0 ; j < aryCmd.length; j++) {
			sb.append(getCodeName(aryCmd[j], aryPltCmdItem));
			if (j != aryCmd.length - 1) {
				sb.append(CommonConst.KEY_SEPARATOR);
			}
		}
		return sb.toString();
	}

	// アクセサメソッド
	public String[] getPltSelected() { return pltSelected == null ? null : pltSelected.clone(); }

	public String getTxtCompCode	() 	{ return txtCompCode	; }
	public String getTxtCompName	()	{ return txtCompName	; }
	public String getPltManage		() 	{ return pltManage		; }
	public String getPltDelete		()	{ return pltDelete		; }

	public String getEscTxtCompCode	() { return escapeHTML(txtCompCode)	; }
	public String getEscTxtCompName	() { return escapeHTML(txtCompName)	; }

	public void setTxtCompCode		(String txtCompCode	) { this.txtCompCode 	= txtCompCode	; }
	public void setTxtCompName		(String txtCompName	) { this.txtCompName	= txtCompName	; }
	public void setPltManage		(String pltManage	) { this.pltManage		= pltManage		; }
	public void setPltDelete		(String pltDelete	) { this.pltDelete 		= pltDelete		; }

	public void setMode(int mode) { this.mode = mode; }
	public int getMode() { return mode; }

	public String getHidDivision() { return hidDivision; }
	public void setHidDivision(String hidDivision) { this.hidDivision = hidDivision; }

	public String[] getEscAryCompCode	() { return aryCompCode.clone()		; }
	public String[] getEscAryCompName	() { return aryCompName.clone()		; }
	public String[] getEscAryManage		() { return aryManage.clone()		; }
	public String[] getEscAryComponent	() { return aryComponent.clone()	; }
	public String[] getEscAryDelete		() { return aryDelete.clone()		; }

	public void setSearchmode(int searchmode) { this.searchmode = searchmode; }
	public int getSearchmode() { return searchmode; }

}
