package de.thomasjacob.improve.presentation.login;

import de.thomasjacob.improve.logic.DaoLocator;
import de.thomasjacob.improve.logic.user.Digester;
import de.thomasjacob.improve.logic.user.User;
import de.thomasjacob.improve.presentation.ImproveAction;
import de.thomasjacob.webcore.TechnicalException;

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

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;


public class LoginRHAction extends ImproveAction
{
	/*
	 * (non-Javadoc)
	 * 
	 * @see de.thomasjacob.webcore.presentation.BaseAction#executeChecked(org.apache.struts.action.ActionMapping,
	 * org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	protected ActionForward executeChecked(ActionMapping mapping, ActionForm form, HttpServletRequest request,
		HttpServletResponse response) throws TechnicalException
	{
		HttpSession session = request.getSession();

		LoginForm loginForm = (LoginForm) form;
		User user = DaoLocator.getInstance().getUserDao().getValidUser(loginForm.getName(),
			Digester.getInstance().hash(loginForm.getPassword()));

		if (user == null)
		{
			getActionErrors(request).add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("LoginRHAction.invalidLogin"));
			return mapping.findForward("login");
		}
		LoginManager.getInstance(session).login(user);

		return mapping.findForward("success");
	}
}