Example 17: Custom Validator

Overview

This example demonstrates how to create a custom validator using the Extension Development Kit.

You can find the code for this example in our GitHub repository.

Implementation

The Plus version of the Validator extensions -Promotion Validate Plus and Change Validate Plus- allows users to add their own validators as dynamic Windchill extensions. Unlike static code, which requires a system restart whenever changes are made, Windchill extensions can be added and updated without disrupting the system. This makes the dynamic approach far more flexible and cloud-compliant. Additionally, Windchill extensions are fully manageable through the Extension Manager, allowing for version control and easier deployment. In contrast, static code requires direct access to the Windchill codebase, making it less efficient and harder to manage in modern cloud environments.

The methods for both Extensions must be static and return an Object array with two elements:

The only difference between the signatures of the custom Promotion Validate Plus and Change Validate Plus Validators are the arguments each of them take. They are detailed below.

Promotion Validate Plus

The arguments of the custom Validator methods for this extension are as follows:

Note: We use POJOs (Plain Old Java Objects) for both the parameters and the return value. This is intentional, as it reduces the coupling required when using a shared data model.

@WexComponent(uid = "wex-promote-validators", description = "Wex Promotion Validators")
public class PromoteMethodValidators {

   @WexMethod(name = "validate-OK", description = "Will be ok")
   public static Object[] validateOK(Promotable item, List items, String targetState) {
       Object[] result = new Object[2];
       result[0] = "This is an OK message";
       result[1] = 0;
       return result;
   }

   @WexMethod(name = "validate-error", description = "Will always throw an error")
   public static Object[] validateError(Promotable item, List items, String targetState) {
       Object[] result = new Object[2];
       result[0] = "This is an error message";
       result[1] = 1;
       return result;
   }

   @WexMethod(name = "validate-warn", description = "Will always throw a warning")
   public static Object[] validateWarn(Promotable item, List items, String targetState) {
       Object[] result = new Object[2];
       result[0] = "This is a warning message";
       result[1] = 2;
       return result;
   }
}

Change Validate Plus

The arguments of the custom Validator methods for this extension are as follows:

@WexComponent(uid = "wex-change-validators", description = "Wex Change Validators")
public class ChangeMethodValidators {

   @WexMethod(name = "validate-test-error", description = "Will always throw an error")
    public static Object[] validateTestError(Persistable persistable, List<Persistable> items) {
        Object[] result = new Object[2];
        result[0] = "This is an error message";
        result[1] = 1;
        return result;
    }

    @WexMethod(name = "validate-test-OK", description = "Will always return an OK")
    public static Object[] validateTestOK(Persistable persistable, List<Persistable> items) {
        Object[] result = new Object[2];
        result[0] = "This is an OK message";
        result[1] = 0;
        return result;
    }

    @WexMethod(name = "validate-test-WARN", description = "Will always throw a warning")
    public static Object[] validateTestWarn(Persistable persistable, List<Persistable> items) {
        Object[] result = new Object[2];
        result[0] = "This is a WARN message";
        result[1] = 2;
        return result;
    }

    @WexMethod(name = "validate-test-IGN", description = "Will always throw an ignore")
    public static Object[] validateTestIgnore(Persistable persistable, List<Persistable> items) {
        Object[] result = new Object[2];
        result[0] = "This is a IGNORE message";
        result[1] = 3;
        return result;
    }
}

Configuration

This code can be used in Promotion Validate Plus and Change Validate Plus just by adding the method into the Extension's Configuration as shown below:

Promotion Validator Example

Syntax

The syntax for using the validator methods is as follows:

[extension id]:[uid of class with the promotion methods].[method id]

An example of this syntax using the previous example would be:

com.wincomplm.wex-promote-adv:wex-promote-validators.validate-OK