package de.snailshell.consensus.demo.actions;

import java.util.HashMap;
import java.util.Map;

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

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.snailshell.consensus.demo.exceptions.RegisterPasswordException;
import de.uka.ipd.consensus.foundation.schema.User;
import de.uka.ipd.consensus.foundation.user.UserManager;
import de.uka.ipd.consensus.impl.UserManagerImpl;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class ChangeAccountAction 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 vorname        = ((DynaActionForm)form).getString("vorname");
		String nachname       = ((DynaActionForm)form).getString("nachname");
		String email          = ((DynaActionForm)form).getString("email");
		String comment        = ((DynaActionForm)form).getString("comment");
		String altespasswort  = ((DynaActionForm)form).getString("altespasswort");
		String neuespasswort1 = ((DynaActionForm)form).getString("neuespasswort1");
		String neuespasswort2 = ((DynaActionForm)form).getString("neuespasswort2");

		((DynaActionForm)form).set("altespasswort","");
		((DynaActionForm)form).set("neuespasswort1","");
		((DynaActionForm)form).set("neuespasswort2","");

		if (!neuespasswort1.equals(neuespasswort2)) {
			throw new RegisterPasswordException();
		}
		
		Map data = new HashMap();

		data.put( UserManagerImpl.USERDATA_FIRSTNAME_KEY, vorname );
		data.put( UserManagerImpl.USERDATA_LASTNAME_KEY, nachname );
		data.put( UserManagerImpl.USERDATA_EMAIL_KEY, email );
		data.put( UserManagerImpl.USERDATA_COMMENT_KEY, comment );

		UserManager manager = this.getUserManager();

		User user = this.getUser(request);
		
		manager.changeUser( user, altespasswort, neuespasswort1, data );

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