/*
 * Created on 2004-6-13
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package workflow.script.interpretor;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import workflow.script.Constants;
import workflow.script.ScriptCompilationContext;
import workflow.script.ScriptException;
import workflow.script.compiler.Compiler;
/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class ScriptGenerator
{
	private ScriptWriter out;
	private ScriptCompilationContext ctxt;

	/**
	 * Constructor.
	 */
	ScriptGenerator(ScriptWriter out, Compiler compiler)
	{
		this.out = out;
		ctxt = compiler.getCompilationContext();
	}

	/**
	 * The main entry for Generator.
	 * @param out The servlet output writer
	 * @param compiler The compiler
	 */
	public static void generate(ScriptWriter out, Compiler compiler)
		throws ScriptException, IOException
	{

		ScriptGenerator gen = new ScriptGenerator(out, compiler);
		gen.generate();

	}

	public void generate() throws ScriptException, IOException
	{
		String servletPackageName = ctxt.getServletPackageName();
		String servletClassName = ctxt.getServletClassName();
		String evaluteMethodName = Constants.EVALUTE_METHOD_NAME;

		// First the package name:
		genPreamblePackage(servletPackageName);

		// Generate imports
		genPreambleImports();

		// Generate class declaration
		out.print("public final class ");
		out.print(servletClassName);
		out.print(" extends ");
		out.println(Constants.SERVLET_BASE_CLASS);
		out.println(" {");

		generateInit();

		// Now the service method
		out.print("public boolean ");
		out.print(evaluteMethodName);
		out.println("(WorkflowWork workflowWork,ParameterContext paramContext)");
		out.println("throws ScriptException,WorkflowException");
		out.println("{");
		generateBody();
		out.println("}");

		generateDestroy();
		out.println("}");

		out.println();

	}
	private void generateInit() throws ScriptException
	{
		out.print("public void _script_init() {}");
		out.println();
	}

	private void generateBody() throws ScriptException, IOException
	{
		String wfsFile = ctxt.getScriptFilePath();
		InputStream wfsInput = ctxt.getResourceAsStream(wfsFile);
		BufferedReader wfs_reader = new BufferedReader(new InputStreamReader(wfsInput));
		String line = null;

		try
		{
			while ((line = wfs_reader.readLine()) != null)
			{
				out.println(line);
			}
		}
		finally
		{
			try
			{
				wfsInput.close();
			}
			catch (IOException e)
			{
				;
			}
		}
	}

	private void generateDestroy() throws ScriptException
	{

		out.print("public void _script_destroy() {}");
		out.println();
	}

	private void genPreamblePackage(String packageName) throws ScriptException
	{
		if (!"".equals(packageName) && packageName != null)
		{
			out.print("package " + packageName + ";");
			out.println();
		}
	}

	private void genPreambleImports() throws ScriptException
	{
		for (int i = 0; i < Constants.STANDARD_IMPORTS.length; i++)
		{
			out.print("import ");
			out.print(Constants.STANDARD_IMPORTS[i]);
			out.println(";");
		}
		out.println();
	}
}
