<?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>On The Rhoads/Brad &#187; grails</title>
	<atom:link href="http://ontherhoads.org/brad/category/grails/feed/" rel="self" type="application/rss+xml" />
	<link>http://ontherhoads.org/brad</link>
	<description></description>
	<lastBuildDate>Sat, 04 Feb 2012 00:00:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Creating One-To-Many Records In A Grails Service</title>
		<link>http://ontherhoads.org/brad/2009/11/creating-one-to-many-records-in-a-grails-service/</link>
		<comments>http://ontherhoads.org/brad/2009/11/creating-one-to-many-records-in-a-grails-service/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 06:33:33 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[grails]]></category>

		<guid isPermaLink="false">http://ontherhoads.org/brad/?p=63</guid>
		<description><![CDATA[A parent can have many children. This article explains how to  write a service such that, if after adding a parent there is an error when adding a child, the entire transaction is rolled back. For example, add parent p1, successfully add child c1, then when adding child c2 an error occurs, both p1 [...]]]></description>
			<content:encoded><![CDATA[<p>A parent can have many children. This article explains how to  write a service such that, if after adding a parent there is an error when adding a child, the entire transaction is rolled back. For example, add parent p1, successfully add child c1, then when adding child c2 an error occurs, both p1 and c1 should be rolled back.</p>
<p>Note that this requires Grails 1.2-M4.</p>
<p>Here are the domain objects:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Parent <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> hasMany <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#91;</span> children : Child <span style="color: #66cc66;">&#93;</span>
&nbsp;
<span style="color: #aaaadd; font-weight: bold;">String</span>  name
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
name<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span>,unique:<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Child <span style="color: #66cc66;">&#123;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> belongsTo <span style="color: #66cc66;">=</span> Parent
&nbsp;
<span style="color: #aaaadd; font-weight: bold;">String</span> name
&nbsp;
<span style="color: #aaaadd; font-weight: bold;">String</span> pet
&nbsp;
Parent parent
&nbsp;
<span style="color: #000000; font-weight: bold;">static</span> constraints <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
name<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span>,unique:<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
pet<span style="color: #66cc66;">&#40;</span>blank:<span style="color: #000000; font-weight: bold;">false</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Now if you do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">grails generate<span style="color: #66cc66;">-</span>all <span style="color: #66cc66;">*</span></pre></td></tr></table></div>

<p>You&#8217;ll get separate forms that do work. But if we were talking about an invoice application, for example, you&#8217;d really want to have 1 data entry form. That&#8217;s what we do here:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;">&nbsp;
<span style="color: #66cc66;">&lt;%</span>@ page contentType<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html;charset=UTF-8&quot;</span> <span style="color: #66cc66;">%&gt;</span>
&nbsp;
<span style="color: #66cc66;">&lt;</span>html<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>head<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>meta http<span style="color: #66cc66;">-</span>equiv<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> content<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>meta name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;layout&quot;</span> content<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;main&quot;</span> /<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>title<span style="color: #66cc66;">&gt;</span>Sample title<span style="color: #66cc66;">&lt;</span>/title<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>/head<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>body<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>h1<span style="color: #66cc66;">&gt;</span>Add A Record<span style="color: #66cc66;">&lt;</span>/h1<span style="color: #66cc66;">&gt;</span>
&nbsp;
  <span style="color: #66cc66;">&lt;</span>g:<span style="color: #b1b100;">if</span> test<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${flash.message}&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;message&quot;</span><span style="color: #66cc66;">&gt;</span>$<span style="color: #66cc66;">&#123;</span>flash.<span style="color: #006600;">message</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&lt;</span>/div<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>/g:if<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>g:<span style="color: #b1b100;">if</span> test<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${flash.errors}&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>div <span style="color: #000000; font-weight: bold;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;errors&quot;</span><span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>ul<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>g:render template<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;renderflasherrors&quot;</span> collection<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${flash.errors}&quot;</span>  <span style="color: #000000; font-weight: bold;">as</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;list&quot;</span>/<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/ul<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>/div<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>/g:if<span style="color: #66cc66;">&gt;</span>
&nbsp;
&nbsp;
  <span style="color: #66cc66;">&lt;</span>g:form action<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;add&quot;</span> name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;doAdd&quot;</span><span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>table<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
          Parent <span style="color: #aaaadd; font-weight: bold;">Name</span>
        <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
          Child <span style="color: #aaaadd; font-weight: bold;">Name</span>
        <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
          Pet <span style="color: #aaaadd; font-weight: bold;">Name</span>
        <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/tr<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;</span>
        <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
      <span style="color: #808080; font-style: italic;">//&lt;g:textField name=&quot;parentName&quot; value=&quot;${flash.params?.parentName}&quot; /&gt;</span>
       <span style="color: #66cc66;">&lt;</span>g:textField name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;parentName&quot;</span> value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${params?.parentName}&quot;</span> /<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
      <span style="color: #808080; font-style: italic;">//&lt;g:textField name=&quot;childName&quot; value=&quot;${flash.params?.childName}&quot; /&gt;</span>
       <span style="color: #66cc66;">&lt;</span>g:textField name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;childName&quot;</span> value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${params?.childName}&quot;</span> /<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>td<span style="color: #66cc66;">&gt;</span>
      <span style="color: #808080; font-style: italic;">//&lt;g:textField name=&quot;petName&quot; value=&quot;${flash.params?.petName}&quot; /&gt;</span>
      <span style="color: #66cc66;">&lt;</span>g:textField name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;petName&quot;</span> value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;${params?.petName}&quot;</span> /<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/td<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>/tr<span style="color: #66cc66;">&gt;</span>
      <span style="color: #66cc66;">&lt;</span>tr<span style="color: #66cc66;">&gt;&lt;</span>td<span style="color: #66cc66;">&gt;&lt;</span>g:submitButton name<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;update&quot;</span> value<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Update&quot;</span> /<span style="color: #66cc66;">&gt;&lt;</span>/td<span style="color: #66cc66;">&gt;&lt;</span>/tr<span style="color: #66cc66;">&gt;</span>
    <span style="color: #66cc66;">&lt;</span>/table<span style="color: #66cc66;">&gt;</span>
  <span style="color: #66cc66;">&lt;</span>/g:form<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>/body<span style="color: #66cc66;">&gt;</span>
<span style="color: #66cc66;">&lt;</span>/html<span style="color: #66cc66;">&gt;</span></pre></td></tr></table></div>

<p>The tricky part in this was figuring out how to create both records in the controller and return any errors and the entered data back to client. More on that later, but for now just notice lines 13-16 where we render the errors. This is a little different than in the generated forms. There you can check if the object has errors and display them.</p>
<p>But in this example, all of the objects are created in a service, so the .gsp (and the controller) don&#8217;t know anything about the objects. So we end up passing the errors back in a collection flash.errors.</p>
<p>Now look at lines 37, 40 and 43 and compare those to similar lines in the generated forms. There you use a bean to get the value from. But here, we just redisplay the params which contains everything the user originally entered. <del datetime="2009-11-18T06:33:20+00:00">Actually, the params are being stored in flash. For some reason, the form fields end up getting dropped from the params by the time we get back to action:show.</del> </p>
<p>Here&#8217;s the controller:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AddrecordController <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> addRecordsService
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> index <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
        redirect action:<span style="color: #ff0000;">&quot;show&quot;</span>, params:params
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> add <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;do add&quot;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>
            addRecordsService.<span style="color: #006600;">addAll</span><span style="color: #66cc66;">&#40;</span>params<span style="color: #66cc66;">&#41;</span>
        <span style="color: #66cc66;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #66cc66;">&#40;</span>java.<span style="color: #006600;">lang</span>.<span style="color: #aaaadd; font-weight: bold;">RuntimeException</span> re<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
&nbsp;
          <span style="color: #000000; font-weight: bold;">def</span> errorMessages <span style="color: #66cc66;">=</span> re.<span style="color: #006600;">errors</span>.<span style="color: #006600;">allErrors</span>.<span style="color: #663399;">collect</span> <span style="color: #66cc66;">&#123;</span> g.<span style="color: #006600;">message</span><span style="color: #66cc66;">&#40;</span>error:it<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#125;</span>
            <span style="color: #993399;">println</span>  errorMessages
            flash.<span style="color: #006600;">message</span> <span style="color: #66cc66;">=</span> g.<span style="color: #006600;">message</span><span style="color: #66cc66;">&#40;</span>code:<span style="color: #ff0000;">&quot;addRecord.save.failed&quot;</span><span style="color: #66cc66;">&#41;</span>
            flash.<span style="color: #006600;">errors</span> <span style="color: #66cc66;">=</span> errorMessages
           <span style="color: #808080; font-style: italic;">//flash.params = params</span>
            <span style="color: #808080; font-style: italic;">//render action:&quot;show&quot;,  params:params</span>
            render view: <span style="color: #ff0000;">&quot;show&quot;</span>, params:params
            <span style="color: #000000; font-weight: bold;">return</span>
        <span style="color: #66cc66;">&#125;</span>
        redirect action:<span style="color: #ff0000;">&quot;show&quot;</span>
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> show <span style="color: #66cc66;">=</span> <span style="color: #66cc66;">&#123;</span><span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Make sure you render the view not the action and follow it with a return.</p>
<p>If there&#8217;s an error in the service a RunTimeError get&#8217;s thrown. As of Grails 1.2-M4, the exception thrown is a grails.validation.ValidationException, which has the list of errors, so at line 18 we pass each error to a message tag to get the readable messages [1].</p>
<p>Finally, here&#8217;s the service:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> AddRecordsService <span style="color: #66cc66;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span> transactional <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">true</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> addAll<span style="color: #66cc66;">&#40;</span>params<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;add all&quot;</span>
        <span style="color: #993399;">println</span> params
        <span style="color: #000000; font-weight: bold;">def</span> Parent theParent <span style="color: #66cc66;">=</span>  addParent<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">parentName</span><span style="color: #66cc66;">&#41;</span>
        <span style="color: #000000; font-weight: bold;">def</span> Child theChild   <span style="color: #66cc66;">=</span> addChild<span style="color: #66cc66;">&#40;</span>params.<span style="color: #006600;">childName</span>,params.<span style="color: #006600;">petName</span>,theParent<span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #993399;">println</span> theParent
        <span style="color: #993399;">println</span> theChild
&nbsp;
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> addParent<span style="color: #66cc66;">&#40;</span>pName<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;add parent: ${pName}&quot;</span>
        <span style="color: #000000; font-weight: bold;">def</span> theParent <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Parent<span style="color: #66cc66;">&#40;</span>name:pName<span style="color: #66cc66;">&#41;</span>
        theParent.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>failOnError:<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> theParent
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">def</span> addChild<span style="color: #66cc66;">&#40;</span>cName,pName,Parent theParent<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
        <span style="color: #993399;">println</span> <span style="color: #ff0000;">&quot;add child: ${cName}&quot;</span>
        <span style="color: #000000; font-weight: bold;">def</span> theChild <span style="color: #66cc66;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Child<span style="color: #66cc66;">&#40;</span>name:cName,pet:pName,parent:theParent<span style="color: #66cc66;">&#41;</span>
&nbsp;
       theChild.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span>failOnError:<span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> theChild
    <span style="color: #66cc66;">&#125;</span>
&nbsp;
<span style="color: #66cc66;">&#125;</span></pre></td></tr></table></div>

<p>Grails 1.2-M4 added failOnError:true as an option to save(). So now the cause of an error is returned.</p>
<p>Thanks to everyone who answered my questions on this ([1],[2],[3],[4]).</p>
<p><a href="http://old.nabble.com/How-To-Parse-Runttime-Exception-Error-%28GRAILS-5146%29-td26272781.html#a26272781">[1]</a>Re: How To Parse Runttime Exception Error (GRAILS-5146)</p>
<p><a href="http://stackoverflow.com/questions/1628982/how-to-make-transactions-work-in-grails">[2]</a>How To Make Transactions Work In Grails</p>
<p><a href="http://stackoverflow.com/questions/1640666/how-to-know-the-cause-of-a-validation-error">[3]</a>How To Know The Cause Of A Validation Error</p>
<p><a href="http://stackoverflow.com/questions/1646855/render-errors-from-a-service">[4]</a>Render Errors From A Service</p>
<p><a href="http://ontherhoads.org/brad/wp-content/uploads/2009/11/transtest1.zip">Download transtest</a></p>


<!-- Begin TwitThis script (http://twitthis.com/) -->
<div style="text-align:left;">
<script type="text/javascript" src="http://s3.chuug.com/chuug.twitthis.scripts/twitthis.js"></script>
<script type="text/javascript">
<!--
document.write('<a href="javascript:;" onclick="TwitThis.pop();"><img src="http://s3.chuug.com/chuug.twitthis.resources/twitthis_grey_72x22.gif" alt="TwitThis" style="border:none;" /></a>');
//-->
</script>
</div>
<!-- /End -->


<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F11%2Fcreating-one-to-many-records-in-a-grails-service%2F&amp;title=Creating+One-To-Many+Records+In+A+Grails+Service" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F11%2Fcreating-one-to-many-records-in-a-grails-service%2F&amp;title=Creating+One-To-Many+Records+In+A+Grails+Service" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F11%2Fcreating-one-to-many-records-in-a-grails-service%2F&amp;title=Creating+One-To-Many+Records+In+A+Grails+Service" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F11%2Fcreating-one-to-many-records-in-a-grails-service%2F&amp;title=Creating+One-To-Many+Records+In+A+Grails+Service', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://del.icio.us/favicon.ico" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F11%2Fcreating-one-to-many-records-in-a-grails-service%2F&amp;t=Creating+One-To-Many+Records+In+A+Grails+Service" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F11%2Fcreating-one-to-many-records-in-a-grails-service%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F11%2Fcreating-one-to-many-records-in-a-grails-service%2F&amp;title=Creating+One-To-Many+Records+In+A+Grails+Service" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://ontherhoads.org/brad/2009/11/creating-one-to-many-records-in-a-grails-service/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Google Docs For Software Localization</title>
		<link>http://ontherhoads.org/brad/2009/09/using-google-docs-for-software-localization/</link>
		<comments>http://ontherhoads.org/brad/2009/09/using-google-docs-for-software-localization/#comments</comments>
		<pubDate>Fri, 18 Sep 2009 20:59:52 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[I18N]]></category>

		<guid isPermaLink="false">http://ontherhoads.org/brad/?p=58</guid>
		<description><![CDATA[Google provides some truly amazing automatic translation tools.  And now translation is included in Google Docs. This turns out to be a powerful tool for software localization.
My current and probably long-term favorite development environment is Grails. Grails has great localization support build into it. An important piece of that is the messages.properties file, where [...]]]></description>
			<content:encoded><![CDATA[<p>Google provides some truly amazing <a href="http://translate.google.com/translate_tools?hl=en">automatic translation tools</a>.  And now translation is included in Google Docs. This turns out to be a powerful tool for software localization.</p>
<p>My current and probably long-term favorite development environment is Grails. Grails has great localization support build into it. An important piece of that is the <code>messages.properties</code> file, where you store all of your values that you want to be localized. </p>
<p>Actually, there can be lots of properties files. Grails actually ships with:</p>
<ul>
<li><code>messages_de.properties</code></li>
<li><code>messages_es.properties</code></li>
<li><code>messages_fr.properties</code></li>
<li><code>messages_it.properties</code></li>
<li><code>messages_ja.properties</code></li>
<li><code>messages_nl.properties</code></li>
<li><code>messages_pt_BR.properties</code></li>
<li><code>messages_ru.properties</code></li>
<li><code>messages_th.properties</code></li>
<li><code>messages_zh_CN.properties</code></li>
</ul>
<p>For example, <code>messages_fr.properties</code> is for French. </p>
<p>All the base files have translations of commonly used words. As you add more functionality to your application, you of course need more labels and buttons, etc. So you add them to your messages.properties file. Then all you have to do is translate your file into all these and other languages. The good news is that you can use Google Docs to get the job done. Or at least give you a head start on the translation process.</p>
<p>Just load your properties file into Google Docs and select Tools|Translate. <div id="attachment_59" class="wp-caption alignleft" style="width: 310px"><a href="http://ontherhoads.org/brad/wp-content/uploads/2009/09/translate.png"><img src="http://ontherhoads.org/brad/wp-content/uploads/2009/09/translate-300x123.png" alt="translate" title="translate" width="300" height="123" class="size-medium wp-image-59" /></a><p class="wp-caption-text">translate</p></div></p>


<!-- Begin TwitThis script (http://twitthis.com/) -->
<div style="text-align:left;">
<script type="text/javascript" src="http://s3.chuug.com/chuug.twitthis.scripts/twitthis.js"></script>
<script type="text/javascript">
<!--
document.write('<a href="javascript:;" onclick="TwitThis.pop();"><img src="http://s3.chuug.com/chuug.twitthis.resources/twitthis_grey_72x22.gif" alt="TwitThis" style="border:none;" /></a>');
//-->
</script>
</div>
<!-- /End -->


<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F09%2Fusing-google-docs-for-software-localization%2F&amp;title=Using+Google+Docs+For+Software+Localization" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F09%2Fusing-google-docs-for-software-localization%2F&amp;title=Using+Google+Docs+For+Software+Localization" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F09%2Fusing-google-docs-for-software-localization%2F&amp;title=Using+Google+Docs+For+Software+Localization" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F09%2Fusing-google-docs-for-software-localization%2F&amp;title=Using+Google+Docs+For+Software+Localization', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://del.icio.us/favicon.ico" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F09%2Fusing-google-docs-for-software-localization%2F&amp;t=Using+Google+Docs+For+Software+Localization" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F09%2Fusing-google-docs-for-software-localization%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F09%2Fusing-google-docs-for-software-localization%2F&amp;title=Using+Google+Docs+For+Software+Localization" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://ontherhoads.org/brad/2009/09/using-google-docs-for-software-localization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling A Service From The Grails Console</title>
		<link>http://ontherhoads.org/brad/2009/08/calling-a-service-from-the-grails-console/</link>
		<comments>http://ontherhoads.org/brad/2009/08/calling-a-service-from-the-grails-console/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 07:47:05 +0000</pubDate>
		<dc:creator>brad</dc:creator>
				<category><![CDATA[grails]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://ontherhoads.org/brad/?p=54</guid>
		<description><![CDATA[I&#8217;m doing my first project in Grails and intend to blog some of thing things I&#8217;m learning.
During development, the grails console is very useful for testing specific pieces of code without having to go through your whole system to get to that particular bit of code.
For the most part, you can just enter grails commands [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m doing my first project in Grails and intend to blog some of thing things I&#8217;m learning.</p>
<p>During development, the grails console is very useful for testing specific pieces of code without having to go through your whole system to get to that particular bit of code.</p>
<p>For the most part, you can just enter grails commands and hit execute. But apparently dependency inject doesn&#8217;t happen in the console. So there&#8217;s a bit of a trick to execute a service from the console.</p>
<p>If you had a service named MySecretService with a function named secretSquirell, you could access it via dependency injection in a domain class, a controller or another service by first adding</p>
<p><code>def mySecretService</code></p>
<p>then later you can say<br />
<code>def result = mySecretService.secretSquirell()</code></p>
<p>But to test in the console you need to do this:</p>
<p><code>def service = ctx.getBean("mySecretService")</code><br />
<code>def result = service.secretSquirell()</code></p>


<!-- Begin TwitThis script (http://twitthis.com/) -->
<div style="text-align:left;">
<script type="text/javascript" src="http://s3.chuug.com/chuug.twitthis.scripts/twitthis.js"></script>
<script type="text/javascript">
<!--
document.write('<a href="javascript:;" onclick="TwitThis.pop();"><img src="http://s3.chuug.com/chuug.twitthis.resources/twitthis_grey_72x22.gif" alt="TwitThis" style="border:none;" /></a>');
//-->
</script>
</div>
<!-- /End -->


<span class="slashdigglicious">
<a href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F08%2Fcalling-a-service-from-the-grails-console%2F&amp;title=Calling+A+Service+From+The+Grails+Console" title="Slashdot It!"><img src="http://slashdot.org/favicon.ico" height="16" width="16" alt="[Slashdot]" /></a>
<a href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F08%2Fcalling-a-service-from-the-grails-console%2F&amp;title=Calling+A+Service+From+The+Grails+Console" title="Digg This Story"><img src="http://digg.com/favicon.ico" width="16" height="16" alt="[Digg]" /></a>
<a href="http://reddit.com/submit?url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F08%2Fcalling-a-service-from-the-grails-console%2F&amp;title=Calling+A+Service+From+The+Grails+Console" title="Reddit"><img src="http://reddit.com/favicon.ico" width="16" height="16" alt="[Reddit]" /></a>
<a href="http://del.icio.us/post" title="Save to del.icio.us" onclick="window.open('http://del.icio.us/post?v=4&amp;noui&amp;jump=close&amp;url=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F08%2Fcalling-a-service-from-the-grails-console%2F&amp;title=Calling+A+Service+From+The+Grails+Console', 'delicious', 'toolbar=no,width=700,height=400'); return false;"><img src="http://del.icio.us/favicon.ico" width="16" height="16" alt="[del.icio.us]" /></a>
<a href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F08%2Fcalling-a-service-from-the-grails-console%2F&amp;t=Calling+A+Service+From+The+Grails+Console" title="Share on Facebook"><img src="http://www.facebook.com/favicon.ico" width="16" height="16" alt="[Facebook]" /></a>
<a href="http://technorati.com/faves?add=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F08%2Fcalling-a-service-from-the-grails-console%2F" title="Add to my Technorati Favorites"><img src="http://technorati.com/favicon.ico" width="16" height="16" alt="[Technorati]" /></a>
<a href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fontherhoads.org%2Fbrad%2F2009%2F08%2Fcalling-a-service-from-the-grails-console%2F&amp;title=Calling+A+Service+From+The+Grails+Console" title="Save to Google Bookmarks"><img src="http://www.google.com/favicon.ico" width="16" height="16" alt="[Google]" /></a>
</span>]]></content:encoded>
			<wfw:commentRss>http://ontherhoads.org/brad/2009/08/calling-a-service-from-the-grails-console/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

