<%
	class InvocationGenerator
	{
		private String hyadesTestSuiteClassName;
		
		public InvocationGenerator(String hyadesTestSuiteClassName)
		{
			this.hyadesTestSuiteClassName = hyadesTestSuiteClassName;
		}
		
		public StringBuffer generate(String owner, List actions)
		{
			StringBuffer code = new StringBuffer();
			StringBuffer sb = new StringBuffer();
			for(Iterator i=actions.iterator(); i.hasNext();)
			{
				IAction action = (IAction)i.next();
				
				if(action instanceof ITestInvocation)
					sb = generate(owner, (ITestInvocation)action);
				else if(action instanceof ILoop)
					sb = generate(owner, (ILoop)action);
				else
					continue;

				if (sb == null)
				{ //error condition
					code.append(NL).append("//ERROR: No definition found for test element: ").append(action.getName()).append(NL).append(NL);
					continue;	
				}
				if(sb.length() == 0)
					continue;
				
				if(code.length() > 0)
					code.append(NL).append(NL);
				code.append(sb.toString());
			}

			return code;
		}
		
		private StringBuffer generate(String owner, ITestInvocation testInvocation)
		{
			StringBuffer sb = new StringBuffer(owner).append(".addTest(");
			ITest invokedTest = testInvocation.getInvokedTest();
			if(invokedTest == null)
				return null;
				
			if(invokedTest instanceof ITestSuite)
			{
				sb.append("((").append(hyadesTestSuiteClassName).append(")").append(helper.getImportedName(invokedTest.getImplementor().getResource())).append(".suite())");
			}
			else
			{
				ITestCase testCase = (ITestCase)invokedTest;
				String identifier = testCase.getImplementor().getResource();
				if(identifier == null)
					identifier = helper.retrieveIdentifierName(testCase, "METHOD");

				sb.append("new ").append(helper.getImportedName(testCase.getOwner().getImplementor().getResource())).append("(\"").append(identifier).append("\")");
				sb.append(".setId(\"").append(invokedTest.getId()).append("\")");
			}
			sb.append(".setTestInvocationId(\"").append(testInvocation.getId()).append("\")");			
			if(!testInvocation.isSynchronous())
				sb.append(".setSynchronous(false)");
			
			return sb.append(");");
		}

		private StringBuffer generate(String owner, ILoop loop)
		{
			String identifier = helper.retrieveIdentifierName(loop, "METHOD");
			String name = loop.getName();
			if(name == null)
				name = "";
			else
				name = "\"" + name + "\"";
				
			StringBuffer sb = new StringBuffer(hyadesTestSuiteClassName).append(" ").append(identifier);
			sb.append(" = new ").append(hyadesTestSuiteClassName).append("(").append(name).append(");").append(NL);
			sb.append(owner).append(".addTest(new ").append(helper.getImportedName("junit.extensions.RepeatedTest")).append("(").append(identifier).append(", ").append(loop.getCondition().getConstraint()).append("));").append(NL);
			sb.append(identifier).append(".setId(\"").append(loop.getId()).append("\")");
			if(!loop.isSynchronous())
				sb.append(".setSynchronous(false)");				
			sb.append(";");
			
			//search for page data
			IProperty prop = loop.getActionProperties().getPropertyByName("HTTPPAGE");
			if (prop != null)
			{
				//Because we can't go back from the ITest (in MethodBodyGenerator), we will determine page enumerations here 
				//and store them in a variable (to be retrieved in MethodBody Generator)
				List actions = loop.getBlock().getActions();
				if (actions.size() >= 1)
				{
					if (actions.size() == 1)
					{//PAGE_ONLY
						if (actions.get(0) instanceof ITestInvocation)
						{ 
							ITest test = ((ITestInvocation)actions.get(0)).getInvokedTest();
							if (test != null)
							{
								IVariable var = HyadesFactory.INSTANCE.createVariable();
								var.setInitialValue("PAGE_ONLY");
								var.setName("pageOrder");
								test.getVariables().add(var);
							}											
						}
					}
					for (int nCount = 0; nCount < actions.size(); nCount++)
					{
						Object o = actions.get(nCount);
						if (o instanceof ITestInvocation)
						{
							ITest itest = ((ITestInvocation)o).getInvokedTest();
							if (itest != null)
							{
								IVariable var = HyadesFactory.INSTANCE.createVariable();
								
								if (nCount == 0)
									var.setInitialValue("PAGE_START");
								else if (nCount == actions.size() - 1)
									var.setInitialValue("PAGE_END");
								else
									var.setInitialValue("PAGE_MID");
								var.setName("pageOrder");
								itest.getVariables().add(var);
							}
						}							
					}
					
				}
					
			}
			return sb.append(NL).append(NL).append(generate(identifier, loop.getBlock().getActions()).toString());
		}
	}
%>