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 de.snailshell.consensus.demo.Constants;
import de.snailshell.consensus.demo.TopicBean;
import de.uka.ipd.consensus.foundation.query.QueryEngine;
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 TopicHierarchyAction extends BasisAction {

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

		if (!isUserLoggedOn(request)) {
			return mapping.findForward( Constants.MAPPING_FAILURE );			
		}
		
		User user = this.getUser( request );

		QueryEngine queryEngine = this.getOntologyManager().getQueryEngine();
		
		Collection liste = queryEngine.getInstanceRoots( user );
		
		ArrayList beans = new ArrayList(50);
		
		Iterator it = liste.iterator();
		
		while (it.hasNext()) {
			Topic t = (Topic)it.next();

			TopicBean tb = new TopicBean( user, t );
			
			beans.add( tb );
		}
		
		request.setAttribute(Constants.REQUEST_ATTR_TOPICS, beans );

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