package io.biblia.workflows.evaluation;

import io.biblia.workflows.action.Action;
import com.google.common.base.Preconditions;

/**
 * This class it is used as an action stub.
 * For the purpose of evaluating the workflow system, 
 * an action is something that takes computational time 
 * and produces an output of a given size. 
 * 
 * This action stub will receive three parameters:
 * args[0]: path to input file
 * args[1]: path to output file.
 * args[2]: size of output file to produce in MBs
 * args[3]: computational time to take. 
 * @author dearj019
 *
 */
public class ActionStub implements Action {

	public static void main(String[] args) {
		
		if (args.length != 4) {
			//Passing the wrong number of parameters.
			System.err.println("ERROR: Not enough number of parameters were passed in args");
			System.exit(1);
		}
		String inputFilePath = args[0];
		String outputFilePath = args[1];
		
		int outputSizeMB = 0;
		try{
			outputSizeMB = Integer.parseInt(args[2]);
		}
		catch(NumberFormatException ex) {
			System.err.println("ERROR: Did not contain a valid integer "
					+ "for parameter representing the size of output file in MBs");
			throw ex;
		}
		
		long computationTimeMilli = 0;
		try {
			computationTimeMilli = Long.parseLong(args[3]);
		}
		catch(NumberFormatException ex) {
			System.err.println("ERROR: Did not contain a valid long for parameter "
					+ "representing the computational time in milliseconds");
			throw ex;
		}
		
		Preconditions.checkArgument(outputSizeMB >= 0, "The output size must be greater than or equal to 0");
		Preconditions.checkArgument(computationTimeMilli > 0, "The computation time in milliseconds needs to be greater than 0");
		ActionStub stub = new ActionStub();
		stub.compute(computationTimeMilli);
		stub.writeDataset(outputFilePath, outputSizeMB);
	}
	
	private void writeDataset(String filePath, int size) {
		
		//1. Verify that the file path is correct.
		
		//2. Write bogus data into that file until size in MBs is satisfied.
		
	}
	
	private void compute(long milliseconds) {
		Preconditions.checkArgument(milliseconds > 0);
		try {
			Thread.sleep(milliseconds);
		}
		catch(InterruptedException ex) {
			System.err.println("ERROR: Current thread interrupted while sleeping for " + milliseconds + " milliseconds");
			Thread.currentThread().interrupt();
		}
	}
}

@@ -1828,118 +1828,303 @@
 %0A%09%0A%09
-private void
+/**%0A%09 * Writes a file to HDFS
  w
-r
 it
-eDataset(String filePath, int size) %7B%0A%09%09%0A%09%09//1. Verify that the file path is correct.
+h the properties specified by the parameters.%0A%09 * @param filePath: The complete path to the file to write in the hadoop filesystem%0A%09 * @param size: The size of the file to produce in megabytes%0A%09 */ %0A%09private void writeDataset(String filePath, int size) %7B
 %0A%09%09%0A%09%09//
 2. W
@@ -2119,17 +2119,17 @@
 %0A%09%09%0A%09%09//
-2
+1
 . Write 
@@ -2186,16 +2186,218 @@
 isfied.%0A
+%09%09Configuration conf = new Configuration();%0A%09%09FileSystem fs = FileSystem.get(URI.create(filePath), conf);%0A%09%09OutputStream out = fs.create(new Path(dst));%0A%09%09IOUtils.copyBytes(%22testing%22, out, 4096, true);%0A
 %09%09%0A%09%7D%0A%09%0A

Update ActionStub.java