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

<channel>
	<title>Armbruster IT Blog &#187; groovy</title>
	<atom:link href="http://blog.armbruster-it.de/tag/groovy/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.armbruster-it.de</link>
	<description>Real world fun with Java, Grails, Groovy, Zope, Plone, Linux and much others.</description>
	<lastBuildDate>Fri, 30 Jul 2010 17:09:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>getting a list of all i18n properties used in a Grails application</title>
		<link>http://blog.armbruster-it.de/2010/07/getting-a-list-of-all-i18n-properties-used-in-a-grails-application/</link>
		<comments>http://blog.armbruster-it.de/2010/07/getting-a-list-of-all-i18n-properties-used-in-a-grails-application/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 19:43:45 +0000</pubDate>
		<dc:creator>Stefan Armbruster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[i18n]]></category>

		<guid isPermaLink="false">http://blog.armbruster-it.de/?p=152</guid>
		<description><![CDATA[You might know this situation: in a project you start by hacking code that uses i18n properties instead of fixed strings in order to support multiple languages. The normal process in Grails is to use the g:message tag in controllers or gsp templates. Side by side you append the new introduced i18n property with some [...]]]></description>
			<content:encoded><![CDATA[<p>You might know this situation: in a project you start by hacking code that uses i18n properties instead of fixed strings in order to support multiple languages. The normal process in Grails is to use the <a href="http://grails.org/doc/latest/ref/Tags/message.html">g:message</a> tag in controllers or gsp templates. Side by side you append the new introduced i18n property with some value in your messsages.properties file.</p>
<p>When support for a new language is requested, all you have to do is translating messages.properties. So far so good &#8211; this make i18n really easy.</p>
<p>But: when the project evolves, there&#8217;s a good chance that some of your i18n properties in messages.properties gets orphaned. Assume you remove a block of code from a gsp. It happens often that the i18n properties used in this block are not removed from messages*.properties because at some point you are not sure if it is referenced elsewhere. So what would be really useful here would be a list of all referenced i18n properties from your *.groovy/*.gsp files.</p>
<p>Doing so is pretty easy, just add a new <a href="http://grails.org/doc/latest/guide/4.%20The%20Command%20Line.html">gant script</a> to your Grails application&#8217;s script folder, let&#8217;s name it i18nList.groovy. This script basically contains:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">includeTargets <span style="color: #66cc66;">&lt;&lt;</span> grailsScript<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Init&quot;</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
target<span style="color: #66cc66;">&#40;</span>main: <span style="color: #ff0000;">&quot;create a list of all i18n properties used in groovy code and gsp templates&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> properties <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span><span style="color: #66cc66;">&#93;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">File</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;.&quot;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #FFCC33;">eachFileRecurse</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>it.<span style="color: #006600;">file</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
            <span style="color: #b1b100;">switch</span> <span style="color: #66cc66;">&#40;</span>it<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
                <span style="color: #b1b100;">case</span> ~/.<span style="color: #66cc66;">*</span>\.<span style="color: #006600;">groovy</span>/:
                    <span style="color: #000000; font-weight: bold;">def</span> matcher <span style="color: #66cc66;">=</span> it.<span style="color: #006600;">text</span> <span style="color: #66cc66;">=</span>~ /code:\s<span style="color: #66cc66;">*</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;'](.*?)[&quot;</span><span style="color: #ff0000;">']/
                    matcher.each { properties &lt;&lt; it[1] }
                    break
                case ~/.*<span style="color: #000099; font-weight: bold;">\.</span>gsp/:
                    def matcher = it.text =~ /code=[&quot;'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#40;</span>.<span style="color: #66cc66;">*?</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;']/
                    matcher.each { properties &lt;&lt; it[1] }
                    break
            }
        }
    }
    println properties.sort().unique().join(&quot;</span>\n<span style="color: #ff0000;">&quot;)
&nbsp;
}
&nbsp;
setDefaultTarget(main)</span></pre></div></div>

<p>(sorry the color coding seems to fail for some Groovy regexes) The script recursivly iterates over all *.groovy and *.gsp files in your project and extract the part after the &#8216;code&#8217; attribute of the message tag using a regex. The regex result are collected into an array. This array is sorted, unique&#8217;d and printed to the console. That&#8217;s it.</p>
<p>One word of caution: this gant script &#8216;works for me&#8217;. So depending on your code, you might notice that the used regex are not sufficient or even fail. Feel free to modify them for your needs, even better send back your modifications.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.armbruster-it.de/2010/07/getting-a-list-of-all-i18n-properties-used-in-a-grails-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>running Groovy on the Nokia N900</title>
		<link>http://blog.armbruster-it.de/2010/03/running-groovy-on-the-nokia-n900/</link>
		<comments>http://blog.armbruster-it.de/2010/03/running-groovy-on-the-nokia-n900/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 16:08:48 +0000</pubDate>
		<dc:creator>Stefan Armbruster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[n900]]></category>

		<guid isPermaLink="false">http://blog.armbruster-it.de/?p=115</guid>
		<description><![CDATA[My favorite gadget for the last few months is definitely the Nokia N900. It&#8217;s a geeky device with a real Linux OS aboard. In opposite to it&#8217;s locked down competitors, the N900 runs Maemo, a platform consisting (mostly) of open source software. So I wonder if it&#8217;s possible to use Groovy on that. And yes, [...]]]></description>
			<content:encoded><![CDATA[<p>My favorite gadget for the last few months is definitely the <a href="http://www.nokia.de/produkte/mobiltelefone/nokia-n900">Nokia N900</a>. It&#8217;s a geeky device with a real Linux OS aboard. In opposite to it&#8217;s locked down competitors, the N900 runs <a href="http://www.maemo.org">Maemo</a>, a platform consisting (mostly) of open source software. So I wonder if it&#8217;s possible to use Groovy on that. And yes, it is possible!<span id="more-115"></span>Unfortunately the Maemo platform doesn&#8217;t contain a JVM by itself. Some days ago, I saw a <a href="http://twitter.com/ahynes1/statuses/10722426299">tweet</a> that there a <a href="http://mint.camswl.com/openjdk.htm">OpenJDK port for ARM</a>. All you have to do, is downloading the JDK and JRE from <a href="http://mint.camswl.com/openjdk/release.htm">this page</a>, bunzip it and move the directory to the N900. The next step is downloading <a href="http://groovy.codehaus.org/Download">Groovy</a> and setting the envionment variables</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">JAVA_HOME</span>=<span style="color: #000000; font-weight: bold;">&lt;</span>jdk-dir<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GROOVY_HOME</span>=<span style="color: #000000; font-weight: bold;">&lt;</span>groovy-dir<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #007800;">$GROOVY_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin:<span style="color: #007800;">$PATH</span></pre></div></div>

<p>With that, it&#8217;s possible to run all the stuff in $GROOVY_HOME/bin, e.g. the groovyConsole on the N900. It terribly slow, the groovyConsole takes abount 60 secs to come up, the keyboard has a very long lag when typing &#8211; but it works!</p>
<p><a href="http://blog.armbruster-it.de/wp-content/uploads/2010/03/Screenshot-20100321-163332.png"><img class="alignnone size-full wp-image-120" title="groovyConsole on Nokia N900" src="http://blog.armbruster-it.de/wp-content/uploads/2010/03/Screenshot-20100321-163332.png" alt="groovyConsole on Nokia N900" width="560" height="336" /></a></p>
<p>Anyone else got a &#8216;groovy&#8217; smartphone?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.armbruster-it.de/2010/03/running-groovy-on-the-nokia-n900/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Grails Neo4j plugin 0.2 released</title>
		<link>http://blog.armbruster-it.de/2010/03/grails-neo4j-plugin-0-2-released/</link>
		<comments>http://blog.armbruster-it.de/2010/03/grails-neo4j-plugin-0-2-released/#comments</comments>
		<pubDate>Wed, 10 Mar 2010 16:15:22 +0000</pubDate>
		<dc:creator>Stefan Armbruster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[neo4j]]></category>

		<guid isPermaLink="false">http://blog.armbruster-it.de/?p=87</guid>
		<description><![CDATA[Today an important update of the Grails Neo4j plugin has been released. Neo4j is a graph database, it&#8217;s main concepts are described in brevity in a previous post.  The plugin provides a convenient way to use Neo4j as a persistence layer for Grails domain classes.
The key features / changes of this release are:

domain classes managed [...]]]></description>
			<content:encoded><![CDATA[<p>Today an important update of the <a href="http://www.grails.org/plugin/neo4j">Grails Neo4j plugin</a> has been released. <a href="http://www.neo4j.org">Neo4j</a> is a graph database, it&#8217;s main concepts are described in brevity in a <a href="http://blog.armbruster-it.de/2009/10/neo4j-grails-plugin/">previous post</a>.  The plugin provides a convenient way to use Neo4j as a persistence layer for Grails domain classes.</p>
<p>The key features / changes of this release are:</p>
<ul>
<li>domain classes managed by Neo4j can now co-existing with traditional domain classes (aka mapped by Hibernate)</li>
<li>Upgrade to Neo4j 1.0</li>
<li>usage of Grails dependency resolution instead of embedding the jars in /lib directory</li>
<li>added a seperate controller to inspect the Neo4j node space</li>
<li>major refactoring using AST transformation, just like in the couchdb plugin</li>
<li>support for the <a href="http://components.neo4j.org/index-util/">Neo4j indexer</a></li>
<li>support for non-declared properties</li>
<li>support for traversers</li>
</ul>
<p><span id="more-87"></span>In more detail:</p>
<h2>Co-existence with other domain implementations</h2>
<p>In the initial 0.1 release there was no possibility to have hibernate-based domain classes and neo4j-bases domain classes within the same application. By massively learning from the implementation of the <a href="http://www.grails.org/plugin/gorm-couchdb">CouchDB plugin</a>, the neo4j plugin shares now the same principles:</p>
<ul>
<li>each domain class annotated with &#8220;@Neo4jEntity&#8221; is managed by Neo4j</li>
<li>the @Neo4jEntity  annotation triggers a <a href="http://groovy.codehaus.org/Local+AST+Transformations">Groovy AST transformation</a> at compile time. This transformation e.g. injects a reference to the Neo4j node instance in each domain class.</li>
<li>the plugin class delegates most of its work to a singleton-style plugin support class</li>
<li>the plugin support class uses Groovy MOP to inject (most of) the GORM methods into the domain classes</li>
</ul>
<h2>Upgrade to Neo4j 1.0</h2>
<p>About two weeks ago, the Neo4j team released their 1.0 version. In order to keep pace, the 0.2 version of the plugin upgrades to this version.</p>
<h2>Using dependency resolution</h2>
<p>In its recent versions Grails provides a powerful <a href="http://grails.org/doc/latest/guide/3.%20Configuration.html#3.7%20Dependency%20Resolution">dependency resolution DSL</a>. Using this feature, the Neo4j plugin does not contain the neo4j jars itself any longer. Instead they are automatically downloaded upon plugin installation from the Neo4j&#8217;s maven repository.</p>
<h2>Controller for visualizing the node space</h2>
<p>The plugin contains a Neo4jController that provides an easy way to inspect and walk through the nodespace. All the properties and relationships are visible and navigable, see the screenshot. <a href="http://blog.armbruster-it.de/wp-content/uploads/2010/03/Bildschirmfoto-explore-node-space-Mozilla-Firefox.png"><img class="alignnone size-full wp-image-90" title="Screenshot Neo4jController" src="http://blog.armbruster-it.de/wp-content/uploads/2010/03/Bildschirmfoto-explore-node-space-Mozilla-Firefox.png" alt="Screenshot Neo4jController" width="718" height="436" /></a></p>
<p>Aside from navigating through the nodespace, the Neo4jController also provides an action for viewing some statistics: counting nodes an relationship by type, and an inspector for viewing the Grails domain class structure. One word of warning: <strong>do not expose this controller to the public!</strong> Instead use e.g. the acegi plugin and protect it from public access.</p>
<h2>Support for Neo4j Indexer</h2>
<p>Since there is no easy way to ask the nodespace questions like &#8220;Give me all cars with more than 250 hp?&#8221;, Neo4j provides an <a href="http://components.neo4j.org/index-util/">addon</a> that indexes property values. The plugin supports this in two ways:</p>
<ul>
<li>Defining index properties in a domain class by annotating the properties to be indexed with @Neo4jIndexed</li>
<li>Using the indexer when the GORM methods &#8216;findBy&lt;prop&gt;&#8217; or &#8216;findAllBy&lt;prop&gt;&#8217; are called on an indexed property</li>
</ul>
<p>The following code example will make that clearer:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #a1a100;">grails.plugins.neo4j.*</span>
@Neo4jEntity
<span style="color: #000000; font-weight: bold;">class</span> Car <span style="color: #66cc66;">&#123;</span>
   <span style="color: #aaaadd; font-weight: bold;">String</span> licenseTag <span style="color: #808080; font-style: italic;">// normal, aka non-indexed property</span>
   @Neo4jIndex
   <span style="color: #aaaadd; font-weight: bold;">String</span> color  <span style="color: #808080; font-style: italic;">// indexed property</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<h2>Support for non-declared properties</h2>
<p>Since Neo4j is a schema-less database, there is no limitation on what properties are allowed for a certain node. A Grails domain model is much more strict on this. To fill this gap, the plugin supports non-declared properties:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> car <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Car<span style="color: #66cc66;">&#40;</span>licenseTag:<span style="color: #ff0000;">'ABC123'</span>, color:<span style="color: #ff0000;">'red'</span><span style="color: #66cc66;">&#41;</span>
car.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">'red'</span> <span style="color: #66cc66;">==</span> car.<span style="color: #006600;">color</span> <span style="color: #808080; font-style: italic;">// declared property</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #66cc66;">=</span> car.<span style="color: #006600;">numberOfSeats</span> <span style="color: #808080; font-style: italic;">// N.B: numberOfSeats is _not_ declared</span>
car.<span style="color: #006600;">numberOfSeats</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">5</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #ff0000;">'5'</span> <span style="color: #66cc66;">==</span> car.<span style="color: #006600;">numberOfSeats</span></pre></div></div>

<p>Using non-declared properties has one limitation: since there&#8217;s no type information available, they are stored as String. That&#8217;s why the last assert need &#8216;5&#8242; in quotes.</p>
<h2>Support for traversers</h2>
<p>A very powerful method to search and find nodes in the nodespace are <a href="http://wiki.neo4j.org/content/Design_Guide#Searching_using_traversing">traversers</a>. The plugin supports them on two levels:</p>
<ul>
<li>static:  DomainClass.traverse uses the reference node as starting point and</li>
<li>instance: &lt;domainClassInstance&gt;.traverse uses the domainClassInstance as starting point.</li>
</ul>
<p>Both variants come with some overloaded implementations:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">traverse<span style="color: #66cc66;">&#40;</span>StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator, <span style="color: #aaaadd; font-weight: bold;">Object</span>... <span style="color: #006600;">args</span><span style="color: #66cc66;">&#41;</span>
traverse<span style="color: #66cc66;">&#40;</span>Traverser.<span style="color: #006600;">Order</span> order, StopEvaluator stopEvaluator, ReturnableEvaluator returnableEvaluator, <span style="color: #aaaadd; font-weight: bold;">Object</span>... <span style="color: #006600;">args</span><span style="color: #66cc66;">&#41;</span>
traverse<span style="color: #66cc66;">&#40;</span>Closure stopEvaluator, Closure returnableEvaluator, <span style="color: #aaaadd; font-weight: bold;">Object</span>... <span style="color: #006600;">arg</span><span style="color: #66cc66;">&#41;</span>
traverse<span style="color: #66cc66;">&#40;</span>Traverser.<span style="color: #006600;">Order</span> order, Closure stopEvaluator, Closure returnableEvaluator, <span style="color: #aaaadd; font-weight: bold;">Object</span>... <span style="color: #006600;">args</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Note that StopEvaluators and ReturnableEvaluator might be coded in a <a href="http://groovy.codehaus.org/Groovy+way+to+implement+interfaces">&#8216;groovy&#8217; way as closures</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> traverser <span style="color: #66cc66;">=</span> car.<span style="color: #006600;">traverse</span><span style="color: #66cc66;">&#40;</span>
 <span style="color: #66cc66;">&#123;</span> <span style="color: #000000; font-weight: bold;">true</span> <span style="color: #66cc66;">&#125;</span>,  <span style="color: #808080; font-style: italic;">// StopEvaluator</span>
 <span style="color: #66cc66;">&#123;</span> it.<span style="color: #006600;">currentNode</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">getProperty</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;color&quot;</span>,<span style="color: #000000; font-weight: bold;">null</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">==</span><span style="color: #ff0000;">&quot;red&quot;</span><span style="color: #66cc66;">&#125;</span> <span style="color: #808080; font-style: italic;">// ReturnableEvaluator</span>
<span style="color: #66cc66;">&#41;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.armbruster-it.de/2010/03/grails-neo4j-plugin-0-2-released/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Customizing Grails data binding with a &#8220;groovy&#8221; PropertyEditor</title>
		<link>http://blog.armbruster-it.de/2010/01/customizing-grails-data-binding-with-a-groovy-propertyeditor/</link>
		<comments>http://blog.armbruster-it.de/2010/01/customizing-grails-data-binding-with-a-groovy-propertyeditor/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 21:44:19 +0000</pubDate>
		<dc:creator>Stefan Armbruster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>

		<guid isPermaLink="false">http://blog.armbruster-it.de/?p=61</guid>
		<description><![CDATA[When Grails binds data e.g. when the controller&#8217;s bindData method is called, it instantiates a GrailsDataBinder to take the action. GrailsDataBinder configures itself with  some basic ProperyEditors. The neat thing is you can extend that behaviour by adding an arbitrary named PropertyEditorRegistrar implementation to the application context. The PropertyEditorRegistrar registers one or multiple PropertyEditors.
A recent [...]]]></description>
			<content:encoded><![CDATA[<p>When Grails binds data e.g. when the controller&#8217;s <a href="http://grails.org/doc/latest/ref/Controllers/bindData.html">bindData</a> method is called, it instantiates a GrailsDataBinder to take the action. GrailsDataBinder configures itself with  some basic <a href="http://java.sun.com/javase/6/docs/api/java/beans/PropertyEditor.html">ProperyEditors</a>. The neat thing is you can extend that behaviour by adding an arbitrary named <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/PropertyEditorRegistrar.html">PropertyEditorRegistrar</a> implementation to the application context. The PropertyEditorRegistrar registers one or multiple PropertyEditors.</p>
<p>A recent use case was the ability to look up some domain instance by a given key and use this for data binding. Coding a seperate PropertyEditor for each domain class would not really be <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>, so I decided to go the groovy way: a DomainClassLookupPropertyEditor:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DomainClassLookupPropertyEditor <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #aaaadd; font-weight: bold;">PropertyEditorSupport</span> <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">Class</span> domainClass
    <span style="color: #aaaadd; font-weight: bold;">String</span> <span style="color: #000000; font-weight: bold;">property</span>
    <span style="color: #993333;">boolean</span> doAssert <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span>
&nbsp;
    <span style="color: #aaaadd; font-weight: bold;">String</span> getAsText<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        value.<span style="color: #ff0000;">&quot;$property&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #993333;">void</span> setAsText<span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">String</span> text<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        value <span style="color: #66cc66;">=</span> domainClass.<span style="color: #ff0000;">&quot;findBy${StringUtils.capitalize(property)}&quot;</span><span style="color: #66cc66;">&#40;</span>text<span style="color: #66cc66;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">assert</span> doAssert <span style="color: #66cc66;">&amp;&amp;</span> value, <span style="color: #ff0000;">&quot;no $domainClass found for $property '$text'&quot;</span>
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>The PropertyEditor calls the domain class&#8217; <a href="http://grails.org/doc/latest/ref/Domain%20Classes/findBy.html"><em>findBy&lt;Property&gt;</em></a> method to look up the desired instance. The PropertyRegistrar looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> MyPropertyEditorRegistrar <span style="color: #000000; font-weight: bold;">implements</span> PropertyEditorRegistrar <span style="color: #66cc66;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #993333;">void</span> registerCustomEditors<span style="color: #66cc66;">&#40;</span>PropertyEditorRegistry propertyEditorRegistry<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        propertyEditorRegistry.<span style="color: #006600;">registerCustomEditor</span><span style="color: #66cc66;">&#40;</span>Author, <span style="color: #000000; font-weight: bold;">new</span> DomainClassLookupPropertyEditor<span style="color: #66cc66;">&#40;</span>domainClass: Author, <span style="color: #000000; font-weight: bold;">property</span>: <span style="color: #ff0000;">&quot;name&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
        propertyEditorRegistry.<span style="color: #006600;">registerCustomEditor</span><span style="color: #66cc66;">&#40;</span><span style="color: #aaaadd; font-weight: bold;">Book</span>, <span style="color: #000000; font-weight: bold;">new</span> DomainClassLookupPropertyEditor<span style="color: #66cc66;">&#40;</span>domainClass: <span style="color: #aaaadd; font-weight: bold;">Book</span>, <span style="color: #000000; font-weight: bold;">property</span>: <span style="color: #ff0000;">&quot;isbn&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
    <span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

<p>Last, we need to configure that in resources.groovy:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;">beans <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
    myEditorRegistrar<span style="color: #66cc66;">&#40;</span>MyPropertyEditorRegistrar<span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.armbruster-it.de/2010/01/customizing-grails-data-binding-with-a-groovy-propertyeditor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
