package com.excilys.liferay.gatling;

import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.sample.model.Request;
import com.liferay.sample.model.Scenario;
import com.liferay.sample.service.RequestLocalServiceUtil;
import com.liferay.sample.service.ScenarioLocalServiceUtil;

import java.util.List;

public class ScriptGenerator {

	private static Log log = LogFactoryUtil.getLog(GatlingPortlet.class);

	static public void generateImports(StringBuilder sb){

		sb.append("import com.excilys.ebi.gatling.core.Predef._\n")
		.append("import com.excilys.ebi.gatling.http.Predef._\n")
		.append("import com.excilys.ebi.gatling.jdbc.Predef._\n")
		.append("import Headers._\n")
		.append("import akka.util.duration._\n")
		.append("import bootstrap._\n")
		.append("import assertions._\n\n");
	}

	static public void generateClass(StringBuilder sb, Long simulationId) throws Exception {

		//list of the scenario
		List<Scenario> listScenario = ScenarioLocalServiceUtil.findBySimulationId(simulationId);

		generateVar(sb, listScenario);
		generateSetUp(sb, listScenario);
	}

	private static void generateSetUp(StringBuilder sb, List<Scenario> listScenario) {

		sb.append("\tsetUp(");

		for (Scenario scenario : listScenario) {
			sb.append("\n\t\t")
			.append(scenario.getName())
			.append(".users(")
			.append(scenario.getUsers_per_seconds())
			.append(").ramp(")
			.append(scenario.getDuration())
			.append(")");
		}

		sb.append(")\n\n");

	}

	static private void generateVar(StringBuilder sb, List<Scenario> listScenario) throws Exception {
		for (Scenario scenario : listScenario) {
			//declare un nouveau scenario
			sb.append("\tval ")
			.append(scenario.getName())
			.append(" = scenario(\"")
			.append(scenario.getName())
			.append("\") ");

			List<Request> listRequest = RequestLocalServiceUtil.findByScenarioId(scenario.getScenario_id());
			for (Request request : listRequest) {
				if(request.getChecked() == true ){
					generateRequest(sb, request, scenario);
				}
			}
			sb.append("\n\n");
		}

	}

	static private void generateRequest(StringBuilder sb, Request request, Scenario scenario) throws Exception {

		sb.append("\n\t\t.exec( http(\"")
		.append(request.getName())
		.append("\").get(\"")
		.append(scenario.getUrl_site())
		.append(request.getUrl())
		.append("\"))");
	}
}

@@ -2017,16 +2017,45 @@
 uest) %7B%0A
+%09%09%09%09//if checked in the jsp %0A
 %09%09%09%09if(r
@@ -2085,16 +2085,16 @@
 true )%7B%0A
-
 %09%09%09%09%09gen
@@ -2282,17 +2282,186 @@
 ption %7B%0A
+%09%09%0A%09%09String site = scenario.getUrl_site();%0A%09%09if(request.isPrivatePage())%7B%0A%09%09%09//String is a f****** immutable classe ;(%0A%09%09%09site = site.replace(%22/web/%22, %22/group/%22);%0A%09%09%7D%0A%09%09
 %0A
-
 %09%09sb.app
@@ -2551,38 +2551,20 @@
 .append(
-scenario.getUrl_
 site
-()
 )%0A%09%09.app
@@ -2581,24 +2581,24 @@
 t.getUrl())%0A
-
 %09%09.append(%22%5C
@@ -2604,13 +2604,14 @@
 %5C%22))%22);%0A
+%0A
 %09%7D%0A%7D%0A

update scriptGenerator 22/07 16:04