//===========================================================================
// package
//===========================================================================
package jp.gr.java_conf.bugslife.form;

//===========================================================================
// import
//===========================================================================
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.validator.ValidatorForm;
import java.io.UnsupportedEncodingException;

import jp.gr.java_conf.bugslife.util.*;
/**
 *<PRE>
 * SelectionChoiceFormクラス
 *
 *</PRE>
 * @author 鰐川　正寛
 * @version 1.0.0
 */
public class SelectionChoiceForm extends ValidatorForm {
//===========================================================================
// attributes
//===========================================================================
	/**
	 * アクション
	 */
	private String m_action;
	/**
	 * table
	 */
	private String m_table;
	/**
	 * code
	 */
	private String m_code;
//===========================================================================
// methods
//===========================================================================
	/**
	 * アクションのセット
	 * 
	 * @param action アクション
	 */
	public void setAction(String action) {
		m_action = action;
	}
	/**
	 * アクションの取得
	 * 
	 * @return アクション
	 */
	public String getAction() {
		return m_action;
	}
	/**
	 * テーブル名の取得
	 * 
	 * @return テーブル名
	 */
	public String getT()
	{
		return m_table;
	}
	/**
	 * テーブル名のセット
	 * 
	 * @param t テーブル名
	 */
	public void setT(String t)
	{
		m_table = t;
	}
	/**
	 * コードの取得
	 * 
	 * @return コード
	 */
	public String getCode()
	{
		return m_code;
	}
	/**
	 * コードのセット
	 * 
	 * @param code コード
	 */
	public void setCode(String code)
	{
		m_code = code;
	}
	/**
	 * 文字列化
	 *
	 * @return 文字列
	 */
	public String toString() {
		
		StringBuffer sb = new StringBuffer();
		sb.append("action :"  + m_action + "\n");
		sb.append("table  :"  + m_table + "\n");
		sb.append("code   :"  + m_code + "\n");
		return new String(sb);
	}
	/**
	 * デバッグ文字出力
	 *
	 * @param s 出力文字列
	 */
	private void debug(String s) {
		System.out.println("[SelectionChoiceForm]" + s);
	}
//===========================================================================
// Public Methods
//===========================================================================
	/**
	 * リセット
	 *
	 * @param mapping The mapping used to select this instance
	 * @param request The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		
		//debug
		debug("reset");
		//encodeing セット
		try {
			request.setCharacterEncoding("SJIS");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		m_action = null;
		m_table = null;
		m_code = null;
	}
	/**
	* 値の検証(Validate) 
	*
	* @param mapping The mapping used to select this instance
	* @param request The servlet request we are processing
	*/
	public ActionErrors validate(ActionMapping mapping,
								 HttpServletRequest request) {
		
		//debug
		debug("validate");
		ActionErrors errors = new ActionErrors();
		
		Varidater.checkRequired(m_action, "action", errors);
		Varidater.checkRequired(m_table, "table", errors);
		
		//code
		String[] actions = new String[] {"update", "delete", "view"};
		
		if (Varidater.isInput(m_action)) {	//actionが入力されているとき
			//actionがupdate/delete/viewのとき
			if (Varidater.isType(m_action, actions)) {
				//入力チェック
				Varidater.checkRequired(m_code, "code", errors);
			}
		}
		
		return errors;
	}
}