package com.jware.apps.scheduler.web.struts.definition;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.betwixt.io.BeanWriter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;
import org.xml.sax.SAXException;

import com.jware.apps.scheduler.model.DefinitionManager;
import com.jware.apps.scheduler.util.JobSchedulerUtils;
import com.jware.base.web.struts.jWareBaseAction;

/**
 * @author Jrmt
*/
public final class JobDefinitionDump extends jWareBaseAction {

    /**
     * Commons Logging instance.
    */
    private Log log = LogFactory.getLog(this.getClass().getName());

	/** 
	 * Process the specified HTTP request, and create the corresponding HTTP
	 * response (or forward to another web component that will create it).
	 * Return an <code>ActionForward</code> instance describing where and how
	 * control should be forwarded, or <code>null</code> if the response has
	 * already been completed.
	 *
	 * @param mapping The ActionMapping used to select this instance
	 * @param actionForm The optional ActionForm bean for this request (if any)
	 * @param request The HTTP request we are processing
	 * @param response The HTTP response we are creating
	 * @param errors The ActionErrors created in BaseAction
	 * @param mochFlag The flag using Mocha
	 *
	 * @exception Exception if an input/output error or servlet exception occurs
	 */
	public ActionForward doExecute(ActionMapping mapping,
				 ActionForm form,
				 HttpServletRequest request,
				 HttpServletResponse response,
				 ActionErrors errors,
				 boolean mochaFlag)
	throws Exception {

		String xmlResult = null; 

		// Extract attributes we will need
		HttpSession session = request.getSession();
		Locale locale = getLocale(request);
	
		MessageResources messages = getResources(request);
	
		if (log.isDebugEnabled()) {
			log.debug("JobDefinitionDump:  Processing actions");
		}

		StringWriter outputWriter = new StringWriter(); 
		// Betwixt just writes out the bean as a fragment
		// So if we want well-formed xml, we need to add the prolog
		outputWriter.write("<?xml version='1.0' ?><JobDefinitions>");

	    BeanWriter writer = new BeanWriter(outputWriter);

	    try {
			ServletContext context = super.servlet.getServletContext();
	    	DefinitionManager definitionManager = (DefinitionManager)context.getAttribute(JobSchedulerUtils.JOB_DEFINITIONS_PROP);
	    	
			writer.write("definitions", definitionManager);
		
			xmlResult = outputWriter.toString() + "</JobDefinitions>";
		
		} catch (IOException e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IntrospectionException e) {
		// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute( "XmlResult", xmlResult);
	
		return mapping.findForward("success");
    }

}

