package de.snailshell.consensus.demo.actions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

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.snailshell.consensus.demo.RoleBean;
import de.uka.ipd.consensus.foundation.ontology.OntologyManager;
import de.uka.ipd.consensus.foundation.schema.Association;
import de.uka.ipd.consensus.foundation.schema.AssociationRole;
import de.uka.ipd.consensus.foundation.schema.Rating;
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 AssociationViewAction 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 id = request.getParameter( "id" );

		OntologyManager manager = this.getOntologyManager();
		
		User user = this.getUser(request);
		
		Association a = manager.getQueryEngine().getAssociationById( user, id );
		
		if (a == null) {
			a = (Association)request.getAttribute( Constants.REQUEST_ATTR_ASSOCIATION );
		}
		
		Topic t = a.getType();
		
		String assoctypeid;
		
		if (t != null) {
			assoctypeid = t.getId();
		}
		else {
			assoctypeid = "";
		}
		
		String assocname;
		
		if ((t != null) && a.getName().equals( t.getName() )) {
			assocname = "";
		}
		else {
			assocname = a.getName();
		}

		Rating r = a.getRating( user );
		
		Collection history = null;

		String ratingstr;

		if ((r != null) && (this.getRatingManager() != null)) {
			ratingstr = this.getRatingManager().ratingValueToString( r.getValue() );
			history   = r.getHistory();
		}
		else {
			ratingstr = "";
		}
		
		ArrayList roles = null;
		
		Collection c = a.getAssociationRoles( user );
		
		if (!c.isEmpty()) {
			roles = new ArrayList(10);
			
			Iterator it = c.iterator();
			
			while (it.hasNext()) {
				AssociationRole ar = (AssociationRole)it.next();
				
				RoleBean rb = new RoleBean( user, ar );
				
				roles.add( rb );
			}
		}

		((DynaActionForm)form).set( "assocname", assocname );
		((DynaActionForm)form).set( "associd", a.getId() );
		((DynaActionForm)form).set( "assoctypeid", assoctypeid );
		((DynaActionForm)form).set( "assocrating", ratingstr );
		((DynaActionForm)form).set( "ratingcomment", "" );
		
		request.setAttribute( Constants.REQUEST_ATTR_ASSOCIATION, a );
		request.setAttribute( Constants.REQUEST_ATTR_ASSOCIATIONROLES, roles );
		request.setAttribute( Constants.REQUEST_ATTR_RATING, r );
		request.setAttribute( Constants.REQUEST_ATTR_RATINGHISTORY, history );

		ConsensusFoundationUtils.setLastVisitedAssociation( request, a );
		ConsensusFoundationUtils.addLastVisitedTopic( request, t );
		
		return mapping.findForward( Constants.MAPPING_SUCCESS );
	}
}
