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.AssociationBean;
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.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 TopicViewAction 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" );
		
		Topic      t           = null;
		Rating     r           = null;
		Collection aot         = null;
		Collection history     = null;
		Collection types       = null;
		Collection instances   = null;
		Collection occurrences = null;
		Collection attributes  = null;
		Collection synonyms    = null;
		ArrayList  assoc       = null;
		
		if ((id != null) && (id.length() > 0)) {
			t = om.getQueryEngine().getTopicById( user, id );
		}

		if (t == null) {
			t = (Topic)request.getAttribute( Constants.REQUEST_ATTR_TOPIC );
		}

		if (t != null) {
			r = t.getRating( user );

			String ratingstr;

			if ((r != null) && (this.getRatingManager() != null)) {
				ratingstr = this.getRatingManager().ratingValueToString( r.getValue() );	
				history   = r.getHistory();
			}
			else {
				ratingstr = "";
			}

			((DynaActionForm)form).set( "topicrating", ratingstr );
			((DynaActionForm)form).set( "ratingcomment", "" );
			((DynaActionForm)form).set( "occurrencedata", "" );
			
			aot = om.getQueryEngine().getAssociationsOfType( user, t );

			types       = t.getTypes( user );
			instances   = t.getInstances( user );
			occurrences = t.getOccurrences( user );
			attributes  = t.getAttributes( user );
			synonyms    = t.getSynonyms( user );
			
			Collection c = t.getAssociations( user );
			
			if (!c.isEmpty()) {
				assoc = new ArrayList(30);
				
				Iterator it = c.iterator();
				
				while (it.hasNext()) {
					Association a = (Association)it.next();
					
					AssociationBean ab = new AssociationBean( user, a );
					
					assoc.add( ab );
				}
			}
		}
		else {
			this.getLoggingManager().warn("Neither request Parameter \"id\" nor Topic request attribute found");
		}
		
		request.setAttribute( Constants.REQUEST_ATTR_TOPIC, t );
		request.setAttribute( Constants.REQUEST_ATTR_TOPICTYPES, types );
		request.setAttribute( Constants.REQUEST_ATTR_TOPICINSTANCES, instances );
		request.setAttribute( Constants.REQUEST_ATTR_TOPICASSOCIATIONS, assoc );
		request.setAttribute( Constants.REQUEST_ATTR_ASSOCIATIONSOFTYPE, aot );
		request.setAttribute( Constants.REQUEST_ATTR_OCCURRENCES, occurrences );
		request.setAttribute( Constants.REQUEST_ATTR_RATING, r );
		request.setAttribute( Constants.REQUEST_ATTR_RATINGHISTORY, history );
		request.setAttribute( Constants.REQUEST_ATTR_ATTRIBUTES, attributes );
		request.setAttribute( Constants.REQUEST_ATTR_SYNONYMS, synonyms );

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