<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

  <title><![CDATA[Felipe Cypriano]]></title>
  <link href="http://felipecypriano.com//atom.xml" rel="self"/>
  <link href="http://felipecypriano.com//"/>
  <updated>2012-01-24T11:22:27-02:00</updated>
  <id>http://felipecypriano.com//</id>
  <author>
    <name><![CDATA[Felipe Cypriano]]></name>
    
  </author>
  <generator uri="http://octopress.org/">Octopress</generator>

  
  <entry>
    <title type="html"><![CDATA[The Python Ecosystem]]></title>
    <link href="http://felipecypriano.com//2011/11/28/the-python-ecosystem/"/>
    <updated>2011-11-28T16:20:00-02:00</updated>
    <id>http://felipecypriano.com//2011/11/28/the-python-ecosystem</id>
    <content type="html"><![CDATA[<p>Yesterday I wrote about why <a href="http://felipecypriano.com//2011/11/27/why-i-choose-to-learn-ruby-over-python/">I&#8217;m learning Ruby before Pyhton</a>.
And today I just came across <a href="http://mirnazim.org/writings/python-ecosystem-introduction/">a post from Mir Nazin</a> that aims
to be a great comprehensive tutorial for beginners:</p>

<blockquote><p>When developers shift from PHP, Ruby or any other platform to Python, the very
first road block they face(most often) is the lack of overall understanding the
Python ecosystem.</p></blockquote>

<p>Read more: <a href="http://mirnazim.org/writings/python-ecosystem-introduction/">Python Ecosystem - An Introduction</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why I Choose to Learn Ruby Over Python]]></title>
    <link href="http://felipecypriano.com//2011/11/27/why-i-choose-to-learn-ruby-over-python/"/>
    <updated>2011-11-27T12:36:00-02:00</updated>
    <id>http://felipecypriano.com//2011/11/27/why-i-choose-to-learn-ruby-over-python</id>
    <content type="html"><![CDATA[<p><img class="right" src="http://felipecypriano.com//images/2011/11/ruby-python-logo.png" width="150" height="300"></p>

<p>A few months ago I was looking for a technology to run the server side of a
iPhone app. And I had two in mind: <a href="http://rubyonrails.org/">Ruby on Rails</a> and
<a href="https://www.djangoproject.com/">Django (Python)</a>.</p>

<p>Why not others options? I already knew Java and its web frameworks but I didn&#8217;t
want to use any of them because Java needs a lot of RAM even for the most simple
things and I don&#8217;t want a big server before I really need it. PHP I don&#8217;t like
the syntax, it is ugly.</p>

<p>This post is not a technical comparison between the languages, it&#8217;s why I&#8217;ve
chosen to learn one of them first.</p>

<!-- more -->


<h2>The First Impression</h2>

<p>Obviously before I learn any framework I need to learn the language it runs on.
The first place I went was the language&#8217;s official site: <a href="http://www.ruby-lang.org/en/">Ruby</a> and
<a href="http://python.org/">Python</a>.</p>

<p><a href="http://felipecypriano.com//images/2011/11/ruby-and-python-sites.png"><img class="center" src="http://felipecypriano.com//images/2011/11/ruby-and-python-sites-450x470.png" width="450" height="470"></a></p>

<p>As a developer keen on design my first impression was a big point to Ruby.
Ruby&#8217;s site is more beautiful, but this isn&#8217;t why it&#8217;s better. <strong>It is better
because it is easy to find how to get started.</strong> I found quickly the language&#8217;s
overview, the first things to do and where to go to learn more in each step.</p>

<p>While in Python&#8217;s site I felt lost, it has too much text and no explicit
direction to follow. It&#8217;d be very good if Python&#8217;s site become more digestible
for beginners.</p>

<h2>Syntax, sugar is good</h2>

<p>At this poins I&#8217;m much more familiar with Ruby than Python.</p>

<ul>
<li>Ruby has blocks, I love blocks since I started programming with Groovy.</li>
<li>Ruby support ternary operator. E.g. <code>beer.amazing_taste? ? me.drink : garbage.put(beer)</code></li>
<li>Python supports multiple inheritance. All my background is with single inheritance languages, I get scared with the idea of having multiple inheritance in my code.</li>
</ul>


<p>And my favorite is Ruby&#8217;s similarity with functional programming. For example,
looping over a collection by passing a block directly to a <code>each</code>
method.</p>

<figure class='code'><figcaption><span>Ruby - Looping using blocks</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='ruby'><span class='line'><span class="n">pubs_in_dublin</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">pub</span><span class="o">|</span>
</span><span class='line'>  <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">has_good_beer?</span>
</span><span class='line'>      <span class="s2">&quot;I need to go to </span><span class="si">#{</span><span class="n">pub_name</span><span class="si">}</span><span class="s2"> in Dublin before I die.&quot;</span>
</span><span class='line'>  <span class="k">end</span>
</span><span class='line'><span class="k">end</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>Python - Looping using functions or for in</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
</pre></td><td class='code'><pre><code class='python'><span class='line'><span class="k">def</span> <span class="nf">should_go_to_pub</span><span class="p">(</span><span class="n">pub</span><span class="p">):</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">has_good_beer</span><span class="p">:</span>
</span><span class='line'>        <span class="s">&quot;I need to go to &quot;</span> <span class="o">+</span> <span class="n">pub_name</span> <span class="o">+</span> <span class="s">&quot; in Dublin before I die.&quot;</span>
</span><span class='line'>
</span><span class='line'><span class="n">pubs_in_dublin</span><span class="o">.</span><span class="n">each</span><span class="p">(</span><span class="n">should_go_to_pub</span><span class="p">)</span>
</span><span class='line'>
</span><span class='line'><span class="c"># or</span>
</span><span class='line'>
</span><span class='line'><span class="k">for</span> <span class="n">pub</span> <span class="ow">in</span> <span class="n">pubs_in_dublin</span><span class="p">:</span>
</span><span class='line'>    <span class="k">if</span> <span class="n">pub</span><span class="o">.</span><span class="n">has_good_beer</span><span class="p">:</span>
</span><span class='line'>        <span class="s">&quot;I need to go to &quot;</span> <span class="o">+</span> <span class="n">pub_name</span> <span class="o">+</span> <span class="s">&quot; in Dublin before I die.&quot;</span>
</span></code></pre></td></tr></table></div></figure>


<p>The last <em>and least</em> thing is that I&#8217;m not sure if I like the idention as being
part of the language.</p>

<h2>Wrapping Up</h2>

<p>As it&#8217;s clear by now, I&#8217;m studing Ruby. And in the future I&#8217;ll sure learn
Python as well as other languages.</p>

<p>If you&#8217;re into Ruby on Rails you should check out <a href="http://www.codeschool.com/">Code School</a>
they have excelent courses, I&#8217;m in love by their teaching way: Learn by Doing.
Other materials I like about Ruby [on Rails]:</p>

<ul>
<li><a href="http://www.rubyinside.com/">RubyInside</a></li>
<li><a href="http://www.rubyinside.com/the-ruby-1-9-walkthrough-how-to-go-from-ruby-1-8-7-to-1-9-2-and-1-9-3-5409.html">The Ruby 1.9 Walkthrough</a></li>
<li><a href="https://railscasts.com/">RailsCasts</a></li>
</ul>


<p>I&#8217;d love to hear what you think. Let&#8217;s discuss and learn more in the comments.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Keep Changing]]></title>
    <link href="http://felipecypriano.com//2011/10/06/keep-changing/"/>
    <updated>2011-10-06T11:30:00-03:00</updated>
    <id>http://felipecypriano.com//2011/10/06/keep-changing</id>
    <content type="html"><![CDATA[<p>Back them in 2005 I was starting my developer career and as almost all other
developers I didn&#8217;t care about anything that wasn&#8217;t the quality of my code, how
many features it has and if it <em>&#8220;works as expected&#8221;</em>.</p>

<!--more-->


<p>The meaning of &#8220;works&#8221; was if the code is executed correctly when the users
clicks on some button on the interface. And the &#8220;expected&#8221; was mostly my own
expectations not the users. Since the <em>user was always wrong</em> that did make
sense.</p>

<p>My first Apple product was an <a href="http://felipecypriano.com//images//2011/10/ipod-video-5.5.jpg">iPod Video 5.5G</a>,
I bought it after I regret buying a <a href="http://felipecypriano.com//imagess/2011/10/crap-mp3-player.jpg">crap mp3 player</a>.
Buy the time I didn&#8217;t realize but that was the sparkle that light up the
change in my way of thinking.</p>

<p>A couple years later I bought my first notebook, a Macbook Pro 13&#8221;. As I was
getting used to OS X I began to pay attention on the little details. I was
loving the way the whole system was designed, my notion of &#8220;works as expected&#8221;
has been changed to &#8220;simple, beautiful and flawlessly&#8221;. I didn&#8217;t need as much
as features as I could get, I needed only the best designed features. Even if
it meant to sacrifice the quantity of features available.</p>

<p>At this point I changed myself as a new user, I was expecting that all the
application I use was so well designed as OS X. If an application works but
isn&#8217;t well designed, it&#8217;s not good enough for me.</p>

<p>A year and a half ago I started to develop to iOS, I bought an iPhone later
on an iPad too. And this changed myself as a developer. Now what I develop
needs to meet my expectations of good design as well as quality of code without
forgetting the users expectations. The user is important and if he can&#8217;t use
something that is my fault not his.</p>

<p>Thank you Steve Jobs. I&#8217;ll stay hungry.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Core Data - How To Do a SELECT DISTINCT]]></title>
    <link href="http://felipecypriano.com//2011/09/21/core-data-how-to-do-a-select-distinct/"/>
    <updated>2011-09-21T15:44:00-03:00</updated>
    <id>http://felipecypriano.com//2011/09/21/core-data-how-to-do-a-select-distinct</id>
    <content type="html"><![CDATA[<p>Actually it&#8217;s pretty simple to do. But if you try just by reading the documentation you&#8217;ll find
the same problem I did.</p>

<p>Suppose that I need to get a list of all ages I had on my database to make a hierarchical
navigation that starts by selecting an age. If were working directly with the SQLite store I could
execute this SQL to do this:</p>

<figure class='code'><figcaption><span></span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='sql'><span class='line'><span class="k">SELECT</span> <span class="k">DISTINCT</span> <span class="n">AGE</span>
</span><span class='line'><span class="k">FROM</span> <span class="n">PEOPLE</span>
</span><span class='line'><span class="k">ORDER</span> <span class="k">BY</span> <span class="n">AGE</span>
</span></code></pre></td></tr></table></div></figure>




<!--more-->


<p>Since the project uses Core Data I shouldn&#8217;t access the underlying database store directly.
Use <a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html">NSFetchRequest</a>
class instead of plain SQL. The <code>returnsDistinctsResults</code> property seems to do the trick,
its documentation warns about a special consideration:</p>

<blockquote><p>Returns a Boolean value that indicates whether the fetch request returns only
distinct values for the fields specified by propertiesToFetch.</p>

<p><strong>Special Considerations</strong></p>

<p>This value is only used if a value has been set for <code>propertiesToFetch</code>.</p></blockquote>

<p>So it&#8217;s saying is that the <code>returnsDistinctsResults</code> only works if the property
<code>propertiesToFetch</code> is also set. Pretty straightforward. <strong>Except that there&#8217;s one more
- <em>undocumented</em> - property that needs to be set:</strong> <code>resultType</code>.</p>

<p>The default value of <code>resultType</code> is <code>NSManagedObjectResultType</code> which returns
the expected managed objects. To make the distinct results works the value needs to be set to
<code>NSDictionaryResultType</code>.</p>

<figure class='code'><figcaption><span>Select Distinct Using Core Data</span></figcaption><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">NSFetchRequest</span> <span class="o">*</span><span class="n">request</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSFetchRequest</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span>
</span><span class='line'><span class="n">NSEntityDescription</span> <span class="o">*</span><span class="n">entity</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSEntityDescription</span> <span class="nl">entityForName:</span><span class="s">@&quot;People&quot;</span> <span class="nl">inManagedObjectContext:</span><span class="n">managedObjectContext</span><span class="p">];</span>
</span><span class='line'><span class="n">request</span><span class="p">.</span><span class="n">entity</span> <span class="o">=</span> <span class="n">entity</span><span class="p">;</span>
</span><span class='line'><span class="n">request</span><span class="p">.</span><span class="n">propertiesToFetch</span> <span class="o">=</span> <span class="p">[</span><span class="n">NSArray</span> <span class="nl">arrayWithObject:</span><span class="p">[[</span><span class="n">entity</span> <span class="n">propertiesByName</span><span class="p">]</span> <span class="nl">objectForKey:</span><span class="s">@&quot;age&quot;</span><span class="p">]];</span>
</span><span class='line'><span class="n">request</span><span class="p">.</span><span class="n">returnsDistinctResults</span> <span class="o">=</span> <span class="n">YES</span><span class="p">;</span>
</span><span class='line'><span class="n">request</span><span class="p">.</span><span class="n">resultType</span> <span class="o">=</span> <span class="n">NSDictionaryResultType</span><span class="p">;</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSSortDescriptor</span> <span class="o">*</span><span class="n">sortDescriptor</span> <span class="o">=</span> <span class="p">[[</span><span class="n">NSSortDescriptor</span> <span class="n">alloc</span><span class="p">]</span> <span class="nl">initWithKey:</span><span class="s">@&quot;age&quot;</span> <span class="nl">ascending:</span><span class="n">YES</span><span class="p">];</span>
</span><span class='line'><span class="p">[</span><span class="n">request</span> <span class="nl">setSortDescriptors:</span><span class="p">[</span><span class="n">NSArray</span> <span class="nl">arrayWithObject:</span><span class="n">sortDescriptors</span><span class="p">]];</span>
</span><span class='line'><span class="p">[</span><span class="n">sortDescriptor</span> <span class="n">release</span><span class="p">];</span>
</span><span class='line'>
</span><span class='line'><span class="n">NSError</span> <span class="o">*</span><span class="n">error</span> <span class="o">=</span> <span class="nb">nil</span><span class="p">;</span>
</span><span class='line'><span class="n">NSArray</span> <span class="o">*</span><span class="n">distincResults</span> <span class="o">=</span> <span class="p">[</span><span class="n">managedObjectContext</span> <span class="nl">executeFetchRequest:</span><span class="n">request</span> <span class="nl">error:</span><span class="o">&amp;</span><span class="n">error</span><span class="p">];</span>
</span><span class='line'><span class="c1">// Use the results</span>
</span><span class='line'><span class="p">[</span><span class="n">request</span> <span class="n">release</span><span class="p">];</span>
</span></code></pre></td></tr></table></div></figure>


<p>If you forget to set the <code>resultType</code> there will be no errors, but the
<code>returnsDistinctResults</code> will be ignored and your result will have all
ages of the database.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Metro Is Going Towards the Right Road]]></title>
    <link href="http://felipecypriano.com//2011/09/20/metro-is-going-towards-the-right-road/"/>
    <updated>2011-09-20T12:00:00-03:00</updated>
    <id>http://felipecypriano.com//2011/09/20/metro-is-going-towards-the-right-road</id>
    <content type="html"><![CDATA[<p><em>Disclaimer: I don&#8217;t care about anything outside Metro on Windows 8
and this post is strictly about Metro apps.</em></p>

<p>The best thing about Metro is that it is being designed to be consistent. They&#8217;ve created
a set guidelines to show how a Metro app should behave and look like. (The <em>other best</em> thing
is that <a href="https://blogs.msdn.com/b/b8/archive/2011/09/14/metro-style-browsing-and-plug-in-free-html5.aspx">it doesn&#8217;t run Flash</a>)</p>

<p>It&#8217;s surprisingly good see how Microsoft is teaching Windows developers that in
order to build a great app they need to be consistent with the platform and Metro
is being built from the ground up to be consistent.</p>

<!--more-->


<p>Metro is going to throw away all the weight of crapness from Windows by starting new,
<a href="http://daringfireball.net/2011/09/metro" title="Daring Fireball - Metro">without legacy support</a>.
Metro is touch based, older Windows applications will run outside Metro. And maybe the ARM
version of Windows 8 will be Metro-only.</p>

<p>On the video <a href="http://channel9.msdn.com/events/BUILD/BUILD2011/BPS-1004">8 traits of great Metro style apps</a>
 Jensen Harris <a href="http://www.marco.org/2011/09/16/some-other-tablets-you-may-have-seen" title="Some Other Tablets You May Have Seen - Marco.org">walks you trought</a> 8 important concepts of Metro apps. If you&#8217;re going to develop for Metro, this video is mandatory.
 These are the 8 importants things to pay attention when developing for Metro, I&#8217;ll not explain
 each of them because what matters here is to understand that there&#8217;s something to follow
 to make the user experience good:</p>

<ol>
<li>Metro style design</li>
<li>Fast and fluid</li>
<li>Snap and scale beautifully</li>
<li>Use the right Contracts</li>
<li>Invest in a great Tile</li>
<li>Feel connected and alive</li>
<li>Roam to the cloud</li>
<li>Embrace Metro principles</li>
</ol>


<p>By creating <em>and teaching</em> these traits Microsft is aiming the user. As soon as they
learn how to navigate on IE10 and how to install a new app they will know how to use
all other apps, they won&#8217;t have to think. All the basic behaviors should be the same across the apps.</p>

<p>This consistence lead by the platform resulting in ease of use is what I see as
an iOS developer and user. Apple just started a set of design guidelines and
the community evolved these guidelines. Some of the current &#8220;standards&#8221; were
created by 3rd party apps, like the pull-to-refresh from Tweetie (now the official
Twitter app) and the action-button-centered-on-the-tab-bar popularized by Instagram.
 Android seems to lack this leadership from Google.</p>

<p>One more good think borrowed from Apple
<a href="http://arstechnica.com/business/news/2011/09/only-enterprise-and-developers-can-bypass-windows-store-for-metro-apps.ars">Metro apps will be distributed only on Windows Store</a>
and they will have to be approved.</p>

<blockquote><p>[Metro apps will be] Distributed through the Windows Store. Apps must pass certification so that
users download and try apps with confidence in their safety and privacy.
Side-loading is available for enterprises and developers.</p></blockquote>

<p>Microsoft is leading the way by creating a consistent experience. If the platform apps are great the users will expect the 3rd party apps to be <em>at least as great</em>. If Metro developers follow the design principles that Microsft is teaching I do believe a good contestant of the current iPad/iOS is coming. One year later tough.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[More Secure With Convenience]]></title>
    <link href="http://felipecypriano.com//2011/09/19/more-secure-with-convenience/"/>
    <updated>2011-09-19T00:33:00-03:00</updated>
    <id>http://felipecypriano.com//2011/09/19/more-secure-with-convenience</id>
    <content type="html"><![CDATA[<p>Having a &#8220;baked&#8221; blog, meaning every page is static baked by a system (Octopress)
on my machine before it goes to the server, makes it more secure. I realize that
I forget to talk about this on my <a href="http://felipecypriano.com//2011/09/16/why-ive-migrated-to-octopress/">last post</a>
when I was reading a post
<a href="http://blog.dreamhost.com/2011/09/13/dissecting-web-site-attacks-what-you-should-know/" title="Dissecting web site attacks: What you should know.">about security from Dreamhost</a>.</p>

<p>My previous configuration was Wordpress with more or less 10 plug-ins. Very
often I forget to update them all, so my blog for various periods of time was
vulnerable to known attacks until I remember to update everything. Of course
this isn&#8217;t Wordpress fault, it&#8217;s mine. I forget to update.</p>

<!--more-->


<p>By removing the Wordpress layer I&#8217;ve automatically made it more secure. There&#8217;s
nothing but the web server running, and it&#8217;s updated and maintained by my host.
There isn&#8217;t any script running that something or someone could use to gain access
to the server. In more technical terms I&#8217;ve reduced the <a href="http://en.wikipedia.org/wiki/Attack_surface">attack surface</a>.</p>

<p>Usually something more secure is less convenient. In my case I&#8217;m satisfied with
what I &#8220;lost&#8221;. I don&#8217;t have a dashboard to edit the blog anymore, I can&#8217;t create
a new post from anywhere that has a browser for example. I&#8217;ve traded this ability
to plain text files, that are completely portable, easy to edit, easy to backup,
easy to merge.</p>

<p>Now I start an idea on Evernote, writing down things I&#8217;d like to comment on a
notebook called &#8220;Blog&#8221;. Depending on my mood I even start to style the post on
Evernote using Markdown. When I think that the idea is good enough I copy and
paste the note from Evernote to my text editor and finalize the post.</p>

<p>This is how the first paragraph of this post looks like on the text editor:</p>

<figure class='code'><figcaption><span>Inception </span></figcaption>
<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>---
</span><span class='line'>layout: post
</span><span class='line'>title: "More Secure More Convenient"
</span><span class='line'>date: 2011-09-19 00:33
</span><span class='line'>comments: true
</span><span class='line'>categories: [My Thoughts]
</span><span class='line'>---
</span><span class='line'>
</span><span class='line'>Having a "baked" blog, meaning every page is static baked by a system (Octopress)
</span><span class='line'>on my machine before it goes to the server, makes it more secure. I realize that
</span><span class='line'>I forget to talk about this on my [last post](/2011/09/16/why-ive-migrated-to-octopress/)
</span><span class='line'>when I was reading a post
</span><span class='line'>[about security from Dreamhost](http://blog.dreamhost.com/2011/09/13/dissecting-web-site-attacks-what-you-should-know/ "Dissecting web site attacks: What you should know.").
</span><span class='line'>
</span><span class='line'>By removing the Wordpress</span></code></pre></td></tr></table></div></figure>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why I've Migrated to Octopress]]></title>
    <link href="http://felipecypriano.com//2011/09/16/why-ive-migrated-to-octopress/"/>
    <updated>2011-09-16T10:58:00-03:00</updated>
    <id>http://felipecypriano.com//2011/09/16/why-ive-migrated-to-octopress</id>
    <content type="html"><![CDATA[<p>In simples terms: to make the whole blog simpler. I don&#8217;t need a database to run my blog anymore.
I don&#8217;t need any server technology. Everything I need is a &#8220;plain old web server&#8221;.</p>

<p><a href="http://twitter.com/mattgemmell">Matt Gemmell</a> motivated me to use <a href="http://octopress.org/">Octopress</a> in his post
<a href="http://mattgemmell.com/2011/09/12/blogging-with-octopress/">Blogging With Octopress</a>. Unlike him
I&#8217;ve never been <a href="http://fireballed.org/">fireballed</a> and I think I&#8217;m far from it, so his arguments
about the improvements under high load doesn&#8217;t apply to my blog. Yet. I hope.</p>

<p>I&#8217;ve chosen Octopress over Wordpress self hosted because I don&#8217;t need to worry about caching (I was
using W3 Total Cache) and I don&#8217;t depend on MySQL not even PHP to run the blog. <!--more--> All the technology
I need is running on my Mac: Ruby 1.9.2 (<a href="https://github.com/sstephenson/rbenv">rbenv</a>),
Python, Octopress, and a text editor (<a href="http://www.sublimetext.com/2">Sublime Text 2</a>) to write using
<a href="http://daringfireball.net/projects/markdown/">Markdown</a> format.</p>

<p>To migrate my posts from Wordpress to Markdonw format text files I&#8217;ve used
<a href="https://github.com/thomasf/exitwp">exitwp</a>, which is a Python script that converts all your
posts and pages from the XML exported by Wordpress. I had to manually edit the generated files,
because exitwp doesn&#8217;t seem to understand some paragraphs and other little things of my old posts.
My laziness of writing have helped me in this task, I&#8217;ve very few publisheds posts. If you blog is
bigger it&#8217;s a good idea to edit the script so it correctly migrate your posts.</p>

<p>The comment system now uses <a href="http://www.disqus.com">Disqus</a>. Just import your old comments into
Disqus using the Wordpress plugin, and keep the same url for your posts to not lose any comments.</p>

<p>The blog is now &#8220;baked&#8221; by Octopress which is built around <a href="https://github.com/mojombo/jekyll">Jekyll</a>.
It&#8217;s super easy to run the blog locally and deploy it to your server, Octopress will generate all
the static web site for you. Two commands and your site is up and running:</p>

<figure class='code'><div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class=''><span class='line'>bundle exec rake generate
</span><span class='line'>bundle exec rake deploy</span></code></pre></td></tr></table></div></figure>


<p>So I don&#8217;t need to handle caching anymore. The default theme is beautiful. Adjusts automatically to
fit screens of all sizes. Integrates with Github, Twitter and more. Simple. Easy. Stunning. Love it.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Native Code is The Current Winner]]></title>
    <link href="http://felipecypriano.com//2011/06/14/native-code-is-the-current-winner/"/>
    <updated>2011-06-14T09:43:30-03:00</updated>
    <id>http://felipecypriano.com//2011/06/14/native-code-is-the-current-winner</id>
    <content type="html"><![CDATA[<p>Michael Mahemoff on <a href="http://www.html5rocks.com/en/mobile/nativedebate.html">HTML5 vs native</a>:</p>

<blockquote><p>It would be nice to declare a winner here, but right now, there is no
clear winner. Some apps are best suited for native and some are best
suited for the web.</p></blockquote>

<p>I don&#8217;t think Mahemoff read his own article before writing the
conclusion. Native won most of the categories: Feature Richness,
Performance, Developer Experience, Look-And-Feel, Discoverability and
Monetization. Of course HTML5 is catching up but it&#8217;s not an even yet,
native is still better to build great apps.</p>

<p>One last think, in my opinion PhoneGap is a huge workaround and should
never be used.</p>

<p><a href="http://www.html5rocks.com/en/mobile/nativedebate.html">HTML5 vs Native: The Mobile App Debate</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Why Windows 8 Isn't That Great]]></title>
    <link href="http://felipecypriano.com//2011/06/13/why-windows-8-isnt-that-great/"/>
    <updated>2011-06-13T23:15:25-03:00</updated>
    <id>http://felipecypriano.com//2011/06/13/why-windows-8-isnt-that-great</id>
    <content type="html"><![CDATA[<p>Aaron Holesgrove wrote <a href="http://www.businessinsider.com/why-windows-8-is-not-fundamentally-flawed-as-a-response-to-the-ipad-2011-6" title="Why Windows 8 Is Not Fundamentally Flawed As A Response To The iPad  Read more: http://www.businessinsider.com/why-windows-8-is-not-fundamentally-flawed-as-a-response-to-the-ipad-2011-6">a response to John Gruber&#8217;s</a>
post about <a href="http://daringfireball.net/2011/06/windows_8_fundamentally_flawed">&#8220;Why Windows 8 Is Fundamentally Flawed as a Response to the iPad&#8221;</a>
telling his thoughts on why Windows 8 isn&#8217;t flawed at all. I recommend
you read both articles to get the bigger picture before continuing here.</p>

<!--more-->


<p>Aaron starts by agreeing with John about Windows 8 appearance which I
also agree it looks really good. And just after this he begins to fire
counter arguments to protect Windows 8 ideas at all costs. Not being
fair in some points.</p>

<blockquote><p>If Apple never released the iPhone, we’d be sitting here today talking
about how if it weren’t for Android, those three companies [Nokia,
Palm/HP and Microsoft] wouldn’t be making all of those same changes or
something like that – the crippling of those companies was always
inevitable. Or perhaps in your case John, you’d be saying it was the
Mac and Mac OS X that proudly toppled those giants instead because
Android wasn’t made by Apple and therefore doesn’t warrant the same
amount of credit or boasting on your part.</p></blockquote>

<p>This is fundamentally wrong because Aaron assumes that Android would
have innovate in the exact same way as the iPhone did even if Apple
never had released the iPhone. Android in 2007, before the iPhone,
<a href="http://www.engadget.com/2007/11/12/a-visual-tour-of-androids-ui/" title="A Visual Tour of Android UI (pre-iPhone) - Engadget">was more like the Blackberry</a>,
completely different than today&#8217;s version. It completely changed to the
current form after the iPhone was announced and I believe
<a href="http://www.youtube.com/watch?v=89xc_1Vv69k">Vika Gupta confirmed that change on the second day keynote of Google IO 2010. Hesaid at 2&#8217;45&#8221;</a>:</p>

<blockquote><p>He argued that if Google did not act, we faced a Draconian future, a
future where one man, one company, one device, one carrier would be
our only choice.</p></blockquote>

<p>Since Apple showed how to make incredible useful multitouch interface
the standard has changed from RIM&#8217;s Blackberry style to iPhone&#8217;s.</p>

<p>It&#8217;s impossible to assume that the IT industry would be in the same
situation that it&#8217;s today without the iPhone. Apple reinvented the touch
interfaces, without the iPhone we would be using touch interfaces as the
secondary way to interact with cellphones and notebooks. Do you remember
Microsoft&#8217;s first attempt to build tablets, those notebooks with
reversible touchscreen and the terrible stylus? It was so bad UI and UX
that I can&#8217;t believe we would have today full fledge multitouch
interfaces without Apple&#8217;s first release of the iPhone.</p>

<p>Another mistake is believing that iOS is simply another version of Mac
OS X with a touch interface on top of it. Just like Metro on Windows 8
seems to be.</p>

<blockquote><p>Guess what Windows 8 for tablets is? You guessed it – the core of
Windows – MinWin – with an alternate shell to Win32 on top that is
touch friendly – the ‘Metro’ immersive shell we saw today.</p></blockquote>

<p>Windows 8 is the exact same Windows as we know it, meaning a desktop OS,
with a new separate touch interface called Metro. The interface is so
separate that it looks like another program that can be installed on top
of Windows. So I guess Metro is for Windows 8 what Windows 3.11 was for
DOS.</p>

<p>Just because Mac OS X is based on Unix it doesn&#8217;t mean that it&#8217;s Unix.
And just because iOS is based on Mac OS X doesn&#8217;t mean it is Mac OS X.
Windows 8 and Metro aren&#8217;t two distinct operating systems with latter
been based on the former, Metro is just a new UI on top of the plain old
Windows.</p>

<p>Now on Office.</p>

<blockquote><p>John, I don’t know what has you so convinced that Office has to look
different in order to qualify that it belongs on a tablet. The ribbon
interface was supposed to introduce bigger buttons along the top <a href="http://www.youtube.com/watch?v=AX0g1OtuNx4">so
that menu options were easier to
find</a> and I think a lot of
users don’t want to re-learn how to navigate Office all over again –
they just want it pretty much the way it is on the devices they want
to use.</p></blockquote>

<p>I love the Ribbon interface, really I think it&#8217;s great and it&#8217;s years
ahead of the other Office tools. The Ribbon interface is really good and
its options are easier to find, but this doesn&#8217;t mean that this
interface is suitable for tablets and multitouch interfaces. At this
point it&#8217;s clear that Aaron sees tablets as just touchscreen monitors.</p>

<blockquote><p>What neither of those applications [iWork and Google Apps] are,
though, is Microsoft Office – say all you want about Microsoft
products but Office has no peers, particularly in the enterprise, and
has three times the amount of features of anything else.</p></blockquote>

<p>And then he stop talking about good interface for tablets and compares
who has more features.</p>

<p>Aaron is defending Microsoft so vigorously that he didn&#8217;t notice - or
didn&#8217;t mind - the flaws in his arguments. I don&#8217;t think Windows 8 will
be a failure. What I wanted to show here is how wrong Aaron interpreted
Gruber&#8217;s post.</p>

<p>If you&#8217;re curious about Grube&#8217;s response to Aaron:
<a href="http://daringfireball.net/linked/2011/06/12/i-like-my-odds"> I Like My Odds in This Argument</a>.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Dear Flash Give Up the Mobile World]]></title>
    <link href="http://felipecypriano.com//2011/04/15/dear-flash-give-up-the-mobile-world/"/>
    <updated>2011-04-15T12:00:28-03:00</updated>
    <id>http://felipecypriano.com//2011/04/15/dear-flash-give-up-the-mobile-world</id>
    <content type="html"><![CDATA[<p>Neil McAllister:</p>

<blockquote><p>Scrolling the screen is a particularly egregious example. (&#8230;) users
swipe with their fingers to scroll. Flash apps don&#8217;t seem to
understand this concept. Once a Flash movie loads into part of the
browser window, that part of the window no longer responds to swipes.</p></blockquote>

<p>As developers it&#8217;s our job to <strong>deliver a great user experience</strong>,
nowadays Flash is exactly the opposite.</p>

<p>Source: <a href="http://www.infoworld.com/d/mobile-technology/flash-android-look-dont-touch-838?page=0,1" title="Flash on Android: Look byt don't touch">InfoWorld</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Impressions about Objective-C]]></title>
    <link href="http://felipecypriano.com//2010/06/01/impressions-about-objective-c/"/>
    <updated>2010-06-01T14:59:32-03:00</updated>
    <id>http://felipecypriano.com//2010/06/01/impressions-about-objective-c</id>
    <content type="html"><![CDATA[<p>I&#8217;m starting to learn
<a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163-CH1-SW2" title="The Objective-C Programing Language">Objective-C reading Apple&#8217;s documentation</a>
and it&#8217;s very interesting to see features that I love in Groovy are
available to use in such old-school language
(<a href="http://en.wikipedia.org/wiki/Objective-C#History" title="Objective-C History">30 years old</a>).
At a first sight I thought this would be a painful language to learn
without any cool feature from modern language like Ruby and Python. Also
thought Apple doesn&#8217;t care about its programming language as much as the
user experience, fortunately I was wrong.</p>

<!--more-->


<p>The strangest thing for those who have a Java background is how methods
are called it doesn&#8217;t uses dot notation. See the between a method call
&#8216;workHard&#8217; on an &#8216;employee&#8217; object and instantiate a new instance in
Java and Objective-C:</p>

<table class="color-table">
    <thead>
        <tr>
            <th>Java</th>
            <th>Objective-C</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>new Employee()</td>
            <td>[[Employee alloc] init];</td>
        </tr>
        <tr>
            <td>employee.workHard();</td>
            <td>[employee workHard];</td>
        </tr>
    </tbody>
</table>


<h2>A Dynamic Language</h2>

<p>Just like Groovy the Objective-C language defers as many decisions as it
can from compile time and link time to runtime. That means among other
things that in order to run a code you need not only a compiler but also
a runtime system to execute the compiled code, this system is already
ready to use in iPhone OS and Mac OS X.</p>

<p>It&#8217;s possible to declare a variable as strong or weak type, giving us tremendous flexibility.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="n">NSString</span> <span class="o">*</span><span class="n">myVar</span><span class="p">;</span> <span class="c1">// strong typing</span>
</span><span class='line'><span class="kt">id</span> <span class="n">myVar</span><span class="p">;</span> <span class="c1">// weak typing</span>
</span></code></pre></td></tr></table></div></figure>


<p>The examples above shows how to declare variables to store objects, the
first myVar reference accepts only NSString instances and the later
accepts any object. The <em>id</em> is just like Groovy&#8217;s <em>def</em> keyword. Using
the <em>id</em> means that we can send a message to that instance even if the
compiler can&#8217;t guarantee it&#8217;ll work.</p>

<p>(Yes! I thing the word &#8216;id&#8217; meaning anything isn&#8217;t good.)</p>

<h3>Categories</h3>

<p>Categories let&#8217;s you add method to an existing class, even if you
haven&#8217;t the source. This is my favorite similaraty with Groovy I found
so far I hope there&#8217;s many more to come, see a sneak peek of the syntax:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='objc'><span class='line'><span class="k">@interface</span> <span class="nc">ClassName</span> <span class="p">(</span> <span class="n">CategoryName</span> <span class="p">)</span>
</span><span class='line'><span class="c1">// new methods declarations</span>
</span><span class='line'><span class="k">@end</span>
</span></code></pre></td></tr></table></div></figure>


<h2>Protocols, the Java&#8217;s Interface</h2>

<p>Just like Java&#8217;s interface, so no need to explain it here, except for
one cool feature: we can declare optional methods. In practice when one
of our classes conforms to a protocol that define options methods we
aren&#8217;t obligated to implement them.</p>

<h2>More Resources</h2>

<p>Besides the language itself Cocoa and Cocoa Touch - Apple&#8217;s framework
for Mac OS X and iPhone OS respectively - are great frameworks with lots
of functionalities, it&#8217;s easy to build beautiful applications.
Interested in learning more about Objective-C? Take a look at this docs:</p>

<ul>
<li><a href="http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/Learning_Objective-C_A_Primer/">Learning Objective-C: A Premier</a></li>
<li><a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163-CH1-SW2">Objective-C Programming Guide</a></li>
<li><a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CocoaFundamentals/Introduction/Introduction.html">Cocoa Fundamentals Guide</a></li>
</ul>


<p>Have fun improving your programming skills.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Synchronizing IntelliJ IDEA dependencies with Gradle [Updated]]]></title>
    <link href="http://felipecypriano.com//2010/04/08/synchronizing-intellij-idea-dependencies-with-gradle/"/>
    <updated>2010-04-08T10:45:58-03:00</updated>
    <id>http://felipecypriano.com//2010/04/08/synchronizing-intellij-idea-dependencies-with-gradle</id>
    <content type="html"><![CDATA[<p>Based on the great post
<a href="http://techscursions.blogspot.com/2010/04/here-i-am-once-again-facing-legacy.html">Managing IntelliJ dependencies with Gradle</a>
from Kallin Nagelberg, I&#8217;ve came to a solution about how to manage
Intellij IDEA dependencies on projects that uses .idea directory as its
structure.</p>

<!--more-->


<p>Basically instead of changing <em>your_project.iml</em> file we
create a xml file in <em>.idea/libraries</em> with the jars listed on it. For
more details, please, read Kallin&#8217;s post. This is the task:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="c1">// IntelliJ IDEA configuration sync</span>
</span><span class='line'><span class="n">task</span> <span class="n">intellijSync</span> <span class="o">&lt;&lt;</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">final</span> <span class="n">def</span> <span class="n">librariesDir</span> <span class="o">=</span> <span class="k">new</span> <span class="n">File</span><span class="o">(</span><span class="s">&quot;.idea${File.separator}libraries&quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="n">librariesDir</span><span class="o">.</span><span class="na">mkdirs</span><span class="o">()</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">final</span> <span class="n">def</span> <span class="n">gradleLibsPath</span> <span class="o">=</span> <span class="n">configurations</span><span class="o">.</span><span class="na">compile</span><span class="o">.</span><span class="na">asPath</span>
</span><span class='line'>    <span class="kd">final</span> <span class="n">def</span> <span class="n">userHomeGradle</span> <span class="o">=</span> <span class="n">project</span><span class="o">.</span><span class="na">gradle</span><span class="o">.</span><span class="na">gradleUserHomeDir</span>
</span><span class='line'>    <span class="kd">final</span> <span class="n">def</span> <span class="n">libsJar</span> <span class="o">=</span> <span class="n">gradleLibsPath</span><span class="o">.</span><span class="na">split</span><span class="o">(</span><span class="n">File</span><span class="o">.</span><span class="na">pathSeparator</span><span class="o">).</span><span class="na">collect</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">it</span><span class="o">.</span><span class="na">replaceAll</span> <span class="n">userHomeGradle</span><span class="o">.</span><span class="na">path</span><span class="o">,</span> <span class="s">&quot;\\\$USER_HOME_GRADLE\\\$&quot;</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>    <span class="n">println</span> <span class="n">libsJar</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">final</span> <span class="n">def</span> <span class="n">gradleLibXml</span> <span class="o">=</span> <span class="k">new</span> <span class="n">File</span><span class="o">(</span><span class="n">librariesDir</span><span class="o">,</span> <span class="err">&#39;</span><span class="n">Gradle_Libraries</span><span class="o">.</span><span class="na">xml</span><span class="err">&#39;</span><span class="o">)</span>
</span><span class='line'>    <span class="n">gradleLibXml</span><span class="o">.</span><span class="na">write</span> <span class="s">&quot;&quot;&quot;</span>
</span><span class='line'><span class="s">&lt;component name=&quot;</span><span class="n">libraryTable</span><span class="s">&quot;&gt;</span>
</span><span class='line'><span class="s">  &lt;library name=&quot;</span><span class="n">Gradle</span> <span class="n">Libraries</span><span class="s">&quot;/&gt;</span>
</span><span class='line'><span class="s">&lt;/component&gt;</span>
</span><span class='line'><span class="s">&quot;&quot;&quot;</span>
</span><span class='line'>    <span class="kd">final</span> <span class="n">def</span> <span class="n">xmlRoot</span> <span class="o">=</span> <span class="k">new</span> <span class="n">XmlParser</span><span class="o">().</span><span class="na">parse</span><span class="o">(</span><span class="n">gradleLibXml</span><span class="o">)</span>
</span><span class='line'>    <span class="kd">final</span> <span class="n">def</span> <span class="n">classesNode</span> <span class="o">=</span> <span class="n">xmlRoot</span><span class="o">.</span><span class="na">library</span><span class="o">[</span><span class="mi">0</span><span class="o">].</span><span class="na">appendNode</span><span class="o">(</span><span class="err">&#39;</span><span class="n">CLASSES</span><span class="err">&#39;</span><span class="o">)</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">libsJar</span><span class="o">.</span><span class="na">each</span> <span class="o">{</span> <span class="n">jar</span> <span class="o">-&gt;</span>
</span><span class='line'>        <span class="n">classesNode</span><span class="o">.</span><span class="na">appendNode</span><span class="o">(</span><span class="err">&#39;</span><span class="n">root</span><span class="err">&#39;</span><span class="o">,</span> <span class="o">[</span><span class="nl">url:</span> <span class="s">&quot;jar://$jar!/&quot;</span><span class="o">])</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="n">writer</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringWriter</span><span class="o">()</span>
</span><span class='line'>    <span class="k">new</span> <span class="nf">XmlNodePrinter</span><span class="o">(</span><span class="k">new</span> <span class="n">PrintWriter</span><span class="o">(</span><span class="n">writer</span><span class="o">)).</span><span class="na">print</span><span class="o">(</span><span class="n">xmlRoot</span><span class="o">)</span>
</span><span class='line'>    <span class="n">println</span> <span class="n">writer</span><span class="o">.</span><span class="na">toString</span><span class="o">()</span>
</span><span class='line'>    <span class="n">gradleLibXml</span><span class="o">.</span><span class="na">write</span> <span class="n">writer</span><span class="o">.</span><span class="na">toString</span><span class="o">()</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The complete <a href="http://gist.github.com/360092">build.gradle file is on Gist</a>.</p>

<p><strong># Update 1</strong></p>

<p>Check the <a href="http://gist.github.com/360092">file on Gist</a>, I&#8217;ve updated it to sync test
libraries as well.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Grails with ZK: How To Create Components in Runtime with The Builder]]></title>
    <link href="http://felipecypriano.com//2010/03/04/grails-with-zk-how-to-create-components-in-runtime-with-the-builder/"/>
    <updated>2010-03-04T11:42:30-03:00</updated>
    <id>http://felipecypriano.com//2010/03/04/grails-with-zk-how-to-create-components-in-runtime-with-the-builder</id>
    <content type="html"><![CDATA[<p><a href="http://code.google.com/p/zkgrails/">ZKGrails</a> plugin has an extremely
nice feature which make easier than ever to create ZK components in
runtime.</p>

<!--more-->


<p>First lets see how is the usual way to create components in
runtime using Java. This is the zul representation of what we’re trying
to achieve:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;div&gt;</span>
</span><span class='line'>  <span class="nt">&lt;toolbarbutton</span> <span class="na">label=</span><span class="s">&quot;Clickable Item&quot;</span> <span class="na">href=</span><span class="s">&quot;#&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>  <span class="nt">&lt;toolbarbutton</span> <span class="na">image=</span><span class="s">&quot;edit.png&quot;</span> <span class="na">if=</span><span class="s">&quot;${userHasAccess}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>  <span class="nt">&lt;toolbarbutton</span> <span class="na">image=</span><span class="s">&quot;delete.png&quot;</span> <span class="na">if=</span><span class="s">&quot;${userHasAccess}&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'><span class="nt">&lt;/div&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>And the java code to do the same:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">Div</span> <span class="n">div</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Div</span><span class="o">();</span>
</span><span class='line'><span class="n">Toolbarbutton</span> <span class="n">tbb</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Toolbarbutton</span><span class="o">(</span><span class="s">&quot;Clickable Item&quot;</span><span class="o">);</span>
</span><span class='line'><span class="n">tbb</span><span class="o">.</span><span class="na">setHref</span><span class="o">(</span><span class="s">&quot;#&quot;</span><span class="o">);</span>
</span><span class='line'><span class="n">tbb</span><span class="o">.</span><span class="na">setParent</span><span class="o">(</span><span class="n">div</span><span class="o">);</span>
</span><span class='line'><span class="k">if</span> <span class="o">(</span><span class="n">userHasAccess</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">Toolbarbutton</span> <span class="n">tbbEdit</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Toolbarbutton</span><span class="o">();</span>
</span><span class='line'>    <span class="n">tbbEdit</span><span class="o">.</span><span class="na">setImage</span><span class="o">(</span><span class="s">&quot;edit.png&quot;</span><span class="o">);</span>
</span><span class='line'>    <span class="n">tbbEdit</span><span class="o">.</span><span class="na">setParent</span><span class="o">(</span><span class="n">div</span><span class="o">);</span>
</span><span class='line'>    <span class="n">Toolbarbutton</span> <span class="n">tbbDelete</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Toolbarbutton</span><span class="o">();</span>
</span><span class='line'>    <span class="n">tbbDelete</span><span class="o">.</span><span class="na">setImage</span><span class="o">(</span><span class="s">&quot;delete.png&quot;</span><span class="o">);</span>
</span><span class='line'>    <span class="n">tbbDelete</span><span class="o">.</span><span class="na">setParent</span><span class="o">(</span><span class="n">div</span><span class="o">);</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can easily see the java code is pretty verbose. Using ZKBuilder
the code will be easier to understand and smaller, take a look:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">Div</span> <span class="n">div</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Div</span><span class="o">();</span>
</span><span class='line'><span class="n">div</span><span class="o">.</span><span class="na">append</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">toolbarbutton</span><span class="o">(</span><span class="nl">label:</span> <span class="s">&quot;Clickable Item&quot;</span><span class="o">,</span> <span class="nl">href:</span> <span class="s">&quot;#&quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="k">if</span> <span class="o">(</span><span class="n">userHasAccess</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">toolbarbutton</span><span class="o">(</span><span class="nl">image:</span> <span class="s">&quot;edit.png&quot;</span><span class="o">)</span>
</span><span class='line'>        <span class="n">toolbarbutton</span><span class="o">(</span><span class="nl">image:</span> <span class="s">&quot;delete.png&quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Which one do your prefer? :) If you want more continue reading to see
how to use the builder as a template to a list of items.</p>

<h2>Using ZKBuilder as a Template</h2>

<p>I think the builder is specially useful when I’ve a list of items and I
need to display these items with a specific layout one that the grid nor
the listbox <a href="http://www.zkoss.org/smalltalks/databind4/databind4.dsp">can provide</a>.
So I start as I started this post making the layout on a zul page and after I
get what I want, I made a ZKBuilder version and put it in a loop to
execute for each item I need to show.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="c1">// ...</span>
</span><span class='line'><span class="n">def</span> <span class="n">items</span> <span class="o">=</span> <span class="n">myService</span><span class="o">.</span><span class="na">getItems</span><span class="o">()</span>
</span><span class='line'><span class="n">items</span><span class="o">.</span><span class="na">each</span> <span class="o">{</span> <span class="n">item</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="n">div</span><span class="o">.</span><span class="na">append</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">toolbarbutton</span><span class="o">(</span><span class="nl">label:</span> <span class="n">item</span><span class="o">.</span><span class="na">name</span><span class="o">,</span> <span class="nl">href:</span>  <span class="n">item</span><span class="o">.</span><span class="na">link</span><span class="o">)</span>
</span><span class='line'>        <span class="k">if</span> <span class="o">(</span><span class="n">userHasAccess</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">toolbarbutton</span><span class="o">(</span><span class="nl">image:</span> <span class="s">&quot;edit.png&quot;</span><span class="o">,</span> <span class="nl">onClick:</span> <span class="o">{</span><span class="n">editItem</span> <span class="n">item</span><span class="o">})</span>
</span><span class='line'>            <span class="n">toolbarbutton</span><span class="o">(</span><span class="nl">image:</span> <span class="s">&quot;delete.png&quot;</span><span class="o">,</span> <span class="nl">onClick:</span> <span class="o">{</span><span class="n">deleteItem</span> <span class="n">item</span><span class="o">})</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span><span class='line'><span class="c1">//...</span>
</span><span class='line'><span class="kd">public</span> <span class="n">def</span> <span class="nf">editItem</span><span class="o">(</span><span class="n">def</span> <span class="n">item</span><span class="o">)</span> <span class="o">{</span> <span class="cm">/* code to edit the item */</span><span class="o">}</span>
</span><span class='line'><span class="kd">public</span> <span class="n">def</span> <span class="nf">deletetem</span><span class="o">(</span><span class="n">def</span> <span class="n">item</span><span class="o">)</span> <span class="o">{</span> <span class="cm">/* code to delete the item */</span><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>ZkBuilder is bundled in ZKGrails 0.7.6 and earlier. Enjoy!</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Handling events on ZK macro components]]></title>
    <link href="http://felipecypriano.com//2010/01/15/handling-events-on-zk-macro-components/"/>
    <updated>2010-01-15T07:41:55-02:00</updated>
    <id>http://felipecypriano.com//2010/01/15/handling-events-on-zk-macro-components</id>
    <content type="html"><![CDATA[<p>One of the great features of ZK is the possibility to create a new
component based only on other existing components using the declarative
language ZUML - the same you&#8217;ve been using in you zul files. Those are
called Macro Components. The beauty of macro components is that it&#8217;s
very easy to make a new one and hence avoid code duplication on your
pages.</p>

<!--more-->


<p>Talk about how to implement a macro component isn&#8217;t the scope of
this post, so I suggest you to read the
<a href="http://docs.zkoss.org/wiki/Macro_Component" title="ZK - Macro Component - Documentation">documentation</a>
before continue reading. We&#8217;ll work on a very simple macro component.
But, don&#8217;t hesitate when you need to make a very complex one. Our macro
component is this:</p>

<figure class='code'><figcaption><span>WEB-INF/mymacro.zul  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;hbox&gt;</span>
</span><span class='line'>    <span class="nt">&lt;label</span> <span class="na">id=</span><span class="s">&quot;myLabel&quot;</span> <span class="na">value=</span><span class="s">&quot;Click the button&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;button</span> <span class="na">id=</span><span class="s">&quot;myButton&quot;</span> <span class="na">label=</span><span class="s">&quot;The Button&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'><span class="nt">&lt;/hbox&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now that you already knows what is and how to make a macro component,
let&#8217;s start talking about what you may think it&#8217;ll work but actually it
doesn&#8217;t.</p>

<h2>Automatically Forward Event Doesn&#8217;t Work</h2>

<p>The standard way to <a href="docs.zkoss.org/wiki/ZK_MVC_Made_Easy" title="ZK - MVC Made Easy">register events on ZK&#8217;s component, using </a>
<a href="docs.zkoss.org/wiki/ZK_MVC_Made_Easy" title="ZK - MVC Made Easy">MVC</a>,
is to use the &#8220;autowire&#8221; feature of GenericForwardComposer. Like this:</p>

<figure class='code'><figcaption><span>WEB-INF/index.zul  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
</span><span class='line'><span class="nt">&lt;zk&gt;</span>
</span><span class='line'>    <span class="nt">&lt;window</span> <span class="na">apply=</span><span class="s">&quot;com.felipecypriano.IndexComposer&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>        <span class="nt">&lt;label</span> <span class="na">id=</span><span class="s">&quot;lblOutside&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;button</span> <span class="na">id=</span><span class="s">&quot;btnOutside&quot;</span> <span class="na">label=</span><span class="s">&quot;Not In Macro&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/window&gt;</span>
</span><span class='line'><span class="nt">&lt;/zk&gt;</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>src/com/felipecypriano/IndexComposer.java  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">package</span> <span class="n">com</span><span class="o">.</span><span class="na">felipecypriano</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zk.ui.util.GenericForwardComposer</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zul.Label</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">IndexComposer</span> <span class="kd">extends</span> <span class="n">GenericForwardComposer</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">private</span> <span class="n">Label</span> <span class="n">lblOutside</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="n">onClick$btnOutside</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">lblOutside</span><span class="o">.</span><span class="na">setValue</span><span class="o">(</span><span class="s">&quot;Button &#39;not in macro&#39; clicked&quot;</span><span class="o">);</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The button &#8216;btnOutside&#8217; belongs to the window component and therefore
its composer can automatically access and configure btnOutside events.
When the Composer is initializing it detects the method
onClick$btnOutside() and sets this method as a &#8216;btnOutside&#8217; onClick
event listener, in other words when the component is clicked this method
is executed.</p>

<p>Knowing this we can think that add an event to &#8216;myButton&#8217;,
which belongs to &#8216;mymacro&#8217;, is pretty much the same. Don&#8217;t we? Let&#8217;s add
the macro in the index.zul and create a new method to handle onClick
event of &#8216;myButton&#8217;:</p>

<figure class='code'><figcaption><span>WEB-INF/index.zul  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
</span><span class='line'><span class="cp">&lt;?component name=&quot;mymacro&quot; macroURI=&quot;/mymacro.zul&quot; ?&gt;</span>
</span><span class='line'><span class="nt">&lt;zk&gt;</span>
</span><span class='line'>    <span class="nt">&lt;window</span> <span class="na">apply=</span><span class="s">&quot;com.felipecypriano.IndexComposer&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>        <span class="nt">&lt;label</span> <span class="na">id=</span><span class="s">&quot;lblOutside&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;button</span> <span class="na">id=</span><span class="s">&quot;btnOutside&quot;</span> <span class="na">label=</span><span class="s">&quot;Not In Macro&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;separator</span> <span class="na">bar=</span><span class="s">&quot;true&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;mymacro/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/window&gt;</span>
</span><span class='line'><span class="nt">&lt;/zk&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Just before <em>&lt;zk></em> tag is the macro component definition (line 2), this
directive tells to ZK where is the file that contains the component -
<em>macroURI=&#8221;/mymacro.zul&#8221;</em> - and what&#8217;s the tag name we&#8217;ll use -
<em>name=&#8221;mymacro&#8221;</em> - to reference our component (line 8).</p>

<p>Adding the
onClick event on &#8216;myButton&#8217; cannot be done by just adding this method to
IndexComposer:</p>

<figure class='code'><figcaption><span>src/com/felipecypriano/IndexComposer.java  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="n">onClick$myButton</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">lblOutside</span><span class="o">.</span><span class="na">setValue</span><span class="o">(</span><span class="s">&quot;Button &#39;myButton&#39; clicked&quot;</span><span class="o">);</span>
</span><span class='line'>    <span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>So, if this doesn&#8217;t work what should I do?</p>

<h2>Using a Speciliazed Class</h2>

<p>To take care of mymacro&#8217;s behavior we need to write some code. We need
to create a class to specifically handle our component&#8217;s needs. The
purpose of mymacro component is to update the label when the button is
clicked, this will not affect any of the components that doesn&#8217;t belong
to mymacro so it makes sense to put the event in a specialized class
that can be used to all pages.</p>

<p>The two components inside the macro - label and button - will be accessed using
<a href="http://www.zkoss.org/javadoc/3.6.1/zk/org/zkoss/zk/ui/Component.html#getFellow(java.lang.String">getFellow(string)</a>)
method and the
<a href="http://docs.zkoss.org/wiki/Event_listening_&amp;_processing#Add_and_Remove_Event_Listeners_Dynamically">event will be dynamically added</a>
to the button using
<a href="http://www.zkoss.org/javadoc/3.6.1/zk/org/zkoss/zk/ui/Component.html#addEventListener(java.lang.String,%20org.zkoss.zk.ui.event.EventListener">addEventListener</a>)
method.</p>

<figure class='code'><figcaption><span>src/com/felipecypriano/MyMacro.java  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">package</span> <span class="n">com</span><span class="o">.</span><span class="na">felipecypriano</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zk.ui.HtmlMacroComponent</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zk.ui.event.*</span><span class="o">;</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zul.*</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">MyMacro</span> <span class="kd">extends</span> <span class="n">HtmlMacroComponent</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">private</span> <span class="n">Label</span> <span class="n">myLabel</span><span class="o">;</span>
</span><span class='line'>
</span><span class='line'>    <span class="nd">@Override</span>
</span><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">afterCompose</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="kd">super</span><span class="o">.</span><span class="na">afterCompose</span><span class="o">();</span> <span class="c1">// DON&#39;T forget this</span>
</span><span class='line'>        <span class="n">Button</span> <span class="n">myButton</span> <span class="o">=</span> <span class="o">(</span><span class="n">Button</span><span class="o">)</span> <span class="n">getFellow</span><span class="o">(</span><span class="s">&quot;myButton&quot;</span><span class="o">);</span>
</span><span class='line'>        <span class="n">myLabel</span> <span class="o">=</span> <span class="o">(</span><span class="n">Label</span><span class="o">)</span> <span class="n">getFellow</span><span class="o">(</span><span class="s">&quot;myLabel&quot;</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">myButton</span><span class="o">.</span><span class="na">addEventListener</span><span class="o">(</span><span class="n">Events</span><span class="o">.</span><span class="na">ON_CLICK</span><span class="o">,</span> <span class="k">new</span> <span class="n">EventListener</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>            <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onEvent</span><span class="o">(</span><span class="n">Event</span> <span class="n">event</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'>                <span class="n">myLabel</span><span class="o">.</span><span class="na">setValue</span><span class="o">(</span><span class="s">&quot;Hooray! &#39;myButton&#39; was clicked.&quot;</span><span class="o">);</span>
</span><span class='line'>            <span class="o">}</span>
</span><span class='line'>        <span class="o">});</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now change the component&#8217;s definition to point to MyMacro.java as the
component&#8217;s class:</p>

<pre><code>&lt;?component name="mymacro" macroURI="/mymacro.zul" class="com.felipecypriano.MyMacro" ?&gt;
</code></pre>

<p>Voilà.</p>

<h2>Global Component Definition</h2>

<p>There&#8217;s a DRY principle violation and we must solve it. The main reason
to use a macro component is to share a set of components between
multiple pages, to avoid code duplication. The macro itself does this,
but if you pay attention you&#8217;ll see that the component&#8217;s definition must
be at the very beginning of each page which wants to use the component
and this, definitely, isn&#8217;t a good a thing.</p>

<p>For instance, supposing that you have 25 pages that are currently using the macro component, and in
each one of them has the component&#8217;s definition. Some months later, for
some reason, you must change yours component&#8217;s class. Instead of
changing just one simple place you&#8217;ll have to update 24 more places.
Keep your code simple and don&#8217;t repeat anything. In order to make the
component&#8217;s definition global we need to create a new xml file called
<em>lang-addon.xml</em>:</p>

<figure class='code'><figcaption><span>WEB-INF/lang-addon.xml  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;language-addon&gt;</span>
</span><span class='line'>    <span class="nt">&lt;addon-name&gt;</span>myaddon<span class="nt">&lt;/addon-name&gt;</span>
</span><span class='line'>    <span class="nt">&lt;language-name&gt;</span>xul/html<span class="nt">&lt;/language-name&gt;</span>
</span><span class='line'>    <span class="nt">&lt;component&gt;</span>
</span><span class='line'>        <span class="nt">&lt;component-name&gt;</span>mymacro<span class="nt">&lt;/component-name&gt;</span>
</span><span class='line'>        <span class="nt">&lt;component-class&gt;</span>com.felipecypriano.MyMacro<span class="nt">&lt;/component-class&gt;</span>
</span><span class='line'>        <span class="nt">&lt;macro-uri&gt;</span>/mymacro.zul<span class="nt">&lt;/macro-uri&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/component&gt;</span>
</span><span class='line'><span class="nt">&lt;/language-addon&gt;</span>
</span></code></pre></td></tr></table></div></figure>




<figure class='code'><figcaption><span>WEB-INF/zml.xml  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="nt">&lt;zk&gt;</span>
</span><span class='line'>    <span class="nt">&lt;language-config&gt;</span>
</span><span class='line'>        <span class="nt">&lt;addon-uri&gt;</span>/WEB-INF/lang-addon.xml<span class="nt">&lt;/addon-uri&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/language-config&gt;</span>
</span><span class='line'><span class="nt">&lt;/zk&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>And at last, remove <em>&lt;? component ?></em> directive from <em>index.zul.</em> From
now on <em>mymacro</em> component is available to all zul pages in your
application.</p>

<h2>Making the macro component shout to the world</h2>

<p>Just one more detail to learn, the component can handle its own
components events, but how do the page that has the macro component
knows what&#8217;s going on? We need to make the macro component send events
to the page, or any listener. And this is the our final step.</p>

<p>To keep it simple the macro component will respond just to onClick event, that&#8217;s
when the button is clicked &#8216;myLabel&#8217; will be update as usual and the
macro component will send the onClick event to all its listeners.
Replace myButton EventListener with the following code:</p>

<figure class='code'><figcaption><span>src/com/felipecypriano/MyMacro.java  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">myButton</span><span class="o">.</span><span class="na">addEventListener</span><span class="o">(</span><span class="n">Events</span><span class="o">.</span><span class="na">ON_CLICK</span><span class="o">,</span> <span class="k">new</span> <span class="n">EventListener</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onEvent</span><span class="o">(</span><span class="n">Event</span> <span class="n">event</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">myLabel</span><span class="o">.</span><span class="na">setValue</span><span class="o">(</span><span class="s">&quot;Hooray! &#39;myButton&#39; was clicked.&quot;</span><span class="o">);</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">if</span> <span class="o">(</span><span class="n">isListenerAvailable</span><span class="o">(</span><span class="s">&quot;onClick&quot;</span><span class="o">,</span> <span class="kc">true</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">Event</span> <span class="n">clicked</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Event</span><span class="o">(</span><span class="n">Events</span><span class="o">.</span><span class="na">ON_CLICK</span><span class="o">,</span> <span class="n">getParent</span><span class="o">());</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">Iterator</span> <span class="n">listeners</span> <span class="o">=</span> <span class="n">getListenerIterator</span><span class="o">(</span><span class="n">Events</span><span class="o">.</span><span class="na">ON_CLICK</span><span class="o">);</span>
</span><span class='line'>            <span class="k">while</span> <span class="o">(</span><span class="n">listeners</span><span class="o">.</span><span class="na">hasNext</span><span class="o">())</span> <span class="o">{</span>
</span><span class='line'>                <span class="n">EventListener</span> <span class="n">listener</span> <span class="o">=</span> <span class="o">(</span><span class="n">EventListener</span><span class="o">)</span> <span class="n">listeners</span><span class="o">.</span><span class="na">next</span><span class="o">();</span>
</span><span class='line'>                <span class="n">listener</span><span class="o">.</span><span class="na">onEvent</span><span class="o">(</span><span class="n">clicked</span><span class="o">);</span>
</span><span class='line'>            <span class="o">}</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>The code between lines 5 and 13 is responsible to send the event to the
listeners. But we need a couple more changes, like make IndexComposer
listen to mymacro. First put an id in mymacro:</p>

<p>Liquid error: ClassNotFound: no lexer for alias &#8216;zul&#8217; found</p>

<p>And register the event using the standard way in IndexComposer:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="n">onClick$mymacro</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">lblOutside</span><span class="o">.</span><span class="na">setValue</span><span class="o">(</span><span class="s">&quot;I can hear you &#39;mymacro&#39;!&quot;</span><span class="o">);</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>With all this it&#8217;s possible to do complex and incredible powerful macro
components. Go ahead and try.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Grails with ZK: Embedding ZUL in GSP]]></title>
    <link href="http://felipecypriano.com//2009/12/10/grails-with-zk-embedding-zul-in-gsp/"/>
    <updated>2009-12-10T14:31:54-02:00</updated>
    <id>http://felipecypriano.com//2009/12/10/grails-with-zk-embedding-zul-in-gsp</id>
    <content type="html"><![CDATA[<p>How about zk pages and gsp were so friends that we could use both
together? Imagine the possibility to use Sitemesh to decorate your page
or use <a href="http://grails.org/URL+mapping">UrlMapping</a> to choose what the
URL will be. Or you could move your project to ZK painlessly, you could
update the pages one by one and the new code will work side by side.</p>

<p><a href="http://code.google.com/p/zkgrails/wiki/ReleaseNote076">ZKGrails plugin 0.7.6 has been released</a> with
this great new feature, you can use two simple tags to include any zul
page in your GSP. <!--more--> To include the necessary css and javascript into head
use &lt;z:head/> and to insert the content anywhre you want in the body of
your gsp you&#8217;ll use &lt;z:body/>.</p>

<p>To see it in practice let&#8217;s change
<a href="http://www.grails.org/Quick+Start">Grails Quick Start</a>. Follow the
steps described there and after you complete all the steps, go back here
to update the list.gsp to use ZK Grid component. Don&#8217;t forget to use
Grails 1.1.2 and ZKGrails 0.7.6 at least.</p>

<h2>Embedding ZK in GSP</h2>

<p>Firstly create this file: <em>grails-app/conf/BuildConfig.groovy</em> and
configure <a href="http://code.google.com/p/zkgrails/">ZKGrails repository</a>:</p>

<pre><code>grails.plugin.repos.discovery.zkgrails = "http://zkgrails.googlecode.com/svn/plugins"
grails.plugin.repos.resolveOrder = ['zkgrails','default','core']
</code></pre>

<p>Now you can install ZKGrails version 0.7.6 by executing <em>grails
install-plugin zk 0.7.6</em>. After installing ZK plugin, create the zul
page we&#8217;ll use: <em>web-app/book/list.zul</em>. You can let this file blank for
now, before editing it we need to change automatic scaffolding by
creating the the gsp pages executing this command:</p>

<pre><code>grails generate-views Book
</code></pre>

<p>The command will create 4 gsp files in <em>grails-app/views/book</em>:
<em>create.gsp</em>, <em>edit.gsp</em>, <em>list.gsp</em> and <em>show.gsp</em>. We will change the
list.gsp to instead of use simple HTML table we&#8217;ll use ZK components.
Now let&#8217;s get back to our <em>web-app/book/list.zul</em>, the objective is to
create a grid using zk components and to add a new functionality to
delete all selected books. Open <em>list.zul</em> file and put the following
code in it:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="cp">&lt;?variable-resolver class=&quot;org.zkoss.zkplus.spring.DelegatingVariableResolver&quot;?&gt;</span>
</span><span class='line'><span class="cp">&lt;?init class=&quot;org.zkoss.zkplus.databind.AnnotateDataBinderInit&quot; arg0=&quot;./wnd&quot;?&gt;</span>
</span><span class='line'><span class="nt">&lt;zk</span> <span class="na">xmlns:n=</span><span class="s">&quot;http://www.zkoss.org/2005/zk/native&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>    <span class="nt">&lt;window</span> <span class="na">id=</span><span class="s">&quot;wnd&quot;</span> <span class="na">apply=</span><span class="s">&quot;${bookListComposer}&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>        <span class="nt">&lt;listbox</span> <span class="na">id=</span><span class="s">&quot;listBoxBooks&quot;</span> <span class="na">model=</span><span class="s">&quot;@{wnd$composer.booksModel}&quot;</span> <span class="na">checkmark=</span><span class="s">&quot;true&quot;</span> <span class="na">multiple=</span><span class="s">&quot;true&quot;</span>
</span><span class='line'>                 <span class="na">fixedLayout=</span><span class="s">&quot;true&quot;</span> <span class="na">width=</span><span class="s">&quot;500px&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>            <span class="nt">&lt;listhead&gt;</span>
</span><span class='line'>                <span class="nt">&lt;listheader</span> <span class="na">label=</span><span class="s">&quot;ID&quot;</span> <span class="na">sort=</span><span class="s">&quot;auto(id)&quot;</span> <span class="na">width=</span><span class="s">&quot;50px&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>                <span class="nt">&lt;listheader</span> <span class="na">label=</span><span class="s">&quot;Author&quot;</span> <span class="na">sort=</span><span class="s">&quot;auto(author)&quot;</span> <span class="na">width=</span><span class="s">&quot;225px&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>                <span class="nt">&lt;listheader</span> <span class="na">label=</span><span class="s">&quot;Title&quot;</span> <span class="na">sort=</span><span class="s">&quot;auto(title)&quot;</span> <span class="na">width=</span><span class="s">&quot;225px&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>            <span class="nt">&lt;/listhead&gt;</span>
</span><span class='line'>            <span class="nt">&lt;listitem</span> <span class="na">self=</span><span class="s">&quot;@{each=book}&quot;</span> <span class="na">value=</span><span class="s">&quot;@{book}&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>                <span class="nt">&lt;listcell</span> <span class="na">label=</span><span class="s">&quot;@{book.id}&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>                <span class="nt">&lt;listcell</span> <span class="na">label=</span><span class="s">&quot;@{book.author}&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>                <span class="nt">&lt;listcell</span> <span class="na">label=</span><span class="s">&quot;@{book.title}&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>            <span class="nt">&lt;/listitem&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/listbox&gt;</span>
</span><span class='line'>        <span class="nt">&lt;n:span</span> <span class="na">class=</span><span class="s">&quot;buttons&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>            <span class="nt">&lt;button</span> <span class="na">id=</span><span class="s">&quot;btnDelete&quot;</span> <span class="na">sclass=</span><span class="s">&quot;delete&quot;</span> <span class="na">mold=</span><span class="s">&quot;os&quot;</span> <span class="na">label=</span><span class="s">&quot;Delete Selected&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/n:span&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/window&gt;</span>
</span><span class='line'><span class="nt">&lt;/zk&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Next step is create the composer for this list.zul page, the composer is
the thing that controls the page&#8217;s behavior, just like a controller.
Create a <em>BookListComposer.groovy</em> file in <em>grails-app/composers</em>, and
this is the content:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zkgrails.*</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zkplus.databind.BindingListModelList</span>
</span><span class='line'>
</span><span class='line'><span class="kd">class</span> <span class="nc">BookListComposer</span> <span class="kd">extends</span> <span class="n">GrailsComposer</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="n">wnd</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">listBoxBooks</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">booksModel</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">binder</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="n">afterCompose</span> <span class="o">=</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">booksModel</span> <span class="o">=</span> <span class="k">new</span> <span class="n">BindingListModelList</span><span class="o">([],</span> <span class="kc">true</span><span class="o">)</span>
</span><span class='line'>        <span class="n">reloadBooks</span><span class="o">()</span>
</span><span class='line'>        <span class="n">binder</span> <span class="o">=</span> <span class="n">wnd</span><span class="o">.</span><span class="na">getVariable</span><span class="o">(</span><span class="s">&quot;binder&quot;</span><span class="o">,</span> <span class="kc">true</span><span class="o">)</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onClick_btnDelete</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="o">(</span><span class="n">listBoxBooks</span><span class="o">.</span><span class="na">selectedCount</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">listBoxBooks</span><span class="o">.</span><span class="na">selectedItems</span><span class="o">.</span><span class="na">each</span> <span class="o">{</span> <span class="n">listItem</span> <span class="o">-&gt;</span>
</span><span class='line'>                <span class="n">def</span> <span class="n">book</span> <span class="o">=</span> <span class="n">listItem</span><span class="o">.</span><span class="na">value</span>
</span><span class='line'>                <span class="n">book</span><span class="o">.</span><span class="na">delete</span><span class="o">(</span><span class="nl">flush:</span> <span class="kc">true</span><span class="o">)</span>
</span><span class='line'>            <span class="o">}</span>
</span><span class='line'>            <span class="n">reloadBooks</span><span class="o">()</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">private</span> <span class="n">def</span> <span class="nf">reloadBooks</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">def</span> <span class="n">books</span> <span class="o">=</span> <span class="n">Book</span><span class="o">.</span><span class="na">list</span><span class="o">()</span>
</span><span class='line'>        <span class="n">booksModel</span><span class="o">.</span><span class="na">clear</span><span class="o">()</span>
</span><span class='line'>        <span class="n">booksModel</span><span class="o">.</span><span class="na">addAll</span><span class="o">(</span><span class="n">books</span><span class="o">)</span>
</span><span class='line'>        <span class="n">binder</span><span class="o">?.</span><span class="na">loadAll</span><span class="o">()</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now we only need to put the zul page in the list.gsp replacing the HTML
&lt;table> tag to &lt;z:body />, the final code is this:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt">&lt;html&gt;</span>
</span><span class='line'>    <span class="nt">&lt;head&gt;</span>
</span><span class='line'>        <span class="nt">&lt;meta</span> <span class="na">http-equiv=</span><span class="s">&quot;Content-Type&quot;</span> <span class="na">content=</span><span class="s">&quot;text/html; charset=UTF-8&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;meta</span> <span class="na">name=</span><span class="s">&quot;layout&quot;</span> <span class="na">content=</span><span class="s">&quot;main&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;z:head</span> <span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;title&gt;</span>Book List<span class="nt">&lt;/title&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/head&gt;</span>
</span><span class='line'>    <span class="nt">&lt;body&gt;</span>
</span><span class='line'>        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;nav&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>            <span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">&quot;menuButton&quot;</span><span class="nt">&gt;&lt;a</span> <span class="na">class=</span><span class="s">&quot;home&quot;</span> <span class="na">href=</span><span class="s">&quot;${resource(dir:&#39;&#39;)}&quot;</span><span class="nt">&gt;</span>Home<span class="nt">&lt;/a&gt;&lt;/span&gt;</span>
</span><span class='line'>            <span class="nt">&lt;span</span> <span class="na">class=</span><span class="s">&quot;menuButton&quot;</span><span class="nt">&gt;&lt;g:link</span> <span class="na">class=</span><span class="s">&quot;create&quot;</span> <span class="na">action=</span><span class="s">&quot;create&quot;</span><span class="nt">&gt;</span>New Book<span class="nt">&lt;/g:link&gt;&lt;/span&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/div&gt;</span>
</span><span class='line'>        <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;body&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>            <span class="nt">&lt;h1&gt;</span>Book List<span class="nt">&lt;/h1&gt;</span>
</span><span class='line'>            <span class="nt">&lt;g:if</span> <span class="na">test=</span><span class="s">&quot;${flash.message}&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>            <span class="nt">&lt;div</span> <span class="na">class=</span><span class="s">&quot;message&quot;</span><span class="nt">&gt;</span>${flash.message}<span class="nt">&lt;/div&gt;</span>
</span><span class='line'>            <span class="nt">&lt;/g:if&gt;</span>
</span><span class='line'>
</span><span class='line'>            <span class="nt">&lt;z:body</span> <span class="nt">/&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/div&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/body&gt;</span>
</span><span class='line'><span class="nt">&lt;/html&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>As you can see - lines 5 and 19 - we only need to put the tags where we
want it&#8217;s content to be in the page. But you might be wondering where is
the path to list.zul file? By default the same convention used by
Grails&#8217; view, so for our example both tags - head and body -
automatically resolves to /book/list.zul. And if you need to specify the
files you want you can by setting the attribute zul, like:</p>

<pre><code>&lt;z:body zul="/path/to/file.zul" /&gt;
</code></pre>

<p>Now we can mashup our both favorite frameworks to make our projects look
and behave even better.</p>

<p><strong># Update 1</strong></p>

<p>Fixed a missing namespace on zul file; and the binder call on BookListComposer. Thanks André.</p>

<p><strong># Update 2</strong></p>

<p>GitHub repository with full source code used to made this post:
<a href="http://github.com/fmcypriano/embedding-zul-gsp">http://github.com/fmcypriano/embedding-zul-gsp</a></p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Grails with ZK: Where are my Controllers?]]></title>
    <link href="http://felipecypriano.com//2009/11/19/grails-with-zk-where-are-my-controllers/"/>
    <updated>2009-11-19T17:37:31-02:00</updated>
    <id>http://felipecypriano.com//2009/11/19/grails-with-zk-where-are-my-controllers</id>
    <content type="html"><![CDATA[<p>ZK&#8217;s zul pages works very differently compared to Grails&#8217; gsp pages but
they have one thing in common both have a class to control the page&#8217;s
state, the behavior of each piece, the data that is showed or is being
inserted by the user and this class is called Composer and Controller
respectively. This is the second article about my work with ZK and
Grails, read the first part before reading this one to get an overview:
<a href="http://felipecypriano.com/blog/wp-admin/post.php?action=edit&amp;post=211">Grails with ZK Understanding Both Together</a>.</p>

<!--more-->


<blockquote><p>A [grails] controller handles requests and creates or prepares the
response. They can generate the response or delegate to a view.</p></blockquote>

<p>This is the <a href="http://grails.org/Controllers">definition taken from grails website</a>. In other words the controller
receives a request do what it has to do and sends the response back.</p>

<p>ZK&#8217;s composers are a little different, they can do things that you&#8217;d
need javascript to do using gsp and controllers. The main power of a
controller is that you&#8217;ve full control of all components in the page
without need to write any javascript code. You could add or remove any
component dynamically from the page, register events and whatever more
you java and groovy skills let&#8217;s you do.</p>

<h2>Pages Shouldn&#8217;t Have Any Code</h2>

<p>In both technologies you can embed code directly in the page, in gsp you
can use scriptlets:</p>

<pre><code>&lt;% items.each {
    total = it.quantity * it.price
}%&gt;
</code></pre>

<p>An in zul pages you can use zkscript tags:</p>

<pre><code>&lt;zkscript&gt;
    items.each {
        total = it.quantity * it.price
    }
&lt;/zkscript&gt;
</code></pre>

<p>It&#8217;s fairly easy to use code inside the pages and I really do believe
you shouldn&#8217;t do this, it makes your code harder to read and a mess to
maintain.</p>

<p>The only places where this kind of code is good is in
tutorials and that&#8217;s why you&#8217;ll see lots of tutorials using it, but in
your actual project the composer/controller should do this job always.</p>

<h2>Your First Composer</h2>

<p>To demonstrate what a composer does lets create a simple editable page.
The page will show a text to the user an the user can click on some
buttons to change the text. All the actions will happen without refresh
and you won&#8217;t need to write any javascript.</p>

<p>Create a file named <em>editable.zul</em> in &lt;<em>your-project>/web-app/</em> with this content:</p>

<figure class='code'><figcaption><span><your-project\>/web-app/editable.zul  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="cp">&lt;?variable-resolver class=&quot;org.zkoss.zkplus.spring.DelegatingVariableResolver&quot;?&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;window</span> <span class="na">id=</span><span class="s">&quot;wndEditable&quot;</span> <span class="na">title=</span><span class="s">&quot;Editable Text&quot;</span> <span class="na">apply=</span><span class="s">&quot;${editableComposer}&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>    <span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;divText&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>
</span><span class='line'>    <span class="nt">&lt;button</span> <span class="na">id=</span><span class="s">&quot;btnEdit&quot;</span> <span class="na">label=</span><span class="s">&quot;Edit&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;button</span> <span class="na">id=</span><span class="s">&quot;btnSave&quot;</span> <span class="na">label=</span><span class="s">&quot;Save&quot;</span> <span class="na">visible=</span><span class="s">&quot;false&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;button</span> <span class="na">id=</span><span class="s">&quot;btnCancel&quot;</span> <span class="na">label=</span><span class="s">&quot;Cancel&quot;</span> <span class="na">visible=</span><span class="s">&quot;false&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'><span class="nt">&lt;/window&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>Pay attention to the id attribute it&#8217;s value will be used in the
composer to get the java object corresponding to the component. We
create an empty div called &#8216;divText&#8217; just to make sure where the text
will be in: at the top, before the buttons.</p>

<p>Now let&#8217;s go to the composer
- hooray finally eh -. Create a new directory in <em>grails-app</em> folder
called <em>composers</em> and inside this new directory create a file called
<em>EditableComposer.groovy</em>.</p>

<figure class='code'><figcaption><span>grails-app/composers/EditableComposer.groovy  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
<span class='line-number'>35</span>
<span class='line-number'>36</span>
<span class='line-number'>37</span>
<span class='line-number'>38</span>
<span class='line-number'>39</span>
<span class='line-number'>40</span>
<span class='line-number'>41</span>
<span class='line-number'>42</span>
<span class='line-number'>43</span>
<span class='line-number'>44</span>
<span class='line-number'>45</span>
<span class='line-number'>46</span>
<span class='line-number'>47</span>
<span class='line-number'>48</span>
<span class='line-number'>49</span>
<span class='line-number'>50</span>
<span class='line-number'>51</span>
<span class='line-number'>52</span>
<span class='line-number'>53</span>
<span class='line-number'>54</span>
<span class='line-number'>55</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zkgrails.GrailsComposer</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zul.*</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.zkforge.fckez.FCKeditor</span>
</span><span class='line'>
</span><span class='line'><span class="kd">class</span> <span class="nc">EditableComposer</span> <span class="kd">extends</span> <span class="n">GrailsComposer</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">divText</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">btnEdit</span>
</span><span class='line'>    <span class="n">Button</span> <span class="n">btnSave</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">btnCancel</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">FCKeditor</span> <span class="n">editor</span>
</span><span class='line'>    <span class="n">String</span> <span class="n">text</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="n">afterCompose</span> <span class="o">=</span> <span class="o">{</span>
</span><span class='line'>        <span class="c1">// This is code execute just after all the components are created, so you can access them safely</span>
</span><span class='line'>        <span class="n">showText</span><span class="o">()</span>
</span><span class='line'>        <span class="n">viewMode</span> <span class="kc">true</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kt">void</span> <span class="nf">onClick_btnEdit</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="o">(!</span><span class="n">editor</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">editor</span> <span class="o">=</span> <span class="k">new</span> <span class="n">FCKeditor</span><span class="o">()</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>        <span class="n">editor</span><span class="o">.</span><span class="na">value</span> <span class="o">=</span> <span class="n">text</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">divText</span><span class="o">.</span><span class="na">children</span><span class="o">.</span><span class="na">clear</span><span class="o">()</span>
</span><span class='line'>        <span class="n">divText</span><span class="o">.</span><span class="na">appendChild</span> <span class="n">editor</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">viewMode</span> <span class="kc">false</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kt">void</span> <span class="nf">onClick_btnSave</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">text</span> <span class="o">=</span> <span class="n">editor</span><span class="o">.</span><span class="na">value</span>
</span><span class='line'>        <span class="n">showText</span><span class="o">()</span>
</span><span class='line'>        <span class="n">viewMode</span> <span class="kc">true</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kt">void</span> <span class="nf">onClick_btnCancel</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">showText</span><span class="o">()</span>
</span><span class='line'>        <span class="n">viewMode</span> <span class="kc">true</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">private</span> <span class="n">def</span> <span class="nf">showText</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">text</span> <span class="o">=</span> <span class="n">text</span> <span class="o">?:</span> <span class="s">&quot;This is my first Composer&quot;</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">divText</span><span class="o">.</span><span class="na">children</span><span class="o">.</span><span class="na">clear</span><span class="o">()</span>
</span><span class='line'>        <span class="n">divText</span><span class="o">.</span><span class="na">appendChild</span> <span class="k">new</span> <span class="nf">Html</span><span class="o">(</span><span class="nl">content:</span> <span class="n">text</span><span class="o">)</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">private</span> <span class="n">def</span> <span class="n">viewMode</span> <span class="o">=</span> <span class="o">{</span> <span class="n">isViewing</span> <span class="o">-&gt;</span>
</span><span class='line'>        <span class="n">btnEdit</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="n">isViewing</span>
</span><span class='line'>        <span class="n">btnSave</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="o">!</span><span class="n">btnEdit</span><span class="o">.</span><span class="na">visible</span>
</span><span class='line'>        <span class="n">btnCancel</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="n">btnSave</span><span class="o">.</span><span class="na">visible</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The components in zul are injected in composer&#8217;s attributes with the
same name as the id on zul file. On line 8 we use a specific type to
declare the btnSave, this is just to show you that for each component in
zul there&#8217;s a corresponding java class where you can access and/or
change all the attributes. You don&#8217;t need to worry about thread safety
here because for each accessed page a new composer is instantiated, this
commonly known as prototype scope.</p>

<p>The afterCompose closure - lines 14
through 18 - is execute just after all the components declared in the
zul are created and ready to use, here is the right place to put all
your initialization code.</p>

<p>One of the features that I really like is how
we can declare events to each component. As you can see in lines 20, 32
and 38 we&#8217;ve declared events to listen to the user clicks on the button
for each of the 3 buttons. Basically this is how you declared all kinds
events for all components:</p>

<pre><code>public void onEventName_componentId(Event event)
</code></pre>

<p>Before the underscore character is the event name and after is the
component id. You can set events even for components that you didn&#8217;t
declared as composers attributes.</p>

<p>You don&#8217;t need to worry about
javascript to change the state in the client-side ZK will handle it for
you, just change the attributes values in your groovy code and you&#8217;re
done. I highly recommend you give ZK a try and start playing with it, if
the application you&#8217;re developing fits this desktop like model ZK is a
great framework to boost your productivity.</p>

<p><strong>See Also:</strong></p>

<ul>
<li><a href="http://www.zkoss.org/smalltalks/mvc3/" title="A walkthrough of how it was and how it is easy today">ZK MVC Made Easy</a></li>
<li><a href="http://www.zkoss.org/zkdemo/userguide/#f4">ZK FCKEditor Live Demo</a></li>
</ul>

]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Grails with ZK Understanding Both Together [updated 2x]]]></title>
    <link href="http://felipecypriano.com//2009/11/07/grails-with-zk-understanding-both-together/"/>
    <updated>2009-11-07T19:38:30-02:00</updated>
    <id>http://felipecypriano.com//2009/11/07/grails-with-zk-understanding-both-together</id>
    <content type="html"><![CDATA[<p>You may already know both technologies but I&#8217;ll briefly introduce them.
<a href="http://www.grails.org" title="Grails - The Search is Over">Grails</a> is the
best web framework that I ever work with, it uses
<a href="http://groovy.codehaus.org" title="Groovy an agile dynamic language for the Java Platform">Groovy</a>
and convention over configuration to simplify our development efforts.
To name a few this is what you&#8217;ve out-of-the-box: persistence layer to
save your entities to a relational database, dependency injection, lots
and lots of great plugins and GSP to build the view.</p>

<p>If Grails is so complete why do I need another framework and why ZK? I&#8217;m building an
enterprise web app which means it will be used by users of a company to
do daily tasks, I needed a more responsiveness app without refresh and
without increase the effort of getting it done. <!--more--> Searching for a solution
I consider mainly this two frameworks: <a href="http://www.extjs.com">ExtJS</a>,
beautiful interface and a bunch of built-in components but you&#8217;ve to
deal with javascript;
<a href="http://code.google.com/webtoolkit/" title="Google Web Toolkit">GWT</a> clever
javascript compilation from Java code, but only a few built-in
components and build interfaces using only java code isn&#8217;t a pleasant
work for me.</p>

<p><a href="http://www.zkoss.org" title="ZK Direct RIA">ZK Direct RIA</a> is
bundled with lots of very useful built-in components, you can build your
pages using a declarative syntax like XHTML or using Java code, since
we&#8217;ll use Grails we can use Groovy to simplify the java code. Your code
is processed on the server and because of this you can use your classes
without any modifications, all your classes groovy or java will work as
is in ZK no modification is need.</p>

<p>ZK will be responsible to render and
process the events of the view layer (the pages), the main difference
compared to standard Grails is that instead of use Controllers and gsp
files you&#8217;ll use Composers and zul files. It doesn&#8217;t mean that you can&#8217;t
use Controllers nor gsp anymore you can mix both without problems. From
a gsp page you can go to a zul page, and there is no secret just link
one to another with an &lt;a> tag, for example. And it&#8217;s possible to share
objects between the pages as well, using Services, zkgrails plugin&#8217;s
Facade or with your own spring bean.</p>

<p>This is the first of a series of
articles I&#8217;ll write about my experience working with Grails and ZK. Now
I&#8217;ll show a very simple example of how to start using both and what we
gain using both in the same project. First of all create a new grails
project and install zk plugin:</p>

<pre><code>$ cd &lt;path_you_want&gt;
$ grails create-app grails-and-zk-app1
    ...
    Created Grails Application at &lt;path_you_want&gt;/grails-and-zk-app1
$ cd grails-and-zk-app1
$ grails install-plugin zk 0.7.4
    ....
    Plugin zk-0.7.4 installed
    Plug-in provides the following new scripts:
    ------------------------------------------
    grails create-composer
    grails create-facade
    grails create-zul
</code></pre>

<p>Your project is now created and configured to use ZK. Now I&#8217;ll show you
how easy is to build an rich interface with ZK and Grails, create the
zul file:</p>

<pre><code>$ grails create-zul myFirstZkPage
</code></pre>

<p>This will create two files the page and it&#8217;s Composer, remember the
composer is the ZK controller.</p>

<p><a href="http://felipecypriano.com//images/2009/11/created-by-create-zul.png"><img class="center" src="http://felipecypriano.com//images/2009/11/created-by-create-zul-300x298.png" width="300" height="298" title="Created by create-zul myFirstZkPage" ></a></p>

<p>The plugin creates the files with a Hello World page ready to work.
Execute it to see it running:</p>

<pre><code>$ grails run-app
</code></pre>

<p>Open this url in your browser
<em>http://localhost:8080/grails-and-zk-app1/myFirstZkPage.zul</em>. Write a
text inside the text box and click on &#8220;Hello&#8221; button and a greeting text
will be show. This is very simple but the implementation shows some cool
features, let&#8217;s see them.</p>

<p>This is the page <em>grails-and-zk/web-app/myFirstZkPage.zul</em>, the language is called ZUML
it&#8217;s a markup language to build rich interfaces every component (some
tags) here has it&#8217;s own Java class so you can use them in your code
without any problems:</p>

<figure class='code'><figcaption><span>grails-and-zk/web-app/myFirstZkPage.zul  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
</pre></td><td class='code'><pre><code class='xml'><span class='line'><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
</span><span class='line'><span class="cp">&lt;?init class=&quot;org.zkoss.zkplus.databind.AnnotateDataBinderInit&quot; ?&gt;</span>
</span><span class='line'><span class="cp">&lt;?variable-resolver class=&quot;org.zkoss.zkplus.spring.DelegatingVariableResolver&quot;?&gt;</span>
</span><span class='line'><span class="cp">&lt;?page zscriptLanguage=&quot;GroovyGrails&quot;?&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;zk</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.zkoss.org/2005/zul&quot;</span>
</span><span class='line'>    <span class="na">xmlns:h=</span><span class="s">&quot;http://www.w3.org/1999/xhtml&quot;</span>
</span><span class='line'>    <span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
</span><span class='line'>    <span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://www.zkoss.org/2005/zul http://www.zkoss.org/2005/zul/zul.xsd&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;window</span> <span class="na">apply=</span><span class="s">&quot;${myFirstZkPageComposer}&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>
</span><span class='line'>    <span class="nt">&lt;vbox&gt;</span>
</span><span class='line'>        <span class="nt">&lt;image</span> <span class="na">src=</span><span class="s">&quot;images/grails_logo.jpg&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/vbox&gt;</span>
</span><span class='line'>
</span><span class='line'>    <span class="nt">&lt;grid</span> <span class="na">id=</span><span class="s">&quot;gdMain&quot;</span><span class="nt">&gt;</span>
</span><span class='line'>        <span class="nt">&lt;rows&gt;</span>
</span><span class='line'>            <span class="nt">&lt;row&gt;</span>
</span><span class='line'>                Name: <span class="nt">&lt;textbox</span> <span class="na">id=</span><span class="s">&quot;txtName&quot;</span> <span class="nt">/&gt;</span>
</span><span class='line'>                <span class="nt">&lt;button</span> <span class="na">id=</span><span class="s">&quot;btnHello&quot;</span> <span class="na">label=</span><span class="s">&quot;Hello&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'>            <span class="nt">&lt;/row&gt;</span>
</span><span class='line'>        <span class="nt">&lt;/rows&gt;</span>
</span><span class='line'>    <span class="nt">&lt;/grid&gt;</span>
</span><span class='line'>
</span><span class='line'>    <span class="nt">&lt;listbox</span> <span class="na">id=</span><span class="s">&quot;lstResult&quot;</span> <span class="na">mold=</span><span class="s">&quot;paging&quot;</span><span class="nt">/&gt;</span>
</span><span class='line'><span class="nt">&lt;/window&gt;</span>
</span><span class='line'>
</span><span class='line'><span class="nt">&lt;/zk&gt;</span>
</span></code></pre></td></tr></table></div></figure>


<p>On line 11, the apply attribute tells ZK which class is the Composer of
the Window component it expects the class&#8217; full qualified name. Hum,
what is being passed to apply doesn&#8217;t look like any full qualified name.
Yes it isn&#8217;t, <em>apply=&#8221;${myFirstZkPageComposer}&#8221;</em> is an EL expression
which is resolved to the Spring Bean named &#8220;myFirstZkPageComposer&#8221; and
this bean is, as you can easily guess:
<em>grails-app/composers/MyFirstZkPageComposer.groovy</em>, cool eh. Every
class in composers directory which ends with &#8220;Composer&#8221; will be
automatically a spring bean.</p>

<p>Inside the window we&#8217;ve three components
with an id setted (lines 17, 21 and 26), looking the composer&#8217;s code we
can see that for each of this three components there&#8217;s one attribute
with the same name:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">import</span> <span class="nn">org.zkoss.zkgrails.*</span>
</span><span class='line'>
</span><span class='line'><span class="kd">class</span> <span class="nc">MyFirstZkPageComposer</span> <span class="kd">extends</span> <span class="n">GrailsComposer</span> <span class="o">{</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="n">txtName</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">btnHello</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">lstResult</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="n">myFirstZkPageFacade</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="nf">onClick_btnHello</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">lstResult</span><span class="o">.</span><span class="na">clear</span><span class="o">()</span>
</span><span class='line'>        <span class="n">lstResult</span><span class="o">.</span><span class="na">append</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">listitem</span> <span class="o">{</span> <span class="n">listcell</span> <span class="o">{</span> <span class="n">label</span><span class="o">(</span><span class="nl">value:</span> <span class="s">&quot;Hello, ${txtName.value} !&quot;</span><span class="o">)</span> <span class="o">}</span> <span class="o">}</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="n">afterCompose</span> <span class="o">=</span> <span class="o">{</span> <span class="n">c</span> <span class="o">-&gt;</span>
</span><span class='line'>        <span class="c1">// initialize component here</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>This mean you&#8217;ve full access of this components own&#8217;s attributes you can
read and write it&#8217;s values. For example, try to add this new line at the
end of onClick_btnHello method and save it:</p>

<pre><code>btnHello.label = "Hello There!"
</code></pre>

<p>Run the app an see what happens when you click the Hello button. Play
with the code to see what more you can do. To learn more about ZK I
recommend you to read the <a href="http://docs.zkoss.org/wiki/Documentation">documentation
page</a> specially the
Developer&#8217;s Guide and Developer&#8217;s Reference. Use your curiosity to make
great things.</p>

<p>In the next articles I&#8217;ll enter in more details.</p>

<p><strong>#Update 1</strong></p>

<p><a href="http://felipecypriano.com/blog/2009/11/19/grails-with-zk-where-are-my-controllers/">ZK with Grails: Where are my controllers?</a>
Learn what is and how to use ZK Composers.</p>

<p><strong># Update 2</strong></p>

<p>As of ZkGrails 1.0M2 the syntax to refer a Spring Bean on window&#8217;s apply
attribute has changed, it&#8217;s now without the ${}. This makes possible to
use package to refer to composers:</p>

<pre><code>&lt;window apply="my.package.nameComposer"/&gt;
</code></pre>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Tweak ZK to make event processing call groovy's invokeMethod]]></title>
    <link href="http://felipecypriano.com//2009/10/26/tweak-zk-to-make-event-processing-call-groovys-invokemethod/"/>
    <updated>2009-10-26T08:32:50-02:00</updated>
    <id>http://felipecypriano.com//2009/10/26/tweak-zk-to-make-event-processing-call-groovys-invokemethod</id>
    <content type="html"><![CDATA[<p>My current project needs a very dynamic and fast pages without refreshs,
or using the buzz word ajax, to accomplish this requirement I&#8217;m using
<a href="http://www.zkoss.org/">ZK Direct RIA</a>. As ZK heavily uses ajax requests
with unpredictable url I need another way to protect the controller&#8217;s
actions (composer&#8217;s method) instead of url.
<a href="http://felipecypriano.com/blog/2009/10/19/enable-secured-annotation-with-grails-spring-security-plugin/">Using @Secured and groovy&#8217;s invokeMethod I was able to protect each method of any class</a>
(please read the post to get a better picture of what I&#8217;ll discuss)
which works fines if the method call is from a groovy code, ZK is fully
implemented in java though.</p>

<!--more-->


<p>When a user clicks on a button, or does
anything that generates events, zk process it and call the corresponding
method in the composer class. ZK events processing executes this code
when it find the method to call:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">mtd</span><span class="o">.</span><span class="na">invoke</span><span class="o">(</span><span class="n">controller</span><span class="o">,</span> <span class="k">new</span> <span class="n">Object</span><span class="o">[]</span> <span class="o">{</span><span class="n">evt</span><span class="o">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>This uses Java Reflection API and completely bypass groovy&#8217;s
invokeMethod, what we need to do is use groovy&#8217;s InvokerHelper class
instead of direct calling the method using reflection.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">InvokerHelper</span><span class="o">.</span><span class="na">invokeMethod</span><span class="o">(</span><span class="n">controller</span><span class="o">,</span> <span class="n">mtd</span><span class="o">.</span><span class="na">getName</span><span class="o">(),</span> <span class="k">new</span> <span class="n">Object</span><span class="o">[]</span> <span class="o">{</span><span class="n">params</span><span class="o">});</span>
</span></code></pre></td></tr></table></div></figure>


<p>Now that we know how to do it for any kind of java code let&#8217;s dig into
the problem with ZK event and find out where we should use
InvokerHelper.</p>

<h2>Implementing InvokerHelper in GrailsComposer</h2>

<p>The GrailsComposer in ZKGrails plugin is a subclass of
GenericForwardComposer that changes the default char separator from &#8216;$&#8217;
to &#8216;_&#8217;, because groovy already uses the &#8216;$&#8217; with another meaning, and
allows the use of a closure as the <a href="http://www.zkoss.org/javadoc/3.6.1/zk/org/zkoss/zk/ui/util/GenericAutowireComposer.html#doAfterCompose(org.zkoss.zk.ui.Component">doAfterCompose method</a>).
So when you create a new composer using <em>grails create-composer
<composer name\></em> your new class extends GrailsComposer. Let&#8217;s create a
composer and protect two of it&#8217;s buttons.</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">import</span> <span class="nn">org.springframework.security.annotation.Secured</span>
</span><span class='line'><span class="kd">class</span> <span class="nc">MainAreaComposer</span> <span class="kd">extends</span> <span class="n">GrailsComposer</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">private</span> <span class="n">Button</span> <span class="n">btnEditText</span> <span class="c1">// automatically wired from the zul file</span>
</span><span class='line'>    <span class="kd">private</span> <span class="n">Button</span> <span class="n">btnSaveText</span>
</span><span class='line'>    <span class="kd">private</span> <span class="n">Button</span> <span class="n">btnCancelText</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="n">afterCompose</span> <span class="o">=</span> <span class="o">{</span>
</span><span class='line'>        <span class="c1">// loads the current text</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="nd">@Secured</span><span class="o">(</span><span class="s">&quot;mainArea_editText&quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onClick_btnEditText</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="c1">// shows the rich text editor to edit the text</span>
</span><span class='line'>        <span class="n">btnSaveText</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="kc">true</span>
</span><span class='line'>        <span class="n">btnCancelText</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="n">btnSaveText</span><span class="o">.</span><span class="na">visible</span>
</span><span class='line'>        <span class="n">btnEditText</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="kc">false</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="nd">@Secured</span><span class="o">(</span><span class="s">&quot;mainArea_editText&quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onClick_btnSaveText</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="c1">// saves the text, hide the editor and load the new text</span>
</span><span class='line'>         <span class="n">btnSaveText</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="kc">false</span>
</span><span class='line'>         <span class="n">btnCancelText</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="n">btnSaveText</span><span class="o">.</span><span class="na">visible</span>
</span><span class='line'>         <span class="n">btnEditText</span><span class="o">.</span><span class="na">visible</span> <span class="o">=</span> <span class="kc">true</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="kd">public</span> <span class="kt">void</span> <span class="nf">onClick_btnCancelText</span><span class="o">(){</span>
</span><span class='line'>         <span class="c1">// hide the editor and loads the text without any modifications</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The idea here is that only some users have access to edit the text. ZK
is fully AJAX based and every event cames from client through javascript
request. Because of this even if the btnEditText is invisible a
malicious users could forge a request to call the method - not easy, but
possible - the use of @Secured is to assure that an attempt to call this
method from an unauthorized user will fail.</p>

<p>If you run your app using ZKGrails 0.7.5, the current version for Grails 1.1.1, and test if an
unauthorized user fails when call the method - just make the button
always visible to test - you will see that nothing is blocked no
AccessDeniedException is thrown, and of course this isn&#8217;t good. Humm,
and how could we solve this? How to make zk event handling aware of the
dynamic methods?</p>

<p>My first thought was &#8220;ZK is open source, what about
reading it&#8217;s code to understand the event system?&#8221; and there I go,
downloaded the source and start reading how ZK maps the methods name to
components event, in other words how it does this magic:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">onClick_btnCancelText</span><span class="o">()</span> <span class="o">{</span><span class="c1">// event code}</span>
</span></code></pre></td></tr></table></div></figure>


<p>The above method is the same as the not so nice:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">btnCancelText</span><span class="o">.</span><span class="na">addEventListener</span><span class="o">(</span><span class="s">&quot;onClick&quot;</span><span class="o">,</span> <span class="o">{</span><span class="c1">// event code} as EventListener)</span>
</span></code></pre></td></tr></table></div></figure>


<p>Reading the source code I find out that this kind of event is a
<a href="http://www.zkoss.org/javadoc/3.6.1/zk/org/zkoss/zk/ui/event/ForwardEvent.html">ForwardEvent</a>
but I couldn&#8217;t find where the original method is called until I look the
class diagram for GrailsComposer:</p>

<p><a href="http://felipecypriano.com//images/2009/10/GrailsComposer-class-hierarchy.png"><img class="center" src="http://felipecypriano.com//images/2009/10/GrailsComposer-class-hierarchy-300x149.png" width="300" height="149" title="GrailsComposer class hierarchy" ></a></p>

<p>And there was the solution GenericEventListener onEvent method, I&#8217;ve
finally found where the original method (onClick_componentId) is
invoked and now we just need to override onEvent in GrailsComposer, this
is the implementation:</p>

<figure class='code'><figcaption><span>org/zkoss/zkgrails/GrailsComposer.java  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="cm">/**</span>
</span><span class='line'><span class="cm"> * Overrides GenericEventListener to use InvokerHelper to call methods. Because of this the events are now</span>
</span><span class='line'><span class="cm"> * part of groovy&#39;s dynamic methods, e.g. metaClass.invokeMethod works for event methods.</span>
</span><span class='line'><span class="cm"> * @param evt</span>
</span><span class='line'><span class="cm"> * @throws Exception</span>
</span><span class='line'><span class="cm"> */</span>
</span><span class='line'><span class="nd">@Override</span>
</span><span class='line'><span class="kd">public</span> <span class="kt">void</span> <span class="nf">onEvent</span><span class="o">(</span><span class="n">Event</span> <span class="n">evt</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">final</span> <span class="n">Object</span> <span class="n">controller</span> <span class="o">=</span> <span class="n">getController</span><span class="o">();</span>
</span><span class='line'>    <span class="kd">final</span> <span class="n">Method</span> <span class="n">mtd</span> <span class="o">=</span>  <span class="n">ComponentsCtrl</span><span class="o">.</span><span class="na">getEventMethod</span><span class="o">(</span><span class="n">controller</span><span class="o">.</span><span class="na">getClass</span><span class="o">(),</span> <span class="n">evt</span><span class="o">.</span><span class="na">getName</span><span class="o">());</span>
</span><span class='line'>    <span class="k">if</span> <span class="o">(</span><span class="n">mtd</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="o">(</span><span class="n">mtd</span><span class="o">.</span><span class="na">getParameterTypes</span><span class="o">().</span><span class="na">length</span> <span class="o">==</span> <span class="mi">0</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">InvokerHelper</span><span class="o">.</span><span class="na">invokeMethod</span><span class="o">(</span><span class="n">controller</span><span class="o">,</span> <span class="n">mtd</span><span class="o">.</span><span class="na">getName</span><span class="o">(),</span> <span class="kc">null</span><span class="o">);</span>
</span><span class='line'>        <span class="o">}</span> <span class="k">else</span> <span class="k">if</span> <span class="o">(</span><span class="n">evt</span> <span class="k">instanceof</span> <span class="n">ForwardEvent</span><span class="o">)</span> <span class="o">{</span> <span class="c1">//ForwardEvent</span>
</span><span class='line'>            <span class="kd">final</span> <span class="n">Class</span> <span class="n">paramcls</span> <span class="o">=</span> <span class="o">(</span><span class="n">Class</span><span class="o">)</span> <span class="n">mtd</span><span class="o">.</span><span class="na">getParameterTypes</span><span class="o">()[</span><span class="mi">0</span><span class="o">];</span>
</span><span class='line'>            <span class="c1">//paramcls is ForwardEvent || Event</span>
</span><span class='line'>            <span class="k">if</span> <span class="o">(</span><span class="n">ForwardEvent</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">isAssignableFrom</span><span class="o">(</span><span class="n">paramcls</span><span class="o">)</span>
</span><span class='line'>            <span class="o">||</span> <span class="n">Event</span><span class="o">.</span><span class="na">class</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">paramcls</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'>                <span class="n">InvokerHelper</span><span class="o">.</span><span class="na">invokeMethod</span><span class="o">(</span><span class="n">controller</span><span class="o">,</span> <span class="n">mtd</span><span class="o">.</span><span class="na">getName</span><span class="o">(),</span> <span class="k">new</span> <span class="n">Object</span><span class="o">[]</span> <span class="o">{</span><span class="n">evt</span><span class="o">});</span>
</span><span class='line'>            <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span><span class='line'>                <span class="k">do</span> <span class="o">{</span>
</span><span class='line'>                    <span class="n">evt</span> <span class="o">=</span> <span class="o">((</span><span class="n">ForwardEvent</span><span class="o">)</span><span class="n">evt</span><span class="o">).</span><span class="na">getOrigin</span><span class="o">();</span>
</span><span class='line'>                <span class="o">}</span> <span class="k">while</span><span class="o">(</span><span class="n">evt</span> <span class="k">instanceof</span> <span class="n">ForwardEvent</span><span class="o">);</span>
</span><span class='line'>                <span class="n">InvokerHelper</span><span class="o">.</span><span class="na">invokeMethod</span><span class="o">(</span><span class="n">controller</span><span class="o">,</span> <span class="n">mtd</span><span class="o">.</span><span class="na">getName</span><span class="o">(),</span> <span class="k">new</span> <span class="n">Object</span><span class="o">[]</span> <span class="o">{</span><span class="n">evt</span><span class="o">});</span>
</span><span class='line'>            <span class="o">}</span>
</span><span class='line'>        <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">InvokerHelper</span><span class="o">.</span><span class="na">invokeMethod</span><span class="o">(</span><span class="n">controller</span><span class="o">,</span> <span class="n">mtd</span><span class="o">.</span><span class="na">getName</span><span class="o">(),</span> <span class="k">new</span> <span class="n">Object</span><span class="o">[]</span> <span class="o">{</span><span class="n">evt</span><span class="o">});</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Just replacing method.invoke to InvokerHelper.invokeMethod (lines 13,
19, 24 and 27) is enough to make the methods actually secured. Open your
GrailsComposer class and add this method to the class, this is all you
need to do. If you don&#8217;t understand how the @Secured is now working, read the
<a href="http://felipecypriano.com/blog/2009/10/19/enable-secured-annotation-with-grails-spring-security-plugin/" title="Enable @Secured annotation with Grails Spring Security plugin">previous post</a>
which is about the implementation of @Secured.</p>

<p>I&#8217;ve submitted this as a patch to Chanwit, the plugin&#8217;s maintainer, and ZKGrails 1.0 will have
this overridin onEvent out-of-the-box. No need to change the plugin source code anymore, great news.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Enable @Secured annotation with Grails Spring Security plugin]]></title>
    <link href="http://felipecypriano.com//2009/10/19/enable-secured-annotation-with-grails-spring-security-plugin/"/>
    <updated>2009-10-19T17:46:15-02:00</updated>
    <id>http://felipecypriano.com//2009/10/19/enable-secured-annotation-with-grails-spring-security-plugin</id>
    <content type="html"><![CDATA[<p>How could I protect each method of my classes? Using Spring Security it
should be easy, right? After a quick search I realize that the best way
is to use
<a href="http://static.springsource.org/spring-security/site/apidocs/org/springframework/security/annotation/Secured.html">@Secured</a>
annotation, good! Another issue fixed. But as life isn&#8217;t fun without
problems to solve it didn&#8217;t work as expected.</p>

<!--more-->


<p>The problem started because grails acegi plugin, in version 0.5.2, doesn&#8217;t support this
annotation.
<a href="http://www.nabble.com/How-can-I-activate-@Secured-from-Spring-Security--td25878294.html">Talking about this in grail user mailing list</a>
Benjamin Doerr gave me a nice idea: use <a href="http://groovy.codehaus.org/Using+invokeMethod+and+getProperty" title="Using invokeMethod and getProperty">groovy&#8217;s
invokeMethod</a>
to add the support that I needed.</p>

<p>The idea is to use groovy meta magic to add behavior to the classes that have at least one method annotated,
we well override the metaClass.invokeMethod of the class we want to
enable the annotation. To keep things organized I create a new boot
strap file in <em>grail-app/conf/SecurityBootStrap.groovy</em> and all the
related code is place in this file.</p>

<p>First off all let&#8217;s create a closure that can be used to override invokeMethod of any class:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">def</span> <span class="n">verifyMethodAccess</span> <span class="o">=</span> <span class="o">{</span><span class="n">String</span> <span class="n">name</span><span class="o">,</span> <span class="n">args</span> <span class="o">-&gt;</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">method</span> <span class="o">=</span> <span class="n">delegate</span><span class="o">.</span><span class="na">metaClass</span><span class="o">.</span><span class="na">getMetaMethod</span><span class="o">(</span><span class="n">name</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">logMsg</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="o">(</span><span class="s">&quot;invoking ${method}, &quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="k">if</span> <span class="o">(</span><span class="n">method</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">def</span> <span class="n">annotation</span> <span class="o">=</span> <span class="n">method</span><span class="o">.</span><span class="na">cachedMethod</span><span class="o">.</span><span class="na">getAnnotation</span><span class="o">(</span><span class="n">Secured</span><span class="o">)</span>
</span><span class='line'>        <span class="n">logMsg</span> <span class="o">+=</span> <span class="s">&quot;is @Secured ${annotation != null}&quot;</span>
</span><span class='line'>        <span class="k">if</span> <span class="o">(</span><span class="n">annotation</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">def</span> <span class="n">annotationValue</span> <span class="o">=</span> <span class="n">annotation</span><span class="o">.</span><span class="na">value</span><span class="o">()?.</span><span class="na">toString</span><span class="o">()?.</span><span class="na">replaceAll</span><span class="o">(~/[</span><span class="err">\</span><span class="o">[-</span><span class="err">\</span><span class="o">]]/,</span> <span class="err">&#39;&#39;</span><span class="o">)</span> <span class="c1">// remove [ and ]</span>
</span><span class='line'>            <span class="n">logMsg</span> <span class="o">+=</span> <span class="s">&quot; by ${annotationValue}&quot;</span>
</span><span class='line'>            <span class="k">if</span> <span class="o">(!</span><span class="n">authenticateService</span><span class="o">?.</span><span class="na">ifAnyGranted</span><span class="o">(</span><span class="n">annotationValue</span><span class="o">))</span> <span class="o">{</span>
</span><span class='line'>                <span class="n">log</span><span class="o">.</span><span class="na">debug</span> <span class="n">logMsg</span><span class="o">.</span><span class="na">toString</span><span class="o">()</span>
</span><span class='line'>                <span class="k">throw</span> <span class="k">new</span> <span class="nf">AccessDeniedException</span><span class="o">(</span><span class="s">&quot;Access denied to ${delegate.class.simpleName}.${name} from user ${authenticateService.principal()?.username}&quot;</span><span class="o">)</span>
</span><span class='line'>            <span class="o">}</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>        <span class="n">log</span><span class="o">.</span><span class="na">debug</span> <span class="n">logMsg</span><span class="o">.</span><span class="na">toString</span><span class="o">()</span>
</span><span class='line'>        <span class="k">return</span> <span class="n">method</span><span class="o">.</span><span class="na">invoke</span><span class="o">(</span><span class="n">delegate</span><span class="o">,</span> <span class="n">args</span><span class="o">)</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">log</span><span class="o">.</span><span class="na">error</span> <span class="s">&quot;Method ${delegate.class.simpleName}.${name} not found&quot;</span>
</span><span class='line'>    <span class="k">return</span> <span class="kc">null</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>In a glance this closure gets the
<a href="http://groovy.codehaus.org/api/groovy/lang/MetaMethod.html" title="MetaMethod API">method</a>
that is been invoked and verifies if this method is annotated with
@Secured annotation, if it does then the granted authorities passed to
the annotation&#8217;s value is obtained - line 8 - and is passed to the
authenticationService if the current user has access the method is
invoked otherwise an AccessDeniedException is thrown.</p>

<p>To active this behavior in a class we only need to override it&#8217;s
metaClass.invokeMethod:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">import</span> <span class="nn">org.springframework.security.annotation.Secured</span>
</span><span class='line'><span class="kn">import</span> <span class="nn">org.springframework.security.AccessDeniedException</span>
</span><span class='line'>
</span><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SecurityBootStrap</span> <span class="o">{</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">init</span> <span class="o">=</span> <span class="o">{</span><span class="n">servletContext</span> <span class="o">-&gt;</span>
</span><span class='line'>        <span class="n">ClassifiedClass</span><span class="o">.</span><span class="na">metaClass</span><span class="o">.</span><span class="na">invokeMethod</span> <span class="o">=</span> <span class="n">verifyMethodAccess</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>    <span class="c1">// verifyMethodAccess declaration and body goes here</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>To complete the example this is the ClassifiedClass:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">ClassifiedClass</span> <span class="o">{</span>
</span><span class='line'>    <span class="nd">@Secured</span><span class="o">(</span><span class="s">&quot;ROLE_PRESIDENT&quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="n">String</span> <span class="nf">protectedMethod</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="s">&quot;I&#39;m a top classified information!&quot;</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>    <span class="n">def</span> <span class="nf">ordinaryMethod</span><span class="o">()</span> <span class="o">{</span>
</span><span class='line'>        <span class="s">&quot;Anyone can call me.&quot;</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>Every time a groovy code calls any method of ClassifiedClass it&#8217;s
metaClass.invokeMethod will be executed before the actual method. Just
pay attention to use the same Secured annotation in your class and in
SecurityBootStrap.</p>

<p>Unfortunately this solution only works if the call came from groovy code, if any java code calls
classifiedObj.protectedMethod() it will succeed even without permission.
Java code completely ignores the invokeMethod. Now stop thinking that
you read all of this for nothing, because on next post I&#8217;ll talk about
using
<a href="http://groovy.codehaus.org/api/org/codehaus/groovy/runtime/InvokerHelper.html">InvokerHelper</a>
class to make java code obey the law.</p>

<p><strong># Update 1</strong></p>

<p>Read this <a href="http://felipecypriano.com/blog/2009/10/26/tweak-zk-to-make-event-processing-call-groovys-invokemethod/">post to see how to make java code be aware of groovy&#8217;s dynamic method</a>,
including invokeMethod.</p>
]]></content>
  </entry>
  
  <entry>
    <title type="html"><![CDATA[Customizing SpringSecurity to protect each button of a page using Grails Acegi plugin]]></title>
    <link href="http://felipecypriano.com//2009/10/14/customizing-springsecurity-to-protect-each-button-of-a-page-using-grails-acegi-plugin/"/>
    <updated>2009-10-14T19:36:57-03:00</updated>
    <id>http://felipecypriano.com//2009/10/14/customizing-springsecurity-to-protect-each-button-of-a-page-using-grails-acegi-plugin</id>
    <content type="html"><![CDATA[<p>I&#8217;m very happy with grails <a href="http://grails.org/plugin/acegi">acegi plugin</a>, aka Spring Security Plugin, but
on my newest project I needed a finner grained way to do control access
than using simple urls filters and roles.</p>

<p>I wanted a way to control which button, link, action of the current page the user can access. If
the user has only read access to a page than the page is shown but edit
action isn&#8217;t, because of this requirement using only roles to grant
access isn&#8217;t enough and could easily became a mess if I create one role
per action. Use urls filters won&#8217;t work because most urls are generated
by <a href="http://www.zkoss.org/">ZK framework</a> and hence are non predictable.</p>

<p>The solution is fully based on SpringSecurity capabilities and should
work on every project that uses it independent of plugins or frameworks
that I use.<!--more--> Since spring security plugin does the hard work for us, we
just need to create two more classes besides acegi&#8217;s default user and
role and extends
<a href="http://static.springsource.org/spring-security/site/apidocs/org/springframework/security/userdetails/UserDetailsService.html">UserDetailsService</a>
interface. This is based on <a href="http://code.google.com/p/zk-sample-db/" title="Stephan's ZK Sample Project">zk_sample_project</a>
and is a database implementation of this <a href="http://blog.springsource.com/2009/01/02/spring-security-customization-part-1-customizing-userdetails-or-extending-grantedauthority/" title="Spring Security customization (Part 1 – Customizing UserDetails or extending GrantedAuthority)">article by Oleg Zhurakousky</a>.</p>

<h3>The database schema</h3>

<p>The plugin needs User and Role class by default, to extend (not meaning
inheritance) them I create two new classes to add more control over
which resource can be accessed namely: Group and Access. The Group class
purpose is only to organize the Access, since there will be a lot of
them. It&#8217;s important to notice that the <strong>access&#8217; name and the role&#8217;s
authority must be unique</strong>.</p>

<p><img class="center" src="http://felipecypriano.com//images/2009/10/security_class_diagram1.png" title="User, Role, Group and Access class diagram" ></p>

<p>With this classes we can create roles that contains a list of granted
accesses, for instance:</p>

<ul>
<li><p>ROLE_ADMIN</p>

<ul>
<li>pagePolicy.btnNew</li>
<li>pagePolicy.btnSave</li>
<li>pagePolicy.btnDelete</li>
</ul>
</li>
<li><p>ROLE_ORDINARY</p>

<ul>
<li>pagePolicy.btnNew</li>
</ul>
</li>
</ul>


<p>With our granted access setup in database we need to teach spring
security how to authorize an access based on the access name:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="n">authenticateService</span><span class="o">.</span><span class="na">ifAllGranted</span> <span class="s">&quot;ROLE_ADMIN&quot;</span> <span class="c1">//  works out of the box</span>
</span><span class='line'><span class="n">authenticateService</span><span class="o">.</span><span class="na">ifAllGranted</span> <span class="s">&quot;pagePolicy.btnNew&quot;</span> <span class="c1">//  don&#39;t work</span>
</span></code></pre></td></tr></table></div></figure>


<p>The goal is to make both options works, to do this we need to provide a
custom implementation of UserDetailsService which will load to the
UserDetails object both roles and accesses as
<a href="http://static.springsource.org/spring-security/site/apidocs/org/springframework/security/GrantedAuthority.html" title="GrantedAuthority API">GrantedAuthority</a>.
Let&#8217;s
<a href="http://www.grails.org/AcegiSecurity+Plugin+-+Custom+UserDetailsService" title="Customizing UserDetailsService">customize</a>
spring security and extend
<a href="http://plugins.grails.org/grails-acegi/trunk/src/groovy/org/codehaus/groovy/grails/plugins/springsecurity/GrailsDaoImpl.groovy" title="GrailsDaoImpl source code">GrailsDAOImpl</a>
to add our logic:</p>

<figure class='code'><figcaption><span>src/groovy/HierachyUserDetailsService.groovy  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
<span class='line-number'>17</span>
<span class='line-number'>18</span>
<span class='line-number'>19</span>
<span class='line-number'>20</span>
<span class='line-number'>21</span>
<span class='line-number'>22</span>
<span class='line-number'>23</span>
<span class='line-number'>24</span>
<span class='line-number'>25</span>
<span class='line-number'>26</span>
<span class='line-number'>27</span>
<span class='line-number'>28</span>
<span class='line-number'>29</span>
<span class='line-number'>30</span>
<span class='line-number'>31</span>
<span class='line-number'>32</span>
<span class='line-number'>33</span>
<span class='line-number'>34</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">HierachyUserDetailsService</span> <span class="kd">extends</span> <span class="n">GrailsDaoImpl</span> <span class="o">{</span>
</span><span class='line'>    <span class="kd">protected</span> <span class="n">GrantedAuthority</span><span class="o">[]</span> <span class="nf">loadAuthorities</span><span class="o">(</span><span class="n">user</span><span class="o">,</span> <span class="n">String</span> <span class="n">username</span><span class="o">,</span> <span class="kt">boolean</span> <span class="n">loadRoles</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="k">if</span> <span class="o">(!</span><span class="n">loadRoles</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>            <span class="k">return</span> <span class="o">[]</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="n">def</span> <span class="n">accessCriteria</span> <span class="o">=</span> <span class="n">Access</span><span class="o">.</span><span class="na">createCriteria</span><span class="o">()</span>
</span><span class='line'>        <span class="c1">// get all the accesses for this user</span>
</span><span class='line'>        <span class="n">def</span> <span class="n">accesses</span> <span class="o">=</span> <span class="n">accessCriteria</span><span class="o">.</span><span class="na">list</span> <span class="o">{</span>
</span><span class='line'>            <span class="n">authorizedGroups</span> <span class="o">{</span>
</span><span class='line'>                <span class="n">authorizedRoles</span> <span class="o">{</span>
</span><span class='line'>                    <span class="n">users</span> <span class="o">{</span>
</span><span class='line'>                        <span class="n">eq</span><span class="o">(</span><span class="s">&quot;login&quot;</span><span class="o">,</span> <span class="n">username</span><span class="o">)</span>
</span><span class='line'>                    <span class="o">}</span>
</span><span class='line'>                <span class="o">}</span>
</span><span class='line'>            <span class="o">}</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>        <span class="c1">// now we iterate over the accesses to get the roles associated to each access</span>
</span><span class='line'>        <span class="n">def</span> <span class="n">allAuthorities</span> <span class="o">=</span> <span class="o">[]</span> <span class="n">as</span> <span class="n">Set</span>
</span><span class='line'>        <span class="n">accesses</span><span class="o">.</span><span class="na">each</span> <span class="o">{</span> <span class="n">access</span> <span class="o">-&gt;</span>
</span><span class='line'>            <span class="n">def</span> <span class="n">roles</span> <span class="o">=</span> <span class="o">[]</span> <span class="n">as</span> <span class="n">Set</span>
</span><span class='line'>            <span class="n">access</span><span class="o">.</span> <span class="n">authorizedGroups</span><span class="o">*.</span> <span class="n">authorizedRoles</span><span class="o">*.</span><span class="na">each</span> <span class="o">{</span> <span class="n">role</span> <span class="o">-&gt;</span>
</span><span class='line'>                <span class="n">roles</span> <span class="o">&lt;&lt;</span> <span class="k">new</span> <span class="n">GrantedAuthorityImpl</span><span class="o">(</span><span class="n">role</span><span class="o">.</span><span class="na">authorityValue</span><span class="o">)</span>
</span><span class='line'>            <span class="o">}</span>
</span><span class='line'>
</span><span class='line'>            <span class="n">allAuthorities</span><span class="o">.</span><span class="na">addAll</span><span class="o">(</span><span class="n">roles</span><span class="o">)</span>
</span><span class='line'>            <span class="n">allAuthorities</span> <span class="o">&lt;&lt;</span> <span class="k">new</span> <span class="n">AccessGrantedAuthority</span><span class="o">(</span><span class="n">access</span><span class="o">.</span><span class="na">name</span><span class="o">,</span> <span class="n">roles</span><span class="o">)</span>
</span><span class='line'>        <span class="o">}</span>
</span><span class='line'>        <span class="n">log</span><span class="o">.</span><span class="na">debug</span> <span class="s">&quot;Authorities for user ${username}: ${allAuthorities}&quot;</span>
</span><span class='line'>
</span><span class='line'>        <span class="k">return</span> <span class="n">allAuthorities</span> <span class="n">as</span> <span class="n">GrantedAuthority</span><span class="o">[]</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>GrailsDaoImpl already implements UserDetailsService interface and
provide a very good set of methods we just need to override one of them
to achieve our goal. Great. The
<a href="http://felipecypriano.com//files/2009/10/AccessGrantedAuthority.groovy" title="AccessGranteduthority source code">AccessGrantedAuthority</a>
(line 28) is just to make it easier to debug from which role the access
came from. See the log debug output using this class:</p>

<blockquote><p>Authorities for user admin: [ROLE_ADMIN, ROLE_ORDINARY,
pagePolicy.btnNew[ROLE_ADMIN, ROLE_ORDINARY],
pagePolicy.btnSave[ROLE_ADMIN], pagePolicy.btnDelete[ROLE_ADMIN]]</p></blockquote>

<p>If don&#8217;t want this information you could use the default implementation,
GrantedAuthorityImpl as in line 24.</p>

<p>Now that we have our implementation the last step is to configure our implementation as the current
implementation used by Spring Security:</p>

<figure class='code'><figcaption><span>grails-app/conf/spring/resources.groovy  </span></figcaption>
 <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
<span class='line-number'>3</span>
<span class='line-number'>4</span>
<span class='line-number'>5</span>
<span class='line-number'>6</span>
<span class='line-number'>7</span>
<span class='line-number'>8</span>
<span class='line-number'>9</span>
<span class='line-number'>10</span>
<span class='line-number'>11</span>
<span class='line-number'>12</span>
<span class='line-number'>13</span>
<span class='line-number'>14</span>
<span class='line-number'>15</span>
<span class='line-number'>16</span>
</pre></td><td class='code'><pre><code class='java'><span class='line'><span class="kn">import</span> <span class="nn">org.codehaus.groovy.grails.plugins.springsecurity.AuthorizeTools</span>
</span><span class='line'><span class="n">beans</span> <span class="o">=</span> <span class="o">{</span>
</span><span class='line'>    <span class="c1">// Spring Security</span>
</span><span class='line'>    <span class="n">def</span> <span class="n">securityConf</span> <span class="o">=</span> <span class="n">AuthorizeTools</span><span class="o">.</span><span class="na">securityConfig</span><span class="o">.</span><span class="na">security</span>
</span><span class='line'>    <span class="n">userDetailsService</span><span class="o">(</span><span class="n">br</span><span class="o">.</span><span class="na">com</span><span class="o">.</span><span class="na">litoraltextil</span><span class="o">.</span><span class="na">vc</span><span class="o">.</span><span class="na">springsecurity</span><span class="o">.</span><span class="na">AuthorityHierachyUserDetailsService</span><span class="o">)</span> <span class="o">{</span>
</span><span class='line'>        <span class="n">usernameFieldName</span> <span class="o">=</span> <span class="n">securityConf</span><span class="o">.</span><span class="na">userName</span>
</span><span class='line'>        <span class="n">passwordFieldName</span> <span class="o">=</span> <span class="n">securityConf</span><span class="o">.</span><span class="na">password</span>
</span><span class='line'>        <span class="n">enabledFieldName</span> <span class="o">=</span> <span class="n">securityConf</span><span class="o">.</span><span class="na">enabled</span>
</span><span class='line'>        <span class="n">authorityFieldName</span> <span class="o">=</span> <span class="n">securityConf</span><span class="o">.</span><span class="na">authorityField</span>
</span><span class='line'>        <span class="n">loginUserDomainClass</span> <span class="o">=</span> <span class="n">securityConf</span><span class="o">.</span><span class="na">loginUserDomainClass</span>
</span><span class='line'>        <span class="n">relationalAuthoritiesField</span> <span class="o">=</span> <span class="n">securityConf</span><span class="o">.</span><span class="na">relationalAuthorities</span>
</span><span class='line'>        <span class="n">authoritiesMethodName</span> <span class="o">=</span> <span class="n">securityConf</span><span class="o">.</span><span class="na">getAuthoritiesMethod</span>
</span><span class='line'>        <span class="n">sessionFactory</span> <span class="o">=</span> <span class="n">ref</span><span class="o">(</span><span class="err">&#39;</span><span class="n">sessionFactory</span><span class="err">&#39;</span><span class="o">)</span>
</span><span class='line'>        <span class="n">authenticateService</span> <span class="o">=</span> <span class="n">ref</span><span class="o">(</span><span class="s">&quot;authenticateService&quot;</span><span class="o">)</span>
</span><span class='line'>    <span class="o">}</span>
</span><span class='line'><span class="o">}</span>
</span></code></pre></td></tr></table></div></figure>


<p>As a full configuration of spring security plugin isn&#8217;t the focus of
this post I suggest you to read
<a href="http://www.grails.org/AcegiSecurity+Plugin+-+Customizing+with+SecurityConfig">how to customize the plugin using SecurityConfig</a>.
Finally we can use all the
<a href="http://www.grails.org/AcegiSecurity+Plugin+-+Artifacts" title="AcegiSecurity Plugin - Artifacts">artifacts</a>
using both the role name or the access name, some examples:</p>

<figure class='code'> <div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers"><span class='line-number'>1</span>
<span class='line-number'>2</span>
</pre></td><td class='code'><pre><code class='html'><span class='line'><span class="nt">&lt;g:ifAllGranted</span> <span class="na">role=</span><span class="s">&quot;ROLE_ADMIN&quot;</span><span class="nt">&gt;</span>secure stuff here<span class="nt">&lt;/g:ifAllGranted&gt;</span>
</span><span class='line'><span class="nt">&lt;g:ifAllGranted</span> <span class="na">role=</span><span class="s">&quot;ROLE_ADMIN, aAccess.name&quot;</span><span class="nt">&gt;</span>secure stuff using both<span class="nt">&lt;/g:ifAllGranted&gt;</span>
</span></code></pre></td></tr></table></div></figure>



]]></content>
  </entry>
  
</feed>

