package org.data2semantics.platform.run;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.data2semantics.platform.Global;
import org.data2semantics.platform.core.Workflow;
import org.data2semantics.platform.execution.LocalExecutionProfile;
import org.data2semantics.platform.execution.Orchestrator;
import org.data2semantics.platform.reporting.CSVReporter;
import org.data2semantics.platform.reporting.HTMLReporter;
import org.data2semantics.platform.reporting.Reporter;
import org.data2semantics.platform.resourcespace.ResourceSpace;
import org.data2semantics.platform.util.WorkflowParser;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;

public class Run
{	
	private static WorkflowParser wfParser = new WorkflowParser();
	
    @Option(name="--classpath", usage="A directory containing source code and resources to be loaded. Each source file should be in a directory that matches the name of its controller (ie. java files should be in a directory called 'java'). (default: none)")
	private static String classPath = "";

    @Option(name="--output", usage="The output directory (default: the working directory)")    
	private static File output = new File(".");
    
    @Argument
    private List<String> arguments = new ArrayList<String>(1);
    
    public static void main(String[] args) throws IOException
    {
    	
    	// * Parse and check the command line input
    	Run run = new Run();
    	
    	CmdLineParser parser = new CmdLineParser(run);
    	try
		{
			parser.parseArgument(args);
		} catch (CmdLineException e)
		{
			usageExit(e.getMessage(), parser);
		}
    	
    	if(run.arguments.size() > 1)
    		usageExit("Too many non-option arguments: " + run.arguments, parser);
    	
    	File file;
    	if(run.arguments.isEmpty())
    		file = new File("workflow.yaml");
    	else 
    		file = new File(run.arguments.get(0));
    	
    	if(! file.exists())
    		usageExit("Workflow file ("+run.arguments.get(0)+") does not exist.", parser);
    	
    	// -- Beyond this point, errors are not the user's fault, and should not
    	//    cause a usage print. 
    	
    	
    	// * Scan the classpath for any Domains and add them dynamically to the 
    	//   global Domain store.
    	
    	// * Read the workflow description from a yaml file into a map
    	
    	Workflow workflow = WorkflowParser.parseYAML(file);
    	
    	// -- The workflow object will check the consistency of the inputs and 
    	//    outputs and make sure that everything can be executed.  
    	    	
		LocalExecutionProfile localEP = new LocalExecutionProfile();
		
		
		ResourceSpace rp = new ResourceSpace();
		
		
		List<Reporter> reporters = Arrays.asList(
					new HTMLReporter(workflow, new File(output, "report/")),
					new CSVReporter(workflow, new File(output, "csv/"))
				);
		
    	Orchestrator orchestrator = new Orchestrator(workflow,  localEP, rp);
    	
    	orchestrator.orchestrate();
    	
    	for(Reporter reporter : reporters)
    		reporter.report();
    	
    	Global.log().info("Workflow execution finished.");
    }
    
    public static void usageExit(String message, CmdLineParser parser)
    {
    	System.err.println(message);
        System.err.println("java -jar Platform.jar [options...] [input file (default:workflow.yaml)]");
        parser.printUsage(System.err);
        
        System.exit(1);
    }

}

@@ -254,16 +254,79 @@
 rkflow;%0A
+import org.data2semantics.platform.execution.ExecutionProfile;%0A
 import o
@@ -444,16 +444,92 @@
 trator;%0A
+import org.data2semantics.platform.execution.ThreadedLocalExecutionProfile;%0A
 import o
@@ -1054,16 +1054,248 @@
 er();%0A%09%0A
+%09enum ExecutionProfiles %7BLOCAL, THREADED, HADOOP%7D%0A%09@Option(name=%22--profile%22, usage=%22Execution profile to be used LOCAL THREADED HADOOP (default: LOCAL) %22)%0A%09private static ExecutionProfiles execProfile = ExecutionProfiles.LOCAL;%0A%09  %0A
     @Opt
@@ -3067,37 +3067,268 @@
 %09%0A%09%09
-LocalExecutionProfile localEP
+ExecutionProfile executionProfile;%0A%09%09%0A%09%09switch(execProfile)%7B%0A%09%09%09case LOCAL:%0A%09%09%09%09executionProfile = new LocalExecutionProfile();%0A%09%09%09%09break;%0A%09%09%09case THREADED:%0A%09%09%09%09executionProfile = new ThreadedLocalExecutionProfile();%0A%09%09%09%09break;%0A%09%09%09default:%0A%09%09%09%09executionProfile
  = n
@@ -3353,24 +3353,25 @@
 rofile();%0A%09%09
+%7D
 %0A%09%09%0A%09%09Resour
@@ -3648,15 +3648,24 @@
 w,  
-localEP
+executionProfile
 , rp

Add option for execution profile