Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

The primary goals of this example are to introduce you to the following:

  1. Transforming from an edi file to Javabean object graph.
  2. The Smooks configuration file.
  3. Executing the Smooks Transformation.

 
SVN - Download - Other Tutorials

Other Relevant Info:

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


work in progress...

Transforming from an edi file to Javabean object graph

This example is an extension of the edi-to-xmlexample. It extends that example to include Javabean Object Graph population from the same EDI message.

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-populatorexample, 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:

==============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

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

The Smooks Configuration

In order to apply this transformer to a message fragment, a Smooks Configurationneeds to be created. This configuration will target the transformer at a particular message fragment. For more information on configuring XSLT resources, see XslContentDeliveryUnitCreator.

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

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

    <!--
    Configure the EDI Parser to parse the message stream into a stream of SAX events.
    -->
    <resource-config selector="org.xml.sax.driver">
	    <resource>org.milyn.smooks.edi.SmooksEDIParser</resource>
		<param name="mapping-model">/example/edi-to-xml-order-mapping.xml</param>
	</resource-config>


    <!--
    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".
    -->
    <resource-config selector="order">
        <resource>org.milyn.javabean.BeanPopulator</resource>
        <param name="beanId">order</param>
        <param name="beanClass">example.model.Order</param>
    </resource-config>

    <!--
    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.
    Set the new bean instance on the "order" bean.
    -->
    <resource-config selector="order">
        <resource>org.milyn.javabean.BeanPopulator</resource>
        <param name="beanClass">example.model.Header</param>
        <param name="setOn">order</param>
        <param name="bindings">
            <!-- Header bindings... -->
            <binding property="orderId" selector="header order-id" />
            <binding property="orderStatus" type="Long" selector="header status-code" />
            <binding property="netAmount" type="BigDecimal" selector="header net-amount" />
            <binding property="totalAmount" type="BigDecimal" selector="header total-amount" />
            <binding property="tax" type="BigDecimal" selector="header tax" />
            <binding property="date" type="OrderDateLong" selector="header date" />
        </param>
    </resource-config>

    <!--
    Create an "example.beans.Customer" bean instance when we visit the start of the
    <customer-details> element.
    Set the new bean instance on the "header" bean.
    -->
    <resource-config selector="customer-details">
        <resource>org.milyn.javabean.BeanPopulator</resource>
        <param name="beanClass">example.model.Customer</param>
        <param name="setOn">header</param>
        <param name="bindings">
            <!-- Customer bindings... -->
            <binding property="userName" selector="customer-details username" />
            <binding property="firstName" selector="customer-details name firstname" />
            <binding property="lastName" selector="customer-details name lastname" />
            <binding property="state" selector="customer-details state" />
        </param>
    </resource-config>

    <!--
    Create an "example.beans.OrderItem" bean instance when we visit the start of the <order-item> element.
    Add the OrderItem instances to a list (because there are potentially many of them on an order).
    Set the OrderItem list on the "order" bean.
    -->
    <resource-config selector="order-item">
        <resource>org.milyn.javabean.BeanPopulator</resource>
        <param name="beanClass">example.model.OrderItem</param>
        <param name="addToList">true</param>
        <param name="setOn">order</param>
        <param name="bindings">
            <!-- OrderItem bindings... -->
            <binding property="quantity" type="Integer" selector="order-item quantity" />
            <binding property="productId" type="String" selector="order-item product-id" />
            <binding property="price" type="BigDecimal" selector="order-item price" />
            <binding property="title" selector="order-item title" />
        </param>
    </resource-config>

    <!--
    Configure a special decoder for the <date> field of the <header> - to set the date format.
    -->
    <resource-config selector="decoder:OrderDateLong">
        <resource>org.milyn.javabean.decoders.DateDecoder</resource>
        <param name="format">EEE MMM dd HH:mm:ss z yyyy</param>
    </resource-config>

</smooks-resource-list>

Here's the edi mapping ("/src/main/java/example/edi-to-xml-order-mapping.xml"):

<?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>

Description of the mapping:



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");
// Create an exec context - no profiles....
StandaloneExecutionContext executionContext = smooks.createExecutionContext();

// Filter the input message to the outputWriter, using the execution context...
DOMResult domResult = new DOMResult();
smooks.filter(new StreamSource(new ByteArrayInputStream(messageIn)), domResult, executionContext);

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

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

  • No labels