This tutorial is an extension of the edi-to-xml and javabean-populator tutorials. It illustrates how to go from (transform) an EDI message to a populated Java Object Model.

 
SVN - Download - Other Tutorials

Other Relevant Info:

 
To Build: "mvn clean install"
To Run: "mvn exec:java"

Transforming from an EDI to Javabean Object Graph

Something to note when looking at this example is that Smooks doesn't perform this EDI-to-Java transform in a 2 step "pipeline" like process. It doesn't generate and intermediate XML, which it then has to parse and process etc. Smooks takes the EDI message and, using the EdiSax parser, goes straight to a DOM representation of the EDI message.  This is then used by Smooks, through the Javabean Cartridge, to populate the Java Object Graph.

If you haven't already looked at the javabean-populator tutorial, you may be interested in looking at that before looking at this example.

So here's the source edi file that is to be transformed:

HDR*1*0*59.97*64.92*4.95*Wed Nov 15 13:45:28 EST 2006
CUS*user1*Harry^Fletcher*SD
ORD*1*1*364*The 40-Year-Old Virgin*29.98
ORD*2*1*299*Pulp Fiction*29.99

And this is the expected result of our transformation (i.e. a simple System.out of the populated Object Model - see the toString method in the Order class):

==============EDI as Java Object Graph=============
Order Header:
        Customer: Fletcher, Harry
        Date: Wed Nov 15 19:45:28 CET 2006
        Details: ID=1, Status=0, Total=64.92
Order Items:
        (0): ProductID=364, Quantity=1, Title='The 40-Year-Old Virgin', Price=29.98
        (1): ProductID=299, Quantity=1, Title='Pulp Fiction', Price=29.99

======================================

This transformation could be taken a step further, whereby the EDI populated Object Model is used as part of a Model Driven Transformation. See the model-driven-basic tutorial.

The Smooks Configuration

The Smooks configuration for this transformation contains:

  1. A configuration for generating an XML model (SAX event stream) from the input EDI, as with the edi-to-xml tutorial.
  2. A set of Javabean population configuration for populating the Object Model, as with the javabean-populator tutorial.


Here's the configuration ("smooks-config.xml"):

<?xml version="1.0"?>
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:jb="http://www.milyn.org/xsd/smooks/javabean-1.1.xsd">

    <!--
    Configure the EDI Reader to parse the message stream into a stream of SAX events.
    -->
    <reader class="org.milyn.smooks.edi.SmooksEDIReader">
        <params>
            <!-- Generate the event stream based on this "mapping model". -->
            <param name="mapping-model">/example/edi-to-xml-order-mapping.xml</param>
        </params>
    </reader>


    <!--
    Create an "example.beans.Order" bean instance when we visit the start of the <order> element.
    Assign the new bean instance to the beanId of "order".
    The "header" and "orderItemList" beans are wired in.
    -->
    <jb:bindings beanId="order" class="example.model.Order" createOnElement="order">
        <jb:wiring property="header" beanIdRef="header" />
        <jb:wiring property="orderItems" beanIdRef="orderItemList" />
    </jb:bindings>

    <!--
    Create an "example.beans.Header" bean instance when we visit the start of the <order> element.
    Note, we're creating this bean instance before we encounter the actual <header> element.
    This bean is wired into the "order" bean.
    -->
    <jb:bindings beanId="header" class="example.model.Header" createOnElement="order">
        <jb:wiring property="customer" beanIdRef="customer" />
        <jb:value property="orderId" data="header/order-id" />
        <jb:value property="orderStatus" decoder="Long" data="header/status-code" />
        <jb:value property="netAmount" decoder="BigDecimal" data="header/net-amount" />
        <jb:value property="totalAmount" decoder="BigDecimal" data="header/total-amount" />
        <jb:value property="tax" decoder="BigDecimal" data="header/tax" />
        <jb:value property="date" decoder="Date" data="header/date">
            <jb:decodeParam name="format">EEE MMM dd HH:mm:ss z yyyy</jb:decodeParam>
        </jb:value>
    </jb:bindings>

    <!--
    Create an "example.beans.Customer" bean instance when we visit the start of the
    <customer-details> element. 
	This bean is wired into the "header" bean.
    -->
    <jb:bindings beanId="customer" class="example.model.Customer" createOnElement="customer-details">
        <!-- Customer bindings... -->
        <jb:value property="userName" data="customer-details/username" />
        <jb:value property="firstName" data="customer-details/name/firstname" />
        <jb:value property="lastName" data="customer-details/name/lastname" />
        <jb:value property="state" data="customer-details/state" />
    </jb:bindings>
	
	<!-- 
	Create an ArrayList when we visit the start of the <order> element.
	The "orderItem" beans are wired into this list and this list is wired into the "order" bean.
	 -->
    <jb:bindings beanId="orderItemList" class="java.util.ArrayList" createOnElement="order">
        <jb:wiring beanIdRef="orderItem" />
    </jb:bindings>

    <!--
    Create an "example.beans.OrderItem" bean instance when we visit the start of the <order-item> element.
    This bean is wired into the "orderItemList" ArrayList.
    -->
    <jb:bindings beanId="orderItem" class="example.model.OrderItem" createOnElement="order-item">
        <!-- OrderItem bindings... -->
        <jb:value property="quantity" decoder="Integer" data="order-item/quantity" />
        <jb:value property="productId" decoder="String" data="order-item/product-id" />
        <jb:value property="price" decoder="BigDecimal" data="order-item/price" />
        <jb:value property="title" data="order-item/title" />
    </jb:bindings>

</smooks-resource-list>

Here's the edi mapping ("/src/main/java/example/edi-to-xml-order-mapping.xml"). See the edi-to-xml tutorial for more details on this mapping:

<?xml version="1.0" encoding="UTF-8"?>
<medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.0.xsd">

	<medi:description name="DVD Order" version="1.0" />

	<medi:delimiters segment="&#10;" field="*" component="^" sub-component="~" />

	<medi:segments xmltag="Order">

		<medi:segment segcode="HDR" xmltag="header">
			<medi:field xmltag="order-id" />
			<medi:field xmltag="status-code" />
			<medi:field xmltag="net-amount" />
			<medi:field xmltag="total-amount" />
			<medi:field xmltag="tax" />
			<medi:field xmltag="date" />
		</medi:segment>

		<medi:segment segcode="CUS" xmltag="customer-details">
			<medi:field xmltag="username" />
			<medi:field xmltag="name">
				<medi:component xmltag="firstname" />
				<medi:component xmltag="lastname" />
			</medi:field>
			<medi:field xmltag="state" />
		</medi:segment>

		<medi:segment segcode="ORD" xmltag="order-item" maxOccurs="-1">
			<medi:field xmltag="position" />
			<medi:field xmltag="quantity" />
			<medi:field xmltag="product-id" />
			<medi:field xmltag="title" />
			<medi:field xmltag="price" />
		</medi:segment>

	</medi:segments>

</medi:edimap>

Executing The Transformation

Again, it's exactly the same as with the java-basic tutorial:

// Instantiate Smooks with the config...
Smooks smooks = new Smooks("smooks-config.xml");
JavaResult result = new JavaResult();

// Filter the input message to the JavaResult...
smooks.filter(new StreamSource(messageIn), result);

return (Order) result.getBean("order");

Of course, you'd typically cache the Smooks instance.

See the example/Main.java in the example source.