package etlfit;

import fitlibrary.DoFixture;

import java.io.*;
import java.util.*;

public class RunKettleFixture extends DoFixture {
	private String logLevel = "Basic";
	private String logDirectory = "/tmp/";
	private String kettleDirectory = "/opt/pentaho/data-integration/";
	private String trnExecutor = "pan.sh";
	private String jobExecutor = "kitchen.sh";
	private int exitValue;

	private int runProcess(ProcessBuilder pb) throws IOException, InterruptedException {
		Process process = pb.start();
		return process.waitFor();
	}

	public boolean runTransformationAtWith(String trnName, String trnPath, String[] trnParams) {

	}

	public boolean runTransformationWith(String trnPath, String[] trnParams) {

	}

	public boolean runTransformationAt(String trnName, String trnPath) {
		ProcessBuilder pb = new ProcessBuilder(kettleDirectory + trnExecutor, 
				"/file:" + trnPath + ".ktr", 
				"/level:" + logLevel, 
				"/log:" + logDirectory + logFile + ".log");
		exitValue = runProcess(pb);

		return exitValue == 0;
	}

	public boolean runTransformation(String trnPath) throws IOException, InterruptedException {
		String trnName;
		if (trnPath.lastIndexOf("/") >= 0) {
			trnName = trnPath.substring(trnPath.lastIndexOf("/"));
			trnPath = trnPath.substring(0, trnPath.lastIndexOf("/") + 1);
		} else {
			trnName = trnPath;
			trnPath = "";
		}
	}

	public void setLogLevel(String level) {
		logLevel = level;
	}

	public void setLogDirectory(String dir) {
		logDirectory = dir;
	}

	public void setKettleDirectory(String dir) {
		kettleDirectory = dir;
	}

	public String getExitValue() {
		String exitMessage;
		switch (exitValue) {
			case 0: exitMessage = "The transformation/job ran without a problem";
				break;
			case 1: exitMessage = "Errors occured during processing";
				break;
			case 2: exitMessage = "An unexpected error occurred during loading / running of the transformation/job";
				break;
			case 3: exitMessage = "Unable to prepare and initialize this transformation";
				break;
			case 7: exitMessage = "The transformation/job could not be loaded from XML or the Repository";
				break;
			case 8: exitMessage = "Error loading steps or plugins";
				break;
			case 9: exitMessage = "Command line usage printing";
				break;
			default: exitMessage = "n/a";
				 break;
		}
		return Integer.toString(exitValue) + ": " + exitMessage;
	}
}


@@ -353,24 +353,61 @@
 itchen.sh%22;%0A
+%09private String etlDirectory = %22~/%22;%0A
 %09private int
@@ -650,37 +650,358 @@
 tring%5B%5D trnParam
-s) %7B%0A
+Array) %7B%0A%09%09String trnParams;%0A%09%09for (String param : trnParamArray) %7B%0A%09%09%09%0A%09%09%7D%0A%09%09%0A%09%09ProcessBuilder pb = new ProcessBuilder(kettleDirectory + trnExecutor, %0A%09%09%09%09%22/file:%22 + trnPath + trnName + %22.ktr%22, %0A%09%09%09%09%22/level:%22 + logLevel, %0A%09%09%09%09%22/log:%22 + logDirectory + trnName + %22.log%22);%0A%09%09exitValue = runProcess(pb);%0A%0A%09%09return exitValue == 0;
 %0A%09%7D%0A%0A%09public boo
@@ -1029,36 +1029,36 @@
 nWith(String trn
-Path
+Name
 , String%5B%5D trnPa
@@ -1237,16 +1237,26 @@
 rnPath +
+ trnName +
  %22.ktr%22,
@@ -1317,14 +1317,14 @@
 y + 
-logFil
+trnNam
 e + 
@@ -1436,20 +1436,20 @@
 ring trn
-Path
+Name
 ) throws
@@ -1491,239 +1491,58 @@
 %7B%0A%09%09
-String trnName;%0A%09%09if (trnPath.lastIndexOf(%22/%22) %3E= 0) %7B%0A%09%09%09trnName = trnPath.substring(trnPath.lastIndexOf(%22/%22));%0A%09%09%09trnPath = trnPath.substring(0, trnPath.lastIndexOf(%22/%22) + 1);%0A%09%09%7D else %7B%0A%09%09%09trnName = trnPath;%0A%09%09%09trnPath = %22%22;%0A%09%09%7D
+return runTransformationAt(trnName, etlDirectory);
 %0A%09%7D%0A
@@ -1668,32 +1668,102 @@
 tory = dir;%0A%09%7D%0A%0A
+%09public void setEtlDirectory(String dir) %7B%0A%09%09etlDirectory = dir;%0A%09%7D%0A%09%0A
 %09public void set

save changes