/*******************************************************************************
 * Copyright (c) 2009 David A Carlson.
 * 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
 *     
 * $Id$
 *******************************************************************************/
package org.openhealthtools.mdht.uml.cda.transform;

import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Constraint;
import org.eclipse.uml2.uml.EnumerationLiteral;
import org.eclipse.uml2.uml.Stereotype;
import org.openhealthtools.mdht.uml.cda.resources.util.CDAProfileUtil;
import org.openhealthtools.mdht.uml.cda.resources.util.ICDAProfileConstants;

public class TransformConstraint extends TransformAbstract {
	public TransformConstraint(EcoreTransformerOptions options) {
		super(options);
	}
	
	public Object caseConstraint(Constraint constraint) {
		Class constrainedClass = null;
		if (constraint.getContext() instanceof Class) {
			constrainedClass = (Class) constraint.getContext();
		}
		else {
			return null;
		}

		constraint.setName(normalizeConstraintName(constraint.getName()));
		
		String severity = SEVERITY_ERROR;
		String message = null;
		Stereotype validation = CDAProfileUtil.getAppliedCDAStereotype(constraint, ICDAProfileConstants.CONSTRAINT_VALIDATION);
		if (validation != null) {
			message = (String) constraint.getValue(validation, ICDAProfileConstants.VALIDATION_MESSAGE);
			EnumerationLiteral literal = (EnumerationLiteral) constraint.getValue(validation, ICDAProfileConstants.VALIDATION_SEVERITY);
			severity = (literal != null) ? literal.getName() : SEVERITY_ERROR;
			CDAProfileUtil.unapplyCDAStereotype(constraint, ICDAProfileConstants.CONSTRAINT_VALIDATION);
		}
		
		if (SEVERITY_INFO.equals(severity)) {
			addValidationInfo(constrainedClass, constraint.getName(), message);
		} else if (SEVERITY_WARNING.equals(severity)) {
			addValidationWarning(constrainedClass, constraint.getName(), message);
		} else {
			addValidationError(constrainedClass, constraint.getName(), message);
		}

		return constraint;
	}
}

@@ -618,16 +618,71 @@
 orm;%0D%0A%0D%0A
+import java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0A%0D%0A
 import o
@@ -827,26 +827,122 @@
 ml2.uml.
-Stereotype
+OpaqueExpression;%0D%0Aimport org.eclipse.uml2.uml.Stereotype;%0D%0Aimport org.eclipse.uml2.uml.ValueSpecification
 ;%0D%0Aimpor
@@ -1296,16 +1296,850 @@
 int) %7B%0D%0A
+%09%09// remove all spec languages other than the first OCL expression%0D%0A%09%09ValueSpecification spec = constraint.getSpecification();%0D%0A%09%09if (spec instanceof OpaqueExpression) %7B%0D%0A%09%09%09List%3CString%3E languages = new ArrayList%3CString%3E(((OpaqueExpression) spec).getLanguages());%0D%0A%09%09%09String oclBody = null;%0D%0A%09%09%09for (int i=0; i%3Clanguages.size(); i++) %7B%0D%0A%09%09%09%09String lang = languages.get(i);%0D%0A%09%09%09%09if (%22OCL%22.equals(lang)) %7B%0D%0A%09%09%09%09%09oclBody = ((OpaqueExpression) spec).getBodies().get(i);%0D%0A%09%09%09%09%09break;%0D%0A%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%09%0D%0A%09%09%09if (oclBody == null) %7B%0D%0A%09%09%09%09removeModelElement(constraint);%0D%0A%09%09%09%09return null;%0D%0A%09%09%09%7D%0D%0A%09%09%09else %7B%0D%0A%09%09%09%09((OpaqueExpression) spec).getLanguages().clear();%0D%0A%09%09%09%09((OpaqueExpression) spec).getBodies().clear();%0D%0A%09%09%09%09((OpaqueExpression) spec).getLanguages().add(%22OCL%22);%0D%0A%09%09%09%09((OpaqueExpression) spec).getBodies().add(oclBody);%0D%0A%09%09%09%7D%0D%0A%09%09%7D%0D%0A%09%09%0D%0A
 %09%09Class 

Remove all non-OCL constraint bodies