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 de.snailshell.consensus.demo.Constants;
import de.uka.ipd.consensus.foundation.ontology.OntologyManager;
import de.uka.ipd.consensus.foundation.schema.Rating;
import de.uka.ipd.consensus.foundation.schema.Synonym;
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 ChangeSynonymRatingAction extends BasisAction {

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

		if (!isUserLoggedOn(request)) {
			return mapping.findForward( Constants.MAPPING_FAILURE );			
		}

		OntologyManager om = this.getOntologyManager();

		User user = this.getUser( request );
		
		String id        = request.getParameter( "id" );
		String synid     = request.getParameter( "synid" );
		String ratingstr = request.getParameter( "synrating" );

		double value;
		
		if (this.getRatingManager() != null) {
			value = this.getRatingManager().stringToRatingValue( ratingstr );
		}
		else {
			value = Rating.NEUTRAL;
		}
		
		Topic t = om.getQueryEngine().getTopicById( user, id );
		
		Synonym synonym = t.getSynonym( user, synid );
		
		synonym.addRating( user, value );

		this.getLoggingManager().debug("Change rating for " + synonym + " to " + ratingstr + "/" + value + " (by " + user + ")");
		
		request.setAttribute( Constants.REQUEST_ATTR_TOPIC, t );

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