/*_x000D_
 * Copyright (C) 2016 Dienst voor het kadaster en de openbare registers_x000D_
 * _x000D_
 * This file is part of Imvertor._x000D_
 *_x000D_
 * Imvertor is free software: you can redistribute it and/or modify_x000D_
 * it under the terms of the GNU General Public License as published by_x000D_
 * the Free Software Foundation, either version 3 of the License, or_x000D_
 * (at your option) any later version._x000D_
 *_x000D_
 * Imvertor is distributed in the hope that it will be useful,_x000D_
 * but WITHOUT ANY WARRANTY; without even the implied warranty of_x000D_
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the_x000D_
 * GNU General Public License for more details._x000D_
 *_x000D_
 * You should have received a copy of the GNU General Public License_x000D_
 * along with Imvertor.  If not, see <http://www.gnu.org/licenses/>._x000D_
 *_x000D_
 */_x000D_
_x000D_
package nl.imvertor.EpCompiler;_x000D_
_x000D_
import java.io.IOException;_x000D_
_x000D_
import org.apache.log4j.Logger;_x000D_
_x000D_
import nl.imvertor.common.Configurator;_x000D_
import nl.imvertor.common.Step;_x000D_
import nl.imvertor.common.Transformer;_x000D_
import nl.imvertor.common.exceptions.ConfiguratorException;_x000D_
import nl.imvertor.common.file.AnyFile;_x000D_
import nl.imvertor.common.file.AnyFolder;_x000D_
import nl.imvertor.common.file.XmlFile;_x000D_
_x000D_
// see also https://github.com/Imvertor/Imvertor-Maven/issues/56_x000D_
_x000D_
public class EpCompiler extends Step {_x000D_
_x000D_
	protected static final Logger logger = Logger.getLogger(EpCompiler.class);_x000D_
	_x000D_
	public static final String STEP_NAME = "EpCompiler";_x000D_
	public static final String VC_IDENTIFIER = "$Id: $";_x000D_
_x000D_
	/**_x000D_
	 *  run the main translation_x000D_
	 */_x000D_
	public boolean run() throws Exception{_x000D_
		_x000D_
		// set up the configuration for this step_x000D_
		configurator.setActiveStepName(STEP_NAME);_x000D_
		prepare();_x000D_
		_x000D_
		runner.info(logger,"Compiling EP");_x000D_
		_x000D_
		generate();_x000D_
		_x000D_
		configurator.setStepDone(STEP_NAME);_x000D_
		_x000D_
		// save any changes to the work configuration for report and future steps_x000D_
	    configurator.save();_x000D_
	    _x000D_
	    report();_x000D_
	    _x000D_
	    return runner.succeeds();_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Generate EP file suited for Kadaster and OGC Json schema._x000D_
	 * _x000D_
	 * @throws Exception_x000D_
	 */_x000D_
	public boolean generate() throws Exception {_x000D_
		_x000D_
		// create a transformer_x000D_
		Transformer transformer = new Transformer();_x000D_
						_x000D_
		boolean succeeds = true;_x000D_
		_x000D_
		runner.debug(logger,"CHAIN","Generating EP");_x000D_
		_x000D_
		String epSchema = (requiresMIM() ? "EP2.xsd" : "EP.xsd");_x000D_
		_x000D_
		transformer.setXslParm("ep-schema-path","xsd/" + epSchema);	_x000D_
		_x000D_
		// Create EP_x000D_
		if (requiresMIM()) {_x000D_
			// check of MIM resultaat beschikbaar is _x000D_
			succeeds = succeeds && AnyFile.exists(configurator.getXParm("properties/WORK_MIMFORMAT_XMLPATH",false));_x000D_
			// verwerk MIM naar EP_x000D_
			succeeds = succeeds && transformer.transformStep("properties/WORK_MIMFORMAT_XMLPATH","properties/WORK_EP_XMLPATH_PRE", "properties/IMVERTOR_EP2_XSLPATH_PRE");_x000D_
			succeeds = succeeds && transformer.transformStep("properties/WORK_EP_XMLPATH_PRE","properties/WORK_EP_XMLPATH_CORE", "properties/IMVERTOR_EP2_XSLPATH_CORE");_x000D_
			succeeds = succeeds && transformer.transformStep("properties/WORK_EP_XMLPATH_CORE","properties/WORK_EP_XMLPATH_FINAL", "properties/IMVERTOR_EP2_XSLPATH_POST");_x000D_
		} else _x000D_
			succeeds = succeeds && transformer.transformStep("properties/WORK_EMBELLISH_FILE","properties/WORK_EP_XMLPATH_FINAL", "properties/IMVERTOR_EP_XSLPATH");_x000D_
	_x000D_
		_x000D_
		// if this succeeds, copy the EP schema to the app and validate_x000D_
		if (succeeds) {_x000D_
			AnyFolder workAppFolder = new AnyFolder(Configurator.getInstance().getXParm("system/work-app-folder-path"));_x000D_
			_x000D_
			XmlFile resultEpFile = new XmlFile(configurator.getXParm("properties/WORK_EP_XMLPATH_FINAL"));_x000D_
			XmlFile targetEpFile = new XmlFile(workAppFolder.getCanonicalPath() + "/ep/ep.xml"); // TODO nette naam, bepaald door gebruiker oid._x000D_
			resultEpFile.copyFile(targetEpFile);_x000D_
			_x000D_
			XmlFile managedSchemaFile = new XmlFile(Configurator.getInstance().getBaseFolder().getCanonicalPath() + "/etc/xsd/EP/" + epSchema);_x000D_
			XmlFile targetSchemaFile = new XmlFile(workAppFolder.getCanonicalPath() + "/ep/xsd/" + epSchema);_x000D_
			managedSchemaFile.copyFile(targetSchemaFile);_x000D_
			_x000D_
			// Debug: test if EP is okay_x000D_
			succeeds = succeeds && resultEpFile.isValid();_x000D_
		}_x000D_
		configurator.setXParm("system/ep-schema-created",succeeds);_x000D_
		configurator.setXParm("system/ep-schema-version",requiresMIM() ? "2" : "1"); // when MIM based, generated EP version 2_x000D_
_x000D_
		return succeeds;_x000D_
	}_x000D_
	_x000D_
	public static Boolean requiresMIM() throws IOException, ConfiguratorException {_x000D_
		// bepaal of hier de MIM schema variant moet worden gebruikt_x000D_
		String jsonschemasource = Configurator.getInstance().getXParm("cli/jsonschemasource",false);_x000D_
		return (jsonschemasource == null || jsonschemasource.equals("MIM"));_x000D_
	}				_x000D_
_x000D_
}_x000D_

// check of MIM resultaat beschikbaar is _x000D_
/*_x000D_
 * Copyright (C) 2016 Dienst voor het kadaster en de openbare registers_x000D_
 * _x000D_
 * This file is part of Imvertor._x000D_
 *_x000D_
 * Imvertor is free software: you can redistribute it and/or modify_x000D_
 * it under the terms of the GNU General Public License as published by_x000D_
 * the Free Software Foundation, either version 3 of the License, or_x000D_
 * (at your option) any later version._x000D_
 *_x000D_
 * Imvertor is distributed in the hope that it will be useful,_x000D_
 * but WITHOUT ANY WARRANTY; without even the implied warranty of_x000D_
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the_x000D_
 * GNU General Public License for more details._x000D_
 *_x000D_
 * You should have received a copy of the GNU General Public License_x000D_
 * along with Imvertor.  If not, see <http://www.gnu.org/licenses/>._x000D_
 *_x000D_
 */_x000D_
_x000D_
package nl.imvertor.EpCompiler;_x000D_
_x000D_
import java.io.IOException;_x000D_
_x000D_
import org.apache.log4j.Logger;_x000D_
_x000D_
import nl.imvertor.common.Configurator;_x000D_
import nl.imvertor.common.Step;_x000D_
import nl.imvertor.common.Transformer;_x000D_
import nl.imvertor.common.exceptions.ConfiguratorException;_x000D_
import nl.imvertor.common.file.AnyFile;_x000D_
import nl.imvertor.common.file.AnyFolder;_x000D_
import nl.imvertor.common.file.XmlFile;_x000D_
_x000D_
// see also https://github.com/Imvertor/Imvertor-Maven/issues/56_x000D_
_x000D_
public class EpCompiler extends Step {_x000D_
_x000D_
	protected static final Logger logger = Logger.getLogger(EpCompiler.class);_x000D_
	_x000D_
	public static final String STEP_NAME = "EpCompiler";_x000D_
	public static final String VC_IDENTIFIER = "$Id: $";_x000D_
_x000D_
	/**_x000D_
	 *  run the main translation_x000D_
	 */_x000D_
	public boolean run() throws Exception{_x000D_
		_x000D_
		// set up the configuration for this step_x000D_
		configurator.setActiveStepName(STEP_NAME);_x000D_
		prepare();_x000D_
		_x000D_
		runner.info(logger,"Compiling EP");_x000D_
		_x000D_
		generate();_x000D_
		_x000D_
		configurator.setStepDone(STEP_NAME);_x000D_
		_x000D_
		// save any changes to the work configuration for report and future steps_x000D_
	    configurator.save();_x000D_
	    _x000D_
	    report();_x000D_
	    _x000D_
	    return runner.succeeds();_x000D_
	}_x000D_
_x000D_
	/**_x000D_
	 * Generate EP file suited for Kadaster and OGC Json schema._x000D_
	 * _x000D_
	 * @throws Exception_x000D_
	 */_x000D_
	public boolean generate() throws Exception {_x000D_
		_x000D_
		// create a transformer_x000D_
		Transformer transformer = new Transformer();_x000D_
						_x000D_
		boolean succeeds = true;_x000D_
		_x000D_
		runner.debug(logger,"CHAIN","Generating EP");_x000D_
		_x000D_
		String epSchema = (requiresMIM() ? "EP2.xsd" : "EP.xsd");_x000D_
		_x000D_
		transformer.setXslParm("ep-schema-path","xsd/" + epSchema);	_x000D_
		_x000D_
		// Create EP_x000D_
		if (requiresMIM()) {_x000D_
			// check of<SUF>
			succeeds = succeeds && AnyFile.exists(configurator.getXParm("properties/WORK_MIMFORMAT_XMLPATH",false));_x000D_
			// verwerk MIM naar EP_x000D_
			succeeds = succeeds && transformer.transformStep("properties/WORK_MIMFORMAT_XMLPATH","properties/WORK_EP_XMLPATH_PRE", "properties/IMVERTOR_EP2_XSLPATH_PRE");_x000D_
			succeeds = succeeds && transformer.transformStep("properties/WORK_EP_XMLPATH_PRE","properties/WORK_EP_XMLPATH_CORE", "properties/IMVERTOR_EP2_XSLPATH_CORE");_x000D_
			succeeds = succeeds && transformer.transformStep("properties/WORK_EP_XMLPATH_CORE","properties/WORK_EP_XMLPATH_FINAL", "properties/IMVERTOR_EP2_XSLPATH_POST");_x000D_
		} else _x000D_
			succeeds = succeeds && transformer.transformStep("properties/WORK_EMBELLISH_FILE","properties/WORK_EP_XMLPATH_FINAL", "properties/IMVERTOR_EP_XSLPATH");_x000D_
	_x000D_
		_x000D_
		// if this succeeds, copy the EP schema to the app and validate_x000D_
		if (succeeds) {_x000D_
			AnyFolder workAppFolder = new AnyFolder(Configurator.getInstance().getXParm("system/work-app-folder-path"));_x000D_
			_x000D_
			XmlFile resultEpFile = new XmlFile(configurator.getXParm("properties/WORK_EP_XMLPATH_FINAL"));_x000D_
			XmlFile targetEpFile = new XmlFile(workAppFolder.getCanonicalPath() + "/ep/ep.xml"); // TODO nette naam, bepaald door gebruiker oid._x000D_
			resultEpFile.copyFile(targetEpFile);_x000D_
			_x000D_
			XmlFile managedSchemaFile = new XmlFile(Configurator.getInstance().getBaseFolder().getCanonicalPath() + "/etc/xsd/EP/" + epSchema);_x000D_
			XmlFile targetSchemaFile = new XmlFile(workAppFolder.getCanonicalPath() + "/ep/xsd/" + epSchema);_x000D_
			managedSchemaFile.copyFile(targetSchemaFile);_x000D_
			_x000D_
			// Debug: test if EP is okay_x000D_
			succeeds = succeeds && resultEpFile.isValid();_x000D_
		}_x000D_
		configurator.setXParm("system/ep-schema-created",succeeds);_x000D_
		configurator.setXParm("system/ep-schema-version",requiresMIM() ? "2" : "1"); // when MIM based, generated EP version 2_x000D_
_x000D_
		return succeeds;_x000D_
	}_x000D_
	_x000D_
	public static Boolean requiresMIM() throws IOException, ConfiguratorException {_x000D_
		// bepaal of hier de MIM schema variant moet worden gebruikt_x000D_
		String jsonschemasource = Configurator.getInstance().getXParm("cli/jsonschemasource",false);_x000D_
		return (jsonschemasource == null || jsonschemasource.equals("MIM"));_x000D_
	}				_x000D_
_x000D_
}_x000D_
