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 de.snailshell.consensus.demo.Constants;
import de.uka.ipd.consensus.foundation.ontology.ExportHandler;
import de.uka.ipd.consensus.foundation.ontology.OntologyManager;

/**
 * @author Thomas Much
 * @version 2006-06-14
 */
public class ExportAction extends BasisAction {

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

		if (!isUserLoggedOn(request)) {
			return mapping.findForward( Constants.MAPPING_FAILURE );			
		}

		if (!getUser(request).isAdmin()) {
			return mapping.findForward( Constants.MAPPING_FAILURE );			
		}
		
		OntologyManager om = this.getOntologyManager();

		ExportHandler handler = om.getExportHandler( request.getParameter("format") );
		
		boolean internals = Boolean.valueOf( request.getParameter("internals") ).booleanValue();
		
		String filename = "ConsensusFoundationDemo." + handler.getExtension(); // TODO: Basisname vom ConsensusFoundationManager erfragen
		
		response.setContentType( handler.getContentType() );
		response.setHeader("Content-disposition", "attachment; filename=" + filename);

		handler.exportOntology( response.getOutputStream(), internals );

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