Chapter 7. Advanced topics for decision authoring 207
Consider this case. In a COBOL program, instead of using only T to indicate true, the program
also accepts t, Y, and y as true values. But the built-in boolean converter can accept only one
character as the true value. So, in this case, we write a custom converter.
A
custom converter is a normal Java class. The class must be annotated with the
TypeConverter annotation. In the Converter dialog, users can select those classes with only
the TypeConverter annotation, along with the built-in converters.
Then, we need implement an init method. This method is called by the run time to initialize the
converter with user-defined properties. There is also an optional TypeConverterProperties
annotation with which we can define the list of expected property keys. In this example, we
define two keys:
true-values A comma-separated list of characters that indicate true
false-value The default value that indicates false
In the init method, we retrieve the values of these properties. Then, we save the list of true
values to the trueValues field and set the falseValue field to the false-value character
(Example 7-1).
Example 7-1 Custom converter code (Part 1 of 2)
@TypeConverter
@TypeConverterProperties({ "true-values", "false-value" })
public class MyBooleanConverter {
private List<String> trueValues;
private String falseValue;
public void init(Map<String, String> props) {
String trueStr = props.get("true-values");
trueValues = Arrays.asList(trueStr.split(","));
falseValue = props.get("false-value");
}
Now, we can define two conversion methods:
public <TargetType> convertToTarget(<SourceType> value)
public <SourceType> convertToSource(<TargetType> value) {
The <SourceType> is the default Java type that directly mapped from COBOL data; the
<TargetType> is the type that we want to use in the generated XOM. The convertToTarget is
called during unmarshalling, and the convertToSource is called during marshalling.
In Example 7-2, if the value that comes from COBOL is within the trueValues, the result is
true. Any other values convert to false. During marshalling, if the Java value is true, the first of
the possible true values is used, which is T in this case. If the Boolean value is false, the false
value F is used.
Example 7-2 Custom converter code (Part 2 of 2)
public synchronized boolean convertToTarget(String value) {
return trueValues.contains(value);
}
public synchronized String convertToSource(boolean value) {
return value ? trueValues.get(0) : falseValue;
}
}