<%
	class MethodBodyGenerator
	{	
		int think_time = Integer.parseInt( HttpPreferenceUtil.getInstance().getDefaultThinkTime());
		int variance = ( think_time > 0 ) ? Integer.parseInt( HttpPreferenceUtil.getInstance().getDefaultVariance()) : 0;		 
	
		public StringBuffer generate(ITestCase testCase)
		{
			StringBuffer sb = generateHttp(testCase);
			if(sb == null)
				sb = generateDefault();
			return sb;
		}
		
		protected StringBuffer generateHttp(ITestCase testCase)
		{
			RequestHelper requestHelper = new RequestHelper();
			requestHelper.setTestCase(testCase);
			HttpRequest request = requestHelper.createHttpRequest();
			if(request == null)
				return null;
		
			StringBuffer sb = new StringBuffer();
			
			sb.append(helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpRequest")).append(" request = new ").append(helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpRequest")).append("();");
			String value = null;

			value = request.getMethod();
			if(value != null)
				sb.append(NL).append("request.setMethod(\"").append(adjustString(value)).append("\");");
	
			// aberns: we oly want to add this call if 'getThinkTime' and 'setThinkTime' methods
			// exist in ther HTTPRequest, to ensure compatiblity with older tests. The same applies to HTTPExecutor. 
			

			if( think_time > 0 )
			{				
				long thinkTime = request.getThinkTime();
				if( thinkTime == -1 )
					thinkTime = think_time;
				if( thinkTime > 0 )
					sb.append("\n").append("request.setThinkTime( calcThinkTime( ").append( thinkTime ).append(",").append(variance).append(" ));");				
			}
			
			value = request.getVersion();
			if(value != null)
				sb.append(NL).append("request.setVersion(\"").append(adjustString(value)).append("\");");

			value = request.getHost();
			if(value != null)
				sb.append(NL).append("request.setHost(\"").append(adjustString(value)).append("\");");
			
			if(request.getPort() > 0)
				sb.append(NL).append("request.setPort(").append(request.getPort()).append(");");

			value = request.getAbsolutePath();
			if(value != null)
				sb.append(NL).append("request.setAbsolutePath(\"").append(adjustString(value)).append("\");");
				
			value = request.getBody();
			if(value != null)
				sb.append(NL).append("request.setBody(\"").append(adjustString(value)).append("\");");
			
			if (request.getPageNumber() >= 0)
				sb.append(NL).append("request.setPageNumber(").append(request.getPageNumber()).append(");");

			String pageEnum = helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpRequest") + ".PAGE_UNDEFINED";

			//dynamically determine the pageEnum
			IVariable pageOrder = null;
			for (Iterator it = testCase.getVariables().iterator(); it.hasNext();)
			{
				IVariable var = (IVariable) it.next();
				if (var.getName().equals("pageOrder"));
				{
					pageOrder = var;
					break;
				}
			}
			if (pageOrder != null)
			{
				if (pageOrder.getInitialValue().equals("PAGE_ONLY"))
					pageEnum = helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpRequest") + ".PAGE_ONLY";
				else if (pageOrder.getInitialValue().equals("PAGE_START"))
					pageEnum = helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpRequest") + ".PAGE_START";
				else if (pageOrder.getInitialValue().equals("PAGE_MID"))
					pageEnum = helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpRequest") + ".PAGE_MID";
				else if (pageOrder.getInitialValue().equals("PAGE_END"))
					pageEnum = helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpRequest") + ".PAGE_END";
			}
	
			sb.append(NL).append("request.setPageOrder(").append(pageEnum).append(");");  	

			
			sb.append(NL).append("request.setSecure(").append(request.getSecure()).append(");");
			
			HttpHeader[] headers = request.getHeaders();
			if(headers.length > 0)
			{
				for (int i = 0, maxi = headers.length; i < maxi; i++)
				{
					value = headers[i].getName();
					if(value == null)
						continue;
					sb.append(NL).append(NL);					
					
					if(i == 0 )
						sb.append(helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpHeader")).append(" header;").append(NL);					
					
					sb.append("header = new ").append(helper.getImportedName("org.eclipse.hyades.test.http.runner.HttpHeader")).append("();");
					
					sb.append(NL).append("header.setName(\"").append(adjustString(value)).append("\");");
					
					value = headers[i].getValue();
					if(value != null)
						sb.append(NL).append("header.setValue(\"").append(adjustString(value)).append("\");");
						
					sb.append(NL).append("request.addHeader(header);");
				}
			}
			sb.append(NL);
			sb.append(NL).append("executeRequest(request);");
	
			return sb;
		}

		protected StringBuffer generateDefault()
		{
			StringBuffer sb = new StringBuffer();
			sb.append("// Enter your code here");
			return sb;
		}
		
		protected String adjustString(String string)
		{
			string = StringUtil.replace(string, "\"", "\\\"");
			return string;
		}
	}
%>