capint
10/20/2015 - 9:54 AM

EMF >> Validation >> Batch validator >> Implementation

EMF >> Validation >> Batch validator >> Implementation

<extension
      point="org.eclipse.emf.validation.constraintProviders">
   <category
         name="Library Constraints"
         id="org.eclipse.emf.validation.pde.example.general.ui.library"/>
   <constraintProvider cache="true">
      <package namespaceUri="http:///org/eclipse/emf/metamodel/example/pde/library.ecore/1.0.0"/>
      <constraints categories="org.eclipse.emf.validation.pde.example.general.ui.library">
         <constraint
               lang="Java"
               class="org.eclipse.emf.validation.examples.constraints.NonEmptyNamesConstraint"
               severity="ERROR"
               mode="Batch"
               name="Non-Empty Names"
               id="org.eclipse.emf.validation.pde.example.general.ui.NameNotEmpty"
               statusCode="1">
            <description>
               All items in a library model should have some unique identifier or name.
            </description>
            <message>
               A {0} has been found to have no unique identifier (name or title).
            </message>
            <target class="Library"/>
            <target class="Writer"/>
            <target class="Book"/>
            </target>
         </constraint>
      </constraints>
   </constraintProvider>
</extension>
<extension
      point="org.eclipse.emf.validation.constraintBindings">
   <clientContext
         default="false"
         id="org.eclipse.emf.validation.pde.example.general.ui.libraryContext">
      <selector class="org.eclipse.emf.validation.examples.constraints.ValidationDelegateClientSelector"/>
   </clientContext>
   <binding
         context="org.eclipse.emf.validation.pde.example.general.ui.libraryContext"
         category="org.eclipse.emf.validation.pde.example.general.ui.library"/>
</extension>

public class ValidationDelegateClientSelector
	implements IClientSelector {
}
IBatchValidator validator = (IBatchValidator)ModelValidationService.getInstance()
	.newValidator(EvaluationMode.BATCH);
validator.setIncludeLiveConstraints(true);

IStatus status = validator.validate(selectedEObjects);
public class NonEmptyNamesConstraint
	extends AbstractModelConstraint {

	public IStatus validate(IValidationContext ctx) {
		EObject eObj = ctx.getTarget();
		EMFEventType eType = ctx.getEventType();
		
		// In the case of batch mode.
		if (eType == EMFEventType.NULL) {
			String name = null;
			if (eObj instanceof Writer) {
				name = ((Writer)eObj).getName(); 
			} else if (eObj instanceof Library) {
				name = ((Library)eObj).getName();
			} else if (eObj instanceof Book) {
				name = ((Book)eObj).getTitle();
			}
			
			if (name == null || name.length() == 0) {
				return ctx.createFailureStatus(new Object[] {eObj.eClass().getName()});
			}
		}
		
		return ctx.createSuccessStatus();
	}

}

Source: http://www-01.ibm.com/support/knowledgecenter/SS8PJ7_7.0.0/org.eclipse.emf.validation.doc/tutorials/validationTutorial.html?lang=en