V1.2:ejc

From Smooks

Jump to: navigation, search

This tutorial illustrates how use the Edifact Java Compiler (EJC), which greatly simplifies binding of EDI data to a Java Object Model.

Contents

Executing the example is quite simple:

  1. In "ejc-gen" execute "mvn clean install"
  2. In "ejc-use" execute "mvn clean install"
  3. In "ejc-use" execute "mvn exec:java"


Example source can be viewed and/or checked out from SVN here.

(Back to Examples Index Page...)


The example is split into 2 modules.

  1. The "ejc-gen" module demonstrates how to execute EJC on an EDI Mapping Model (for asn Order message) using the EJC Maven Plugin, which generates the Java and Smooks artifacts, compiles, jars and installs them in the local Maven Repository.
  2. The "ejc-use" module demonstrates how to use the EJC Generated artifacts via the OrderFactory class.

The EDI Message & EDI Mapping Model

The EDI Message is a simple Order message as follows (see the "input-message.edi" file in the "ejc-use" sub-module):

HDR*1*0*59.97*64.92*4.95*20060821
CUS*user1*Harry^Fletcher*SD*harry.fletcher@gmail
ORD*1*1*364*The 40-Year-Old Virgin*29.98
ORD*2*1*299*Pulp Fiction*29.99

The Mapping Model for processing an instance of this message is as follows (see the "edi-model.xml" file in the "ejc-gen" sub-module):

<?xml version="1.0" encoding="UTF-8"?>
<medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.2.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="orderId"/>
<medi:field xmltag="statusCode"/>
<medi:field xmltag="netAmount" type="BigDecimal"/>
<medi:field xmltag="totalAmount" type="BigDecimal"/>
<medi:field xmltag="tax" type="BigDecimal"/>
<medi:field xmltag="date" type="Date" typeParameters="format=yyyyMMdd"/>
 
<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:field xmltag="email"/>
</medi:segment>
 
</medi:segment>
 
<medi:segmentGroup xmltag="order-items">
<medi:segment segcode="ORD" xmltag="order-item" maxOccurs="-1">
<medi:field xmltag="position" type="Integer"/>
<medi:field xmltag="quantity" type="Long"/>
<medi:field xmltag="productId"/>
<medi:field xmltag="title"/>
<medi:field xmltag="price" type="BigDecimal"/>
</medi:segment>
</medi:segmentGroup>
 
</medi:segments>
 
</medi:edimap>

Step 1 - Generating the Java and Smooks Artifacts

We use the EJC Maven Plugin to process the EDI Mapping Model and from it, generate the Java and Smooks Artifacts required to process an EDI message instance described by the EDI Mapping Model.

The EJC Maven Plugin is realy easy to use. You just include it in your Maven POM as follows (see the "ejc-gen" sub-module):

<build>
<plugins>
<plugin>
<groupId>org.milyn</groupId>
<artifactId>maven-ejc-plugin</artifactId>
<version>1.2</version>
<configuration>
<ediMappingFile>edi-model.xml</ediMappingFile>
<packageName>com.acme.order.model</packageName>
</configuration>
<executions>
<execution><goals><goal>generate</goal></goals></execution>
</executions>
</plugin>
</plugins>
</build>

This Maven Plugin generates:

  1. A Java Object model for the EDI Mapping Model.
  2. A Smooks Java Binding config to populate the Java Object model from an instance of the EDI message described by the EDI Mapping Model (see #1 above).
  3. An OrderFactory class that makes it very east to use EJC to bind EDI data to the Java Object Model.

Step 2 - Using the EJC Generated Artifacts

This couldn't really be any easier. EJC produces a jar file that contains a Factory class. In this case, the Factory class is the com.acme.order.model.OrderFactory class. The following code illustrates how to use this class to populate an instance of the Order object model (also generated by EJC) from an instance of the Order EDI message (see the "ejc-use" sub-module):

// Create the Factory class instance.  This should normally be cached
// and reused...
OrderFactory orderFactory = OrderFactory.getInstance();
 
// Bind the EDI message stream data into the EJC generated Order model...
Order order = orderFactory.fromEDI(ediStream);
 
// Process the order data...
Header header = order.getHeader();
Name name = header.getCustomerDetails().getName();
List<OrderItem> orderItems = order.getOrderItems();

And that's it!!