package org.sagebionetworks.tool.migration.v3;

import java.io.IOException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sagebionetworks.client.exceptions.SynapseException;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.tool.migration.MigrationConfigurationImpl;

/**
 * The main entry point for the V3 data migration process.
 * @author jmhill
 *
 */
public class MigrationClientMain {

	static private Log log = LogFactory.getLog(MigrationClientMain.class);
	/**
	 * The main entry for for the V3 migration client.
	 * @param args
	 * @throws IOException 
	 * @throws SynapseException 
	 * @throws JSONObjectAdapterException 
	 */
	public static void main(String[] args) throws Exception {
		// First load the configuration
		MigrationConfigurationImpl configuration = new MigrationConfigurationImpl();
		loadConfigUsingArgs(configuration, args);		
		// Create the client factory
		SynapseClientFactory factory = new SynapseClientFactoryImpl(configuration);
		MigrationClient client = new MigrationClient(factory);
		try{
			client.migrateAllTypes(configuration.getMaximumBatchSize(), configuration.getWorkerTimeoutMs(), configuration.getRetryDenominator());
		}catch (Throwable e){
			log.error("Failed: "+e.getMessage(), e);
			System.exit(-1);
		}
		System.exit(0);
	}
	
	/**
	 * Load the configuration using the passed args.
	 * @param configuration 
	 * @param args
	 * @throws IOException
	 */
	public static void loadConfigUsingArgs(MigrationConfigurationImpl configuration, String[] args) throws IOException {
		// Load the location of the configuration property file
		if (args == null) {
			throw new IllegalArgumentException(	"The first argument must be the configuration property file path.");
		}
		if (args.length != 1) {
			throw new IllegalArgumentException(	"The first argument must be the configuration property file path. args.length: "+args.length);
		}
		String path = args[0];
		// Load all of the configuration information.
		configuration.loadConfigurationFile(path);
	}
}

@@ -1669,301 +1669,133 @@
 %0D%0A%09%09
-// Load the location of the configuration property file%0D%0A%09%09if (args == null) %7B%0D%0A%09%09%09throw new IllegalArgumentException(%09%22The first argument must be the configuration property file path.%22);%0D%0A%09%09%7D%0D%0A%09%09if (args.leng
+if (args != null && args.length == 1) %7B%0D%0A%09%09%09// Load and validate from file%0D%0A%09%09%09String pa
 th 
-!
 = 
-1) %7B%0D%0A%09%09%09throw new IllegalArgumentException(%09%22The first argument must be the c
+args%5B0%5D;%0D%0A%09%09%09configuration.loadC
 onfi
@@ -1806,139 +1806,68 @@
 tion
- property file 
+File(
 path
-. args.length: %22+args.length);%0D%0A%09%09%7D%0D%0A%09%09String path = args%5B0%5D;
+);%0D%0A%09%09%7D else %7B
 %0D%0A
+%09
 %09%09// 
-Load all of the configuration information.
+Validate System properties
 %0D%0A
+%09
 %09%09co
@@ -1870,36 +1870,40 @@
 %09%09configuration.
-load
+validate
 ConfigurationFil
@@ -1899,27 +1899,34 @@
 guration
-File(path);
+Properties();%0D%0A%09%09%7D
 %0D%0A%09%7D%0D%0A%7D%0D

Support using System props