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

<channel>
	<title>Gridshore &#187; grails-ui</title>
	<atom:link href="http://www.gridshore.nl/tag/grails-ui/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.gridshore.nl</link>
	<description>A weblog about software engineering, Architecture, Technology an other things we like.</description>
	<lastBuildDate>Tue, 13 Dec 2011 15:36:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Still doing grails</title>
		<link>http://www.gridshore.nl/2010/01/10/still-doing-grails/</link>
		<comments>http://www.gridshore.nl/2010/01/10/still-doing-grails/#comments</comments>
		<pubDate>Sun, 10 Jan 2010 11:07:31 +0000</pubDate>
		<dc:creator>jettro</dc:creator>
				<category><![CDATA[groovy and grails]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[grails-ui]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[searchable]]></category>

		<guid isPermaLink="false">http://www.gridshore.nl/?p=984</guid>
		<description><![CDATA[<p></p> <p>This is the third post in a series of post about grails. In this series I am describing an application I am creating with grails. It is a scheduling application for scheduling people on projects. You can find the previous posts here:</p> http://www.gridshore.nl/2009/12/20/starting-with-grails/ http://www.gridshore.nl/2010/01/04/continuing-with-grails/ <p>In this post I will discuss the following topics:</p> [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.gridshore.nl/wp-content/uploads/F0720E8F-2336-4FEE-BB96-7F8FA9B9FD9C.jpg" alt="F0720E8F-2336-4FEE-BB96-7F8FA9B9FD9C.jpg" border="0" width="134" height="117" align="right" /></p>
<p>This is the third post in a series of post about grails. In this series I am describing an application I am creating with grails. It is a scheduling application for scheduling people on projects. You can find the previous posts here:</p>
<ul>
<li><a href="http://www.gridshore.nl/2009/12/20/starting-with-grails/">http://www.gridshore.nl/2009/12/20/starting-with-grails/</a></li>
<li><a href="http://www.gridshore.nl/2010/01/04/continuing-with-grails/">http://www.gridshore.nl/2010/01/04/continuing-with-grails/</a></li>
</ul>
<p>In this post I will discuss the following topics:</p>
<ul>
<li>Creating meeting notes using the grails-ui rich text editor</li>
<li>Create a search using the searchable plugin on the meeting notes</li>
<li>Introducing rss to subscribe to new meeting notes</li>
</ul>
<p><span id="more-984"></span><br />
<h2>Meeting notes</h2>
<h3>Create the basics</h3>
<p>For the meeting notes we want a large text field that can contain html to make it look better. We start of with the domain object. We give it three fields: a weekNr a User and the notes. The notes field must become a Text field in the database and a textarea on the screen. To accomplish this, we add a mapping element with the following contents as well as the constraints to make it an html textarea element. (Yes I know this feels strange)</p>
<pre class="brush: groovy; title: ; notranslate">
class MeetingNotes {
    int weekNr
    User user
    String notes
    static constraints = {
        notes(blank:false, widget:&quot;textarea&quot;)
    }
    static mapping = {
        notes type:&quot;text&quot;
    }
}
</pre>
<p>Next step is the controller, for now we add the scoffold property and configuration to add it to the navigation. The next code block shows the first implementation.</p>
<pre class="brush: groovy; title: ; notranslate">
class MeetingNotesController {
    def scaffold = true

    static navigation = [
            group: 'tabs',
            order: 110,
            subItems: [
                    [group: 'tabs', action: 'create']
            ]
    ]
}
</pre>
<p>The create screen now looks like this:</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-01-09-at-17.16.59.png" alt="Screen shot 2010-01-09 at 17.16.59.png" border="0" width="500" height="490" /></div>
<h3>Use the rich text editor</h3>
<p>In include the rich text editor, we generate the views and make changes to those. Than we change the create.jsp in views/meetingnotes. We start by removing the navigation. Than we add the following line to the head element.</p>
<pre>
&lt;gui:resources components="richEditor"/&gt;
</pre>
<p>Then we just need to make a small change to the gsp. Look for the line with <em>g:textArea</em>. We replace this input item with the following line.</p>
<pre>
&lt;gui:richEditor id='notes' value='add your weekly notes here ....'/&gt;
</pre>
<p>This results in the following screen:</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-01-09-at-18.01.22.png" alt="Screen shot 2010-01-09 at 18.01.22.png" border="0" width="500" height="490" /></div>
<p>The same changes need to be made to the edit.gsp. I also removed the navigation from the other gsp&#8217;s. Time to move on, now we are going to include the searchable plugin and make the weekly meeting notes searchable.</p>
<h2>The searchable plugin</h2>
<p>Let us start with the basics and install the plugin, configure the MeetingNotes domain class to be searchable, and look at the generated search screen. You can find these steps as well in the <a href="http://www.grails.org/Searchable+Plugin+-+Quick+start">quick start</a> from the plugin documentation.</p>
<pre>
grails install-plugin searchable
</pre>
<p>Now the hard part to make our MeetingNotes searchable, we have to add the following line to the MeetingNotes domain class.</p>
<pre>
static searchable = true
</pre>
<p>For real, that is it. Now you can browse to the controller that serves the search.</p>
<p>I have created three items that all contain data, the following screen shows the standard search controller. Only two items will be returned if I look for the project name cqrs4j</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-01-10-at-09.59.29.png" alt="Screen shot 2010-01-10 at 09.59.29.png" border="0" width="500" height="490" /></div>
<p>That is all nice, but it does not really integrate with our site. Therefore we want to make a controller that does the search thing and an input box beneath the header menu.</p>
<p>First I add a line to the UrlMappings that maps the /search request to the SearchableController that comes out of the box. Check the file UrlMappings.groovy in the conf folder.</p>
<pre>
"/search" (controller:"searchable")
</pre>
<p>Now we will override the view of that comes with the plugin and make it look more like our own. This page, <a href="http://www.grails.org/Searchable+Plugin+-+SearchableController+and+view">Controller and view</a>, describes how you can do that. I copied the index.gsp from the path ${user.home}/.grails/1.2.0/projects/MyScheduling/plugins/searchable-0.5.5/grails-app/views/searchable/index.gsp to the same location in my own application. Then I alter that index.gsp, add the template, remove some styling and I have a better looking page. I also copied the form into the main.gsp layout so we have a search box on each page. The following screendump shows the result.</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-01-10-at-10.49.38.png" alt="Screen shot 2010-01-10 at 10.49.38.png" border="0" width="500" height="488" /></div>
<p>There you have it, basic search implemented in our own application. Looking really integrated.</p>
<p>There is topic that I also want to mention, this is suggestions. You know them from google and more advanced search solutions. You know what, we have an advanced search now as well. We just have to configure that we want to use suggestions. With this option configured, the results page shows a link to try the search again with suggestions. As an example try to search for <em>holday</em>, you get no results, try the suggestions and you get the following screen.</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-01-10-at-11.02.28.png" alt="Screen shot 2010-01-10 at 11.02.28.png" border="0" width="500" height="488" /></div>
<p>To do this, we change the searchable part of the MeetingNotes into the following lines:</p>
<pre class="brush: groovy; title: ; notranslate">
    static searchable = {
        spellCheck &quot;include&quot;
    }
</pre>
<p>The search plugin has a lot of options and functionality. I urge you to have a look at the <a href="http://www.grails.org/plugin/searchable">plugin page</a>.</p>
<h2>Rss feed</h2>
<p>A very often method used to be informed for new items is rss. I want to create a feed for all meeting notes. That way people get notified when a new Meeting Note is created. To create the feed, I use the <a href="http://www.grails.org/plugin/feeds">feeds plugin</a>.</p>
<p>Again it all starts with the installation of the plugin. The plugin makes use of the framework Rome. So if you are familiar with that, this should not be to hard. Not that it is hard if you do not have the knowledge.</p>
<pre>
grails install-plugin feeds
</pre>
<p>Now adding the feed consists of two parts. The first part is to add a feed method to the MeetingNotes controller. The second part is two add the special html tag, which is done using a grails tag.</p>
<pre>
&lt;feed:meta kind="rss" version="2.0" controller="meetingNotes" action="feed"/&gt;
</pre>
<pre class="brush: groovy; title: ; notranslate">
    def feed = {
        render(feedType:&quot;rss&quot;, feedVersion:&quot;2.0&quot;) {
            title = &quot;Meeting Notes feed&quot;
            link = &quot;${grailsApplication.config.grails.serverURL}/meetingNotes/feed&quot;
            description = &quot;All meeting notes that have been written per week&quot;
            MeetingNotes.list().each() { note -&gt;
                entry(&quot;weekNr : ${note.weekNr}&quot;) {
                    link = &quot;${grailsApplication.config.grails.serverURL}/meetingNotes/show/${note.id}&quot;
                    &quot;Meeting notes for week number ${note.weekNr} by ${note.user.userRealName}&quot; // return the content
                }
            }
        }
    }
</pre>
<p>The code is so easy I hardly need to explain anything. One thing that had a problem at the moment is hard wiring the name of the server to get the feed from. The only trick I want to point out is the way to construct the link (line 4 and 8). We use the special mechanism to read parameters from the config that can be environment specific. The <em>grails.serverURL parameter</em> is by default configured in the Config.groovy configuration file.</p>
<p>In safari I now have the following result.</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2010-01-10-at-12.00.01.png" alt="Screen shot 2010-01-10 at 12.00.01.png" border="0" width="500" height="426" /></div>
<p>That is it for this post. I am still amazed by how easy things are using grails. Most of the time I spend on getting the ui done. In the next post I will not introduce new plugin, but I will focus on getting the solutions better. Add validation, better configured access control, internationalization. Those kind of topics.</p>
<p>Hope to see you back.</p>
<div class='dd_post_share'><div class='dd_buttons'><div class='dd_button'><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http%3A%2F%2Fwww.gridshore.nl%2F2010%2F01%2F10%2Fstill-doing-grails%2F&amp;title=Still%20doing%20grails&amp;t=2' height='25' width='155' frameborder='0' scrolling='no'></iframe></div></div><div style='clear:both'></div></div><!-- Social Buttons Generated by Digg Digg plugin v4.5.3.4, 
    Author : Yong Mook Kim
    Website : http://www.diggdigg2u.com -->]]></content:encoded>
			<wfw:commentRss>http://www.gridshore.nl/2010/01/10/still-doing-grails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Starting with grails</title>
		<link>http://www.gridshore.nl/2009/12/20/starting-with-grails/</link>
		<comments>http://www.gridshore.nl/2009/12/20/starting-with-grails/#comments</comments>
		<pubDate>Sun, 20 Dec 2009 15:25:56 +0000</pubDate>
		<dc:creator>jettro</dc:creator>
				<category><![CDATA[groovy and grails]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[google chart]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[grails-ui]]></category>
		<category><![CDATA[navigation]]></category>

		<guid isPermaLink="false">http://www.gridshore.nl/?p=955</guid>
		<description><![CDATA[<p> <p>In this blog post I am writing down the steps I took to create a new grails application. Maybe it can be of help to other that want to start using grails.</p> <p>In short I am going to create an application that you can use to schedule people on projects. We used to [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.gridshore.nl/wp-content/uploads/F0720E8F-2336-4FEE-BB96-7F8FA9B9FD9C.jpg" alt="F0720E8F-2336-4FEE-BB96-7F8FA9B9FD9C.jpg" border="0" width="134" height="117" align="left" />
<p>In this blog post I am writing down the steps I took to create a new grails application. Maybe it can be of help to other that want to start using grails.</p>
<p>In short I am going to create an application that you can use to schedule people on projects. We used to have an excel sheet to do this, but I really do not like this. So I am writing an application for it. Grails is ideal for these kind of projects.</p>
<p>I am, of course, going to use some nice grails plugins, I will mention them all and give some details about the steps I took to make them run. Some of the plugins are:</p>
<ul>
<li>DBUtil</li>
<li>Navigation</li>
<li>grails-ui</li>
<li>joda-time</li>
</ul>
<p>Comments, questions and feedback are of course welcome.</p>
<p><span id="more-955"></span><br />
<h2>First steps</h2>
<p>A grails application starts with creating the application. For some reason I still like to do this on the command line. I do not use the excellent intellij to do this. But I will use it extensively for the remainder.</p>
<pre>
grails create-app MyScheduling
</pre>
<p>Next step is adding it to a source repository. I am getting more enthusiastic about using git, so I create a repository at github. You can find my sources here:</p>
<p><a href="http://github.com/jettro/MyScheduling">http://github.com/jettro/MyScheduling</a></p>
<p>One of the things I like about git is the .gitignore file. A very clean way to prevent certain files from being added to the repository. The following shows the contents of my .gitignore file.</p>
<pre>
# used to ignore certain files by git
# mac osx files
.DS_Store
# intellij files
.idea/*
*.ipr
*.iws
# (grails) generated files
target/*
out/*
stacktrace.log
</pre>
<p>enough about git, let us move on to the nice parts about grails.</p>
<h2>Starting with the domain</h2>
<p>At the beginning I start with only a few domain classes.</p>
<ul>
<li>Person &#8211; the people we want to schedule on projects</li>
<li>Project &#8211; well it is a project</li>
<li>User &#8211; the ones that actually are allowed to use the application</li>
</ul>
<p>There is not a lot of information in these domain objects for now. I use intelllij to create the domain classes, but command is easy as well, <em>grails create-domain-class nl.gridshore.scheduling.Person</em>There is a many to many relationship between projects and persons. I also created the controllers for the three domain classes with just one line of code in each class.</p>
<pre>
def scaffold = true
</pre>
<p>This line makes all the views available, you can now start the application and browse to the homepage. I use intellij to start the application but <em>grails run-app</em> will work fine as well.</p>
<p>THe following is a tip that you can use when creating your domain model.</p>
<h2>DBUtil plugin</h2>
<p>this plugin can be used to look at the data and the datamodel of an in memory database, which is the default for new applications. To me this is a must have plugin.</p>
<pre>
grails install-plugin db-util
</pre>
<h2>Navigation</h2>
<p>I make use of the navigation plugin, you can find the information about the plugin here:</p>
<p><a href="http://www.grails.org/Navigation+plugin">http://www.grails.org/Navigation+plugin</a></p>
<p>We start of by introducing the navigation to our won (non-scaffolding) controllers. So we create a HomeController. Next we tell the navigation plugin to include the home controller in the navigation. We have to add one line to the controller.</p>
<pre>
static navigation = true
</pre>
<p>Yep, it is that easy. We also need to create the index.gsp in views/home. Now enrich our template with the navigation component, therefore we open up the main.gsp in views/layouts. There is two things we need to do, first add a header part to include styling and scripts. Then we add a <em>div</em> with the menu elements.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;html&gt;
&lt;head&gt;
  &lt;title&gt;&lt;g:layoutTitle default=&quot;Grails&quot;/&gt;&lt;/title&gt;
  &lt;link rel=&quot;stylesheet&quot; href=&quot;${resource(dir: 'css', file: 'main.css')}&quot;/&gt;
  &lt;link rel=&quot;shortcut icon&quot; href=&quot;${resource(dir: 'images', file: 'favicon.ico')}&quot; type=&quot;image/x-icon&quot;/&gt;
  &lt;g:layoutHead/&gt;
  &lt;nav:resources group=&quot;tabs&quot;/&gt;
  &lt;g:javascript library=&quot;application&quot;/&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;spinner&quot; class=&quot;spinner&quot; style=&quot;display:none;&quot;&gt;
  &lt;img src=&quot;${resource(dir: 'images', file: 'spinner.gif')}&quot; alt=&quot;Spinner&quot;/&gt;
&lt;/div&gt;
&lt;div id=&quot;grailsLogo&quot; class=&quot;logo&quot;&gt;&lt;a href=&quot;http://grails.org&quot;&gt;&lt;img src=&quot;${resource(dir: 'images', file: 'grails_logo.png')}&quot; alt=&quot;Grails&quot; border=&quot;0&quot;/&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div id=&quot;menu&quot;&gt;
  &lt;nav:render/&gt;
&lt;/div&gt;
&lt;g:layoutBody/&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>If you are following along and you are like me seeing an almost white page without any navigation what soever, check if you have added the meta tag for sitemesh to your gsp. Go to our homepage or to the url: <a href="http://localhost:8080/MyScheduling/home">http://localhost:8080/MyScheduling/home</a>. You should now see the following screen:</p>
<p><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2009-12-19-at-10.53.47.png" alt="Screen shot 2009-12-19 at 10.53.47.png" border="0" width="749" height="360" align="center"/></p>
<p>Of course we also want to be able to create persons from the home page controller. So we add other controllers to the menu as well. Let us start with the person controller. In my first try the Person button was placed before the Home button, that is not what I want. So we add ordering to the menu. This can be done by assigning an array with configuration options to the static navigation declaration. As you might have expected, the name of the property to configure is called <em>order</em>.</p>
<p>Next up is subnavigation, let us add a submenu item for adding a new person. The following code shows how to do just that.</p>
<pre>
    static navigation = [group:'tabs',order:10]
</pre>
<p>We now need to add the generation of the submenu to our template as well. Add the following line to the main.gsp right belof the nav:render tag.</p>
<pre>
&lt;nav:renderSubItems/&gt;
</pre>
<p>And the subnavigation for person becomes</p>
<pre class="brush: groovy; title: ; notranslate">
    static navigation = [
            group:'tabs',
            order:150,
            title:'person',
            subItems:[
                    [group:'tabs',action:'create',title:'create']
            ]
    ]
</pre>
<p>The following screen is the result:</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2009-12-19-at-11.13.46.png" alt="Screen shot 2009-12-19 at 11.13.46.png" border="0" width="756" height="440" /></div>
<p>As you can see, we have the default scaffolding buttons as well, so we have to remove the navigation from the scaffolding screens. More on that later, for now I want to add one latest thing and that is i18n messages or labels. This is not the easiest thing, you need to use the groups thing or it won&#8217;t work. But other than that it is again pretty painless.</p>
<p>Let us move on to the next topic, more ui, we will continue with the grails ui plugin</p>
<h2>The grails-ui plugin</h2>
<p>The <a href="http://www.grails.org/plugin/grails-ui">grails ui plugin</a> contains a lot of different ui components. For now we focus on the autocomplete component, the date picker and the datatable component. We are going to create a screen that accepts a start date, an end date, and the amount of hours per week for a certain person and project. The person and project will use the autocomplete component and the date will use the date picker. Finally all the created ScheduleItems will be presented in a data table. First we install the grails ui plugin. I think you know how by now. If you are using intellij on the mac command+option+k.</p>
<p>Before this to be created screen has use, we need to introduce a few new things. We create a new domain class ScheduleItem with the week number, the amount of hours per week and of course the project and the person. Then we also create a service to create a schedule item, the ScheduleItemService. Of course we also need the controller to present the data and accept the form.</p>
<p>Before I start introducing the gui components I want to stress that you must not forget to add the following tag to each header of the page with the components you use on that page.</p>
<pre>
&lt;gui:resources components="dataTable,autoComplete,datePicker"/&gt;
</pre>
<h3>The datatable component</h3>
<p>This component consists of a controller component and a tag for a gsp page. The controller component is called using an ajax request and returns JSON. Things like filtering and pagination are supported out of the box thanks to the very easy grails way of doing queries. The following code block shows the controller code.</p>
<pre class="brush: groovy; title: ; notranslate">
    def scheduleItemDataAsJSON = {
        def list = []
        def allItems = ScheduleItem.list(params)
        response.setHeader(&quot;Cache-Control&quot;, &quot;no-store&quot;)
        allItems.each {
            list &lt;&lt; [
                id:it.id,
                person:it.person.toString(),
                project:it.project.toString(),
                weekNr:it.weekNr,
                nrHours:it.nrHours
            ]
        }
        def data = [
                totalRecords: ScheduleItem.count(),
                results: list
        ]
        render data as JSON
    }
</pre>
<p>As you can see in line 3 we use the gorm stuff to query the ScheduleItems. Than we create the list with items, check that we actually return the nested properties for person and project. If you forget to do this you will see something like [object Object] in the table. Finally we prepare the data object with the list of items an a count of the total items. This data is than returned as JSON.</p>
<p>Next up is the gsp part, the following code block shows the dataTable in action. I do want to explicitly tell you not to forget to add a component (div or the body) with the class yui-skin-sam. This is a requirement for the components to work. The total code than becomes.</p>
<pre class="brush: xml; title: ; notranslate">
&lt;div class=&quot;yui-skin-sam&quot;&gt;
  &lt;gui:dataTable
          id=&quot;scheduleItemDatatable&quot;
          draggableColumns=&quot;true&quot;
          columnDefs=&quot;[
            [key:'id', sortable:true, resizeable: true, label:'ID'],
            [key:'person', sortable:true, resizeable: true, label:'Person'],
            [key:'project', sortable:true, resizeable: true, label: 'Project'],
            [key:'weekNr', type:'number', sortable:true, resizeable: true, label: 'Week Nr'],
            [key:'nrHours', type:'number', sortable:true, resizeable: true, label: 'Nr Hours']
          ]&quot;
          paginatorConfig=&quot;[
            template:'{PreviousPageLink} {PageLinks} {NextPageLink} {CurrentPageReport}',
            pageReportTemplate:'{totalRecords} total records'
          ]&quot;
          controller=&quot;scheduling&quot;
          action=&quot;scheduleItemDataAsJSON&quot;
          resultsList=&quot;results&quot;
          rowExpansion=&quot;false&quot;
          rowsPerPage=&quot;15&quot;/&gt;
&lt;/div&gt;
</pre>
<p>The code is pretty easy to understand, check the controller and action method. The following screen dump shows the result.</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2009-12-20-at-14.37.14.png" alt="Screen shot 2009-12-20 at 14.37.14.png" border="0" width="764" height="529" /></div>
<h3>Autocomplete</h3>
<p>In the overview screen of ScheduleItems we push the create button. In that screen we have the autocomplete and the datePicker. The datepicker is so easy I will not really go into it. Let us focus on the autocomplete. Again we have a controller part and a gsp part. I have taken the easy way, I provide all options from the controller. Using ajax is almost the same idea as the dataTable component. Still I want to limit these kind of requests. The following code block shows the controller to provided the options for the autocomplete.</p>
<p>    def create = {<br />
        def persons = Person.list()<br />
        def projects = Project.list()</p>
<p>        [projects: projects, persons: persons]<br />
    }</p>
<p>Now we have all the persons and projects available in the gsp view component. The code is pretty easy when not using ajax.</p>
<pre class="brush: xml; title: ; notranslate">
  &lt;div style=&quot;width:300px&quot;&gt;
    Person : &lt;span class=&quot;yui-skin-sam&quot;&gt;&lt;gui:autoComplete id=&quot;person&quot; options=&quot;${persons*.name}&quot;/&gt;&lt;/span&gt;
    Project : &lt;span class=&quot;yui-skin-sam&quot;&gt;&lt;gui:autoComplete id=&quot;project&quot; options=&quot;${projects*.name}&quot;/&gt;&lt;/span&gt;
  &lt;/div&gt;
</pre>
<p>The following image shows the form, yes I know, I need to spend time on the layout of the screen.</p>
<div style="text-align:center;"><img src="http://www.gridshore.nl/wp-content/uploads/Screen-shot-2009-12-20-at-14.45.04.png" alt="Screen shot 2009-12-20 at 14.45.04.png" border="0" width="761" height="643" /></div>
<p>For accepting the form submission we also need some code. The next code block shows the code that reads the form and calls the service.</p>
<pre class="brush: groovy; title: ; notranslate">
    def newScheduleItem = {
        def personName = params.person
        def projectName = params.project
        def nrHours = params.nrhours.toInteger()
        def weekNr = params.weeknr.toInteger()

        flash.message = &quot;New item is created for person ${personName} and project ${projectName}&quot;
        scheduleItemService.createScheduleItem(personName, projectName, weekNr, nrHours)
        redirect(action: &quot;create&quot;)
    }
</pre>
<p>That is it for the grails-ui plugin, let us move on to the next plugin.</p>
<h2>Joda time</h2>
<p>Groovy comes out of the box with a better Date object than standard java. Still joda time is the way to go. Therefore we will introduce the <a href="http://www.grails.org/plugin/joda-time">joda time plugin</a>. One of the reasons I need it, is that I want to work with week numbers. Joda time has out of the box support for this. So we install joda time.</p>
<p>For persisting dates, we need to tell grails to use the special joda time versions. Therefore we need to add special config lines to the Config.groovy.</p>
<pre class="brush: groovy; title: ; notranslate">
grails.gorm.default.mapping = {
   'user-type' (type: PersistentLocalDate, class: LocalDate )
}
</pre>
<p>If you have dates in your domain objects and you use scaffolding, you need to change the scaffolding templates so joda time will keep on working. The following command does just that.</p>
<pre>
grails install-joda-time-templates
</pre>
<p>Now let us try to use the startDate and endDate in the SchedulingController to create entries for every week in that period. For that to work we must make a change in the interface of the service. We now use LocalDate from joda time. We also change the input field of the datepicker. We add the attribute formatString. That way joda time can very easily convert the string into a real LocalDate.</p>
<p>The important part in the SchedulingController is converting the strings into dates.</p>
<pre class="brush: groovy; title: ; notranslate">
        def startDateStr = params.startDate
        def endDateStr = params.endDate
        if (startDateStr &amp;amp;amp;&amp;amp;amp; endDateStr) {
            LocalDate startDate = new LocalDate(startDateStr)
            LocalDate endDate = new LocalDate(endDateStr)
            scheduleItemService.createScheduleItems(personName, projectName, nrHours,startDate,endDate)
        } else {
        }
</pre>
<p>The last part is the service that actually calculates the weeks. Before hand I warn you that this code is not optimal yet. You cannot pass the year with the weeks. If someone has a smarter way of doing this, please comment or make a suggestion using mail, github or using other means.</p>
<pre class="brush: groovy; title: ; notranslate">
        def currentDate = startDate
        for (int i=startDate.weekOfWeekyear;i&lt;endDate.weekOfWeekyear;i++) {
            currentDate = currentDate + Period.weeks(1)
            doCreateScheduleItem(person, project, i, numberHours)
        }
</pre>
<h2>Final remarks</h2>
<p>This blog post is reaching it&#8217;s end. Not that I am done, I will write more posts the coming weeks. The final thing I want to show is something weird with the home page. At the moment pages cannot deal with not having navigation configured. Therefore the default homepage is broken. I want to use this to show you how you can change the mapping to the default homepage. I want to show the /home as the default page. To do just that we need to change the mapping. Open the UrlMappings in the conf folder. Look for the mapping of &#8220;/&#8221; and change that line into the following line:</p>
<pre>
      "/"(controller:"home", action:"index")
</pre>
<p>I am still pretty amazed ahout how easy it is to work with grails. What I have done so far took me about 6 hours. In that time I also wrote this blog post. Not bad for someone that does not have a lot of experience with grails. In my next blog posts I will go into the security part, validation, google charts and searchable plugin. Hope you liked it and hope to see you back.</p>
<div class='dd_post_share'><div class='dd_buttons'><div class='dd_button'><iframe src='http://widgets.dzone.com/links/widgets/zoneit.html?url=http%3A%2F%2Fwww.gridshore.nl%2F2009%2F12%2F20%2Fstarting-with-grails%2F&amp;title=Starting%20with%20grails&amp;t=2' height='25' width='155' frameborder='0' scrolling='no'></iframe></div></div><div style='clear:both'></div></div><!-- Social Buttons Generated by Digg Digg plugin v4.5.3.4, 
    Author : Yong Mook Kim
    Website : http://www.diggdigg2u.com -->]]></content:encoded>
			<wfw:commentRss>http://www.gridshore.nl/2009/12/20/starting-with-grails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

