# Using Java Classes in Transformations

This section describes how to use Java classes as source or target types in transformations.

To Use a Java Class in Transformations

1. Create or open a business process project and application.

For instructions on creating a new business process project and application, see Creating a Business Process Application.

To open an existing application that contains the query:

1. From the WebLogic Workshop menu bar, choose File —> Open —> Application.
2. In the Open Workshop Application dialog box, browse for the desired application and click Open.

2. The Java class for conversion must be available in the current project. To learn more about including a Java class in your project, see Using Existing Applications.

Note: Java interfaces and abstract Java classes are supported as source or target types for transformations but cannot be executed in Test View. However, a transformation with a Java interfaces and/or abstract Java classes as source or target types can be invoked from a business process (JPD) without error.

For the example shown in this procedure, create a Java file called Book.java in a subfolder named processes in the project folder:

1. Right-click the project folder or a subfolder in the project folder.
2. From the drop-down menu, select New —> Java Class.

The New File dialog box appears.

3. In the Field Name field, enter Book.java.
4. Click Create.
5. Paste the following code segment in between the starting and ending curly brackets of the Book class:

    public String title;       // Will convert to xsd type
    public Author[] authors;    // Will convert to xsd type
    private int copiesPrinted;  // Private member with no get/set methods; will not convert to xsd type
    private int copiesSold;     // Private member with get/set methods will convert to xsd
    public int getCopiesSold(){
    	return copiesSold;
    }
    public void setCopiesSold(int in){
    	copiesSold = in;
    }
    public HashMap stores;       // Will not convert to xsd type, HashMap not supported

Errors will be reported in the Source View. You will fix those errors in the following steps.

To learn more about which fields are supported in Java classes, see Java Class Conversion.

Note: If you use the Java class variable in a business process, you may enable process variable instance monitoring for the variable by implementing a toString() method in the custom class.

6. Add the following import definition in the third line of the Book.java file (after the line: package processes;):

    import java.util.HashMap;

7. Save the Book.java file.

In Application tab, expand the your application folder. (If the Application tab is not visible in WebLogic Workshop, from the menu bar choose View —> Application.)

Expand the myproject Web project folder, where myproject represents the name of your project folder.

If required, expand the folder(s) that contain the Book.java file.

Right-click the Book.java file and in the drop-down menu select Save.

You also need to create a Java file called Author.java in the processes subfolder:

1. Right-click the project folder or a subfolder in the project folder.
2. From the drop-down menu, select New —> Java Class.

The New File dialog box appears.

3. In the Field Name field, enter Author.java.
4. Click Create.
5. Paste the following code segment in between the starting and ending curly brackets of the Author class:

    public String lastname;       // Will convert to xsd type
    public String firstname;      // Will convert to xsd type

6. Save the Author.java file.

In Application tab, expand the your application folder. (If the Application tab is not visible in WebLogic Workshop, from the menu bar choose View —> Application.)

Expand the myproject Web project folder, where myproject represents the name of your project folder.

If required, expand the folder(s) that contain the Author.java file.

Right-click the Author.java file and in the drop-down menu select Save.

3. Import the necessary XSD and MFL files for the other source or target parameters of the Transformation method into a Schemas project folder. To learn more, see Selecting Source and Target Types.

For the example in this procedure, import the Book.xsd file. For example, if you installed WebLogic Platform in the c:\bea directory, import the Book.xsd file from the C:\bea\weblogic81\workshop\help\doc\en\integration\reffiles\transform\javaClass directory.

The XML Schema in the Book.xsd file is the target type for this example transformation.

Importing schemas files triggers a build of the current Schemas project folder. Wait until the Schemas project folder is built before proceeding to the next step. (The representations of the schemas will not be available in Available Source Types and Available Target Type panes until build is complete.)

4. Create a Transformation file and Transformation method.

For instructions on creating a new Transformation file, see Creating a Transformation File and a Transformation Method.

5. Open the Transformation method which contains the query.

For instructions on creating a new method in a Transformation file, see To Add a Transformation Method to Transformation File.

To open an existing method which contains the query:

1. Right-click the arrow representing the method that contains the query.
2. From the drop-down menu, select Configure Transformation Method.

The Configure XQuery Transformation Method dialog box is displayed.

6. Select the source and target parameters for the Transformation method. For detailed instructions, see Selecting Source and Target Types.

For this example, in the Available Source Types pane, select the XML option, select Typed/Book.xsd/Book element as the target parameter, and click Add.

For this example, in the Available Target Types pane, select the Java option, enter: processes.Book in the Type field, and click Add.

Click Create Transformation.

A graphical representation of the Java class and XML Schema is displayed in the Design View.

Note: Not all the fields in the Book.java class are displayed. Only supported public members or private members with JavaBean style get and set methods are displayed. In this example, the private member: copiesSold is displayed because the associated JavaBeans set and get methods for this member are provided. However, the class member stores is not displayed because it is of type: java.util.HashMap which is not a supported type. To learn more about which fields of a Java class are supported in transformations, see Java Class Conversion.

7. In the Source pane select a node and drag it into the Target pane.

A link represented by a line between the two nodes is displayed.

Repeat this step as necessary to create additional links.

For this example, in the Source pane select the $BookDoc/Title node and drag it to the Book/title node in the Target pane.

For this example, in the Source pane select the $BookDoc/Author node and drag it to the Book/authors node in the Target pane. These nodes are both repeating nodes. A repeating node means more than one instances of this node can be specified. In the Source pane, repeating nodes are represented with a + symbol to the right of the node. A dashed line linking the two repeating nodes is displayed.

For this example, in the Source pane select the $BookDoc/Author/LastName node and drag it to the Book/authors/Author/lastname node in the Target pane. A solid line linking the two nodes is displayed.

In the Source pane select the $BookDoc/Author/FirstName node and drag it to the Book/authors/firstname node in the Target pane. A solid line linking the two nodes is displayed.

In the Source pane select the $BookDoc/CopiesSold node and drag it to the Book/copiesSold node in the Target pane. A solid line linking the two nodes is displayed.

8. Save the DTF and the XQ file. From the menu bar, choose File —> Save All.
9. Test the query:

1. Select the Test View tab.
2. In the Result Data pane, click Test.
3. The query is run with the default test data. A graphical representation of the resulting XML data is shown in the Result Data pane.
4. To view resulting data as XML, in the Result Data pane, select the XML Source View tab.