//===========================================================================
// 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.selections.SelectionSet;
import jp.gr.java_conf.bugslife.user.*;
import jp.gr.java_conf.bugslife.form.*;
/**
 *<PRE>
 * 
 * UserEditActionクラス
 *
 *</PRE>
 * @version	0.0
 * @author	鰐川 正寛
 */
public final class UserEditAction3 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 synchronized 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;
		UserEditForm3 myForm = (UserEditForm3)form;	//formの取得
		
		//debug
		log.debug(myForm);

		//編集表示
		if (myForm.getAction().equals("edit")) {
			UserInfo user = (UserInfo)getAttribute(session,"EditUser");	//編集ユーザの取得
			
			log.debug(user);
			
			PropertyUtils.copyProperties(myForm, user);
		
			//メール購読
			myForm.setSubscriprion0(user.isSubscription(UserInfo.MAIL_DETAIL0));
			myForm.setSubscriprion1(user.isSubscription(UserInfo.MAIL_DETAIL1));
			myForm.setSubscriprion2(user.isSubscription(UserInfo.MAIL_DETAIL2));
			myForm.setSubscriprion3(user.isSubscription(UserInfo.MAIL_DETAIL3));
			myForm.setSubscriprion4(user.isSubscription(UserInfo.MAIL_DETAIL4));
			myForm.setSubscriprion5(user.isSubscription(UserInfo.MAIL_DETAIL5));
			
		} else {
			UserInfo user = (UserInfo)getAttribute(session,"EditUser");	//編集ユーザの取得
			int uno = user.getUno();
			String uid = user.getUid();
			//データチェック
			ActionErrors errors = myForm.validate(mapping, request);
			if (!errors.empty()) {
				saveErrors(request, errors);
				return (new ActionForward(mapping.getInput()));
			}
			//入力情報セット
			PropertyUtils.copyProperties(user, myForm);
			user.setUid(uid);	//uid
	
			//メール購読
			int subscriprion = 0;
			subscriprion += myForm.getSubscriprion0() ? UserInfo.MAIL_DETAIL0 : 0;
			subscriprion += myForm.getSubscriprion1() ? UserInfo.MAIL_DETAIL1 : 0;
			subscriprion += myForm.getSubscriprion2() ? UserInfo.MAIL_DETAIL2 : 0;
			subscriprion += myForm.getSubscriprion3() ? UserInfo.MAIL_DETAIL3 : 0;
			subscriprion += myForm.getSubscriprion4() ? UserInfo.MAIL_DETAIL4 : 0;
			subscriprion += myForm.getSubscriprion5() ? UserInfo.MAIL_DETAIL5 : 0;
			user.setMailSubscription(subscriprion);
	
			//更新
			user.update(logonUser.getUno());
	        if(user.getUno() == logonUser.getUno()){
	            session.setAttribute("UserInfo", user);	//セッションを更新
	        }			
            session.setAttribute("EditUser", user);	//セッションを更新
//			SessionControl.initSession(session, logonUser);		//セッション情報を再取得
			
			//ユーザリストの再設定
			SelectionSet.setUserList(session.getServletContext(), true);

		}
		// Forward control to the specified success URI
		return mapping.findForward("success");
		
	}
}
