package pete.executables;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.apache.commons.io.FileUtils;

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

public final class AnalysisWorkflow {

	private final Path root;

	private final 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.ADAPTABILITY.equals(type)) {
			fileAnalyzer = new AdaptabilityAnalyzer();
		} 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);
		}

		System.out.println("=======================================");
		System.out.println("Analysis finished; writing results");
		writeResults();

		purgeTempDir();
	}

	private void parseFile(Path file) {
		report = new Report();
		fileAnalyzer.analyzeFile(file).forEach(
				reportEntry -> report.addEntry(reportEntry));
	}

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

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

	private void purgeTempDir() {
		try {
			FileUtils.deleteDirectory(new File("tmp"));
		} catch (IOException e) {
			// Not critical -> ignore
		}
	}
}

@@ -1484,16 +1484,63 @@
 tart() %7B
+%0A%09%09long startTime = System.currentTimeMillis();
 %0A%0A%09%09if (
@@ -1628,16 +1628,74 @@
 );%0A%09%09%7D%0A%0A
+%09%09long duration = System.currentTimeMillis() - startTime;%0A
 %09%09System
@@ -1751,16 +1751,16 @@
 ====%22);%0A
-
 %09%09System
@@ -1791,16 +1791,57 @@
 inished;
+ Duration: %22 + duration%0A%09%09%09%09+ %22 millisec;
  writing

Print timing data to console