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

//===========================================================================
// import
//===========================================================================
import java.io.IOException;
import java.util.Locale;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.commons.logging.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
import org.apache.commons.beanutils.*;

import java.util.*;
import jp.gr.java_conf.bugslife.user.*;
import jp.gr.java_conf.bugslife.selections.*;
import jp.gr.java_conf.bugslife.form.*;
/**
 *<PRE>
 * 
 * SelectionEditActionクラス
 *
 *</PRE>
 * @version	0.0
 * @author	鰐川 正寛
 */
public final class SelectionEditAction extends BaseAction 
{
//===========================================================================
// attributes
//===========================================================================
	/**
	 * Log
	 */
	private Log log = LogFactory.getLog(this.getClass().getName());
//===========================================================================
// methods
//===========================================================================
	/**
	 * exevute
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param actionForm The optional ActionForm bean for this request (if any)
	 * @param request The HTTP request we are processing
	 * @param response The HTTP response we are creating
	 *
	 * @exception Exception if business logic throws an exception
	 */
	public ActionForward doAction(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response)
				throws Exception {
		/*  初期処理
		 */
		HttpSession session = getSession(request);	//sessioの取得
		UserInfo logonUser = (UserInfo)getAttribute(session,"UserInfo");	//ログインユーザの取得
		ActionForward forward = null;
		SelectionEditForm myForm = (SelectionEditForm)form;
		
		//セレクション制御を取得する
		SelectionControl sc = (SelectionControl)session.getAttribute("selectionControl");
		//セレクションを取得する
		Object selection = session.getAttribute("selection");
		
		
		String action = sc.getAction();
		
		//データチェック
		myForm.setAction(action);		//アクションのセット
		ActionErrors errors = myForm.validate(mapping, request);
		if (!errors.empty()) {
			saveErrors(request, errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		//追加
		if (action.equals("add")) {
			PropertyUtils.copyProperties(selection, myForm);	//コピー
			((ISelection)selection).insert(logonUser.getUno());
		//更新
		} else if (action.equals("update")) {
			PropertyUtils.copyProperties(selection, myForm);	//コピー
			((ISelection)selection).setCode(sc.getCode());		//コードセット
			((ISelection)selection).update(logonUser.getUno());
			
		//削除
		} else if (action.equals("delete")) {
			((ISelection)selection).delete(logonUser.getUno());
		}
		//セレクション制御をセッションにセット
		request.setAttribute("selectionList", sc.getCollection());
		
		//add wani 07/10/27
		//本来は変更した区分だけ変更すればよいが時間がないので、
		//すべて入れ替える
		//TODO
		SelectionSet.setAll(session.getServletContext(), logonUser, true);
		
		
		// Forward control to the specified success URI
		return mapping.findForward("success");
		
	}
}
