Mapping an element by using a Custom Java transform
In the Graphical Data Mapping editor, you can use a method in a Java™ class to set the value of an output element. You can use the Custom Java transform to invoke a Java method, optionally passing one or more values to its parameters.
Procedure
Complete the following steps to use Java in a Custom Java transform:
- 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.
- Add a Custom Java transform
to the map, and connect an input and output:
- The input and output elements must be child elements; you cannot use a Custom Java transform to map a root element of a message.
- Ensure that the input elements provide simple data values, and the output element has a data value set by the invoked Java method.
- If you want to map complex or repeating elements in a Custom Java transform, see Processing complex or repeating elements in a Custom Java transform.
- If you have already defined a Java class,
select the class in the General tab of the Properties page
of the transform or, if you have not defined a Java class, complete the following steps to
define the required Java class:
- In the General tab of the Properties page of the transform, click New.
- Select one of the following options, and complete the
associated steps:
- Create a new Java project
to store the class:
- Select Create the class in a new Java project to be created and click Next.
- Complete the fields in the dialog (for more information, see Creating a Java project) and click Next.
- Use an existing Java project
to store the class:
- Select Create the class in one of the existing Java projects.
- Select the Java project from the list and click Next.
The New Java class wizard is displayed. - Create a new Java project
to store the class:
- Complete the New Java class wizard.
At a minimum, complete the following fields:
- In the Source field, select the src folder of your Java project.
- In the Package field, enter the name of a package to contain the new class.
- In the Name field, enter a name for the class.
- Click Finish. If prompted, click Yes to
create a reference from the application that contains your map to
your Java project.
The Java class is created with a sample method.
- In the General tab of the Properties page
of the transform, click Edit to complete your Java class.
The Java class that you provide to the map must have static methods that return the appropriate type for the value of the output element, and take parameters of the appropriate type for the wired inputs.
You can use the MbElement class in a Java method to pass a complex type, a wildcard xsd:any, or a wildcard xsd:anySimpleType. For more information, see Custom Java.
For example, the following Java method might be used in a Custom Java transform that has three input elements, of types a
xs:string
,xs:decimal
, andxs:boolean
and the output element is axs:decimal
:public static BigDecimal calSomething(String memType, BigDecimal stdCost, boolean flag) { BigDecimal actualCost = stdCost; if (flag & memType.startsWith("gold")) { BigDecimal discRate = new BigDecimal(0.9); actualCost = actualCost.multiply(discRate); } return actualCost; }
Note: If the input element that is 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).
- Configure the method that defines the transformation logic
that is applied by the Custom Java transform. In the
General tab of the Properties page
of the transform, complete the following steps:
- Click Browse and complete the
following steps:
- In the Select Type window, enter the name
of the Java package in the Select
entries field. The package must contain 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.
- Select a Java class and click OK.
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 class and method in the General tab of the Properties page. You can see the errors in the Problems tab. - In the Select Type window, enter the name
of the Java package in the Select
entries field. The package must contain the Java class.
- Configure one Java method.
You access the methods that are available through the drop-down.
If the method has parameters, they are added automatically in the Parameters section of the General tab.
- Set the value of each parameter. Complete the following
steps to set the value of a parameter:
- Select a parameter.
- Click the Value column of a parameter.
- Select an element, or select the option Edit custom XPath expression parameter. If you select the edit option, you can define an XPath expression or a call to a static method on an imported Java class that is visible to the map. You can also create a complex expression that includes XPath, Java, and extension functions such as iib:getUserDefinedProperty("propertyname").
When you define a Java method and a Java class in the General tab of the Properties page of a Custom Java transform, an import is automatically added in the map to refer to the package qualified Java class. 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.
Alternatively, you can use Java class methods that use standard Java types in any expression within your map, for example as part of the condition of an
if
transform. Before you can use Java methods in expressions, import the Java class into the map. The method is then available in content assist (Ctrl-space). To import a Java class into a map, configure the Java imports tab in the Properties page of the map. - Click Browse and complete the
following steps:
What to do next
Deploy and test the message map. For more information, see Troubleshooting a message map.