<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd"
>

<channel>
	<title>Smooks Data Integration &#187; Smooks</title>
	<atom:link href="http://blog.smooks.org/category/smooks/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.smooks.org</link>
	<description>The official blog</description>
	<lastBuildDate>Thu, 05 May 2011 11:52:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<!-- podcast_generator="Blubrry PowerPress/1.0.7" mode="simple" entry="normal" -->
	<itunes:summary>The official blog</itunes:summary>
	<itunes:author>Smooks Data Integration</itunes:author>
	<itunes:explicit>no</itunes:explicit>
	<itunes:image href="http://blog.smooks.org/wp-content/plugins/powerpress/itunes_default.jpg" />
	<itunes:owner>
		<itunes:name>Smooks Data Integration</itunes:name>
		<itunes:email>tom.fennelly@gmail.com</itunes:email>
	</itunes:owner>
	<managingEditor>tom.fennelly@gmail.com (Smooks Data Integration)</managingEditor>
	<itunes:subtitle>The official blog</itunes:subtitle>
	<image>
		<title>Smooks Data Integration &#187; Smooks</title>
		<url>http://blog.smooks.org/wp-content/plugins/powerpress/rss_default.jpg</url>
		<link>http://blog.smooks.org/category/smooks/</link>
	</image>
		<item>
		<title>Smooks OSGI: Camel, UN/EDIFACT, ServiceMix</title>
		<link>http://blog.smooks.org/2011/02/19/smooks-osgi-revisited/</link>
		<comments>http://blog.smooks.org/2011/02/19/smooks-osgi-revisited/#comments</comments>
		<pubDate>Sat, 19 Feb 2011 13:44:14 +0000</pubDate>
		<dc:creator>Daniel Bevenius</dc:creator>
				<category><![CDATA[Camel]]></category>
		<category><![CDATA[EDI]]></category>
		<category><![CDATA[Smooks]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=571</guid>
		<description><![CDATA[<p>This post is about the work we have done to improve Smooks OSGI integration. </p>
<p>With Smooks 1.4 we added support for OSGI in the form of making the smooks-all.jar into a bundle. This jar contains all the Smooks classes and resources and is a convenience jar that can be used if you don&#8217;t want to keep track of all the individual jars that make up Smooks. During this work we also added a few classes to make Smooks work well inside an OSGI container.</p>
<p>For some background and more details about the Smooks-OSGI work please take a look at this <a href="http://www.smooks.org/mediawiki/index.php?title=SmooksOsgi">wiki page</a>.</p>
<p><strong>Using Smooks in an OSGI Container (with Camel and ServiceMix)</strong><br />
With 1.4 we added Camel support which has had OSGI support for a long time. We added a few basic examples but it turned out that this was not the best solution. The solution in 1.4 required users to explicitly specify the packages the client bundle required and this made maintenance difficult.<br />
Using Smooks in Camel should not require anything more then declaring that the client bundle uses Camel and perhaps Spring. Client bundles now only need to declare that they import packages for Camel, for example:</p>
<pre class="brush: plain; wrap-lines: true; toolbar: false">
Manifest-Version: 1.0
Export-Package: example;uses:="org.apache.camel,
    org.springframework.context.support,
    org.springframework.context"
Bundle-Version: 1.0
Bundle-Name: Milyn Smooks Example - Smooks Camel CVS to XML
Bundle-ManifestVersion: 2
Import-Package:
    example,org.apache.camel;version="2.6",
    org.springframework.context;version="1.2.0",
    org.springframework.context.support;version="1.2.0"
Bundle-SymbolicName: milyn-smooks-example-camel-csv-to-xml
</pre>
<p><strong>Simplified deployment to ServiceMix</strong><br />
ServiceMix users will hopefully be glad to know that we now generate a features.xml file for easy deployment into service mix. A features.xml specifies all the bundles that a feature need and can be installed/uninstalled as a single unit.<br />
For example, to install the Smooks bundle you can use the following commands:<br />
<code><br />
karaf@root> features:addUrl mvn:org.milyn/milyn-smooks-all/1.5-SNAPSHOT/xml/features<br />
karaf@root> features:install smooks<br />
</code></p>
<p><strong>UN/EDIFACT</strong><br />
Smooks has UN/EDIFACT support which includes pre-generated artifacts that are available in <a href="http://mvnrepository.com/artifact/org.milyn.edi.unedifact">maven</a>.  As part of the OSGi work, we have modified these artifacts so as to make them OSGi fragment bundles</p>
<p>Users can now install the mappings and binding as fragment bundles and these will attach to a specific version of the Smooks bundle. This is what this looks like running in Apache ServiceMix 4.3.0.1-fuse:<br />
<code><br />
[ 430] [Active     ] [            ] [       ] [   60] Smooks OSGi (1.5.0.SNAPSHOT)<br />
                                       Fragments: 439,441<br />
[ 439] [Resolved   ] [            ] [       ] [   60] Smooks EDI - UN/EDIFACT - D96A - Bindings (1.4)<br />
                                       Hosts: 430<br />
[ 441] [Resolved   ] [            ] [       ] [   60] Smooks EDI - UN/EDIFACT - D96A - Mapping Model (1.4)<br />
                                       Hosts: 430<br />
[ 448] [Active     ] [            ] [Started] [   60] Milyn Smooks Example - Smooks Camel UNEDIFACT to String (1.0)<br />
</code><br />
Above we can see that the binding (id=439) and the mapping (id=441) have been attached to the Smooks bundle (id=420). More fragment bundles can be attached as required and you can attach the same fragment to different version of the Smooks bundle since we specify a version on the Fragment-Host header. </p>
<p>As mentioned earlier in this post you can find more information and details about this work on this <a href="http://www.smooks.org/mediawiki/index.php?title=SmooksOsgi">wiki page</a>.</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/camel/">Camel</a> by Daniel Bevenius <a href="http://blog.smooks.org/2011/02/19/smooks-osgi-revisited/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>This post is about the work we have done to improve Smooks OSGI integration. </p>
<p>With Smooks 1.4 we added support for OSGI in the form of making the smooks-all.jar into a bundle. This jar contains all the Smooks classes and resources and is a convenience jar that can be used if you don&#8217;t want to keep track of all the individual jars that make up Smooks. During this work we also added a few classes to make Smooks work well inside an OSGI container.</p>
<p>For some background and more details about the Smooks-OSGI work please take a look at this <a href="http://www.smooks.org/mediawiki/index.php?title=SmooksOsgi">wiki page</a>.</p>
<p><strong>Using Smooks in an OSGI Container (with Camel and ServiceMix)</strong><br />
With 1.4 we added Camel support which has had OSGI support for a long time. We added a few basic examples but it turned out that this was not the best solution. The solution in 1.4 required users to explicitly specify the packages the client bundle required and this made maintenance difficult.<br />
Using Smooks in Camel should not require anything more then declaring that the client bundle uses Camel and perhaps Spring. Client bundles now only need to declare that they import packages for Camel, for example:</p>
<pre class="brush: plain; wrap-lines: true; toolbar: false">
Manifest-Version: 1.0
Export-Package: example;uses:="org.apache.camel,
    org.springframework.context.support,
    org.springframework.context"
Bundle-Version: 1.0
Bundle-Name: Milyn Smooks Example - Smooks Camel CVS to XML
Bundle-ManifestVersion: 2
Import-Package:
    example,org.apache.camel;version="2.6",
    org.springframework.context;version="1.2.0",
    org.springframework.context.support;version="1.2.0"
Bundle-SymbolicName: milyn-smooks-example-camel-csv-to-xml
</pre>
<p><strong>Simplified deployment to ServiceMix</strong><br />
ServiceMix users will hopefully be glad to know that we now generate a features.xml file for easy deployment into service mix. A features.xml specifies all the bundles that a feature need and can be installed/uninstalled as a single unit.<br />
For example, to install the Smooks bundle you can use the following commands:<br />
<code><br />
karaf@root> features:addUrl mvn:org.milyn/milyn-smooks-all/1.5-SNAPSHOT/xml/features<br />
karaf@root> features:install smooks<br />
</code></p>
<p><strong>UN/EDIFACT</strong><br />
Smooks has UN/EDIFACT support which includes pre-generated artifacts that are available in <a href="http://mvnrepository.com/artifact/org.milyn.edi.unedifact">maven</a>.  As part of the OSGi work, we have modified these artifacts so as to make them OSGi fragment bundles</p>
<p>Users can now install the mappings and binding as fragment bundles and these will attach to a specific version of the Smooks bundle. This is what this looks like running in Apache ServiceMix 4.3.0.1-fuse:<br />
<code><br />
[ 430] [Active     ] [            ] [       ] [   60] Smooks OSGi (1.5.0.SNAPSHOT)<br />
                                       Fragments: 439,441<br />
[ 439] [Resolved   ] [            ] [       ] [   60] Smooks EDI - UN/EDIFACT - D96A - Bindings (1.4)<br />
                                       Hosts: 430<br />
[ 441] [Resolved   ] [            ] [       ] [   60] Smooks EDI - UN/EDIFACT - D96A - Mapping Model (1.4)<br />
                                       Hosts: 430<br />
[ 448] [Active     ] [            ] [Started] [   60] Milyn Smooks Example - Smooks Camel UNEDIFACT to String (1.0)<br />
</code><br />
Above we can see that the binding (id=439) and the mapping (id=441) have been attached to the Smooks bundle (id=420). More fragment bundles can be attached as required and you can attach the same fragment to different version of the Smooks bundle since we specify a version on the Fragment-Host header. </p>
<p>As mentioned earlier in this post you can find more information and details about this work on this <a href="http://www.smooks.org/mediawiki/index.php?title=SmooksOsgi">wiki page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2011/02/19/smooks-osgi-revisited/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Splitting and Routing UN/EDIFACT Interchanges using Smooks and Apache Camel</title>
		<link>http://blog.smooks.org/2010/10/27/splitting-and-routing-unedifact-interchanges-using-smooks-and-apache-camel/</link>
		<comments>http://blog.smooks.org/2010/10/27/splitting-and-routing-unedifact-interchanges-using-smooks-and-apache-camel/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 15:00:40 +0000</pubDate>
		<dc:creator>Tom Fennelly</dc:creator>
				<category><![CDATA[Camel]]></category>
		<category><![CDATA[EDI]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=560</guid>
		<description><![CDATA[<p>Two of the main new features in Smooks v1.4 are, out of the box support for <strong>UN/EDIFACT</strong> Interchanges, and <strong>Apache Camel</strong> integration.</p>
<p>The new <strong>UN/EDIFACT</strong> support allows you to process UN/EDIFACT Interchanges by either or both of the following methods:</p>
<ol>
<li>Conversion to <strong>XML</strong>, allowing you to use standard XML tools (or other Smooks extensions) for further processing.</li>
<li>Binding the interchange message data into <strong>pre-built Java object models</strong>, or custom models if you prefer to use your own.</li>
</ol>
<p>All the required artifacts for reading any of the UN/EDIFACT directories (and converting to XML) are available in Maven, as are all of the pre-built Java Object models for all the messages defined in all of the UN/EDIFACT directories.  To use them, all you need to do is include the appropriate artifacts into your project.  <a href="http://www.smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#UN.2FEDIFACT">See the User Guide</a>.</p>
<p>The new <strong>Apache Camel</strong> integration allows you to hook Smooks into your Camel routes as a Camel Component or Camel DataFormat.  Another important part of the Camel integration are the Smooks extensions that allow you to perform inline routing of Smooks BeanContext data to Camel endpoints i.e. you can also process huge (GB+) data streams.</p>
<p>Putting these two new capabilities together allows us to provide some interesting functionality with respect to Splitting and Routing UN/EDIFACT message Interchanges.  We use Smooks to perform the reading, conversion (to XML and/or Java) and splitting of the interchanges and then use Camel to to perform the inline routing of the Java or XML (using the new Camel extensions for Smooks).</p>
<p>We created an example that demonstrates this capability:</p>
<ul>
<li><a href="http://www.screencast.com/t/Pm3eAIR8DSd"><em><strong>See the Screencast</strong></em></a>.</li>
<li><em><strong><a href="http://smooks.org/mediawiki/index.php?title=Smooks_v1.4_Examples#Apache_Camel">Get the code</a></strong></em> and try it yourself (the &#8220;splitting-camel&#8221; example).</li>
</ul>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/camel/">Camel</a> by Tom Fennelly <a href="http://blog.smooks.org/2010/10/27/splitting-and-routing-unedifact-interchanges-using-smooks-and-apache-camel/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>Two of the main new features in Smooks v1.4 are, out of the box support for <strong>UN/EDIFACT</strong> Interchanges, and <strong>Apache Camel</strong> integration.</p>
<p>The new <strong>UN/EDIFACT</strong> support allows you to process UN/EDIFACT Interchanges by either or both of the following methods:</p>
<ol>
<li>Conversion to <strong>XML</strong>, allowing you to use standard XML tools (or other Smooks extensions) for further processing.</li>
<li>Binding the interchange message data into <strong>pre-built Java object models</strong>, or custom models if you prefer to use your own.</li>
</ol>
<p>All the required artifacts for reading any of the UN/EDIFACT directories (and converting to XML) are available in Maven, as are all of the pre-built Java Object models for all the messages defined in all of the UN/EDIFACT directories.  To use them, all you need to do is include the appropriate artifacts into your project.  <a href="http://www.smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#UN.2FEDIFACT">See the User Guide</a>.</p>
<p>The new <strong>Apache Camel</strong> integration allows you to hook Smooks into your Camel routes as a Camel Component or Camel DataFormat.  Another important part of the Camel integration are the Smooks extensions that allow you to perform inline routing of Smooks BeanContext data to Camel endpoints i.e. you can also process huge (GB+) data streams.</p>
<p>Putting these two new capabilities together allows us to provide some interesting functionality with respect to Splitting and Routing UN/EDIFACT message Interchanges.  We use Smooks to perform the reading, conversion (to XML and/or Java) and splitting of the interchanges and then use Camel to to perform the inline routing of the Java or XML (using the new Camel extensions for Smooks).</p>
<p>We created an example that demonstrates this capability:</p>
<ul>
<li><a href="http://www.screencast.com/t/Pm3eAIR8DSd"><em><strong>See the Screencast</strong></em></a>.</li>
<li><em><strong><a href="http://smooks.org/mediawiki/index.php?title=Smooks_v1.4_Examples#Apache_Camel">Get the code</a></strong></em> and try it yourself (the &#8220;splitting-camel&#8221; example).</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2010/10/27/splitting-and-routing-unedifact-interchanges-using-smooks-and-apache-camel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>UN/EDIFACT Editor for Eclipse&#8230; Ecore/XMI Models etc</title>
		<link>http://blog.smooks.org/2010/09/18/unedifact-editor-for-eclipse/</link>
		<comments>http://blog.smooks.org/2010/09/18/unedifact-editor-for-eclipse/#comments</comments>
		<pubDate>Sat, 18 Sep 2010 12:05:01 +0000</pubDate>
		<dc:creator>Tom Fennelly</dc:creator>
				<category><![CDATA[EDI]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=518</guid>
		<description><![CDATA[<p>Renat Zubairov has been doing some really cool stuff with the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#UN.2FEDIFACT">Smooks UN/EDIFACT</a> support that&#8217;s been added for Smooks v1.4.  On top of the ECT tooling, he has built tools for generating <a href="http://www.eclipse.org/modeling/emf/">Eclipse EMF</a> Ecore (XMI) models for each of the EDI Mapping Models being produced by ECT.  On the back of this work, he has been able to build an Editor for Eclipse, allowing you to read and write UN/EDIFACT messages from inside Eclipse.</p>
<p><strong>See more <a href="http://www.smooks.org/mediawiki/index.php?title=EDI_Editor">here on the Smooks Wiki</a>.</strong></p>
<p>The UN/EDIFACT Editor for Eclipse is cool, but I think the wider impact of these capabilities is even cooler.  This is really important work because it will open a number of really important doors in terms of our ability to process and transform not just UN/EDIFACT, but EDI in general (X.12, HL7 etc).  Our long-term goal is to add support for X.12, HL7 etc to ECT (i.e. build conversion tools for these standards).  Once we do this, we will be able to provide out-of-the-box support for these EDI formats on the Smooks Runtime.  We hope it will also mean that we can use this work that Renat is doing to automate building of Ecore/XMI models for these same formats.  Once we have these Ecore/XMI models, we hope we will be able to:</p>
<ol>
<li>Build and provide XML Schemas (XSDs) for all of these messages.</li>
<li>Make use of <a href="http://wiki.eclipse.org/index.php/EMF_Compare">Eclipse EMF Compare</a> to compare models e.g. compare 2 different versions (D93 and D96) of a UN/EDIFACT Invoice specification.</li>
<li>Make use of <a href="http://www.eclipse.org/m2m/">Eclipse EMF M2M</a> to create a transformation model between 2 different models e.g. between UN/EDIFACT and X.12 Invoices.</li>
</ol>
<p>Note that this will not hit the main codebase until after we have released Smooks v1.4 i.e. we hope to make it available with Smooks v1.5.</p>
<p>This is awesome stuff.  Keep up the great work Renat!!!</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/smooks/edi/">EDI</a> by Tom Fennelly <a href="http://blog.smooks.org/2010/09/18/unedifact-editor-for-eclipse/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>Renat Zubairov has been doing some really cool stuff with the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#UN.2FEDIFACT">Smooks UN/EDIFACT</a> support that&#8217;s been added for Smooks v1.4.  On top of the ECT tooling, he has built tools for generating <a href="http://www.eclipse.org/modeling/emf/">Eclipse EMF</a> Ecore (XMI) models for each of the EDI Mapping Models being produced by ECT.  On the back of this work, he has been able to build an Editor for Eclipse, allowing you to read and write UN/EDIFACT messages from inside Eclipse.</p>
<p><strong>See more <a href="http://www.smooks.org/mediawiki/index.php?title=EDI_Editor">here on the Smooks Wiki</a>.</strong></p>
<p>The UN/EDIFACT Editor for Eclipse is cool, but I think the wider impact of these capabilities is even cooler.  This is really important work because it will open a number of really important doors in terms of our ability to process and transform not just UN/EDIFACT, but EDI in general (X.12, HL7 etc).  Our long-term goal is to add support for X.12, HL7 etc to ECT (i.e. build conversion tools for these standards).  Once we do this, we will be able to provide out-of-the-box support for these EDI formats on the Smooks Runtime.  We hope it will also mean that we can use this work that Renat is doing to automate building of Ecore/XMI models for these same formats.  Once we have these Ecore/XMI models, we hope we will be able to:</p>
<ol>
<li>Build and provide XML Schemas (XSDs) for all of these messages.</li>
<li>Make use of <a href="http://wiki.eclipse.org/index.php/EMF_Compare">Eclipse EMF Compare</a> to compare models e.g. compare 2 different versions (D93 and D96) of a UN/EDIFACT Invoice specification.</li>
<li>Make use of <a href="http://www.eclipse.org/m2m/">Eclipse EMF M2M</a> to create a transformation model between 2 different models e.g. between UN/EDIFACT and X.12 Invoices.</li>
</ol>
<p>Note that this will not hit the main codebase until after we have released Smooks v1.4 i.e. we hope to make it available with Smooks v1.5.</p>
<p>This is awesome stuff.  Keep up the great work Renat!!!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2010/09/18/unedifact-editor-for-eclipse/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solving an old problem with Smooks when embedded in other frameworks</title>
		<link>http://blog.smooks.org/2010/09/14/camel-integration-and-new-smooks-configuration-element/</link>
		<comments>http://blog.smooks.org/2010/09/14/camel-integration-and-new-smooks-configuration-element/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 09:19:45 +0000</pubDate>
		<dc:creator>Daniel Bevenius</dc:creator>
				<category><![CDATA[Smooks]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=500</guid>
		<description><![CDATA[<p>All of the existing integrations of Smooks within other frameworks  (JBossESB, Mule and others) suffer from a common design flaw, whereby users need to specify Result type information inside the framework&#8217;s  configurations.  This Result type information is used by the framework to map  Smooks filtering results back into the frameworks messaging constructs.</p>
<p>The problem with the above approach is:</p>
<ol>
<li>It makes the framework configuration more verbose than it should  be.  Also (in some cases), the embedding framework does not  provide a rich enough configuration model for defining this information,  resulting in ugly/un-intuitive configuration hacks for defining the  result.</li>
<li>It means the user needs to read and understand the Smooks  configuration and then make the framework level configurations, based on  what they have interpreted from the Smooks configuration.  This is a pain!</li>
<li>Adding support for new Result types usually involves modifications to the framework integration.</li>
<li>It typically only supports mapping of one result type, restricting the use of Smooks.</li>
</ol>
<p>When doing the Camel integration, we started to encounter this issue again.  The solution that we came up with was to add a configuration element  to the Smooks core namespace, pulling all of this configuration back  into the Smooks configuration and providing a richer configuration  model:</p>
<pre class="brush: xml; wrap-lines: true; toolbar: false">
<smooks-resource-list
    xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd" >

    <core:exports>
        <core:result type="org.milyn.payload.JavaResult">
        </core:result>
    </core:exports>
</pre>
<p>The newly added exports element declares the results that are produced by this Smooks configuration. A export element can contain one or more result elements. A framework that uses Smooks could then perform filtering like this:</p>
<pre class="brush: java; wrap-lines: false; toolbar: false">
// Get the Exported types that were configured.
Exports exports = Exports.getExports(smooks.getApplicationContext());
if (exports.hasExports())
{
    // Create the instances of the Result types.
    // (Only the types, i.e the Class type are declared in the 'type' attribute.
    Result[] results = exports.createResults();
    smooks.filterSource(executionContext, getSource(exchange), results);
    // The Results(s) will now be populate by Smooks filtering process and
    // available to the framework in question.
}</pre>
<p>There might also be cases where you only want a portion of the result extracted and returned. You can use the &#8216;extract&#8217; attribute to specify this:</p>
<pre class="brush: xml; wrap-lines: true; toolbar: false">
<smooks-resource-list
    xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd" >

    <core:exports>
        <core:result type="org.milyn.payload.JavaResult" extract="orderBean">
        </core:result>
    </core:exports>
</pre>
<p>The extract attribute is intended to be used when you are only interested in a sub-section of a produced result. In the example above we are saying that we only want the object named <strong>orderBean</strong> to be exported. The other contents of the JavaResult will be ignored. Another example where you might want to use this kind of extracting could be when you only want a ValidationResult of a certain type, for example to only return validation errors.</p>
<p>Below is an example of using the extracts option from an embedded framework</p>
<pre class="brush: java; wrap-lines: false; toolbar: false">// Get the Exported types that were configured.
Exports exports = Exports.getExports(smooks.getApplicationContext());
if (exports.hasExports())
{
    // Create the instances of the Result types.
    // (Only the types, i.e the Class type are declared in the 'type' attribute.
    Result[] results = exports.createResults();
    smooks.filterSource(executionContext, getSource(exchange), results);
    List<Object> objects = Exports.extractResults(results, exports);
    // Now make the object available to the framework that this code is running:
    // Camel, JBossESB, Mule etc.
</pre>
<p>This feature is now available in Smooks <a href="http://svn.codehaus.org/milyn/trunk/">trunk</a>.</p>
<p>Comments and feedback are as always welcome.</p>
<p>Thanks,</p>
<p>/Daniel</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/smooks/">Smooks</a> by Daniel Bevenius <a href="http://blog.smooks.org/2010/09/14/camel-integration-and-new-smooks-configuration-element/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>All of the existing integrations of Smooks within other frameworks  (JBossESB, Mule and others) suffer from a common design flaw, whereby users need to specify Result type information inside the framework&#8217;s  configurations.  This Result type information is used by the framework to map  Smooks filtering results back into the frameworks messaging constructs.</p>
<p>The problem with the above approach is:</p>
<ol>
<li>It makes the framework configuration more verbose than it should  be.  Also (in some cases), the embedding framework does not  provide a rich enough configuration model for defining this information,  resulting in ugly/un-intuitive configuration hacks for defining the  result.</li>
<li>It means the user needs to read and understand the Smooks  configuration and then make the framework level configurations, based on  what they have interpreted from the Smooks configuration.  This is a pain!</li>
<li>Adding support for new Result types usually involves modifications to the framework integration.</li>
<li>It typically only supports mapping of one result type, restricting the use of Smooks.</li>
</ol>
<p>When doing the Camel integration, we started to encounter this issue again.  The solution that we came up with was to add a configuration element  to the Smooks core namespace, pulling all of this configuration back  into the Smooks configuration and providing a richer configuration  model:</p>
<pre class="brush: xml; wrap-lines: true; toolbar: false">
<smooks-resource-list
    xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd" >

    <core:exports>
        <core:result type="org.milyn.payload.JavaResult">
        </core:result>
    </core:exports>
</pre>
<p>The newly added exports element declares the results that are produced by this Smooks configuration. A export element can contain one or more result elements. A framework that uses Smooks could then perform filtering like this:</p>
<pre class="brush: java; wrap-lines: false; toolbar: false">
// Get the Exported types that were configured.
Exports exports = Exports.getExports(smooks.getApplicationContext());
if (exports.hasExports())
{
    // Create the instances of the Result types.
    // (Only the types, i.e the Class type are declared in the 'type' attribute.
    Result[] results = exports.createResults();
    smooks.filterSource(executionContext, getSource(exchange), results);
    // The Results(s) will now be populate by Smooks filtering process and
    // available to the framework in question.
}</pre>
<p>There might also be cases where you only want a portion of the result extracted and returned. You can use the &#8216;extract&#8217; attribute to specify this:</p>
<pre class="brush: xml; wrap-lines: true; toolbar: false">
<smooks-resource-list
    xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
    xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.4.xsd" >

    <core:exports>
        <core:result type="org.milyn.payload.JavaResult" extract="orderBean">
        </core:result>
    </core:exports>
</pre>
<p>The extract attribute is intended to be used when you are only interested in a sub-section of a produced result. In the example above we are saying that we only want the object named <strong>orderBean</strong> to be exported. The other contents of the JavaResult will be ignored. Another example where you might want to use this kind of extracting could be when you only want a ValidationResult of a certain type, for example to only return validation errors.</p>
<p>Below is an example of using the extracts option from an embedded framework</p>
<pre class="brush: java; wrap-lines: false; toolbar: false">// Get the Exported types that were configured.
Exports exports = Exports.getExports(smooks.getApplicationContext());
if (exports.hasExports())
{
    // Create the instances of the Result types.
    // (Only the types, i.e the Class type are declared in the 'type' attribute.
    Result[] results = exports.createResults();
    smooks.filterSource(executionContext, getSource(exchange), results);
    List<Object> objects = Exports.extractResults(results, exports);
    // Now make the object available to the framework that this code is running:
    // Camel, JBossESB, Mule etc.
</pre>
<p>This feature is now available in Smooks <a href="http://svn.codehaus.org/milyn/trunk/">trunk</a>.</p>
<p>Comments and feedback are as always welcome.</p>
<p>Thanks,</p>
<p>/Daniel</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2010/09/14/camel-integration-and-new-smooks-configuration-element/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Slides Smooks Community Day</title>
		<link>http://blog.smooks.org/2010/09/09/slides-smooks-community-day/</link>
		<comments>http://blog.smooks.org/2010/09/09/slides-smooks-community-day/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 11:48:44 +0000</pubDate>
		<dc:creator>Maurice</dc:creator>
				<category><![CDATA[EDI]]></category>
		<category><![CDATA[Others Using Smooks]]></category>
		<category><![CDATA[Persistence]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Publications]]></category>
		<category><![CDATA[Scribe]]></category>
		<category><![CDATA[Smooks]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[Sopera]]></category>
		<category><![CDATA[un/edifact]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=497</guid>
		<description><![CDATA[<p>Here is a quick overview where you can find the slides presented on the Smooks Community Day 2010:</p>
<ul>
<li><a href="http://www.slideshare.net/tfennelly/unedifact-interchange-processing-with-smooks-v14-5104071" target="_blank">UN/EDIFACT processing with Smooks</a></li>
<li><a href="http://www.slideshare.net/mzeijen/reusing-existing-datalayertopersistmultif" target="_blank">Reusing existing data layer to persist multi format data streams</a></li>
<li><a href="http://www.slideshare.net/RenatZubairov/sopera-di-components-for-smooks-community-day" target="_blank">Smooks Sopera DI Components</a></li>
</ul>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/smooks/edi/">EDI</a> by Maurice <a href="http://blog.smooks.org/2010/09/09/slides-smooks-community-day/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>Here is a quick overview where you can find the slides presented on the Smooks Community Day 2010:</p>
<ul>
<li><a href="http://www.slideshare.net/tfennelly/unedifact-interchange-processing-with-smooks-v14-5104071" target="_blank">UN/EDIFACT processing with Smooks</a></li>
<li><a href="http://www.slideshare.net/mzeijen/reusing-existing-datalayertopersistmultif" target="_blank">Reusing existing data layer to persist multi format data streams</a></li>
<li><a href="http://www.slideshare.net/RenatZubairov/sopera-di-components-for-smooks-community-day" target="_blank">Smooks Sopera DI Components</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2010/09/09/slides-smooks-community-day/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing UN/EDIFACT message Interchanges&#8230;</title>
		<link>http://blog.smooks.org/2010/06/28/processing-unedifact-message-interchanges/</link>
		<comments>http://blog.smooks.org/2010/06/28/processing-unedifact-message-interchanges/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 12:47:30 +0000</pubDate>
		<dc:creator>Tom Fennelly</dc:creator>
				<category><![CDATA[EDI]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=441</guid>
		<description><![CDATA[<p>Bård (Langöy) and I have been doing lots of work on improving our EDI support, specifically in the area of handling <a href="http://www.unece.org/trade/untdid/welcome.htm">UN/EDIFACT</a> (<a href="http://en.wikipedia.org/wiki/UN/EDIFACT">WikipediA</a>) message <a href="http://www.unece.org/trade/edifact/untdid/d300_s.htm#i">Interchanges</a>.  The following improvements are now in the Smooks v1.4 codebase (available from the 1.4-SNAPSHOT):</p>
<ol>
<li><strong>A new <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#UN.2FEDIFACT">UN/EDIFACT Interchange Reader</a></strong> (<em>&lt;unedifact:reader&gt;</em>), which allows Smooks to process UN/EFIFACT message Interchanges (one or more UN/EDIFACT messages wrapped in the UN/EDIFACT Interchange control segments).</li>
<li>A new <strong>Maven/Ant</strong> tool called the &#8220;<strong>EDI Conversion Tool</strong>&#8221; (<strong>ECT</strong>) that can take the <a href="http://www.unece.org/trade/untdid/down_index.htm">official UN/EDIFACT message definition directory zip files</a> and from them, generate a jar file containing a <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">set of equivalent Smooks EDI Mapping Models</a>.  This tool is very easy to configure and use, but we are in the processing of pre-generating a library of these jars for all the UN/EDIFACT message sets and making them available trough the central maven repository, making it even easier to consume UN/EDIFACT messages with Smooks.  We hope to add support for other formats (X.12, HL7 etc) in future releases of Smooks.</li>
</ol>
<p>So that&#8217;s what&#8217;s available in the current 1.4 codebase (1.4-SNAPSHOT), but we&#8217;re also in the process of making more additions to this for Smooks v1.4.  We&#8217;re extending the <strong>EDI Java Compiler</strong> (<strong>EJC</strong>) tools to support Java Model generation from the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI Mapping Model sets</a> generated by the ECT tool (mentioned above).  We&#8217;ll also make these available as pre-built jar files from the Maven repository.</p>
<p>Another improvement we&#8217;ve made in the 1.4 codebase is the addition of support for <strong>Java Model to EDI serialization</strong> on the EJC generated Java object models, meaning we&#8217;ll be able to do full round trip binding of an EDI (and UN/EDIFACT) message to a populated Java Object model that can be modified and then serialize back out to an EDI stream.  Or, your app can &#8220;manually&#8221; construct the same Java Object models and serialize them out to an EDI Stream (think JAXB).</p>
<h2>Generating a Mapping Model Set using ECT</h2>
<p>The easiest way to consume UN/EDIFACT messages using Smooks is to use ECT (EDI Conversion Tool) to generate the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI  Mapping Model set</a> (if we haven&#8217;t already pre-built them).  The steps are really easy:</p>
<ol>
<li>Download the official UN/EDIFACT message definitions directory zip file you are interested in <a href="http://www.unece.org/trade/untdid/down_index.htm">from the UNECE website</a>.</li>
<li>Create a maven module for the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI   Mapping Model set</a> to be generated and add the <em>maven-ect-plugin</em> (see below) to it&#8217;s POM.</li>
<li>Execute &#8220;<em>mvn clean install</em>&#8221; in your maven module and  the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI     Mapping Model set</a> jar file will be generated as normal in the  modules <em>target</em> folder, and installed into your local maven  repository.</li>
</ol>
<p>The following is an example of the maven module POM for generating the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI      Mapping Model set</a> jar for the <a href="http://www.unece.org/trade/untdid/down_index.htm">d03b.zip definitions directory zip file</a>.</p>
<pre class="brush: java; wrap-lines: false; toolbar: false">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.milyn.edi.unedifact&lt;/groupId&gt;
    &lt;artifactId&gt;d03b-mapping&lt;/artifactId&gt;
    &lt;version&gt;1.0.SNAPSHOT&lt;/version&gt;
    &lt;name&gt;UN/EDIFACT - D03B - Mapping Model&lt;/name&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.milyn&lt;/groupId&gt;
                &lt;artifactId&gt;maven-ect-plugin&lt;/artifactId&gt;
		        &lt;version&gt;1.4-SNAPSHOT&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;src&gt;d03b.zip&lt;/src&gt;
                    &lt;srcType&gt;UNEDIFACT&lt;/srcType&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;goals&gt;&lt;goal&gt;generate&lt;/goal&gt;
                    &lt;/goals&gt;&lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;</pre>
<p>Of course when we execute this step, we will perform &#8220;<em>mvn clean deploy</em>&#8221; and install the jar file in the <a href="http://smooks.org/mediawiki/index.php?title=Maven_%26_Ant">public maven repository</a>, making it publicly available and thereby removing the need for you to perform this step.  A peek into the <a href="http://snapshots.repository.codehaus.org/org/milyn/edi/unedifact/">public SNAPSHOT repo</a> shows that we&#8217;ve started this process.</p>
<h2>Using a Mapping Model Set on the UN/EDIFACT Reader</h2>
<p>Using a generated <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI       Mapping Model set</a> jar in your application (via Smooks) is trivial.  You need to:</p>
<ol>
<li>Add the generated <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI        Mapping Model set</a> jar to your classpath (e.g. using a maven dependency configuration).</li>
<li>Add the <em>&lt;unedifact:reader&gt;</em> configuration to your Smooks configuration, using the generated jar&#8217;s maven <em>groupId</em>, <em>artifactId</em> and <em>version</em> to reference that particular mapping model set through a <em>URN</em> (see below).</li>
</ol>
<p>An example of the <em>&lt;unedifact:reader&gt;</em> configuration for the above generated <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI         Mapping Model set</a> jar for the <a href="http://www.unece.org/trade/untdid/down_index.htm">d03b.zip  definitions directory zip file</a> would be as follows:</p>
<pre class="brush: java; wrap-lines: false; toolbar: false">&lt;unedifact:reader
     mappingModel="urn:org.milyn.edi.unedifact:d03b-mapping:1.0-SNAPSHOT" /&gt;</pre>
<p>Using the &#8220;<em>urn:&lt;groupId&gt;:&lt;artifactId&gt;:&lt;version&gt;</em>&#8221; URN pattern, Smooks can find the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI          Mapping Model set</a> jar file on the classpath,</p>
<p>And that&#8217;s about it.  The associated Smooks instance will accept a UN/EDIFACT message interchange containing one or more messages defined in the <a href="http://www.unece.org/trade/untdid/down_index.htm">d03b.zip   definitions </a><a href="http://www.unece.org/trade/untdid/down_index.htm">directory </a><a href="http://www.unece.org/trade/untdid/down_index.htm">zip file</a>, and will generate a stream of SAX events into Smooks, which can be processed using all the other Smooks capabilities (Java Binding, Validation, Templating etc).  As stated earlier, the improvements we are working on for EJC will result in us being able to generate Java Object models from an <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI           Mapping Model set</a> jar.</p>
<h2>Play with the Examples&#8230; and please give feedback</h2>
<p>Check out and build the Smooks examples from https://svn.codehaus.org/milyn/trunk/smooks-examples (<em>mvn install</em>).</p>
<p>Change into the &#8220;ect-unedifact&#8221; directory after building all the examples (must build them all so as to install the poms) and run the example using <em>mvn exec:java. </em>The ect-unedifact example depends on the <em>&#8220;</em>d03b-mapping&#8221; module which has been pre-built and is available in the <a href="http://snapshots.repository.codehaus.org/org/milyn/edi/unedifact/">public SNAPSHOT repo</a>.</p>
<p>And guys&#8230;. PLEASE let us know how you get on with this stuff&#8230; things you like and dislike etc !!!</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/smooks/edi/">EDI</a> by Tom Fennelly <a href="http://blog.smooks.org/2010/06/28/processing-unedifact-message-interchanges/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>Bård (Langöy) and I have been doing lots of work on improving our EDI support, specifically in the area of handling <a href="http://www.unece.org/trade/untdid/welcome.htm">UN/EDIFACT</a> (<a href="http://en.wikipedia.org/wiki/UN/EDIFACT">WikipediA</a>) message <a href="http://www.unece.org/trade/edifact/untdid/d300_s.htm#i">Interchanges</a>.  The following improvements are now in the Smooks v1.4 codebase (available from the 1.4-SNAPSHOT):</p>
<ol>
<li><strong>A new <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#UN.2FEDIFACT">UN/EDIFACT Interchange Reader</a></strong> (<em>&lt;unedifact:reader&gt;</em>), which allows Smooks to process UN/EFIFACT message Interchanges (one or more UN/EDIFACT messages wrapped in the UN/EDIFACT Interchange control segments).</li>
<li>A new <strong>Maven/Ant</strong> tool called the &#8220;<strong>EDI Conversion Tool</strong>&#8221; (<strong>ECT</strong>) that can take the <a href="http://www.unece.org/trade/untdid/down_index.htm">official UN/EDIFACT message definition directory zip files</a> and from them, generate a jar file containing a <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">set of equivalent Smooks EDI Mapping Models</a>.  This tool is very easy to configure and use, but we are in the processing of pre-generating a library of these jars for all the UN/EDIFACT message sets and making them available trough the central maven repository, making it even easier to consume UN/EDIFACT messages with Smooks.  We hope to add support for other formats (X.12, HL7 etc) in future releases of Smooks.</li>
</ol>
<p>So that&#8217;s what&#8217;s available in the current 1.4 codebase (1.4-SNAPSHOT), but we&#8217;re also in the process of making more additions to this for Smooks v1.4.  We&#8217;re extending the <strong>EDI Java Compiler</strong> (<strong>EJC</strong>) tools to support Java Model generation from the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI Mapping Model sets</a> generated by the ECT tool (mentioned above).  We&#8217;ll also make these available as pre-built jar files from the Maven repository.</p>
<p>Another improvement we&#8217;ve made in the 1.4 codebase is the addition of support for <strong>Java Model to EDI serialization</strong> on the EJC generated Java object models, meaning we&#8217;ll be able to do full round trip binding of an EDI (and UN/EDIFACT) message to a populated Java Object model that can be modified and then serialize back out to an EDI stream.  Or, your app can &#8220;manually&#8221; construct the same Java Object models and serialize them out to an EDI Stream (think JAXB).</p>
<h2>Generating a Mapping Model Set using ECT</h2>
<p>The easiest way to consume UN/EDIFACT messages using Smooks is to use ECT (EDI Conversion Tool) to generate the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI  Mapping Model set</a> (if we haven&#8217;t already pre-built them).  The steps are really easy:</p>
<ol>
<li>Download the official UN/EDIFACT message definitions directory zip file you are interested in <a href="http://www.unece.org/trade/untdid/down_index.htm">from the UNECE website</a>.</li>
<li>Create a maven module for the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI   Mapping Model set</a> to be generated and add the <em>maven-ect-plugin</em> (see below) to it&#8217;s POM.</li>
<li>Execute &#8220;<em>mvn clean install</em>&#8221; in your maven module and  the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI     Mapping Model set</a> jar file will be generated as normal in the  modules <em>target</em> folder, and installed into your local maven  repository.</li>
</ol>
<p>The following is an example of the maven module POM for generating the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI      Mapping Model set</a> jar for the <a href="http://www.unece.org/trade/untdid/down_index.htm">d03b.zip definitions directory zip file</a>.</p>
<pre class="brush: java; wrap-lines: false; toolbar: false">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;

    &lt;groupId&gt;org.milyn.edi.unedifact&lt;/groupId&gt;
    &lt;artifactId&gt;d03b-mapping&lt;/artifactId&gt;
    &lt;version&gt;1.0.SNAPSHOT&lt;/version&gt;
    &lt;name&gt;UN/EDIFACT - D03B - Mapping Model&lt;/name&gt;

    &lt;build&gt;
        &lt;plugins&gt;
            &lt;plugin&gt;
                &lt;groupId&gt;org.milyn&lt;/groupId&gt;
                &lt;artifactId&gt;maven-ect-plugin&lt;/artifactId&gt;
		        &lt;version&gt;1.4-SNAPSHOT&lt;/version&gt;
                &lt;configuration&gt;
                    &lt;src&gt;d03b.zip&lt;/src&gt;
                    &lt;srcType&gt;UNEDIFACT&lt;/srcType&gt;
                &lt;/configuration&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;goals&gt;&lt;goal&gt;generate&lt;/goal&gt;
                    &lt;/goals&gt;&lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
        &lt;/plugins&gt;
    &lt;/build&gt;

&lt;/project&gt;</pre>
<p>Of course when we execute this step, we will perform &#8220;<em>mvn clean deploy</em>&#8221; and install the jar file in the <a href="http://smooks.org/mediawiki/index.php?title=Maven_%26_Ant">public maven repository</a>, making it publicly available and thereby removing the need for you to perform this step.  A peek into the <a href="http://snapshots.repository.codehaus.org/org/milyn/edi/unedifact/">public SNAPSHOT repo</a> shows that we&#8217;ve started this process.</p>
<h2>Using a Mapping Model Set on the UN/EDIFACT Reader</h2>
<p>Using a generated <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI       Mapping Model set</a> jar in your application (via Smooks) is trivial.  You need to:</p>
<ol>
<li>Add the generated <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI        Mapping Model set</a> jar to your classpath (e.g. using a maven dependency configuration).</li>
<li>Add the <em>&lt;unedifact:reader&gt;</em> configuration to your Smooks configuration, using the generated jar&#8217;s maven <em>groupId</em>, <em>artifactId</em> and <em>version</em> to reference that particular mapping model set through a <em>URN</em> (see below).</li>
</ol>
<p>An example of the <em>&lt;unedifact:reader&gt;</em> configuration for the above generated <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI         Mapping Model set</a> jar for the <a href="http://www.unece.org/trade/untdid/down_index.htm">d03b.zip  definitions directory zip file</a> would be as follows:</p>
<pre class="brush: java; wrap-lines: false; toolbar: false">&lt;unedifact:reader
     mappingModel="urn:org.milyn.edi.unedifact:d03b-mapping:1.0-SNAPSHOT" /&gt;</pre>
<p>Using the &#8220;<em>urn:&lt;groupId&gt;:&lt;artifactId&gt;:&lt;version&gt;</em>&#8221; URN pattern, Smooks can find the <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI          Mapping Model set</a> jar file on the classpath,</p>
<p>And that&#8217;s about it.  The associated Smooks instance will accept a UN/EDIFACT message interchange containing one or more messages defined in the <a href="http://www.unece.org/trade/untdid/down_index.htm">d03b.zip   definitions </a><a href="http://www.unece.org/trade/untdid/down_index.htm">directory </a><a href="http://www.unece.org/trade/untdid/down_index.htm">zip file</a>, and will generate a stream of SAX events into Smooks, which can be processed using all the other Smooks capabilities (Java Binding, Validation, Templating etc).  As stated earlier, the improvements we are working on for EJC will result in us being able to generate Java Object models from an <a href="http://smooks.org/mediawiki/index.php?title=V1.4:Smooks_v1.4_User_Guide#Mapping_Model_Zip_Sets">EDI           Mapping Model set</a> jar.</p>
<h2>Play with the Examples&#8230; and please give feedback</h2>
<p>Check out and build the Smooks examples from https://svn.codehaus.org/milyn/trunk/smooks-examples (<em>mvn install</em>).</p>
<p>Change into the &#8220;ect-unedifact&#8221; directory after building all the examples (must build them all so as to install the poms) and run the example using <em>mvn exec:java. </em>The ect-unedifact example depends on the <em>&#8220;</em>d03b-mapping&#8221; module which has been pre-built and is available in the <a href="http://snapshots.repository.codehaus.org/org/milyn/edi/unedifact/">public SNAPSHOT repo</a>.</p>
<p>And guys&#8230;. PLEASE let us know how you get on with this stuff&#8230; things you like and dislike etc !!!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2010/06/28/processing-unedifact-message-interchanges/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Introduction: Using a factory to instantiate beans with the javabean cartridge</title>
		<link>http://blog.smooks.org/2009/10/12/introduction-using-a-factory-to-instantiate-beans-with-the-javabean-cartridge/</link>
		<comments>http://blog.smooks.org/2009/10/12/introduction-using-a-factory-to-instantiate-beans-with-the-javabean-cartridge/#comments</comments>
		<pubDate>Mon, 12 Oct 2009 08:01:59 +0000</pubDate>
		<dc:creator>Maurice</dc:creator>
				<category><![CDATA[Java Binding]]></category>
		<category><![CDATA[Smooks]]></category>
		<category><![CDATA[Technical Info & Concepts]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=354</guid>
		<description><![CDATA[<p>I would like to introduce the new factory feature in the Javabean cartridge of the up coming Smooks 1.3 release. This new features makes it possible to use a static factory method or a factory object to instantiate objects with the Javabean cartridge.</p>
<p><span id="more-354"></span></p>
<p>Before this new feature you could only instantiate new beans by providing the class name of the bean and the bean would then be created using it&#8217;s public parameterless constructor. So it was mandatory to have a parameterless constructor on the Javabean. Here is an example:</p>
<pre class="brush: xml;">
&lt;jb:bean
   beanId=&quot;orders&quot;
   class=&quot;java.util.ArrayList&quot;
   createOnElement=&quot;orders&quot;
&gt;
     &lt;!-- ... bindings --&gt;
&lt;/jb:bean&gt;
</pre>
<p>But now it is possible to use a factory to instantiate the bean. So you don&#8217;t need a public parameterless constructur anymore. You even don&#8217;t have to define the actual class name any more. Any of the interfaces that the class implements suffices. However only the methods of that interface are available for binding then. Here is an example where we instantiate an ArrayList object using a static factory method:</p>
<pre class="brush: xml;">
&lt;jb:bean
   beanId=&quot;orders&quot;
   class=&quot;java.util.List&quot;
   factory=&quot;some.package.ListFactory#newList&quot;
   createOnElement=&quot;orders&quot;
&gt;
     &lt;!-- ... bindings --&gt;
&lt;/jb:bean&gt;
</pre>
<p>With the factory attribute you set the factory definition. The class attributes defines that a List object is returned. What kind of List object (ArrayList, LinkedList) is up to the ListFactory to decide. Here is another example:</p>
<pre class="brush: xml;">
&lt;jb:bean
   beanId=&quot;orders&quot;
   class=&quot;java.util.List&quot;
   factory=&quot;some.package.ListFactory#getInstance.newList&quot;
   createOnElement=&quot;orders&quot;
&gt;
     &lt;!-- ... bindings --&gt;
&lt;/jb:bean&gt;
</pre>
<p>Here we define that an instance of the ListFactory needs to be retrieved using the static method getInstance() and that then the newList() method needs to be called on the ListFactory object to create the List object. This construct makes it possible to easily call Singleton Factories.</p>
<p>The default factory definition language looks like this: <strong>factory.class#static_method<em>{.instance_method}</em></strong></p>
<p>It is also possible to use MVEL as the factory definition language. MVEL has some advantages over the basic default definition language, for example you can use objects from the bean context as the factory object or  you can call factory methods with parameters. These parameters can be defined within the definition or they can be objects from the bean context. To be able to use MVEL you must set the &#8216;factory.definition.parser.class&#8217; global parameter to &#8216;org.milyn.javabean.factory.MVELFactoryDefinitionParser&#8217;.</p>
<p>Here is an example using MVEL:</p>
<pre class="brush: xml;">
&lt;smooks-resource-list
   xmlns=&quot;http://www.milyn.org/xsd/smooks-1.1.xsd&quot;
   xmlns:jb=&quot;http://www.milyn.org/xsd/smooks/javabean-1.3.xsd&quot;&gt;

	&lt;params&gt;
		&lt;param name=&quot;factory.definition.parser.class&quot;&gt;
			org.milyn.javabean.factory.MVELFactoryDefinitionParser
		&lt;/param&gt;
	&lt;/params&gt;

    &lt;jb:bean
		beanId=&quot;orders&quot;
		class=&quot;java.util.List&quot;
		factory=&quot;some.package.ListFactory.getInstance().newList()&quot;
		createOnElement=&quot;orders&quot;
	&gt;
		&lt;!-- ... bindings --&gt;
    &lt;/jb:bean&gt;

&lt;/smooks-resource-list&gt;
</pre>
<p>Maybe you wonder why we don&#8217;t use MVEL as the default factory definition language? Currently the performance of the basic definition language and MVEL is about equal. The reason that the basic definition language isn&#8217;t faster is because it currently uses reflection to call the factory methods. However there are plans to use byte code generation instead of reflection. This should improve the performance dramatically. If MVEL where the default language then we couldn&#8217;t do anything to improve the performance for those people who don&#8217;t need any thing more then the basic features that the basic definition language offers.</p>
<p>If you have any questions or remarks then I would gladly hear them.</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/java-binding/">Java Binding</a> by Maurice <a href="http://blog.smooks.org/2009/10/12/introduction-using-a-factory-to-instantiate-beans-with-the-javabean-cartridge/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>I would like to introduce the new factory feature in the Javabean cartridge of the up coming Smooks 1.3 release. This new features makes it possible to use a static factory method or a factory object to instantiate objects with the Javabean cartridge.</p>
<p><span id="more-354"></span></p>
<p>Before this new feature you could only instantiate new beans by providing the class name of the bean and the bean would then be created using it&#8217;s public parameterless constructor. So it was mandatory to have a parameterless constructor on the Javabean. Here is an example:</p>
<pre class="brush: xml;">
&lt;jb:bean
   beanId=&quot;orders&quot;
   class=&quot;java.util.ArrayList&quot;
   createOnElement=&quot;orders&quot;
&gt;
     &lt;!-- ... bindings --&gt;
&lt;/jb:bean&gt;
</pre>
<p>But now it is possible to use a factory to instantiate the bean. So you don&#8217;t need a public parameterless constructur anymore. You even don&#8217;t have to define the actual class name any more. Any of the interfaces that the class implements suffices. However only the methods of that interface are available for binding then. Here is an example where we instantiate an ArrayList object using a static factory method:</p>
<pre class="brush: xml;">
&lt;jb:bean
   beanId=&quot;orders&quot;
   class=&quot;java.util.List&quot;
   factory=&quot;some.package.ListFactory#newList&quot;
   createOnElement=&quot;orders&quot;
&gt;
     &lt;!-- ... bindings --&gt;
&lt;/jb:bean&gt;
</pre>
<p>With the factory attribute you set the factory definition. The class attributes defines that a List object is returned. What kind of List object (ArrayList, LinkedList) is up to the ListFactory to decide. Here is another example:</p>
<pre class="brush: xml;">
&lt;jb:bean
   beanId=&quot;orders&quot;
   class=&quot;java.util.List&quot;
   factory=&quot;some.package.ListFactory#getInstance.newList&quot;
   createOnElement=&quot;orders&quot;
&gt;
     &lt;!-- ... bindings --&gt;
&lt;/jb:bean&gt;
</pre>
<p>Here we define that an instance of the ListFactory needs to be retrieved using the static method getInstance() and that then the newList() method needs to be called on the ListFactory object to create the List object. This construct makes it possible to easily call Singleton Factories.</p>
<p>The default factory definition language looks like this: <strong>factory.class#static_method<em>{.instance_method}</em></strong></p>
<p>It is also possible to use MVEL as the factory definition language. MVEL has some advantages over the basic default definition language, for example you can use objects from the bean context as the factory object or  you can call factory methods with parameters. These parameters can be defined within the definition or they can be objects from the bean context. To be able to use MVEL you must set the &#8216;factory.definition.parser.class&#8217; global parameter to &#8216;org.milyn.javabean.factory.MVELFactoryDefinitionParser&#8217;.</p>
<p>Here is an example using MVEL:</p>
<pre class="brush: xml;">
&lt;smooks-resource-list
   xmlns=&quot;http://www.milyn.org/xsd/smooks-1.1.xsd&quot;
   xmlns:jb=&quot;http://www.milyn.org/xsd/smooks/javabean-1.3.xsd&quot;&gt;

	&lt;params&gt;
		&lt;param name=&quot;factory.definition.parser.class&quot;&gt;
			org.milyn.javabean.factory.MVELFactoryDefinitionParser
		&lt;/param&gt;
	&lt;/params&gt;

    &lt;jb:bean
		beanId=&quot;orders&quot;
		class=&quot;java.util.List&quot;
		factory=&quot;some.package.ListFactory.getInstance().newList()&quot;
		createOnElement=&quot;orders&quot;
	&gt;
		&lt;!-- ... bindings --&gt;
    &lt;/jb:bean&gt;

&lt;/smooks-resource-list&gt;
</pre>
<p>Maybe you wonder why we don&#8217;t use MVEL as the default factory definition language? Currently the performance of the basic definition language and MVEL is about equal. The reason that the basic definition language isn&#8217;t faster is because it currently uses reflection to call the factory methods. However there are plans to use byte code generation instead of reflection. This should improve the performance dramatically. If MVEL where the default language then we couldn&#8217;t do anything to improve the performance for those people who don&#8217;t need any thing more then the basic features that the basic definition language offers.</p>
<p>If you have any questions or remarks then I would gladly hear them.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2009/10/12/introduction-using-a-factory-to-instantiate-beans-with-the-javabean-cartridge/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Data Validation with Smooks v1.2</title>
		<link>http://blog.smooks.org/2009/05/21/data-validation-with-smooks-v12/</link>
		<comments>http://blog.smooks.org/2009/05/21/data-validation-with-smooks-v12/#comments</comments>
		<pubDate>Thu, 21 May 2009 13:43:18 +0000</pubDate>
		<dc:creator>Daniel Bevenius</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Smooks]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=167</guid>
		<description><![CDATA[<p><a href="http://www.smooks.org">Smooks</a> 1.2 adds support for message data Validation as one of its new features. This new feature allows you to perform strong field and fragment <strong>validation</strong> on not just XML data, but also on EDI,  JSON, CSV etc.  It currently supports <strong>Regex</strong> and <a href="http://mvel.codehaus.org"><strong>MVEL</strong></a> rules bases (<strong>Drools</strong> to follow).  <strong>Regex</strong> rules allow you to perform low level field format validation, while <strong>MVEL</strong> rules allow you to perform Business rules validation at a fragment/message level.</p>
<p><span id="more-167"></span>This is not intended as an alternative to Schema based validation such as XSD or Relax.  It provides additional layers of validation in areas not covered by these mechanisms.</p>
<p>Examples of validations one might perform on an Order message containing a collection of Order Item:</p>
<ol> 1. Use a Regex rule to validate that an orderId field follows a predefined format of  &#8220;Uppercase Char, followed by 5 digits&#8221; e.g. &#8220;W12345&#8243; or &#8220;U54321&#8243;.</ol>
<ol> 2. Use an MVEL rule to enforce a business rule by validating that an order item total is not greater than 50.00 i.e. &#8220;price * quantity &lt; 50.00&#8243;.  Of course these can be more complex.</ol>
<p>The easiest way to get familiar with the validation features is to look at an example, so here goes.</p>
<p>For the following example assume that we have the xml that looks like this:</p>
<pre class="brush: xml;">&lt;Order&gt;
    &lt;header&gt;
        &lt;orderId&gt;A188127&lt;/orderId&gt;
        &lt;username&gt;user1&lt;/username&gt;
        &lt;name&gt;
            &lt;firstname&gt;Harry&lt;/firstname&gt;
            &lt;lastname&gt;Fletcher&lt;/lastname&gt;
        &lt;/name&gt;
        &lt;email&gt;harry.fletcher@gmail.&lt;/email&gt;
        &lt;state&gt;South Dakota&lt;/state&gt;
    &lt;/header&gt;
&lt;/Order&gt;</pre>
<p>An example of a validation configuration could look like this:</p>
<pre class="brush: xml;">&lt;?xml version="1.0"?&gt;
&lt;smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                   xmlns:rules="http://www.milyn.org/xsd/smooks/rules-1.0.xsd"
                   xmlns:validation="http://www.milyn.org/xsd/smooks/validation-1.0.xsd"&gt;

   &lt;!-- ruleBases used by validation rules... --&gt;
   &lt;rules:ruleBases&gt;
      &lt;rules:ruleBase name="customer"
                    src="rules/customer.properties"
                    provider="org.milyn.rules.regex.RegexProvider"/&gt;
   &lt;/rules:ruleBases&gt;

   &lt;!-- email validation rule targeted at the email element in the header... --&gt;
   &lt;validation:rule executeOn="Order/header/email" name="customer.email" onFail="WARN"/&gt;

&lt;/smooks-resource-list&gt;</pre>
<p>The &#8216;<strong>exeuteOn</strong>&#8216; attribte specifies the fragment or attribute that you want the validation rule to be applied to.  In the case of a Regex rule, it specifies a specific field of data (attribute or element text), while in the case of an MVEL rule, it simply identifies the fragment that triggers execution of that rule.</p>
<p>The next thing to notice is the &#8216;<strong>name</strong>&#8216; attribute. This attribute refers to a rule in one of the configured ruleBase.  In the case of the example above, we refer to the &#8216;<strong>email</strong>&#8216; rule in the &#8216;<strong>customer</strong>&#8216; ruleBase i.e. &#8216;<strong>customer.email</strong>&#8216;. You can define as many ruleBase elements and validation element as you need, but we are only using one of each here to keep this easy to read.</p>
<p>The &#8216;customer&#8217; ruleBase is handled by the RegexProvider, as defined in the &#8216;provider&#8217; attribute on the ruleBase configuration.  We can also see that the &#8216;customer&#8217; ruleBase rules are located in a properties file named &#8216;<strong>rules/customer.properties</strong>&#8216;:</p>
<pre class="brush: xml;"># Email address...
email=^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$</pre>
<p>Next, lets take a look at how we execute Smooks to capture some validation results in a <a href="http://www.milyn.org/javadoc/v1.2/smooks-cartridges/validation/org/milyn/validation/ValidationResult.html">ValidationResult</a> instance:</p>
<pre class="brush: java;">final Smooks smooks = new Smooks("smooks-config.xml");
try
{
    // Create an exec context - no profiles....
    final ExecutionContext executionContext = smooks.createExecutionContext();
    final ValidationResult validationResult = new ValidationResult();

    // Filter the input message...
    smooks.filterSource(executionContext, new StringSource(messageIn), validationResult);

    for (OnFailResult result : results.getWarnings())
    {
        System.out.println("\t" + result.getMessage());
    }
}
finally
{
    smooks.close();
}</pre>
<p>As you can see, Validation faiures can be reported at different levels of severity.  This is controlled by setting &#8216;<strong>onFail</strong>&#8216; to one of &#8216;<strong>OK</strong>&#8216;, &#8216;<strong>WARN</strong>&#8216;, &#8216;<strong>ERROR</strong>&#8216;, or &#8216;<strong>FATAL</strong>&#8216;. Setting onFail to &#8216;FATAL&#8217; will cause an exception to be thrown and processing to halt.  The other three levels will not generate an exception.  Failures at these lower severity levels can be accessed through the <a href="http://www.milyn.org/javadoc/v1.2/smooks-cartridges/validation/org/milyn/validation/ValidationResult.html">ValidationResult</a> instance supplied to the Smooks.filterSource method.  An example of getting WARN level failures is shown above with the <strong>result.getWarnings()</strong> call.</p>
<p>Having usable validation reports is important and Smooks validation supports <strong>localized messages for validation failures</strong>. These messages can be defined in standard Java ResourceBundle files (.properties format). A convention is used here, based on the rule source name (&#8221;<strong>src</strong>&#8220;). The validation message bundle base name is derived from the rule source (&#8221;<strong>src</strong>&#8220;) by dropping the rule source file extension and appending a &#8220;<strong>_messages</strong>&#8220;.</p>
<p>So for our example we could have a file named &#8216;<strong>customer_messages.properties</strong>&#8216; containing our default locale results:</p>
<pre class="brush: xml;">email=ftl:Invalid email address '${ruleResult.text}' at '${path}'.</pre>
<p>So calling <strong>result.getMessage()</strong> for a validation failure on &#8216;customer.email&#8217; rule will result in the following message:</p>
<p class="brush: xml;" style="padding-left: 30px;"><span style="color: #000000;"><em>Invalid email address &#8216;harry.fletcher@gmail.&#8217; at &#8216;/Order/header/email&#8217;.</em></span></p>
<p>And we could also add a message bundle file for Swedish users.  Following the ResourceBundle naming convention, it should be named &#8216;<strong>customer_messages_sv_SE.properties</strong>&#8216; and would contain the Sweish localized messages:</p>
<pre class="brush: xml;">email=ftl:Felaktig epost adress '${ruleResult.text}' i '${path}'.</pre>
<p>Calling <strong>result.getMessage(new Locale(&#8221;sv, &#8220;SE&#8221;))</strong> will produce:</p>
<p class="brush: xml;" style="padding-left: 30px;"><span style="color: #000000;"><em>Felaktig epost adress &#8216;harry.fletcher@gmail.&#8217; i &#8216;/Order/header/email&#8217;.</em></span></p>
<p>We have only shown a portion of the validation features here.  More information can be found in the <a href="http://www.smooks.org/mediawiki/index.php?title=V1.2:Smooks_v1.2_User_Guide">Smooks User Guide</a>.  An example of the validation features can be found in <a href="http://www.smooks.org/mediawiki/index.php?title=V1.2:validation-basic">Validation Example</a>.</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/blog/">Blog</a> by Daniel Bevenius <a href="http://blog.smooks.org/2009/05/21/data-validation-with-smooks-v12/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.smooks.org">Smooks</a> 1.2 adds support for message data Validation as one of its new features. This new feature allows you to perform strong field and fragment <strong>validation</strong> on not just XML data, but also on EDI,  JSON, CSV etc.  It currently supports <strong>Regex</strong> and <a href="http://mvel.codehaus.org"><strong>MVEL</strong></a> rules bases (<strong>Drools</strong> to follow).  <strong>Regex</strong> rules allow you to perform low level field format validation, while <strong>MVEL</strong> rules allow you to perform Business rules validation at a fragment/message level.</p>
<p><span id="more-167"></span>This is not intended as an alternative to Schema based validation such as XSD or Relax.  It provides additional layers of validation in areas not covered by these mechanisms.</p>
<p>Examples of validations one might perform on an Order message containing a collection of Order Item:</p>
<ol> 1. Use a Regex rule to validate that an orderId field follows a predefined format of  &#8220;Uppercase Char, followed by 5 digits&#8221; e.g. &#8220;W12345&#8243; or &#8220;U54321&#8243;.</ol>
<ol> 2. Use an MVEL rule to enforce a business rule by validating that an order item total is not greater than 50.00 i.e. &#8220;price * quantity &lt; 50.00&#8243;.  Of course these can be more complex.</ol>
<p>The easiest way to get familiar with the validation features is to look at an example, so here goes.</p>
<p>For the following example assume that we have the xml that looks like this:</p>
<pre class="brush: xml;">&lt;Order&gt;
    &lt;header&gt;
        &lt;orderId&gt;A188127&lt;/orderId&gt;
        &lt;username&gt;user1&lt;/username&gt;
        &lt;name&gt;
            &lt;firstname&gt;Harry&lt;/firstname&gt;
            &lt;lastname&gt;Fletcher&lt;/lastname&gt;
        &lt;/name&gt;
        &lt;email&gt;harry.fletcher@gmail.&lt;/email&gt;
        &lt;state&gt;South Dakota&lt;/state&gt;
    &lt;/header&gt;
&lt;/Order&gt;</pre>
<p>An example of a validation configuration could look like this:</p>
<pre class="brush: xml;">&lt;?xml version="1.0"?&gt;
&lt;smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
                   xmlns:rules="http://www.milyn.org/xsd/smooks/rules-1.0.xsd"
                   xmlns:validation="http://www.milyn.org/xsd/smooks/validation-1.0.xsd"&gt;

   &lt;!-- ruleBases used by validation rules... --&gt;
   &lt;rules:ruleBases&gt;
      &lt;rules:ruleBase name="customer"
                    src="rules/customer.properties"
                    provider="org.milyn.rules.regex.RegexProvider"/&gt;
   &lt;/rules:ruleBases&gt;

   &lt;!-- email validation rule targeted at the email element in the header... --&gt;
   &lt;validation:rule executeOn="Order/header/email" name="customer.email" onFail="WARN"/&gt;

&lt;/smooks-resource-list&gt;</pre>
<p>The &#8216;<strong>exeuteOn</strong>&#8216; attribte specifies the fragment or attribute that you want the validation rule to be applied to.  In the case of a Regex rule, it specifies a specific field of data (attribute or element text), while in the case of an MVEL rule, it simply identifies the fragment that triggers execution of that rule.</p>
<p>The next thing to notice is the &#8216;<strong>name</strong>&#8216; attribute. This attribute refers to a rule in one of the configured ruleBase.  In the case of the example above, we refer to the &#8216;<strong>email</strong>&#8216; rule in the &#8216;<strong>customer</strong>&#8216; ruleBase i.e. &#8216;<strong>customer.email</strong>&#8216;. You can define as many ruleBase elements and validation element as you need, but we are only using one of each here to keep this easy to read.</p>
<p>The &#8216;customer&#8217; ruleBase is handled by the RegexProvider, as defined in the &#8216;provider&#8217; attribute on the ruleBase configuration.  We can also see that the &#8216;customer&#8217; ruleBase rules are located in a properties file named &#8216;<strong>rules/customer.properties</strong>&#8216;:</p>
<pre class="brush: xml;"># Email address...
email=^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$</pre>
<p>Next, lets take a look at how we execute Smooks to capture some validation results in a <a href="http://www.milyn.org/javadoc/v1.2/smooks-cartridges/validation/org/milyn/validation/ValidationResult.html">ValidationResult</a> instance:</p>
<pre class="brush: java;">final Smooks smooks = new Smooks("smooks-config.xml");
try
{
    // Create an exec context - no profiles....
    final ExecutionContext executionContext = smooks.createExecutionContext();
    final ValidationResult validationResult = new ValidationResult();

    // Filter the input message...
    smooks.filterSource(executionContext, new StringSource(messageIn), validationResult);

    for (OnFailResult result : results.getWarnings())
    {
        System.out.println("\t" + result.getMessage());
    }
}
finally
{
    smooks.close();
}</pre>
<p>As you can see, Validation faiures can be reported at different levels of severity.  This is controlled by setting &#8216;<strong>onFail</strong>&#8216; to one of &#8216;<strong>OK</strong>&#8216;, &#8216;<strong>WARN</strong>&#8216;, &#8216;<strong>ERROR</strong>&#8216;, or &#8216;<strong>FATAL</strong>&#8216;. Setting onFail to &#8216;FATAL&#8217; will cause an exception to be thrown and processing to halt.  The other three levels will not generate an exception.  Failures at these lower severity levels can be accessed through the <a href="http://www.milyn.org/javadoc/v1.2/smooks-cartridges/validation/org/milyn/validation/ValidationResult.html">ValidationResult</a> instance supplied to the Smooks.filterSource method.  An example of getting WARN level failures is shown above with the <strong>result.getWarnings()</strong> call.</p>
<p>Having usable validation reports is important and Smooks validation supports <strong>localized messages for validation failures</strong>. These messages can be defined in standard Java ResourceBundle files (.properties format). A convention is used here, based on the rule source name (&#8221;<strong>src</strong>&#8220;). The validation message bundle base name is derived from the rule source (&#8221;<strong>src</strong>&#8220;) by dropping the rule source file extension and appending a &#8220;<strong>_messages</strong>&#8220;.</p>
<p>So for our example we could have a file named &#8216;<strong>customer_messages.properties</strong>&#8216; containing our default locale results:</p>
<pre class="brush: xml;">email=ftl:Invalid email address '${ruleResult.text}' at '${path}'.</pre>
<p>So calling <strong>result.getMessage()</strong> for a validation failure on &#8216;customer.email&#8217; rule will result in the following message:</p>
<p class="brush: xml;" style="padding-left: 30px;"><span style="color: #000000;"><em>Invalid email address &#8216;harry.fletcher@gmail.&#8217; at &#8216;/Order/header/email&#8217;.</em></span></p>
<p>And we could also add a message bundle file for Swedish users.  Following the ResourceBundle naming convention, it should be named &#8216;<strong>customer_messages_sv_SE.properties</strong>&#8216; and would contain the Sweish localized messages:</p>
<pre class="brush: xml;">email=ftl:Felaktig epost adress '${ruleResult.text}' i '${path}'.</pre>
<p>Calling <strong>result.getMessage(new Locale(&#8221;sv, &#8220;SE&#8221;))</strong> will produce:</p>
<p class="brush: xml;" style="padding-left: 30px;"><span style="color: #000000;"><em>Felaktig epost adress &#8216;harry.fletcher@gmail.&#8217; i &#8216;/Order/header/email&#8217;.</em></span></p>
<p>We have only shown a portion of the validation features here.  More information can be found in the <a href="http://www.smooks.org/mediawiki/index.php?title=V1.2:Smooks_v1.2_User_Guide">Smooks User Guide</a>.  An example of the validation features can be found in <a href="http://www.smooks.org/mediawiki/index.php?title=V1.2:validation-basic">Validation Example</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2009/05/21/data-validation-with-smooks-v12/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Smooks Persistence part 2: How to use Hibernate/JPA</title>
		<link>http://blog.smooks.org/2009/03/25/smooks-persistence-part-2-how-to-use-hibernatejpa/</link>
		<comments>http://blog.smooks.org/2009/03/25/smooks-persistence-part-2-how-to-use-hibernatejpa/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 11:20:09 +0000</pubDate>
		<dc:creator>Maurice</dc:creator>
				<category><![CDATA[Java Binding]]></category>
		<category><![CDATA[Persistence]]></category>
		<category><![CDATA[Scribe]]></category>
		<category><![CDATA[Smooks]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=113</guid>
		<description><![CDATA[<p>With the new Smooks Persistence cartridge in Smooks 1.2 you can directly use several entity persistence frameworks from within Smooks. In this post I will show you how this works with Hibernate and any other JPA compatible framework.</p>
<p><span id="more-113"></span></p>
<p>This post is part of a multi post article. You can find the previous post here: <a title="Smooks Persistence part 1: The Introduction" href="http://blog.smooks.org/2009/02/26/smooks-persistence-part-1-the-introduction/">Smooks Persistence part 1: The Introduction</a></p>
<p>In this post we will look at an example of how to persist Hibernate Entities. This example is based on the example of the previous post.</p>
<p>First, lets take a look at the data we are going to process. In this example we are processing XML data.  It should be noted however, that the input data could also be <strong>CSV</strong>, <strong>JSON</strong>, <strong>EDI</strong>, <strong>Java</strong> or any other structured/hierarchical data format.  The same principals apply, no matter what the data format is!</p>
<pre class="brush: xml;">&lt;order&gt;
    &lt;ordernumber&gt;1&lt;/ordernumber&gt;
    &lt;customer&gt;123456&lt;/customer&gt;
    &lt;order-items&gt;
        &lt;order-item&gt;
            &lt;product&gt;11&lt;/product&gt;
            &lt;quantity&gt;2&lt;/quantity&gt;
        &lt;/order-item&gt;
        &lt;order-item&gt;
            &lt;product&gt;22&lt;/product&gt;
            &lt;quantity&gt;7&lt;/quantity&gt;
        &lt;/order-item&gt;
    &lt;/order-items&gt;
&lt;/order&gt;</pre>
<p>These are the Hibernate entities we are going to use:</p>
<pre class="brush: java;">@Entity
@Table(name="orders")
public class Order {

	@Id
	private Integer ordernumber;

	@Basic
	private String customerId;

	@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
	private List orderItems = new ArrayList();

	public void addOrderLine(OrderLine orderLine) {
		orderItems.add(orderLine);
	}

       // Getters and Setters....
}</pre>
<pre class="brush: java;">@Entity
@Table(name="orderlines")
public class OrderLine {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;

	@ManyToOne
	@JoinColumn(name="orderid")
	private Order order;

	@Basic
	private Integer quantity;

	@ManyToOne
	@JoinColumn(name="productid")
	private Product product;

       // Getters and Setters....
}</pre>
<pre class="brush: java;">@Entity
@Table(name = "products")
@NamedQuery(name="product.byId", query="from Product p where p.id = :id")
public class Product {

	@Id
	private Integer id;

	@Basic
	private String name;

       // Getters and Setters....
}</pre>
<p>The Smooks configuration looks like this:</p>
<pre class="brush: xml;">
&lt;!--
	In this example, we don't need to reference the Hibernate Session
	anywhere in the configuration. Later on, in the Smooks execution
	code (following snippet), you will see that we have set the
	Session as the default Session. This means that you don't need
	to reference it by name.  In the configuration of the previous
	post, we needed to reference every DAO by its name.
--&gt;
&lt;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" xmlns:dao="http://www.milyn.org/xsd/smooks/persistence-1.2.xsd"&gt;

	&lt;!--
		This is a normal Javabean binding. It creates the order bean and binds
		data into it.
	--&gt;
	&lt;jb:bindings beanId="order" class="example.entity.Order"
		createOnElement="order"&gt;

		&lt;jb:value property="ordernumber" data="ordernumber" /&gt;
		&lt;jb:value property="customerId" data="customer" /&gt;
		&lt;jb:wiring setterMethod="addOrderLine" beanIdRef="orderLine" /&gt;
	&lt;/jb:bindings&gt;

	&lt;!--
		This is a normal Javabean binding. Notice that we have a wiring on
		'product'. The Product entity will not be created, but looked up by a
		locator.
	--&gt;
	&lt;jb:bindings beanId="orderLine" class="example.entity.OrderLine"
		createOnElement="order-item"&gt;

		&lt;jb:value property="quantity" data="quantity" decoder="Integer" /&gt;
		&lt;jb:wiring property="order" beanIdRef="order" /&gt;
		&lt;jb:wiring property="product" beanIdRef="product" /&gt;
	&lt;/jb:bindings&gt;

	&lt;!--
		This locator calls (via Scribe) the SessionDaoAdapter#lookupByQuery()
		method. The result will be added to the bean repository under the bean
		id 'product'. An exception is thrown when no result is found.
	--&gt;
	&lt;dao:locator beanId="product" lookupOnElement="order-item"
		onNoResult="EXCEPTION" uniqueResult="true"&gt;

		&lt;dao:query&gt;from Product p where p.id = :id&lt;/dao:query&gt;
		&lt;dao:params&gt;
			&lt;dao:value name="id" data="product" decoder="Integer" /&gt;
		&lt;/dao:params&gt;
	&lt;/dao:locator&gt;

	&lt;!--
		The inserter calls (via Scribe) the SessionDaoAdapter#insert() method
		at the visitAfter of the Order element. The SessioDaoAdapter#insert()
		uses the Hibernate Session#save() method to persist the entity to the
		database.
	--&gt;
	&lt;dao:inserter beanId="order" insertOnElement="order" /&gt;

&lt;/smooks-resource-list&gt;</pre>
<p>If we want to use the named query &#8216;productById&#8217; instead of the query string then the DAO locator configuration will look like this:</p>
<pre class="brush: xml;">	&lt;!--
		The lookup parameter is the query name
	--&gt;
    &lt;dao:locator beanId="product" lookupOnElement="order-item" lookup="product.byId"
    				onNoResult="EXCEPTION" uniqueResult="true"&gt;
    	&lt;dao:params&gt;
    		&lt;dao:value name="id" data="product" decoder="Integer"/&gt;
    	&lt;/dao:params&gt;
    &lt;/dao:locator&gt;</pre>
<p>The following code executes Smooks. Note that we use a SessionRegister object so that we can access the Hibernate Session from within Smooks.</p>
<pre class="brush: java;">
        // Note: In a real world application you should cache the Smooks instance
        // else you will have enormous performance penalties.
        Smooks smooks = new Smooks("smooks-config.xml");

        ExecutionContext executionContext = smooks.createExecutionContext();

        // The SessionRegister provides the bridge between Hibernate and the
        // Persistence Cartridge. We provide it with the Hibernate session.
        // The Hibernate Session is set as default Session.
        DaoRegister register = new SessionRegister(session);

        // This sets the DAO Register in the executionContext for Smooks
        // to access it.
        PersistenceUtil.setDAORegister(executionContext, register);

        Transaction transaction = session.beginTransaction();

        Source source = new StreamSource(new File("order.xml"));        

        smooks.filter(source, null, executionContext);

        transaction.commit();</pre>
<p>If you want to take a JPA version of this example for a spin, simply check out the examples from subversion (http://svn.codehaus.org/milyn/trunk/smooks-examples/) and you’ll find it in the “dao-router” folder.  It also shows you how to use custom DOA&#8217;s with the Persistence Cartridge.</p>
<p>With Scribe and the Persistence Cartridge it is very easy to use Hibernate, JPA and most other persistence frameworks within Smooks. Later on in this series I will explain how too write your own DAO Adapters, so that you can add support for other persistence frameworks.</p>
<p>In the next post of the series I will explain how you can use Ibatis with the Persistence Cartridge.</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/java-binding/">Java Binding</a> by Maurice <a href="http://blog.smooks.org/2009/03/25/smooks-persistence-part-2-how-to-use-hibernatejpa/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>With the new Smooks Persistence cartridge in Smooks 1.2 you can directly use several entity persistence frameworks from within Smooks. In this post I will show you how this works with Hibernate and any other JPA compatible framework.</p>
<p><span id="more-113"></span></p>
<p>This post is part of a multi post article. You can find the previous post here: <a title="Smooks Persistence part 1: The Introduction" href="http://blog.smooks.org/2009/02/26/smooks-persistence-part-1-the-introduction/">Smooks Persistence part 1: The Introduction</a></p>
<p>In this post we will look at an example of how to persist Hibernate Entities. This example is based on the example of the previous post.</p>
<p>First, lets take a look at the data we are going to process. In this example we are processing XML data.  It should be noted however, that the input data could also be <strong>CSV</strong>, <strong>JSON</strong>, <strong>EDI</strong>, <strong>Java</strong> or any other structured/hierarchical data format.  The same principals apply, no matter what the data format is!</p>
<pre class="brush: xml;">&lt;order&gt;
    &lt;ordernumber&gt;1&lt;/ordernumber&gt;
    &lt;customer&gt;123456&lt;/customer&gt;
    &lt;order-items&gt;
        &lt;order-item&gt;
            &lt;product&gt;11&lt;/product&gt;
            &lt;quantity&gt;2&lt;/quantity&gt;
        &lt;/order-item&gt;
        &lt;order-item&gt;
            &lt;product&gt;22&lt;/product&gt;
            &lt;quantity&gt;7&lt;/quantity&gt;
        &lt;/order-item&gt;
    &lt;/order-items&gt;
&lt;/order&gt;</pre>
<p>These are the Hibernate entities we are going to use:</p>
<pre class="brush: java;">@Entity
@Table(name="orders")
public class Order {

	@Id
	private Integer ordernumber;

	@Basic
	private String customerId;

	@OneToMany(mappedBy = "order", cascade = CascadeType.ALL)
	private List orderItems = new ArrayList();

	public void addOrderLine(OrderLine orderLine) {
		orderItems.add(orderLine);
	}

       // Getters and Setters....
}</pre>
<pre class="brush: java;">@Entity
@Table(name="orderlines")
public class OrderLine {

	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;

	@ManyToOne
	@JoinColumn(name="orderid")
	private Order order;

	@Basic
	private Integer quantity;

	@ManyToOne
	@JoinColumn(name="productid")
	private Product product;

       // Getters and Setters....
}</pre>
<pre class="brush: java;">@Entity
@Table(name = "products")
@NamedQuery(name="product.byId", query="from Product p where p.id = :id")
public class Product {

	@Id
	private Integer id;

	@Basic
	private String name;

       // Getters and Setters....
}</pre>
<p>The Smooks configuration looks like this:</p>
<pre class="brush: xml;">
&lt;!--
	In this example, we don't need to reference the Hibernate Session
	anywhere in the configuration. Later on, in the Smooks execution
	code (following snippet), you will see that we have set the
	Session as the default Session. This means that you don't need
	to reference it by name.  In the configuration of the previous
	post, we needed to reference every DAO by its name.
--&gt;
&lt;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" xmlns:dao="http://www.milyn.org/xsd/smooks/persistence-1.2.xsd"&gt;

	&lt;!--
		This is a normal Javabean binding. It creates the order bean and binds
		data into it.
	--&gt;
	&lt;jb:bindings beanId="order" class="example.entity.Order"
		createOnElement="order"&gt;

		&lt;jb:value property="ordernumber" data="ordernumber" /&gt;
		&lt;jb:value property="customerId" data="customer" /&gt;
		&lt;jb:wiring setterMethod="addOrderLine" beanIdRef="orderLine" /&gt;
	&lt;/jb:bindings&gt;

	&lt;!--
		This is a normal Javabean binding. Notice that we have a wiring on
		'product'. The Product entity will not be created, but looked up by a
		locator.
	--&gt;
	&lt;jb:bindings beanId="orderLine" class="example.entity.OrderLine"
		createOnElement="order-item"&gt;

		&lt;jb:value property="quantity" data="quantity" decoder="Integer" /&gt;
		&lt;jb:wiring property="order" beanIdRef="order" /&gt;
		&lt;jb:wiring property="product" beanIdRef="product" /&gt;
	&lt;/jb:bindings&gt;

	&lt;!--
		This locator calls (via Scribe) the SessionDaoAdapter#lookupByQuery()
		method. The result will be added to the bean repository under the bean
		id 'product'. An exception is thrown when no result is found.
	--&gt;
	&lt;dao:locator beanId="product" lookupOnElement="order-item"
		onNoResult="EXCEPTION" uniqueResult="true"&gt;

		&lt;dao:query&gt;from Product p where p.id = :id&lt;/dao:query&gt;
		&lt;dao:params&gt;
			&lt;dao:value name="id" data="product" decoder="Integer" /&gt;
		&lt;/dao:params&gt;
	&lt;/dao:locator&gt;

	&lt;!--
		The inserter calls (via Scribe) the SessionDaoAdapter#insert() method
		at the visitAfter of the Order element. The SessioDaoAdapter#insert()
		uses the Hibernate Session#save() method to persist the entity to the
		database.
	--&gt;
	&lt;dao:inserter beanId="order" insertOnElement="order" /&gt;

&lt;/smooks-resource-list&gt;</pre>
<p>If we want to use the named query &#8216;productById&#8217; instead of the query string then the DAO locator configuration will look like this:</p>
<pre class="brush: xml;">	&lt;!--
		The lookup parameter is the query name
	--&gt;
    &lt;dao:locator beanId="product" lookupOnElement="order-item" lookup="product.byId"
    				onNoResult="EXCEPTION" uniqueResult="true"&gt;
    	&lt;dao:params&gt;
    		&lt;dao:value name="id" data="product" decoder="Integer"/&gt;
    	&lt;/dao:params&gt;
    &lt;/dao:locator&gt;</pre>
<p>The following code executes Smooks. Note that we use a SessionRegister object so that we can access the Hibernate Session from within Smooks.</p>
<pre class="brush: java;">
        // Note: In a real world application you should cache the Smooks instance
        // else you will have enormous performance penalties.
        Smooks smooks = new Smooks("smooks-config.xml");

        ExecutionContext executionContext = smooks.createExecutionContext();

        // The SessionRegister provides the bridge between Hibernate and the
        // Persistence Cartridge. We provide it with the Hibernate session.
        // The Hibernate Session is set as default Session.
        DaoRegister register = new SessionRegister(session);

        // This sets the DAO Register in the executionContext for Smooks
        // to access it.
        PersistenceUtil.setDAORegister(executionContext, register);

        Transaction transaction = session.beginTransaction();

        Source source = new StreamSource(new File("order.xml"));        

        smooks.filter(source, null, executionContext);

        transaction.commit();</pre>
<p>If you want to take a JPA version of this example for a spin, simply check out the examples from subversion (http://svn.codehaus.org/milyn/trunk/smooks-examples/) and you’ll find it in the “dao-router” folder.  It also shows you how to use custom DOA&#8217;s with the Persistence Cartridge.</p>
<p>With Scribe and the Persistence Cartridge it is very easy to use Hibernate, JPA and most other persistence frameworks within Smooks. Later on in this series I will explain how too write your own DAO Adapters, so that you can add support for other persistence frameworks.</p>
<p>In the next post of the series I will explain how you can use Ibatis with the Persistence Cartridge.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2009/03/25/smooks-persistence-part-2-how-to-use-hibernatejpa/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Smooks Persistence part 1: The Introduction</title>
		<link>http://blog.smooks.org/2009/02/26/smooks-persistence-part-1-the-introduction/</link>
		<comments>http://blog.smooks.org/2009/02/26/smooks-persistence-part-1-the-introduction/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 11:31:44 +0000</pubDate>
		<dc:creator>Maurice</dc:creator>
				<category><![CDATA[Java Binding]]></category>
		<category><![CDATA[Persistence]]></category>
		<category><![CDATA[Scribe]]></category>
		<category><![CDATA[Smooks]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=61</guid>
		<description><![CDATA[<p>One of the major new features in Smooks v1.2 will be the new Persistence Cartridge. This cartridge enables the use of several entity persistence frameworks from within Smooks, currently targeting Hibernate, Ibatis and any JPA compatible framework. It also allows you to use your own Data Access Objects (DAOs).</p>
<p>This cartridge is great for those cases where you already have your data access layer and want to use its power from within Smooks. It also allows you to reuse these persistence resources on any format of data, not just XML e.g. EDI, CSV, JSON etc.</p>
<p>This is the first post of a series of posts about the persistence cartridge. This post is an introduction of the cartridge.  I will give an overview of what the cartridge can do and also show an example where I use custom DAO&#8217;s to persist the data of an XML document.  In the next posts in this series, I will show examples of how Hibernate, JPA and Ibatis entities can be used.</p>
<p><span id="more-61"></span></p>
<p>The Persistence cartridge is build around a data access layer abstraction library called &#8220;Scribe&#8221;.  Scribe is a new Milyn subproject, born out of the needs of the Persistence cartridge. It enables the Persistence cartridge to use your DAOs without it actually needing to know the details of your DAOs. In fact, they don&#8217;t need to be DAOs at all.  You can also use any entity persistence framework without the Persistence cartridge seeing the difference.  Scribe supports abstracting the default actions you can expect from any DAO or persistence library like inserting, updating, deleting and querying for entities.</p>
<p>For Scribe to understand how to use your DAOs, it needs a mapping. Scribe offers two methods for doing this:</p>
<ul>
<li><strong>Java Annotations</strong><br />
Scribe has a set of Java Annotations like @Insert, @Update, @Delete and @Locate. These annotations enable you to map your DAOs to the DAO actions.</li>
<li><strong>Java Interfaces</strong><br />
Scribe defines a set of Java interfaces like DAO, Locator and Queryable.  You build your DAOs By combining and implementing these interfaces. These interfaces are primarily meant to be used for implementing adapters for persistence frameworks.</li>
</ul>
<p>The Persistence Cartridge defines a set of Smooks Visitor components that use the DAO actions defined by Scribe.  For example, the <strong>Inserter </strong>visitor can insert your entities by calling the method defined by the DAO action mapping.  We see an example of this later.</p>
<p>The cartridge has the following Smooks Visitor components:</p>
<ul>
<li><strong>Inserter<br />
</strong>Inserts/Persists entities</li>
<li><strong>Updater<br />
</strong>Updates/Merges entities</li>
<li><strong>Deleter</strong><br />
Deletes entities</li>
<li><strong>Locator</strong><br />
Locates entities with a query or with your own lookup method.</li>
</ul>
<p>Now that you have an idea of what the Persistence cartridge can do, let&#8217;s take a look at a DAO based example.  We will see Hibernate, IBatis and JPA examples in the followup posts in this series.</p>
<p>The example will read an XML file containing order information (note that this works just the same for EDI, CSV etc).  Using the javabean cartridge, it will bind the XML data into a set of entity beans.  Using the id of the products within the order items (the &lt;product&gt; element) it will locate the product entities and bind them to the order entity bean.  Finally, the order bean will be persisted.</p>
<p>The order XML message looks like this:</p>
<pre class="brush: xml;">&lt;order&gt;
    &lt;ordernumber&gt;1&lt;/ordernumber&gt;
    &lt;customer&gt;123456&lt;/customer&gt;
    &lt;order-items&gt;
        &lt;order-item&gt;
            &lt;product&gt;11&lt;/product&gt;
            &lt;quantity&gt;2&lt;/quantity&gt;
        &lt;/order-item&gt;
        &lt;order-item&gt;
            &lt;product&gt;22&lt;/product&gt;
            &lt;quantity&gt;7&lt;/quantity&gt;
        &lt;/order-item&gt;
    &lt;/order-items&gt;
&lt;/order&gt;</pre>
<p>The following custom DAO will be used to persist the Order entity:</p>
<pre class="brush: java;">@Dao
public class OrderDao {

   private final EntityManager em;

   public OrderDao(EntityManager em) {
     this.em = em;
   }

   @Insert
   public void insertOrder(Order order) {
     em.persist(order);
   }
}</pre>
<p>When looking at this class you should notice the <strong>@Dao</strong> and <strong>@Insert</strong> annotations. The <strong>@Dao</strong> annotation declares that the OrderDao is a DAO object. The <strong>@Insert</strong> annotation declares that the insertOrder method should be used to insert Order entities.</p>
<p>The following custom DAO will be used to lookup the Product entities:</p>
<pre class="brush: java;">@Dao
public class ProductDao {

   private final EntityManager em;

   public ProductDao(EntityManager em) {
      this.em = em;
   }

   @Lookup(name="id")
   public Product findProductById(@Param("id") int id) {
      return em.find(Product.class, id);
   }
}</pre>
<p>When looking at this class, you should notice the <strong>@Lookup</strong> and <strong>@Param</strong> annotation. The <strong>@Lookup</strong> annotation declares that the <strong>ProductDao#findByProductId</strong> method is used to lookup <strong>Product</strong> entities. The<strong> name</strong> parameter in the <strong>@Lookup</strong> annotation sets the lookup name reference for that method. When the name isn&#8217;t declared, the method name will be used. The optional <strong>@Param</strong> annotation let&#8217;s you name the parameters. This creates a better  abstraction between Smooks and the DAO. If you don&#8217;t declare the <strong>@Param</strong> annotation the parameters are resolved by there position.</p>
<p>The Smooks configuration look likes this:</p>
<pre class="brush: xml;">&lt;!--
    In this configuration you will see that we reference to the
    DAO's via the names 'order'  and 'product'. The persistence cartridge uses
    a DAO Register to map the DAO's to there names.
    Later on, in the java code that executes Smooks, you will see how that is done.
--&gt;
&lt;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"
   xmlns:dao="http://www.milyn.org/xsd/smooks/persistence-1.2.xsd"&gt;

   &lt;!--
    This is a normal Javabean binding. It creates the order bean
    and binds data into it.
   --&gt;
   &lt;jb:bindings beanId="order" class="example.entity.Order"
                 createOnElement="order"&gt;
      &lt;jb:value property="ordernumber" data="ordernumber"/&gt;
      &lt;jb:value property="customerId" data="customer"/&gt;
      &lt;jb:wiring setterMethod="addOrderLine" beanIdRef="orderLine" /&gt;
   &lt;/jb:bindings&gt;

   &lt;!--
    This is a normal Javabean binding. Notice that we have a wiring on
    'product'. The Product entity will not be created, but looked up
    by a locator.
   --&gt;
   &lt;jb:bindings beanId="orderLine" class="example.entity.OrderLine"
                 createOnElement="order-item"&gt;
      &lt;jb:value property="quantity" data="quantity"/&gt;
      &lt;jb:wiring property="order" beanIdRef="order"/&gt;
      &lt;jb:wiring property="product" beanIdRef="product"/&gt;
   &lt;/jb:bindings&gt;

   &lt;!--
    This locator calls (via Scribe) the ProductDao#findById() method.
    The result will be added to the bean repository under
    the bean id 'product'.
    An exception is thrown when no result is found.
   --&gt;
   &lt;dao:locator beanId="product" dao="product" lookup="id"
           lookupOnElement="order-item" onNoResult="EXCEPTION"&gt;
      &lt;dao:params&gt;
         &lt;dao:value name="id" data="product" decoder="Integer"/&gt;
      &lt;/dao:params&gt;
   &lt;/dao:locator&gt;

   &lt;!--
    The inserter calls the OrderDao#insertOrder() method at
    end of Order message.
   --&gt;
   &lt;dao:inserter beanId="order" dao="order" insertOnElement="order" /&gt;

&lt;/smooks-resource-list&gt;</pre>
<p>The following code executes Smooks:</p>
<pre class="brush: java;">Smooks smooks = new Smooks("./smooks-configs/smooks-dao-config.xml");
ExecutionContext executionContext = smooks.createExecutionContext();

// The register is used to map the DAO's to a DAO name. The DAO name isbe used in
// the configuration.
// The MapRegister is a simple Map like implementation of the DaoRegister.
DaoRegister&lt;object&gt; register =
	MapRegister.builder()
		.put("product", new ProductDao(em))
		.put("order", new OrderDao(em))
		.build();

PersistenceUtil.setDAORegister(executionContext, mapRegister);

// Transaction management from within Smooks isn't supported yet,
// so we need to do it outside the filter execution
EntityTransaction tx = em.getTransaction();
tx.begin();

smooks.filter(new StreamSource(messageIn), null, executionContext);

tx.commit();</pre>
<p>If you want to take this example for a spin, simply check out the examples from subversion (http://svn.codehaus.org/milyn/trunk/smooks-examples/) and you&#8217;ll find it in the &#8220;dao-router&#8221; folder.  It also shows you how to use JPA directly, without any DAOs in between.</p>
<p>The cartridge is almost ready for it&#8217;s first release.  There is one thing that I am still not completely sure about and it would be great if people could provide some feedback.  I am not completely happy about the action names: <strong>insert</strong>, <strong>update </strong>and <strong>delete</strong>.  In the world of persistence frameworks, different sets of names are used for these actions.  JPA, for instance, uses <strong>persist</strong>, <strong>merge </strong>and <strong>remove</strong>.  Hibernate (outside of JPA) uses <strong>save, update</strong> and <strong>delete </strong>and Ibatis uses <strong>insert</strong>, <strong>update</strong> and <strong>delete</strong>. Which names would you choose and why?</p>
<p>In the next post of the series I will explain how to use Hibernate directly.</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/java-binding/">Java Binding</a> by Maurice <a href="http://blog.smooks.org/2009/02/26/smooks-persistence-part-1-the-introduction/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>One of the major new features in Smooks v1.2 will be the new Persistence Cartridge. This cartridge enables the use of several entity persistence frameworks from within Smooks, currently targeting Hibernate, Ibatis and any JPA compatible framework. It also allows you to use your own Data Access Objects (DAOs).</p>
<p>This cartridge is great for those cases where you already have your data access layer and want to use its power from within Smooks. It also allows you to reuse these persistence resources on any format of data, not just XML e.g. EDI, CSV, JSON etc.</p>
<p>This is the first post of a series of posts about the persistence cartridge. This post is an introduction of the cartridge.  I will give an overview of what the cartridge can do and also show an example where I use custom DAO&#8217;s to persist the data of an XML document.  In the next posts in this series, I will show examples of how Hibernate, JPA and Ibatis entities can be used.</p>
<p><span id="more-61"></span></p>
<p>The Persistence cartridge is build around a data access layer abstraction library called &#8220;Scribe&#8221;.  Scribe is a new Milyn subproject, born out of the needs of the Persistence cartridge. It enables the Persistence cartridge to use your DAOs without it actually needing to know the details of your DAOs. In fact, they don&#8217;t need to be DAOs at all.  You can also use any entity persistence framework without the Persistence cartridge seeing the difference.  Scribe supports abstracting the default actions you can expect from any DAO or persistence library like inserting, updating, deleting and querying for entities.</p>
<p>For Scribe to understand how to use your DAOs, it needs a mapping. Scribe offers two methods for doing this:</p>
<ul>
<li><strong>Java Annotations</strong><br />
Scribe has a set of Java Annotations like @Insert, @Update, @Delete and @Locate. These annotations enable you to map your DAOs to the DAO actions.</li>
<li><strong>Java Interfaces</strong><br />
Scribe defines a set of Java interfaces like DAO, Locator and Queryable.  You build your DAOs By combining and implementing these interfaces. These interfaces are primarily meant to be used for implementing adapters for persistence frameworks.</li>
</ul>
<p>The Persistence Cartridge defines a set of Smooks Visitor components that use the DAO actions defined by Scribe.  For example, the <strong>Inserter </strong>visitor can insert your entities by calling the method defined by the DAO action mapping.  We see an example of this later.</p>
<p>The cartridge has the following Smooks Visitor components:</p>
<ul>
<li><strong>Inserter<br />
</strong>Inserts/Persists entities</li>
<li><strong>Updater<br />
</strong>Updates/Merges entities</li>
<li><strong>Deleter</strong><br />
Deletes entities</li>
<li><strong>Locator</strong><br />
Locates entities with a query or with your own lookup method.</li>
</ul>
<p>Now that you have an idea of what the Persistence cartridge can do, let&#8217;s take a look at a DAO based example.  We will see Hibernate, IBatis and JPA examples in the followup posts in this series.</p>
<p>The example will read an XML file containing order information (note that this works just the same for EDI, CSV etc).  Using the javabean cartridge, it will bind the XML data into a set of entity beans.  Using the id of the products within the order items (the &lt;product&gt; element) it will locate the product entities and bind them to the order entity bean.  Finally, the order bean will be persisted.</p>
<p>The order XML message looks like this:</p>
<pre class="brush: xml;">&lt;order&gt;
    &lt;ordernumber&gt;1&lt;/ordernumber&gt;
    &lt;customer&gt;123456&lt;/customer&gt;
    &lt;order-items&gt;
        &lt;order-item&gt;
            &lt;product&gt;11&lt;/product&gt;
            &lt;quantity&gt;2&lt;/quantity&gt;
        &lt;/order-item&gt;
        &lt;order-item&gt;
            &lt;product&gt;22&lt;/product&gt;
            &lt;quantity&gt;7&lt;/quantity&gt;
        &lt;/order-item&gt;
    &lt;/order-items&gt;
&lt;/order&gt;</pre>
<p>The following custom DAO will be used to persist the Order entity:</p>
<pre class="brush: java;">@Dao
public class OrderDao {

   private final EntityManager em;

   public OrderDao(EntityManager em) {
     this.em = em;
   }

   @Insert
   public void insertOrder(Order order) {
     em.persist(order);
   }
}</pre>
<p>When looking at this class you should notice the <strong>@Dao</strong> and <strong>@Insert</strong> annotations. The <strong>@Dao</strong> annotation declares that the OrderDao is a DAO object. The <strong>@Insert</strong> annotation declares that the insertOrder method should be used to insert Order entities.</p>
<p>The following custom DAO will be used to lookup the Product entities:</p>
<pre class="brush: java;">@Dao
public class ProductDao {

   private final EntityManager em;

   public ProductDao(EntityManager em) {
      this.em = em;
   }

   @Lookup(name="id")
   public Product findProductById(@Param("id") int id) {
      return em.find(Product.class, id);
   }
}</pre>
<p>When looking at this class, you should notice the <strong>@Lookup</strong> and <strong>@Param</strong> annotation. The <strong>@Lookup</strong> annotation declares that the <strong>ProductDao#findByProductId</strong> method is used to lookup <strong>Product</strong> entities. The<strong> name</strong> parameter in the <strong>@Lookup</strong> annotation sets the lookup name reference for that method. When the name isn&#8217;t declared, the method name will be used. The optional <strong>@Param</strong> annotation let&#8217;s you name the parameters. This creates a better  abstraction between Smooks and the DAO. If you don&#8217;t declare the <strong>@Param</strong> annotation the parameters are resolved by there position.</p>
<p>The Smooks configuration look likes this:</p>
<pre class="brush: xml;">&lt;!--
    In this configuration you will see that we reference to the
    DAO's via the names 'order'  and 'product'. The persistence cartridge uses
    a DAO Register to map the DAO's to there names.
    Later on, in the java code that executes Smooks, you will see how that is done.
--&gt;
&lt;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"
   xmlns:dao="http://www.milyn.org/xsd/smooks/persistence-1.2.xsd"&gt;

   &lt;!--
    This is a normal Javabean binding. It creates the order bean
    and binds data into it.
   --&gt;
   &lt;jb:bindings beanId="order" class="example.entity.Order"
                 createOnElement="order"&gt;
      &lt;jb:value property="ordernumber" data="ordernumber"/&gt;
      &lt;jb:value property="customerId" data="customer"/&gt;
      &lt;jb:wiring setterMethod="addOrderLine" beanIdRef="orderLine" /&gt;
   &lt;/jb:bindings&gt;

   &lt;!--
    This is a normal Javabean binding. Notice that we have a wiring on
    'product'. The Product entity will not be created, but looked up
    by a locator.
   --&gt;
   &lt;jb:bindings beanId="orderLine" class="example.entity.OrderLine"
                 createOnElement="order-item"&gt;
      &lt;jb:value property="quantity" data="quantity"/&gt;
      &lt;jb:wiring property="order" beanIdRef="order"/&gt;
      &lt;jb:wiring property="product" beanIdRef="product"/&gt;
   &lt;/jb:bindings&gt;

   &lt;!--
    This locator calls (via Scribe) the ProductDao#findById() method.
    The result will be added to the bean repository under
    the bean id 'product'.
    An exception is thrown when no result is found.
   --&gt;
   &lt;dao:locator beanId="product" dao="product" lookup="id"
           lookupOnElement="order-item" onNoResult="EXCEPTION"&gt;
      &lt;dao:params&gt;
         &lt;dao:value name="id" data="product" decoder="Integer"/&gt;
      &lt;/dao:params&gt;
   &lt;/dao:locator&gt;

   &lt;!--
    The inserter calls the OrderDao#insertOrder() method at
    end of Order message.
   --&gt;
   &lt;dao:inserter beanId="order" dao="order" insertOnElement="order" /&gt;

&lt;/smooks-resource-list&gt;</pre>
<p>The following code executes Smooks:</p>
<pre class="brush: java;">Smooks smooks = new Smooks("./smooks-configs/smooks-dao-config.xml");
ExecutionContext executionContext = smooks.createExecutionContext();

// The register is used to map the DAO's to a DAO name. The DAO name isbe used in
// the configuration.
// The MapRegister is a simple Map like implementation of the DaoRegister.
DaoRegister&lt;object&gt; register =
	MapRegister.builder()
		.put("product", new ProductDao(em))
		.put("order", new OrderDao(em))
		.build();

PersistenceUtil.setDAORegister(executionContext, mapRegister);

// Transaction management from within Smooks isn't supported yet,
// so we need to do it outside the filter execution
EntityTransaction tx = em.getTransaction();
tx.begin();

smooks.filter(new StreamSource(messageIn), null, executionContext);

tx.commit();</pre>
<p>If you want to take this example for a spin, simply check out the examples from subversion (http://svn.codehaus.org/milyn/trunk/smooks-examples/) and you&#8217;ll find it in the &#8220;dao-router&#8221; folder.  It also shows you how to use JPA directly, without any DAOs in between.</p>
<p>The cartridge is almost ready for it&#8217;s first release.  There is one thing that I am still not completely sure about and it would be great if people could provide some feedback.  I am not completely happy about the action names: <strong>insert</strong>, <strong>update </strong>and <strong>delete</strong>.  In the world of persistence frameworks, different sets of names are used for these actions.  JPA, for instance, uses <strong>persist</strong>, <strong>merge </strong>and <strong>remove</strong>.  Hibernate (outside of JPA) uses <strong>save, update</strong> and <strong>delete </strong>and Ibatis uses <strong>insert</strong>, <strong>update</strong> and <strong>delete</strong>. Which names would you choose and why?</p>
<p>In the next post of the series I will explain how to use Hibernate directly.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2009/02/26/smooks-persistence-part-1-the-introduction/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

