package de.snailshell.consensus.demo.actions;

import java.util.Collection;

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.query.QueryEngine;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class TopicListAction 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 start = request.getParameter("letter");

		QueryEngine queryEngine = this.getOntologyManager().getQueryEngine();
		
		Collection liste;
		
		if ((start != null) && (start.length() > 0)) {
			liste = queryEngine.getTopicsByBeginning(this.getUser(request),start);
			request.setAttribute(Constants.REQUEST_ATTR_TOPICSEARCH, start);
		}
		else {
			liste = queryEngine.getTopics(this.getUser(request));	
		}

		request.setAttribute(Constants.REQUEST_ATTR_TOPICS, liste);

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