//===========================================================================
// 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.form.*;
/**
 *<PRE>
 * 
 * UserEditActionクラス
 *
 *</PRE>
 * @version	0.0
 * @author	鰐川 正寛
 */
public final class UserEditAction 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;
		UserEditForm myForm = (UserEditForm)form;	//formの取得
		
		UserInfo user = (UserInfo)getAttribute(session,"EditUser");	//編集ユーザの取得
		int uno = user.getUno();
		String uid = user.getUid();
		//データチェック
		myForm.setUno(uno);		//新規かどうかを判断するためにunoをセット
		ActionErrors errors = myForm.validate(mapping, request);
		if (!errors.empty()) {
			saveErrors(request, errors);
			return (new ActionForward(mapping.getInput()));
		}
		
		String oldPassword = myForm.getOldPassword();
		String newPassword = myForm.getNewPassword();
		
		//新規
		if (uno == 0) {
			//入力情報セット
			PropertyUtils.copyProperties(user, myForm);
			user.setPassword(newPassword);	//パスワード
		//更新
		} else {
			//旧パスワードが指定されていた場合
			if ((oldPassword != null) && (oldPassword.length() > 0)) {
				//旧パスワード一致チェック
				if (! oldPassword.equals(user.getPassword())) {
					errors.add("oldPassword", new ActionError("error.oldPassword.unmatch"));
					saveErrors(request, errors);
					return (new ActionForward(mapping.getInput()));
				}
			}
			/*
			 * パスワード変更が無くてもformに同名のプロパティがあるため
			 * 無条件にコピーされてしまい、パスワードが空になるバグあり。
			 * ここでオリジナルを待避する
			 */
			newPassword = user.getPassword();
			//入力情報セット
			PropertyUtils.copyProperties(user, myForm);
			user.setUid(uid);	//uid
			user.setPassword(newPassword);
		}
		//パーミッションセット
		int permission = 0;
		permission += myForm.getUserPermission() ? UserInfo.USER : 0;
		permission += myForm.getTesterPermission() ? UserInfo.TESTER : 0;
		permission += myForm.getDeveloperPermission() ? UserInfo.DEVELOPER : 0;
		permission += myForm.getProjectManagerPermission() ? UserInfo.PROJECT_MANAGER : 0;
		permission += myForm.getSystemManagerPermission() ? UserInfo.SYSTEM_MANAGER : 0;
		user.setPermission(permission);
		
		//メール購読
		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);

		if (user.getUno() == 0) {
			//追加
			user.insert(logonUser.getUno());
			
		} else {
			//更新
			user.update(logonUser.getUno());
                        if(user.getUno() == logonUser.getUno()){
                            session.setAttribute("UserInfo", user);	//セッションを更新
                        }			
		}
		SessionControl.initSession(session, logonUser);		//セッション情報を再取得
		// Forward control to the specified success URI
		return mapping.findForward("success");
		
	}
//=====================================
// debug
//=====================================
	/**
	 */
	public void debug(String s) {
		System.out.println("[UserEditAction]" + s);
	}
}
