Contents


Validating business objects in Websphere Process Server

Comments

IBM customers require that business object (BO) instances are validated in runtime from the defined XSD (XML schema definition) constraints. This validation is desired by specific business systems to ensure data consistency and accuracy. Since version 6.1, WebSphere Process Server has provided BO instance validation support in runtime. This function confirms that the data passed into an operation matches the XSD types and constraints of the operation's inputs. There are two ways to validate BO instances:

  • Utilize the data validation qualifier
  • Invoke the BOInstanceValidator APIs

This article focuses on business objects validation in WebSphere Process Server. It demonstrates how to create business objects with constraints and how to validate business object instances in the runtime from the defined constraints.

Defining business objects with constraints

Business objects are containers for application data, such as a customer or an invoice. The underlying structure of a business object is an XML schema definition (XSD). You can define a business object by creating it in WebSphere Integration Developer directly or importing an existing XSD file. It allows you to add constraints to its elements for further validation in runtime.

To create business objects with constraints, a sample module is provided for you to download.

  1. Select File > Import, follow the Import Wizard, and choose Project Interchange (Figure 1) to import the sample module into your workspace to follow along.
    Figure 1. Import Wizard
    Import Wizard
    Import Wizard
  2. In the sample module, you can find three business objects: User, Order, and Item. They are shown in Figure 2.
    Figure 2. Business Objects in the sample
    Business Objects in the sample
    Business Objects in the sample
  3. Add constraints to elements of business objects User, Order, and Item. You can add constraints to the XSD built-in primitive types for the specific validation checks. See Quality of service qualifier reference for the link to the tables, which show the supporting pairs of data types and constraints. Here we will illustrate this part with the String type and its supporting constraints, such as Length, minLength, maxLength, Pattern, Enumeration, and so on.
  4. Limit the length of the elements in the String type. To define the length constraint on the ID element of User:
    1. Open User in the business object editor and select the ID field by clicking it once.
    2. In the Properties view, set the Minimum length and Maximum length to 8 and 16, respectively, as shown in Figure 3.
    3. Add the same constraint as the above to restrict the length for the ID element of Order and Item as well.
      Figure 3. Defining the length constraint
      Defining the length constraint
      Defining the length constraint
  5. Add the Enumeration constraint to elements in the String type. Assume that the order status only permits certain enumeration values. To define this constraint:
    1. Open Order in the business object editor and select the status field.
    2. In the Properties view, select the checkbox Only permit certain values.
    3. Select Enumerations, then add the enumeration values as shown in Figure 4.
    Figure 4. Defining the Enumeration constraint
    Defining the Enumeration constraint
    Defining the Enumeration constraint
  6. Add the Pattern constraint to elements in the String type:
    1. Assume that the user password complies with a rule that "compose of alphanumeric characters with the minimum length of 6 and the maximum length of 16".
    2. You can use a regular expression to describe this rule. To facilitate the writing and testing of regular expressions, a RegularExpression Wizard is provided in the business object editor.
    3. To define this constraint, open User in the business object editor and select the password field.
    4. In the Properties view, select Only permit certain values.
    5. Click Patterns and the Regular Expression Wizard window opens. To create the regular expression for the password rule, choose Alphanumeric character ( \w ) for Token contents and input type 6 to 16 in Range for Occurrence as shown in Figure 5, then click Add.
      Figure 5. Compose regular expression in Regular Expression Wizard
      Compose regular expression in Regular Expression Wizard
      Compose regular expression in Regular Expression Wizard
    6. Click Next. You can enter sample texts to test the regular expression (Figure 6). The match result is displayed. Then click Finish.
      Figure 6. Test regular expression in Regular Expression Wizard
      Test regular expression in Regular Expression Wizard
      Test regular expression in Regular Expression Wizard
    7. A regular expression is added in Patterns (Figure 7).
      Figure 7. Defining the Pattern constraint
      Defining the Pattern constraint
      Defining the Pattern constraint

Validate business object instances

There are two ways to validate the business object instances in runtime as defined by the above XSD constraints:

  • Using the data validation qualifier
  • Using the BOinstanceValidator APIs

Using the data validation qualifier

You can use a data validation qualifier to validate instance data from a business object. The runtime validator that underlies this qualifier checks if the XSD types being passed by a business object are those that were expected by the client. When you add the qualifier to a WSDL interface, the data objects passed in are validated.

  1. Add the data validation qualifier on WSDL interfaces. You can set the qualifier at three levels for:
    • All the interfaces of the component
    • An individual interface
    • An individual operation of an interface. Here you apply it at the last level.
  2. Open the assembly diagram of the sample module, select the SaveUserInfo component.
  3. In the Properties view, select Interfaces > SaveUser (interface) > saveUserInfo (operation).
  4. Click the Qualifiers label and add the Data validation qualifier as shown in Figure 8.
    Figure 8. Adding the data validation qualifier
    Adding the data                     validation qualifier
    Adding the data validation qualifier
  5. Define the qualifier settings: Log error and continue (default) and Throw exception. The qualifier settings define the action if violations occur in the BO instances validation. The data validation qualifier has two settings:
    • Log error and continue (default): If a violation occurs in validation, the related error is logged and the requested operation continues to be performed. There is no guarantee that the service can handle the invalid data, so a runtime error could result.
    • Throw exception: If a violation occurs in validation, an exception is thrown and the requested operation is not performed.
      Figure 9. Defining qualifier settings
      Defining qualifier settings
      Defining qualifier settings
  6. Test the qualifier:
    1. Start the server and deploy the sample application on it (Figure 10).
      Figure 10. Deploying application
      Deploying application
      Deploying application
    2. Open the assembly diagram of the sample module, right-click the SaveUserInfo component, and select Test Component.
    3. Enter the data for User as the input as shown in Figure 11, and click the green arrow.
      Figure 11. Input data 1
      Input data 1
      Input data 1

      Because the pattern constraint of the password element in User is violated and Throw exception is selected as the action for errors, an exception is thrown. Checking the server log, you find that the requested operation saveUserInfo() is not performed (Figure 12).
      Figure 12. Validation result 1
      Validation result 1
      Validation result 1
    4. Enter the data for User, including its children BOs Order and Item (Figure 13), and click the green arrow.
      Figure 13. Input data 2
      Input data 2
      Input data 2

The data validation qualifier does a deep validation for a BO instance. If a BO has children BOs, the whole BO tree will be validated. So after this instance of User is passed in, the internal BO instances of Order and Item are also checked. Then an exception is thrown indicating that the length constraint of user/orders[0]/items[0]/ID is violated (Figure 14).

Figure 14. Validation result 2
Validation result 2
Validation result 2

Using the BOInstanceValidator APIs

The data validation qualifier is supported by the BO Instance Validator service in WebSphere Process Server. This service also provides BOInstanceValidator APIs to validate a BO instance.

  1. Get the client to locate BO Instance Validator service:
    BOInstanceValidator boValidator=(BOInstanceValidator)ServiceManager.INSTANCE.
      locateService("com/ibm/websphere/bo/BOInstanceValidator");
  2. Get the client to do a deep validation for a BO instance. The following code validates a User instance together with the internal instances of Order and Item.
    ArrayList diagnostics = new ArrayList();
    boolean result = boValidator.validate(user, diagnostics);
    System.out.println("The result of validate() is: " + result);
    
    //Print the diagnostics info
    if (!result) {
          Iterator errorList = diagnostics.iterator();
          while(errorList.hasNext()){
                 DataObject error = (DataObject)errorList.next();
                 System.out.println("Error property: " + error.get("property"));
                 System.out.println("Error message: " + error.get("message"));
    }
    }
  3. Get the client to do a shallow validation for a BO instance. Only the first level of the BO tree is validated. The following code validates a User instance, but the internal instances of Order and Item are not validated.
    boolean result = boValidator.validateShallow(user, diagnostics);
  4. Get the client to do a deep or shallow validation for a BO instance's property. In the following code, when a User instance is passed in, the Order instances specified by the property path (the second parameter) are validated. The validateProperty method does a deep validation for them, while the validatePropertyShallow method only checks the first level and ignores the Item instances inside.
    boolean result1= boValidator.validateProperty(user, "orders", diagnostics);
    boolean result2= boValidator.validatePropertyShallow(user, "orders", diagnostics);

Conclusion

This article described the business object instance validation service in WebSphere Process Server. You can invoke this service for either implicit interface qualifier-based validation, or for explicit programmatic validation. You learned how business objects with constraints are defined and how business object instances are validated.


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=416690
ArticleTitle=Validating business objects in Websphere Process Server
publish-date=07292009