JBoss.orgCommunity Documentation

Chapter 2. Tasks

2.1. New Smooks Configuration File Creation
2.2. Input Task Configuring
2.3. "Java Mapping" or "Apply Template"?
2.4. Java Mapping Task
2.5. Apply Template Task
2.6. Smooks Configuration testing using Smooks Run Configuration

This chapter describes the main tasks that can be performed with Smooks tools. In this chapter we use the example that can be downloaded from here.

Select the project where you want to create the new Smooks Configuration File and right-click on it. Select NewOtherSmooksSmooks Configuration File. Then click the Next button.


Select the src folder to be the files container, and type the name smooks-config.xml. Click the Next button.


Configuring an Input task is mandatory step when creating a smooks project. You can configure it on the Process page of the editor: look for the "Input Task" in the Process Map at the top of the page.


Select it and you will see all the properties to set for the Input reader of your Smooks configuration. "Input type" corresponds to the type of data that you will be working with. For example, to work with incoming CSV (Comma-separated Values) data, you would specify "CSV" in the drop-down list. Each reader type has slightly different configuration details that must be set in the "Input configuration" area. For instance, the CSV reader requires you to specify details such as the encoding, quote character, separator character, and the list of incoming fields. The EDI reader requires the encoding and the path to the Mapping Model describing the incoming data. In the Input data section, you can specify some sample data that conforms to your reader configuration.

Once you've specified your reader configuration and sample data, you can see the input model rendered in a tree form in the Input model section. In the picture below you can see the correct configuration of an XML input task where input-message.xml is set as an input data file.


Though there are many options in Smooks regarding what you can do with input data, such as transformation, routing, and persistence, this version of the Smooks Configuration Editor focuses only on mapping to java and applying templates to create different output formats. If you have a set of Java classes you want to use the incoming data for, you can use the "Java Mapping" task to specify those classes and use drag and drop to map between the input model generated by the reader and elements in the output model. Or if you simply want to transform your output to one or more formats, you can use the Apply Template task to map it to a CSV file, XML or XSD file (with other formats being provided in the future).

If you decide to do Java Mapping, you need to make sure that your Input reader has been set up and you have some sample data specified. Then you should select Input Task in the Process tab and click the plus (+) sign to the right of the icon. Select Java Mapping from the popup menu and it will appear to the right, connected to Input Task. Then select Java Mapping task.


Another method of adding a Java Mapping element to the canvas in the Processing Tasks section is to right click on the Input Task element and select Java Mapping in the popup menu.


Right-click on the canvas in an empty space and select AddJava Class.


This will display the Java Bean Creation wizard. Specify a unique identifier for the new class and the class path. If the Java class is specified, you'll see a list of the properties in the box below. Click the Finish button when you're done. Now, with the input and output models on the canvas, you can click and drag from the various input elements to corresponding output elements. Make sure to connect collection elements to corresponding collection elements. Finally your mapping should look something like the one on the picture below.


The movie "XML to Java" has more information on this process.

If you are interested in transforming EDI to Java you can find more information here.

The "Apply Template" task works very similarly to the section Section 2.4, “Java Mapping Task”, where you define a mapping between an input model and an output model. Select the Java Mapping task you want to use as the input model in the Process Map pane and click the plus (+) sign to the right of the icon.


This will display the Message Type Selection wizard. In our example we will transfer our data to csv output format, so you should select CSV and click the Next button.


On the next wizard page put the following string into the Fields, select Output Fields Names and click the Finish button.


After these steps the "Apply Template" task will appear to the right, connected to the task you created it from. To continue the configuration processyou should click on it and find the CSV Template item with entered fields on the canvas. Now you can click and drag from various input elements to corresponding output elements in the template. Make sure to connect collection elements to the corresponding csv-record. In our example output only the elements of orderItems collection to the CSV format (see the picture below):


This option is used to view the results of Smooks transforming procedure. To do the testing you should select your Smooks Configuration file you want to transfer in the Project Navigator or open it in the Smooks Configuration Editor and then select "Run As..." from the Run toolbar button or select the Run->Smooks Run Configuration option in the top menu bar. The Smooks Configuration file will then be run automatically.


Any errors or warnings will appear in a pop up dialog. The output of the test will appear in the Console view. In our case the following streaming output will appear:

[Stream Templating Result ...]
    |--
    |"productId","quantity","price"
    |"111","2","8.9"
    |"222","7","5.2"
    |--

[Java Mapping Results...]
    |--
    |> order (beanId = "order")
    |    > header (beanId = "header")
    |        > date = "2006-11-15 20:45:28.0 EET"
    |        > customerNumber = 123123L
    |        > customerName = "Joe"
    |    > orderItems (beanId = "orderItems")
    |        > example.model.OrderItem (beanId = "orderItem")
    |            > productId = 111L
    |            > quantity = 2I
    |            > price = 8.9D
    |        > example.model.OrderItem (beanId = "orderItem")
    |            > productId = 222L
    |            > quantity = 7I
    |            > price = 5.2D
    |--

If the test runs but doesn't generate any streaming output the Console output will be the following:

 Nothing to Display:
            - No Java Mappings.
            - No Templates Applied.