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
libraryNote: 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 a message map.
- Create one or more Java projects.
For more information, see Creating a
Java project.
- Create Java object classes.
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 takes 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 had three
input elements, of types a xs:string,
xs:decimal, and xs:boolean and the
output element is a xs: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 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).
- 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.
- In the Application development page,
right-click the project where the map is defined.
- Select Managed included projects.
- Select the Java project, and
then click OK.
- Add a Custom Java transform to the
map.
- 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:
- Configure the Java class. 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.
- 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 code
for which you have first manually imported the Java class into the map. To manually import a Java
class in a map, you configure the Java imports tab in
the Properties page of the map.
What to do next
Deploy and test the message map. For more information, see Troubleshooting a message map.