<?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>Mon, 28 Jun 2010 13:10:25 +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>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>7</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="/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="/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>2</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>
		<item>
		<title>A Mule podcast about Smooks and &#8220;Smooks for Mule&#8221;</title>
		<link>http://blog.smooks.org/2009/02/02/a-mule-podcast-about-smooks-and-smooks-for-mule/</link>
		<comments>http://blog.smooks.org/2009/02/02/a-mule-podcast-about-smooks-and-smooks-for-mule/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 11:01:58 +0000</pubDate>
		<dc:creator>Maurice</dc:creator>
				<category><![CDATA[Podcast]]></category>
		<category><![CDATA[Publications]]></category>
		<category><![CDATA[Smooks]]></category>
		<category><![CDATA[Smooks for Mule]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=48</guid>
		<description><![CDATA[<p>MuleSource recently posted a Podcast about &#8220;Smooks for Mule&#8221; on their &#8220;From the Mule&#8217;s mouth&#8221; blog.  In the Podcast, I get interviewed by Ross Mason about &#8220;Smooks for Mule&#8221; and &#8220;Smooks&#8221;.  I talk about why I started Smooks for Mule and what Smooks can do.  I outline some examples of where Smooks is in use today, as well as what people can expect in the next releases of &#8220;Smooks for Mule&#8221; and &#8220;Smooks&#8221;.</p>
<p>You can listen to the podcast via the player on the end of this post or you can go to the original <a href="http://blog.mulesource.org/2009/01/mulecast-a-conversation-with-the-project-despot-of-the-smooks-for-mule-project/ " target="_blank">Mule blog post</a>.  I hope that you find it interesting and useful.</p>
<p>I would like to thank MuleSource for enabling this interview.</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/podcast/">Podcast</a> by Maurice <a href="http://blog.smooks.org/2009/02/02/a-mule-podcast-about-smooks-and-smooks-for-mule/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>MuleSource recently posted a Podcast about &#8220;Smooks for Mule&#8221; on their &#8220;From the Mule&#8217;s mouth&#8221; blog.  In the Podcast, I get interviewed by Ross Mason about &#8220;Smooks for Mule&#8221; and &#8220;Smooks&#8221;.  I talk about why I started Smooks for Mule and what Smooks can do.  I outline some examples of where Smooks is in use today, as well as what people can expect in the next releases of &#8220;Smooks for Mule&#8221; and &#8220;Smooks&#8221;.</p>
<p>You can listen to the podcast via the player on the end of this post or you can go to the original <a href="http://blog.mulesource.org/2009/01/mulecast-a-conversation-with-the-project-despot-of-the-smooks-for-mule-project/ " target="_blank">Mule blog post</a>.  I hope that you find it interesting and useful.</p>
<p>I would like to thank MuleSource for enabling this interview.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2009/02/02/a-mule-podcast-about-smooks-and-smooks-for-mule/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
<enclosure url="http://blog.smooks.org/wp-content//uploads/2009/02/mule_podcast_smooks_for_mule_090130.mp3" length="4457381" type="audio/mpeg" />
			<itunes:keywords>Smooks,Smooks for Mule</itunes:keywords>
		<itunes:subtitle>MuleSource recently posted a Podcast about &quot;Smooks for Mule&quot; on their &quot;From the Mule&#039;s mouth&quot; blog.  In the Podcast, I get interviewed by Ross Mason about &quot;Smooks for Mule&quot; and &quot;Smooks&quot;.  I talk about why I started Smooks for Mule and what Smooks can do.</itunes:subtitle>
		<itunes:summary>MuleSource recently posted a Podcast about &quot;Smooks for Mule&quot; on their &quot;From the Mule&#039;s mouth&quot; blog.  In the Podcast, I get interviewed by Ross Mason about &quot;Smooks for Mule&quot; and &quot;Smooks&quot;.  I talk about why I started Smooks for Mule and what Smooks can do.  I outline some examples of where Smooks is in use today, as well as what people can expect in the next releases of &quot;Smooks for Mule&quot; and &quot;Smooks&quot;.

You can listen to the podcast via the player on the end of this post or you can go to the original Mule blog post (http://blog.mulesource.org/2009/01/mulecast-a-conversation-with-the-project-despot-of-the-smooks-for-mule-project/ ).  I hope that you find it interesting and useful.

I would like to thank MuleSource for enabling this interview.</itunes:summary>
		<itunes:author>Smooks Data Integration</itunes:author>
		<itunes:explicit>no</itunes:explicit>
	</item>
		<item>
		<title>The start of the official Smooks blog</title>
		<link>http://blog.smooks.org/2009/01/23/the-start-of-the-official-smooks-blog/</link>
		<comments>http://blog.smooks.org/2009/01/23/the-start-of-the-official-smooks-blog/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 13:17:57 +0000</pubDate>
		<dc:creator>Maurice</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Smooks]]></category>

		<guid isPermaLink="false">http://blog.smooks.org/?p=11</guid>
		<description><![CDATA[<p>Yeah, we of the Smooks project are also going to do it. Start blogging about our project. The blog is the perfect place to inform you about the latest things that are going on. With this blog  we want to get our idea&#8217;s out there and introduce you to the latest features of Smooks.  You can also expect  posts with small tutorials featuring common, experimental or less know features of  Smooks or one of it&#8217;s child projects.</p>
<p><span id="more-11"></span> The blog itself is in it&#8217;s Beta phase right now. So you can expect this blog to change  it&#8217;s appearance over the next couple of months.</p>
<p>I hope you will enjoy this blog and find it usefull. Please place comments if you comments or questions. We will try to answer them all.</p>
<p>Regards,</p>
<p>Maurice</p>
<div style="display:block"><small><em>posted in <a href="http://blog.smooks.org/category/blog/">Blog</a> by Maurice <a href="http://blog.smooks.org/2009/01/23/the-start-of-the-official-smooks-blog/#comments">Leave A Comment</a></em></small></div>]]></description>
			<content:encoded><![CDATA[<p>Yeah, we of the Smooks project are also going to do it. Start blogging about our project. The blog is the perfect place to inform you about the latest things that are going on. With this blog  we want to get our idea&#8217;s out there and introduce you to the latest features of Smooks.  You can also expect  posts with small tutorials featuring common, experimental or less know features of  Smooks or one of it&#8217;s child projects.</p>
<p><span id="more-11"></span> The blog itself is in it&#8217;s Beta phase right now. So you can expect this blog to change  it&#8217;s appearance over the next couple of months.</p>
<p>I hope you will enjoy this blog and find it usefull. Please place comments if you comments or questions. We will try to answer them all.</p>
<p>Regards,</p>
<p>Maurice</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.smooks.org/2009/01/23/the-start-of-the-official-smooks-blog/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
