package pete.executables;

import java.nio.file.Files;
import java.nio.file.Path;

import pete.metrics.installability.deployability.DeploymentPackageAnalyzer;
import pete.metrics.installability.server.AverageInstallationTimeCalculator;
import pete.metrics.portability.PortabilityAnalyzer;
import pete.reporting.Report;
import pete.reporting.ReportEntry;
import pete.reporting.ReportWriter;

public class AnalysisWorkflow {

	private Path root;

	private DirectoryAnalyzer dirAnalyzer;

	private FileAnalyzer fileAnalyzer;

	private Report report;

	public AnalysisWorkflow(Path root, AnalysisType type) {
		this.root = root;

		if (!Files.exists(root)) {
			throw new IllegalArgumentException("path " + root.toString()
					+ " does not exist");
		}

		if (AnalysisType.DEPLOYABILITY.equals(type)) {
			fileAnalyzer = new DeploymentPackageAnalyzer();
		} else if (AnalysisType.INSTALLABILITY.equals(type)) {
			fileAnalyzer = new AverageInstallationTimeCalculator();
		} else if (AnalysisType.PORTABILITY.equals(type)) {
			fileAnalyzer = new PortabilityAnalyzer();
		} else if (AnalysisType.UNKNOWN.equals(type)) {
			throw new AnalysisException("no valid AnalysisType found");
		}
		dirAnalyzer = new DirectoryAnalyzer(fileAnalyzer);
	}

	public void start() {
		if (!Files.isDirectory(root)) {
			parseFile(root);
		} else {
			parseDirectory(root);
		}
		writeResults();
	}

	private void parseFile(Path file) {
		report = new Report();
		for (ReportEntry entry : fileAnalyzer.analyzeFile(file)) {
			report.addEntry(entry);
		}
	}

	private void parseDirectory(Path root) {
		report = dirAnalyzer.analyzeDirectory(root);
	}

	private void writeResults() {
		ReportWriter writer = new ReportWriter(report);
		writer.writeToExcelFile("results.csv");
		writer.writeToRFile("r-results.csv");
	}
}

@@ -36,49 +36,139 @@
 ava.
-nio.file.Files;%0Aimport java.nio.file.Path
+io.File;%0Aimport java.io.IOException;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0A%0Aimport org.apache.commons.io.FileUtils
 ;%0A%0Ai
@@ -1346,16 +1346,17 @@
 art() %7B%0A
+%0A
 %09%09if (!F
@@ -1441,16 +1441,17 @@
 t);%0A%09%09%7D%0A
+%0A
 %09%09writeR
@@ -1459,16 +1459,35 @@
 sults();
+%0A%0A%09%09purgeTempDir();
 %0A%09%7D%0A%0A%09pr
@@ -1899,11 +1899,162 @@
 sv%22);%0A%09%7D
+%0A%0A%09private void purgeTempDir() %7B%0A%09%09try %7B%0A%09%09%09FileUtils.deleteDirectory(new File(%22tmp%22));%0A%09%09%7D catch (IOException e) %7B%0A%09%09%09// Not critical -%3E ignore%0A%09%09%7D%0A%09%7D
 %0A%7D%0A

Purge tmp dir on exit