/*
 * MyServlet.java		Date created: 03.10.2008
 * Last modified by: $Author$
 * $Revision$	$Date$
 */

package org.ajax4jsf.autotest;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * TODO Class description goes here.
 * @author Andrey
 *
 */
public class AutoTestServlet extends HttpServlet {
	
	private static final String COMPONENT_PARAMETER_NAME = "c";
	
	private static final String EVENT_PARAMETER_NAME = "ev";
	
	private static final String ATTRIBUTE_PARAMETER_NAME = "at_name";
	
	private static final String ATTRIBUTE_PARAMETER_VALUE = "at_value";
	
	private static final String CONTEXT_NAME = "SeleniumAutoTestContext";
	
	String header = "<html xmlns=\"http://www.w3.org/1999/xhtml\" " + 
		"xmlns:a4j=\"http://richfaces.org/a4j\" " +
		"xmlns:rich=\"http://richfaces.org/rich\" " +
		"xmlns:h=\"http://java.sun.com/jsf/html\" " +
		"xmlns:f=\"http://java.sun.com/jsf/core\" " +
		"xmlns:ui=\"http://java.sun.com/jsf/facelets\">";
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
			AutoTestContext context = (AutoTestContext)getServletContext().getAttribute(CONTEXT_NAME);
			if (context == null) {
				throw new NullPointerException("Test context is null");
			}
		
			PrintWriter writer = resp.getWriter();
			writer.write(header);
			
			writer.write("<");
			writer.write(context.getComponent());
			writer.write(" ");
			writer.write("id=\"test\" ");
			
			for (String ev : context.getEvents()) {
				writer.write(ev + "=\"EventQueue.fire('"+ev+"')\" ");
			}
			
			for (String attribute : context.getAttributes().keySet()) {
				writer.write(attribute + "=\"" + context.getAttributes().get(attribute)+"\" ");
			}
			
			
			writer.write(">");
			writer.write("</" + context.getComponent() + ">");
			
			writer.write("</html>");
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String component = req.getParameter(COMPONENT_PARAMETER_NAME);
		String [] events = req.getParameterValues(EVENT_PARAMETER_NAME);
		String [] attributes_name = req.getParameterValues(ATTRIBUTE_PARAMETER_NAME);
		String [] attributes_value = req.getParameterValues(ATTRIBUTE_PARAMETER_VALUE);
		
		
		AutoTestContext context = new AutoTestContext(component);
		for (String event: events) {
			context.addEvent(event);
		}
		
		for (int i = 0; i < attributes_name.length; i++) {
			context.addAttribute(attributes_name[i], attributes_value[i]);
		}
		
		getServletContext().setAttribute(CONTEXT_NAME, context);
		
		resp.sendRedirect("/richfaces/faces/NEKO/pages/_autotest/autotest.xhtml");

	}
	
	

}
