package de.snailshell.consensus.demo.actions;

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

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.Topic;
import de.uka.ipd.consensus.foundation.schema.TypeInstance;
import de.uka.ipd.consensus.foundation.schema.User;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class AssociationJoinAction 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);

		Topic t = manager.getQueryEngine().getTopicById( user, id );
		
		Association a = ConsensusFoundationUtils.getLastVisitedAssociation( request );

		String topicRoleId;

		Iterator it = t.getTypes(user).iterator();

		if (it.hasNext()) {
			topicRoleId = ((TypeInstance)it.next()).getType().getId();
		}
		else {
			topicRoleId = "";
		}
		
		Set roletypes = ConsensusFoundationUtils.getParentTypes( user, t );
		
		// now add all roles from the association and remove the roles that the topic already plays:
		
		Collection assocRoles = a.getAssociationRoles(user);
		
		it = assocRoles.iterator();
		
		while (it.hasNext()) {
			AssociationRole role = (AssociationRole)it.next();
			
			Topic roleType = role.getType();
			
			if (roleType != null) {
				if (role.hasTopic( t )) {
					roletypes.remove( roleType );
				}
				else {
					// Set ensures no duplicates
					roletypes.add( roleType );
				}
			}
		}
		
		// create RoleBeans from assocRoles:
		
		ArrayList roles = null;
		
		if (!assocRoles.isEmpty()) {
			roles = new ArrayList(10);

			it = assocRoles.iterator();

			while (it.hasNext()) {
				AssociationRole ar = (AssociationRole)it.next();
				
				RoleBean rb = new RoleBean( user, ar );
				
				roles.add( rb );
			}
		}
		
		((DynaActionForm)form).set( "associd", a.getId() );
		((DynaActionForm)form).set( "topicid", id );
		((DynaActionForm)form).set( "topicroleid", topicRoleId );

		request.setAttribute( Constants.REQUEST_ATTR_TOPIC, t );
		request.setAttribute( Constants.REQUEST_ATTR_ASSOCIATION, a );
		request.setAttribute( Constants.REQUEST_ATTR_ASSOCIATIONROLES, roles );
		request.setAttribute( Constants.REQUEST_ATTR_ROLETYPES, roletypes );
		
		if ( a.hasTopic( user, t ) ) {
			// Topic plays already a role in this association
			return mapping.findForward( Constants.MAPPING_WARNING );
		}
		else {
			return mapping.findForward( Constants.MAPPING_SUCCESS );
		}
	}
}
