package parameter_estimation;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import parsers.ConfigurationInput;
import parsers.XMLInputParser;

public class ParameterEstimationDriver {
	public static Logger logger = Logger.getLogger(ParameterEstimationDriver.class);
	/**
	 * @param args
	 * @throws Exception 
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		long time = System.currentTimeMillis();
		initializeLog();

		readINPUT(args);
		
		XMLInputParser read = new XMLInputParser();
		//assume INPUT.xml is passed in as command line argument
		List<ConfigurationInput> readConfig = read.readConfig(args[0]);
		for (ConfigurationInput config : readConfig){
		
			//creation of the invoker object:
			ParamEstimationInvoker invoker = new ParamEstimationInvoker();

			//parity command creation and binding to invoker:
			Command parityCommand = new ParityPlotCommand(config);
			invoker.setCommand(0, parityCommand);
			
			//kinetic parameters optimization command creation and binding to invoker:
			Command optimCommand = new OptimizationCommand(config);
			invoker.setCommand(1, optimCommand);

			//excel postprocessing command creation and binding to invoker:
			Command excelCommand = new ExcelPostProcessingCommand(config);
			invoker.setCommand(2, excelCommand);
			
			//statistics command creation and binding to invoker:
			Command statisticsCommand = new OptimizationCommand(config);
			invoker.setCommand(3, statisticsCommand);
			
			//perform the request:
			invoker.performMode(config.getMODE());
		}
		
		long timeTook = (System.currentTimeMillis() - time)/1000;
		logger.info("Time needed for this program to finish: (sec) "+timeTook);
	}

	private static BufferedReader readINPUT(String[] args)
	throws FileNotFoundException {
		BufferedReader in;
		if(args.length == 0){
			//input file will be searched in working directory under the name INPUT.txt:
			in = new BufferedReader(new FileReader(System.getProperty("user.dir")+"/INPUT.txt"));	
		}
		else {
			//specify user-defined input file:
			in = new BufferedReader(new FileReader(args[0]));
		}
		return in;
	}

	
	public static void initializeLog(){
		/*	
		Layout layout = new SimpleLayout();

		//make Appender, it's a FileAppender, writing to NBMT.log:
		FileAppender appender = null;
		try {
			appender = new FileAppender(layout, "NBMT.log", false);
		} catch(Exception e) {}

		//add Appender:
		logger.addAppender(appender);
		 */	
		BasicConfigurator.configure();
	}
}

@@ -603,28 +603,8 @@
 g();
-%0A%0A%09%09readINPUT(args);
 %0A%09%09%0A
@@ -1827,436 +1827,8 @@
 %09%7D%0A%0A
-%09private static BufferedReader readINPUT(String%5B%5D args)%0A%09throws FileNotFoundException %7B%0A%09%09BufferedReader in;%0A%09%09if(args.length == 0)%7B%0A%09%09%09//input file will be searched in working directory under the name INPUT.txt:%0A%09%09%09in = new BufferedReader(new FileReader(System.getProperty(%22user.dir%22)+%22/INPUT.txt%22));%09%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09//specify user-defined input file:%0A%09%09%09in = new BufferedReader(new FileReader(args%5B0%5D));%0A%09%09%7D%0A%09%09return in;%0A%09%7D%0A%0A
 %09%0A%09p

remove readInput