Defining a Java conditional expression for a transform

You can use a Java™ method to set the conditional expression that determines whether a transform is applied in a message map.

About this task

To define the conditional expression, you can define an XPath expression or a call to a static method on an imported Java class. You can also create a compound (nested) expression that includes XPath, Java and extension functions such as iib:getUserDefinedProperty("propertyname").  

Note: When you define a Java conditional expression, the following rules apply:
  • You can use Java methods that only use standard Java types.
  • You cannot use Java methods that include MbElement data type arguments.
  • You can use Java methods that use the DOM API. For more information on the supported types, see Custom Java.
Note: If you define a conditional expression for a transform, and then realize that the condition is identical for other transforms in the map, change the transform to an If transform. The conditional expression remains unchanged, and the transform is moved within the nested level of the If transform. Then, place all of the transforms that depend on the same condition in the If nested mapping.

Procedure

Complete the following steps to set a conditional expression in a transform:

  1. Create a message map.
    When you use Java code in a map, you can define the map in a static library project, a shared library project, an application project, or an integration project. You can define the Java code in one or more Java projects. Your map project must reference these Java projects. For more information, see Adding a project to an application, integration service, or library
    Note: When the map uses Java code available in a shared library, you must create the map in the shared library project where the Java code is available.

    For more information, see Creating message maps.

  2. Create a Java project.

    For more information, see Creating a Java project.

  3. Create Java object classes.

    The Java class that you provide to the map must have static methods that return Boolean values.

    For example, the following Java method might be used to define a conditional expression on a transform:

    
    public static boolean lessThanHund(BigDecimal val) {
    		if ( val.intValueExact() < 100 )
    			return true;
    		return false;
    	}	
    	
  4. Ensure that the Java project the class is in is referenced by the project that contains the map.

    Before you create a reference to a Java project, ensure that the Java class is available in a Java project in your workspace.

    1. In the Application development page, right-click the project where the map is defined.
    2. Select Managed included projects.
    3. Select the Java project, and then click OK.
  5. Make the Java class visible anywhere in the map. Unless you have already used a method from the Java class in a Custom Java transform elsewhere in the map, you must manually create an import for the class. To manually import the Java class, you must configure the Java imports tab in the Properties page of the map:
    1. Select Add.
    2. Click Browse.
    3. In the Select entries field, enter the name of the Java package that contains the Java class.
      Note: You must start typing the name of your Java package before you can see any classes to choose from in the Select entries field.
    4. Select a Java class.
    5. Click OK.
    6. Enter a unique value in the Prefix field.
    Note: If the Java project that contains the Java class does not build in Eclipse, then the Java class is not visible anywhere in the map. You must resolve all the Java errors before you can configure the Java imports tab in the Properties page of the map. You can see the errors in the Problems tab.

    When you add a Java call in the General tab of the Properties page of a Custom Java transform in your map, an import is automatically added in the map to refer to the package qualified Java class, and a prefix based on the class name is added. All the public static methods in this Java class are then available in content assist when building expressions in other transforms. If you want to use this class, you can skip this step.

  6. For non-repeating elements, select the Condition property in the Properties tab, and enter a Java method.
    Note: Always use content assist to select the variable name of the input elements that you use to define the expressions. If you do not use content assist, you might be using an incorrect element name and your map fails at run time.
    You can identify the Java method in content assist by using the prefix. For example, in the following figure the prefix is Conditionals and the method is lessThanHund():
    Conditionals:lessThanHund( $element11)

    Figure that shows a Java expression on a Move transform.

    Note: If the input element being used to provide a value for a Java method is not of the correct type, you can use a type cast function, for example xs:int( $var ), to set the required type. For more information, see Cast type (xs:type).
  7. For repeating elements, select the Filter Inputs property in the Properties tab, and enter a Java method that is applied to each instance of the repeating element.

Results

If the Java method returns true, the transform is applied to the input element.

Example

You can call the following Java method when you define a conditional expression in a map:

 
public static boolean evalNodeCondition(Node inputNode, String hasChildNamePrefixed) 
    { boolean result = false; 
      if ( inputNode != null ) { 
            Node fcn = inputNode.getFirstChild(); 
            if ( fcn != null ) { result = fcn.getLocalName().startsWith(hasChildNamePrefixed); 
            } 
     } 
return result;

What to do next

Deploy and test the message map. For more information, see Troubleshooting a message map.