Validating input on a transformation
All ZIETrans rich client transformations implement the com.ibm.hats.rcp.transform.IPrePostSubmitHandler
interface which provides the ability to control what happens immediately
before and after a request is made to the ZIETrans runtime when a specific
transformation is displayed. The primary function of this interface
is to give you the opportunity to validate values supplied on a transformation
by a user before a request is made to the ZIETrans runtime. If a problem
is found with one or more of the user's responses, you have the ability
to cancel the request, alter the parameters, alter the command, and
display a message to the user.
Note: Validation should be
performed in the handlePreSubmit method of your transformation
class, since this method is called just before a request is made to
the ZIETrans runtime. Once the request has been made, you cannot alter
or cancel it.
The following sample shows how to perform validation on input provided by the user. If an invalid value is specified, the submission is cancelled and a message is displayed to the user.
If the handlePreSubmit method has not been overridden
in your transformation class, following these steps:
- Right-click on the source of the transformation (in the Java™ Editor) and select Source > Override/Implement Methods
- Expand the RcpTransformation node and place a check next to handlePreSubmit(CommandEvent) and click OK.
- A stub method is inserted into the transformation source (based
on your Java code style preferences,
your sample code may not look like the following):
public void handlePreSubmit(CommandEvent event) { // TODO Auto-generated method stub super.handlePreSubmit(event); }
The following code sample shows how to perform a range check on
a hypothetical part quantity field. If the quantity supplied by the
user is not greater than 500, a message is displayed and the submission
is cancelled. It is assumed that this field resides at row 4, column
20 of the host screen that is being transformed by this transformation.
Replace your existing handlePreSubmit(CommandEvent) method
with the following:
public void handlePreSubmit(CommandEvent event) {
// Avoid validation if the user is attempting to disconnect the session
if (event.getCommand().equals(RuntimeConstants.CMD_DISCONNECT)) {
return;
}
// Retrieve the value of the "quantity" field using the
// IHostScreenDataAccessService interface
// (which is accessed by calling the getHostScreenDataAccessService()
// method on the session service)
// The getFieldValue() method takes three parameters
//
// Returns the value of host screen field at the specified position.
//
// @param startPos The start position of the field.
// @param offset The offset position of the field.
// @param length The length of the field.
//
// @return The value of the host screen field.
//
// The Component.convertRowColToPos() method converts a row, column value
// into a linear screen position.
// "5" indicates the length of the field.
String quantityString = getSessionService().
getHostScreenDataAccessService().
getFieldValue(Component.convertRowColToPos(4, 20,
getHostScreen().getSizeCols()),0, 5);
// Convert the numeric string into an int
int quantity = 0;
try {
quantity = Integer.parseInt(quantityString);
} catch (Exception ex) {
}
// Check to ensure the quantity entered is greater than 500
if (quantity < 500) {
// Display a message to the user indicating what the problem is
MessageDialog.openError(getShell(),
getSessionService().getApplication().getName(),
"You must order at least 500 units.");
// Cancel the event (this prevents the request from being made)
event.setCanceled(true);
}
}