/**
 * EBI Microarray Informatics Team (c) 2007-2008
 */
package uk.ac.ebi.ae3.indexbuilder;

import org.apache.commons.cli2.Argument;
import org.apache.commons.cli2.CommandLine;
import org.apache.commons.cli2.Group;
import org.apache.commons.cli2.builder.ArgumentBuilder;
import org.apache.commons.cli2.builder.DefaultOptionBuilder;
import org.apache.commons.cli2.builder.GroupBuilder;
import org.apache.commons.cli2.commandline.Parser;
import org.apache.commons.cli2.option.DefaultOption;
import org.apache.commons.cli2.util.HelpFormatter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;

import uk.ac.ebi.ae3.indexbuilder.service.IndexBuilderService;

/**
 * The main class which contains main method. Create expt lucene index.
 * Configuration is stored in app-context.xml file
 * 
 * @author mdylag
 * 
 */
public class App
{
	/** */
	private final HelpFormatter		helpFormatter   = new HelpFormatter();
	
	private final DefaultOptionBuilder optionBuilder   = new DefaultOptionBuilder();
	private final ArgumentBuilder	  argumentBuilder = new ArgumentBuilder();
	private final GroupBuilder		 groupBuilder	= new GroupBuilder();
	
	private final Argument			 pathArgument	= argumentBuilder
															   .withName("path")
															   .withMaximum(1)
															   .withMaximum(1)
															   .create();
	
	private final DefaultOption		optionProperty  = optionBuilder
															   .withLongName(
																	   Constants.KEY_PROPERTY)
															   .withRequired(
																	   true)
															   .withArgument(
																	   pathArgument)
															   .withRequired(
																	   true)
															   .withDescription(
																	   "Property file")
															   .create();
	private String					 propertyFile;
	private XmlBeanFactory appContext; 	
	
	private static final Log		   log			 = LogFactory
															   .getLog(App.class);
	
	public static void main(String[] args)
	{
		try
		{
			App app = new App();
			//TODO: Add exception to parse method
			app.parse(args);
			app.startContext();		
			app.run();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			log.error(e);
			System.exit(-1);
		}
		catch (IndexException e)
		{
			e.printStackTrace();
			log.error(e);
			System.exit(-1);
		}
		
	}
	protected void startContext()
	{
		PropertyPlaceholderConfigurer conf = new PropertyPlaceholderConfigurer();
		conf.setLocation(new FileSystemResource(propertyFile));
		
		appContext = new XmlBeanFactory(new ClassPathResource(
				"app-context.xml"));
		conf.postProcessBeanFactory(appContext);
		
	}
	/**
	 * DOCUMENT ME
	 * @throws Exception
	 * @throws IndexException
	 */
	
	protected void run() throws Exception, IndexException
	{
		IndexBuilderService indexBuilderService = (IndexBuilderService) appContext
				.getBean(Constants.indexBuilderServiceID);
		indexBuilderService.buildIndex();
		
	}
	
	protected boolean parse(String[] args)
	{
		Group groupOptions = groupBuilder.withOption(optionProperty).create();
		Parser parser = new Parser();
		
		parser.setGroup(groupOptions);
		parser.setHelpFormatter(helpFormatter);
		parser.setHelpTrigger("--help");
		CommandLine cl = parser.parseAndHelp(args);
		if (cl == null || !cl.hasOption(optionProperty))
		{
			helpFormatter.printException();
			return false;
		}
		else 
		{
			propertyFile = (String) cl.getValue(optionProperty);
			return true;
			
		}
		
	}
	public XmlBeanFactory getAppContext()
	{
		return appContext;
	}
	public void setAppContext(XmlBeanFactory appContext)
	{
		this.appContext = appContext;
	}
	
}

@@ -1789,35 +1789,36 @@
 %09%09%09%09%09%09%09%09%09%09%09%09%09   
-tru
+fals
 e)%0A%09%09%09%09%09%09%09%09%09%09%09%09%09
@@ -1875,67 +1875,8 @@
 nt)%0A
-%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09   .withRequired(%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09   true)%0A
 %09%09%09%09
@@ -2641,16 +2641,106 @@
 ocation(
+propertyFile == null ? new ClassPathResource(%22indexbuilder.properties%22)%0A                : 
 new File
@@ -3534,41 +3534,8 @@
 null
- %7C%7C !cl.hasOption(optionProperty)
 )%0A%09%09
@@ -3621,16 +3621,47 @@
 tyFile =
+ cl.hasOption(optionProperty) ?
  (String
@@ -3689,16 +3689,23 @@
 roperty)
+ : null
 ;%0A%09%09%09ret

Use compiled-in resources properties file if no command line option supplied