package hibergui.generator.tool.ant;

import java.io.IOException;
import java.util.logging.LogManager;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.apache.tools.ant.*;
import org.xml.sax.SAXException;

import freemarker.template.TemplateException;
import hibergui.generator.Generator;

/**
 * Ant task class.
 * 
 * This class represents an Ant task, so with
 * using this task you can use HiberGUI with Ant.
 * 
 * @author Csaba Gyenge
 * @see http://ant.apache.org/manual/develop.html
 */
public class HiberGUITask extends Task {
	/*
	 * ANT task attributes and elements
	 */ 
	private String destdir;
	private Configuration configuration;
	private Logger logger;

	/**
	 * The method executing the task
	 */ 
	public void execute() throws BuildException {
		// Set logger if it is defined
		if (logger != null && logger.getConfigurationFile() != null) {
			System.setProperty("java.util.logging.config.file", logger.getConfigurationFile());
			try {
				LogManager.getLogManager().readConfiguration();
				Generator.getLogger().config("Logger configurations readed successfully from file \"" + logger.getConfigurationFile() + "\"");
			} catch (IOException e1) {
				Generator.getLogger().severe("IOException while reading logger configurations from file \"" + logger.getConfigurationFile() + "\"");
				e1.printStackTrace();
				System.exit(2);
			}
		}
		// Print log message
		Generator.getLogger().config("HiberGUI started by ant task.");
		// Check if there is specified configuration element
		if (configuration == null || configuration.getConfigurationFile() == null) {
			Generator.getLogger().severe("Unspecified configuration XML. Set it as a nested element of this task.");
			System.exit(1);
		}
		// Create generator and start generating
		try {
			if (destdir != null) {
				new Generator(configuration.getConfigurationFile(), destdir);
			} else {
				new Generator(configuration.getConfigurationFile());
			}
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * The setter for the <code>destdir</code> attribute.
	 * 
	 * @param destdir Destination directory of generated java source files
	 */ 
	public void setDestdir(String destdir) {
		this.destdir = destdir;
	}
	
	/**
	 * The creator of the <code>configuration</code> nested element.
	 */
	public Configuration createConfiguration(){
		this.configuration = new Configuration();
		return this.configuration;
	}
	
	/**
	 * Represents nested element, called <code>configuration</code>.
	 */
	public class Configuration{
		private String configurationFile;
			
		/**
		 * The setter for the <code>configurationfile</code> attribute.
		 * 
		 * @param configurationfile Path of configuration xml file.
		 */ 
		public void setConfigurationFile(String configurationFile) {
			this.configurationFile = configurationFile;
		}
		
		/**
		 * The getter for the <code>configurationfile</code> attribute.
		 */
		public String getConfigurationFile(){
			return this.configurationFile;
		}
	}
	
	/**
	 * The creator of the <code>logger</code> nested element.
	 */
	public Logger createLogger(){
		this.logger = new Logger();
		return this.logger;
	}
	
	/**
	 * Represents nested element, called <code>logger</code>.
	 */
	public class Logger{
		private String configurationFile;
			
		/**
		 * The setter for the <code>configurationfile</code> attribute.
		 * 
		 * @param configurationfile Path of configuration xml file.
		 */ 
		public void setConfigurationFile(String configurationFile) {
			this.configurationFile = configurationFile;
		}
		
		/**
		 * The getter for the <code>configurationfile</code> attribute.
		 */
		public String getConfigurationFile(){
			return this.configurationFile;
		}
	}
}
