package jp.yoyoyoyo.webformmanager.action;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jp.yoyoyoyo.webformmanager.dao.MstUserDAO;
import jp.yoyoyoyo.webformmanager.form.EditUserForm;
import jp.yoyoyoyo.webformmanager.model.LoginInfo;
import jp.yoyoyoyo.webformmanager.model.MstUser;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class EditUserAction extends Action {
	public ActionForward execute(ActionMapping mapping,
			ActionForm form,
			HttpServletRequest request,
			HttpServletResponse response){
		HttpSession session = request.getSession();
		EditUserForm editUserForm = (EditUserForm)form;
		// ログイン情報の取得
		LoginInfo info = (LoginInfo)session.getAttribute("loginData");
		// ログインしていないときはエラー
		if(info == null){
			return mapping.findForward("maintenance");
		}
		// UserManagerでないときはエラー
		if(!info.isRoleUserManager()){
			return mapping.findForward("maintenance");
		}
		if(editUserForm.getEditUserBack() != null && !editUserForm.getEditUserBack().equals("")){	// 戻るボタンを押したとき	
			return mapping.findForward("userManager");	
		} else if(editUserForm.getEditUserNext() != null && !editUserForm.getEditUserNext().equals("")){	// 次へボタンを押したとき
			// リクエストデータをフォームにコピー
			request.setAttribute("id", (String)session.getAttribute("userId"));
			request.setAttribute("operatorName", request.getParameter("operatorName"));
			
			// バリデーションの実行
			ActionErrors errors = editUserForm.checkValidate(mapping, request);
			
			// バリデーションエラーが発生したとき
			if(errors.size() > 0){
				// エラー内容をセット
				saveErrors(request, errors);
				// 入力画面に戻る
				return mapping.findForward("editUser");
			}
			
			editUserForm.setRoleText(editUserForm.getRoleTextString());
			
			editUserForm.setId((String)session.getAttribute("userId"));
			session.setAttribute("editUserData", editUserForm);
			
			return mapping.findForward("editUserConfirm");	
		} else {
			String id = request.getParameter("id");
			
			if(id == null || id.equals("")){
				// GETパラメータidがないときはセッションから取得
				id = (String)session.getAttribute("userId");
			} else { 
				session.setAttribute("userId", id);
			}
			
			// ユーザ情報を取得する
			MstUserDAO dao = new MstUserDAO();
			MstUser user = null;
			
			try{
				user = dao.getMstUserById(id);
			} catch (SQLException e){
				return mapping.findForward("maintenance");	
			}
			
			request.setAttribute("id", user.getOperatorId());
			request.setAttribute("operatorName", user.getOperatorName());
			
			editUserForm.setRole(user.getRole());
			if(user.getLockFlag() == 1){
				editUserForm.setLock(true);
			} else {
				editUserForm.setLock(false);
			}
			if(user.getDisabled() == 1){
				editUserForm.setDisabled(true);
			} else {
				editUserForm.setDisabled(false);
			}
			
			return mapping.findForward("editUser");			
		}
	}
}
