package de.snailshell.consensus.demo.actions;

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.DynaActionForm;

import de.snailshell.consensus.demo.Constants;
import de.uka.ipd.consensus.foundation.schema.User;
import de.uka.ipd.consensus.foundation.user.UserManager;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class DeleteAccountAction extends BasisAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		if (!isUserLoggedOn(request)) {
			return mapping.findForward( Constants.MAPPING_FAILURE );			
		}
		String passwort  = ((DynaActionForm)form).getString("passwort");

		((DynaActionForm)form).set("passwort","");

		UserManager manager = this.getUserManager();

		User user = this.getUser(request);
		
		manager.deleteUser( user, passwort );

		HttpSession session = request.getSession(false);

		if (session != null) {
			session.removeAttribute( Constants.SESSION_ATTR_USER );
			session.invalidate();
		}

		return mapping.findForward( Constants.MAPPING_SUCCESS );
	}
}
