/*******************************************************************************
 * Copyright (c) 2009, 2012, 2014 David A Carlson and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     David A Carlson (XMLmodeling.com) - initial API and implementation
 *     Christian W. Damus - validate UML model in UML-to-Ecore transformation
 *     Sean Muir (National E-Health Transition Authority (NEHTA)) - add Path Map Support
 *     
 * $Id$
 *******************************************************************************/
package org.openhealthtools.mdht.uml.cda.ant.taskdefs;

import org.apache.tools.ant.BuildException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.common.util.DiagnosticChain;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.Package;
import org.openhealthtools.mdht.uml.validation.util.UMLDiagnostician;

/**
 * Validate a UML model, reporting issues to the Ant console.
 */
public class ValidateModel extends CDAModelingSubTask {
	private boolean failOnError = true;

	public ValidateModel(CDAModelingTask parentTask) {
		super(parentTask);
	}

	@Override
	protected void checkAttributes() throws BuildException {
		assertTrue("The 'model' attribute must be specified.", getHL7ModelingTask().getDefaultModel() != null);
	}

	@Override
	public void doExecute() throws Exception {
		IProgressMonitor monitor = getProgressMonitor();
		validateUML(monitor);
	}

	private void validateUML(final IProgressMonitor monitor) {

		Package defaultModel = getHL7ModelingTask().getDefaultModel();
		Resource umlResource = defaultModel.eResource();

		org.openhealthtools.mdht.uml.common.UmlPlugin.computeModelPathMapExtensions();

		umlResource.getResourceSet().getURIConverter().getURIMap().putAll(EcorePlugin.computePlatformURIMap(false));

		EcoreUtil.resolveAll(umlResource.getResourceSet());

		monitor.setTaskName("Validating UML source model");
		logInfo("Validating UML source model...");

		UMLDiagnostician diagnostician = new UMLDiagnostician(monitor);
		final Diagnostic health = new LoggingDiagnosticChain(this);
		try {
			diagnostician.validate(defaultModel, (DiagnosticChain) health);
		} catch (Exception e) {
			throw new BuildException(e);
		}

		if ((health.getSeverity() >= Diagnostic.ERROR) && isFailOnError()) {
			throw new BuildException("Validation found one or more errors in the UML model.");
		}
	}

	// ANT task attributes -----------------------------------------------------

	public void setFailOnError(boolean failOnError) {
		this.failOnError = failOnError;
	}

	public boolean isFailOnError() {
		return failOnError;
	}

	// ANT task child elements
	// --------------------------------------------------

}

@@ -814,16 +814,75 @@
 efs;%0D%0A%0D%0A
+import java.io.FileWriter;%0D%0Aimport java.io.IOException;%0D%0A%0D%0A
 import o
@@ -2716,16 +2716,477 @@
 %0A%09%09%7D%0D%0A%0D%0A
+%09%09// Write out the model status - This is for other build processes to use in particular the automated build process%0D%0A%09%09//%0D%0A%09%09org.eclipse.emf.common.util.URI modelPath = umlResource.getURI();%0D%0A%09%09FileWriter fr;%0D%0A%09%09try %7B%0D%0A%09%09%09fr = new FileWriter(modelPath.toFileString().replaceFirst(%0D%0A%09%09%09%09modelPath.segment(modelPath.segmentCount() - 1), %22.modelStatus%22));%0D%0A%09%09%09fr.write(String.valueOf(health.getSeverity()));%0D%0A%09%09%09fr.close();%0D%0A%09%09%7D catch (IOException e) %7B%0D%0A%0D%0A%09%09%7D%0D%0A%0D%0A
 %09%09if ((h
@@ -3245,24 +3245,24 @@
 Error()) %7B%0D%0A
-
 %09%09%09throw new
@@ -3351,89 +3351,8 @@
 %0D%0A%0D%0A
-%09// ANT task attributes -----------------------------------------------------%0D%0A%0D%0A
 %09pub
@@ -3502,100 +3502,13 @@
 r;%0D%0A
+
 %09%7D%0D%0A%0D%0A
-%09// ANT task child elements%0D%0A%09// --------------------------------------------------%0D%0A%0D%0A
 %7D%0D%0A

Write out the status of the validation to local hidden file