package de.snailshell.consensus.demo.actions;

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.ConsensusFoundationUtils;
import de.snailshell.consensus.demo.Constants;
import de.uka.ipd.consensus.foundation.ontology.OntologyManager;
import de.uka.ipd.consensus.foundation.schema.Association;
import de.uka.ipd.consensus.foundation.schema.Topic;
import de.uka.ipd.consensus.foundation.schema.User;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class AssociationChangeAction 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 assocName   = ((DynaActionForm)form).getString( "assocname" ).trim();
		String assocId     = ((DynaActionForm)form).getString( "associd" );
		String assocTypeId = ((DynaActionForm)form).getString( "assoctypeid" );

		OntologyManager manager = this.getOntologyManager();
		
		User user = this.getUser(request);
		
		Association a = manager.getQueryEngine().getAssociationById( user, assocId );
		Topic       t = manager.getQueryEngine().getTopicById( user, assocTypeId );

		a.setName( user, assocName );
		a.setType( user, t );

		request.setAttribute( Constants.REQUEST_ATTR_ASSOCIATION, a );

		ConsensusFoundationUtils.setLastVisitedAssociation( request, a );

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