package de.snailshell.consensus.demo.actions;

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.Constants;
import de.uka.ipd.consensus.foundation.query.QueryEngine;
import de.uka.ipd.consensus.foundation.query.QueryEngineException;
import de.uka.ipd.consensus.foundation.query.QueryEngineResultSet;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class EngineSearchAction 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 search = ((DynaActionForm)form).getString("suchanfrage");

		QueryEngine queryEngine = this.getOntologyManager().getQueryEngine();

		try {
			QueryEngineResultSet qers = queryEngine.executeQuery( this.getUser(request), search );

			request.setAttribute( Constants.REQUEST_ATTR_ENGINERESULTS, qers );
		}
		catch (QueryEngineException e) {
			request.setAttribute( Constants.REQUEST_ATTR_ENGINEERROR, e );
		}

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