/**
The contents of this file are subject to the Mozilla Public License Version 1.1 
(the "License"); you may not use this file except in compliance with the License. 
You may obtain a copy of the License at http://www.mozilla.org/MPL/ 
Software distributed under the License is distributed on an "AS IS" basis, 
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the 
specific language governing rights and limitations under the License. 

The Original Code is "DataTypeGenerator.java".  Description: 
"Generates skeletal source code for Datatype classes based on the 
  HL7 database" 

The Initial Developer of the Original Code is University Health Network. Copyright (C) 
2001.  All Rights Reserved. 

Contributor(s):  James Agnew 

Alternatively, the contents of this file may be used under the terms of the 
GNU General Public License (the  �GPL�), in which case the provisions of the GPL are 
applicable instead of those above.  If you wish to allow use of your version of this 
file only under the terms of the GPL and not to allow others to use your version 
of this file under the MPL, indicate your decision by deleting  the provisions above 
and replace  them with the notice and other provisions required by the GPL License.  
If you do not delete the provisions above, a recipient may use your version of 
this file under either the MPL or the GPL. 

 */

package ca.uhn.hl7v2.mvnplugin;

import java.io.FileReader;

import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.util.IOUtil;

import ca.uhn.hl7v2.conf.parser.ProfileParser;
import ca.uhn.hl7v2.conf.spec.RuntimeProfile;
import ca.uhn.hl7v2.sourcegen.conf.GenerateDataTypesEnum;
import ca.uhn.hl7v2.sourcegen.conf.ProfileSourceGenerator;

/**
 * Maven Plugin for generating HAPI message structure classes which are based on
 * an HL7 conformance profile file. For more information on using this plugin, see
 * the <a href="confgen-usage.html">Confgen Usage</a> page.
 * 
 * @author <a href="mailto:jamesagnew@sourceforge.net">James Agnew</a>
 * @goal confgen
 * @phase generate-sources
 * @requiresDependencyResolution runtime
 * @requiresProject
 * @inheritedByDefault false
	 * @since 2.1
 */
public class ConfGenMojo extends AbstractMojo {

	/**
	 * The maven project.
	 * 
	 * @parameter property="project"
	 * @required
	 * @readonly
	 * @since 2.1
	 */
	MavenProject project;

	/**
	 * The target directory for the generated source
	 * 
	 * @parameter
	 * @required
	 * @since 2.1
	 */
	String targetDirectory;

	/**
	 * The conformance profile (XML file path) to use
	 * 
	 * @parameter
	 * @required
	 * @since 2.1
	 */
	String profile;

	/**
	 * The package for the generated source, e.g. "com.acme.hl7structure.adt"
	 * 
	 * @parameter
	 * @required
	 * @since 2.1
	 */
	String packageName;

	/**
	 * Should data types be generated. Valid options are:
	 * <ul>
	 * <li><b>NONE</b>: Do not generate custom data types, use HAPI's normal
	 * data type classes for the HL7 version that the profile corresponds to
	 * <li><b>SINGLE</b>: Generate a single instance of each data type. In this
	 * case, hapi will generate a custom data type for the first instance of
	 * each type that it finds. So, any customizations that need to be made must
	 * be made in the very first time that a particular data type is used within
	 * a profile. For example, if you need to customize the "SN" data type, you
	 * will need to customize the very first one that appears in the profile, as
	 * all SNs within your message type will use the structure generated by the
	 * first one.
	 * <li><b>ALL</b>: Generate an individual data type object for each and every
	 * instance of a datatype within a structure. Note that this is very powerful
	 * and flexible,
	 * but also has the potential to generate a large number of classes, so use with
	 * caution. 
	 * </ul>
	 * 
	 * @parameter default-value="NONE"
	 * @since 2.1
	 */
	String generateDataTypes = "NONE";

	/**
	 * The package from which to load the templates
	 * 
	 * @parameter default="ca.uhn.hl7v2.sourcegen.templates"
	 * @since 2.1
	 */
	String templatePackage = "ca.uhn.hl7v2.sourcegen.templates";

	/**
	 * Should structures be treated as resources
	 */
	private boolean structuresAsResources;

	/**
	 * File extension for the generated source files.
	 */
	private String fileExt = "java";

	/**
	 * {@inheritDoc}
	 */
	public void execute() throws MojoExecutionException {

		GenerateDataTypesEnum genDt;
		try {
			genDt = GenerateDataTypesEnum.valueOf(generateDataTypes);
		} catch (IllegalArgumentException e) {
			throw new MojoExecutionException("Unknown \"generateDataTypes\" value: " + generateDataTypes);
		}

		try {

			String profileString;
			FileReader reader = new FileReader(profile);
			profileString = IOUtil.toString(reader);

			ProfileParser profileParser = new ProfileParser(false);
			RuntimeProfile runtimeProfile = profileParser.parse(profileString);

			ProfileSourceGenerator gen = new ProfileSourceGenerator(runtimeProfile, targetDirectory, packageName, genDt, templatePackage, fileExt);
			gen.generate();

			if (!structuresAsResources) {
				getLog().info("Adding path to compile sources: " + targetDirectory);
				project.addCompileSourceRoot(targetDirectory);
			}

		} catch (Exception e) {
			throw new MojoExecutionException(e.getMessage(), e);
		}

		project.addCompileSourceRoot(targetDirectory);

	}

	public static void main(String[] args) throws MojoExecutionException, MojoFailureException {

		ConfGenMojo tst = new ConfGenMojo();
		tst.targetDirectory = "hapi-test/target/generated-sources/confgen";
		tst.packageName = "ca.uhn.hl7v2.test.conf.json";
		tst.profile = "hapi-test/src/test/resources/ca/uhn/hl7v2/conf/parser/ADT_A01.xml";
		tst.structuresAsResources = true;
		tst.templatePackage = "ca.uhn.hl7v2.sourcegen.templates.json";
		tst.generateDataTypes = "SINGLE";
		tst.fileExt = "json";
		tst.execute();

	}

}
