//===========================================================================
// 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>
 * SelectionEditFormクラス
 *
 *</PRE>
 * @author 鰐川　正寛
 * @version 1.0.0
 */
public class SelectionEditForm extends ValidatorForm {
//===========================================================================
// attributes
//===========================================================================
	/**
	 * action
	 */
	private String m_action;
	/**
	 * code
	 */
	private String m_code;
	/**
	 * 名称
	 */
	private String m_name;
	/**
	 * 略称
	 */
	private String m_shortName;
	/**
	 * メモ
	 */
	private String m_info;
//===========================================================================
// methods
//===========================================================================
	/**
	 * アクションの取得
	 * 
	 * @return アクション
	 */
	public String getAction()
	{
		return m_action;
	}
	/**
	 * アクションのセット
	 * 
	 * @param action アクション
	 */
	public void setAction(String action)
	{
		m_action = action;
	}
	/**
	 * コードの取得
	 * 
	 * @return コード
	 */
	public String getCode()
	{
		return m_code;
	}
	/**
	 * コードのセット
	 * 
	 * @param code コード
	 */
	public void setCode(String code)
	{
		m_code = code;
	}
	/**
	 * 名称の取得
	 * 
	 * @return 名称
	 */
	public String getName()
	{
		return m_name;
	}
	/**
	 * 名称のセット
	 * 
	 * @param name 名称
	 */
	public void setName(String name)
	{
		m_name = name;
	}
	/**
	 * 略称の取得
	 * 
	 * @return 略称
	 */
	public String getShortName()
	{
		return m_shortName;
	}
	/**
	 * 略称のセット
	 * 
	 * @param shortName 略称
	 */
	public void setShortName(String shortName)
	{
		m_shortName = shortName;
	}
	/**
	 * 説明の取得
	 * 
	 * @return 説明
	 */
	public String getInfo()
	{
		return m_info;
	}
	/**
	 * 説明のセット
	 * 
	 * @param info 説明
	 */
	public void setInfo(String info)
	{
		m_info = info;
	}
//===========================================================================
// Debugging Methods
//===========================================================================
	/**
	 * toString
	 */
	public String toString() {
		
		StringBuffer sb = new StringBuffer();
		sb.append("action    :"  + m_action + "\n");
		sb.append("code      :"  + m_code + "\n");
		sb.append("name      :"  + m_name + "\n");
		sb.append("shortName :"  + m_shortName + "\n");
		sb.append("info      :"  + m_info + "\n");
		return new String(sb);
	}
	/**
	 * debug
	 */
	private void debug(String s) {
		System.out.println("[SelectionEditForm]" + 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_code = null;
		m_name = null;
		m_shortName = null;
		m_info = 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) {
		//debnug
		debug("validate");
		debug(toString());
		ActionErrors errors = new ActionErrors();
		
		//action
		Varidater.checkRequired(m_action, "action", errors);
		
		if (Varidater.isInput(m_action)) {
			//追加の場合
			String[] actions = new String[] { "add" };
			if (Varidater.isType(m_action, actions)) {
					//code
					Varidater.checkRequired(m_code, "code", errors);
			}
			//追加または更新の場合
			actions = new String[] { "add", "update" };
			if (Varidater.isType(m_action, actions)) {
				//name,shortName
				Varidater.checkRequired(m_name, "name", errors);
				Varidater.checkRequired(m_shortName, "shortName", errors);
			}
		}
		return errors;
	}
}