0 Yuuuuh!!!! We got Grails 1.1.1!

As I said earlier, we got Grails 1.1.1 today!!

Take a look in the links below:

But watch out! People in the grails mailing list noticed some broken stuff in the build when trying to upgrade. A little workaround is availabe in this mail thread, just keep looking this Thread, probably we’ll get something here!

Thanks!!

1 Looking forward Grails 1.1.1

Hi,

Yesterday Guillaume Laforge annouced that finally groovy 1.6.3 is out. That means we’ll have the Grails 1.1.1 release in a few days (hoping this for today). Graeme Rocher said that with groovy 1.6.3 released, Grails 1.1.1 is imminent.

So let’s wait!

I’m specially waiting this release since it corrects a little bug in WAR generation using the –nojars options.
Ohhh, and of course, with Grails 1.1.1 we’ll have support to use Grails in the Google Java App Engine!!

Thanks Guillaume and Graeme!

[]s,

0 Esperando o Grails 1.1.1

Hum,

Ontem saiu oficialmente o Groovy 1.6.3, e isto quer dizer que hoje ou no máximo ainda essa semana estaremos com a release oficial do Grails 1.1.1.

Como disse o Graeme Rocher ontem no twitter, com o release do Groovy 1.6.3, o Grails 1.1.1 é iminente. :P
Estou esperando esta versão pois corrige um bug na criação de WARs usando a opção –nojars

Além de que é claro, com o Grails 1.1.1 poderemos usar o plugin do GAE para criar aplicações Grails no Google Java App Engine!!

Estamos aguardando ansiosamente.

[]s,

5 Understanding Groovy Categories

Have you ever used Groovy Categories? Do you even know what its stands for?

Groovy Categories is a great feature from Groovy (inherted from Objective-C) that allows you to have some kind of “extended” control on a class. The category just “add” some more funcionality to your class when you want. It’s different from creating another class that extends from the first one, this approach holds a little more functionality-oriented talk… :)

Categories is nothing more than a Groovy class that you’ve implemented before that effectly adds your functionality. The syntax is very simple and all you have to do, is tell to the runtime environment you’re now using the category.

use (the category class you want to use) {
    //all your code here can access the category funcionality
}
//back to normal code

That’s it. Groovy Core comes with some Categories (ServletCategory, DOMCategory and TimeCategory). The one we use more is the TimeCategory, that adds some calculation methods for us.

Let’s imagine we want to add some days in our current date, using Java you remember how you should do, don’t you?

Get a Calendar Instance, retrieve this calendar’s date, add the ammount of days using the strangest and creepiest method ever and then get the calendar’s date again (actually, get only its time and create a date with it):

//creepiest method
myCalendarInstance.add(Calendar.DAY_OF_MONTH, 10);

You can see in the TimeCategory’s javadoc (link above) that it defines some methods like “getHours(), getDays(), getMinutes()”. This methods we don’t have in some usual Date class, but when we use the TimeCategory object, they just get available for us. The example below show us what should happen when we use the TimeCategory:

Date now = new Date()
println now //just show when am I :) 

use(org.codehaus.groovy.runtime.TimeCategory) {
    now = now + 15.days
}

println now //shows us when we'll be in 15 days!

This will be our output after running this small script:

Sat May 02 02:25:57 BRT 2009
Sun May 17 02:25:57 BRT 2009

That’s it, try the code above and the others methods the TimeCategory gives to you: years, months, hours, minutes and seconds.

Thanks! Be the first to know when I publish some interesting article signing up my feed and following me on twitter!

2 Working with Excel and Grails

I found this 2 links in grailsbrasil.com forum.
It’s a nice feature to include in your system for that situations when user have to input a large amount of data.

First example shows how user can upload the excel file, and your system automatically load all the spreadsheet data into domain objects. And this one, shows the reverse way, by loading your domain objects into an excel file using JExcelAPI.

Recommend!

[]s,

6 Changing the default locale for your grails application

Thank god grails has a wonderful i18n native support. It’s great change all your application language just by setting one more parameter in the URL (lang). If you do not know this behaviour, check this out.

But sometimes you have to preset the default language because not all your applications will be in english, yap ? To make this you’ll have to set your localeResolver in your resources.groovy spring configuration file. just add this code to it. (note that my code is setting my language to brazilian portuguese – pt_BR)

//this is your resources.groovy file
//
beans = {
   localeResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) {
      defaultLocale = new Locale("pt","BR")
      java.util.Locale.setDefault(defaultLocale)
   }
}

0 issues on my dreamhost hosting.

Some of you may ‘ve noticed that my blog was not responsing the hole day… Sorry about that. I just moved from a shared hosting to a private server (so I can run grails here) and DH just broke my server config.

Terrible…

9 [4/25] Jasper Reports in Grails with Dynamic-Jasper

This tutorial will talk about producing Jasper PDF reports (or any other format you’d like) in you grails app.  I took a look in grails plugins portal I found two plugins that could be used to do this.  The Jasper Plugin and the DynamicJasper Plugin. Depending on what you really need you’ll choose one.

I see the JasperPlugin as a more customizable plugin since you’ll use it o link to an existing jasper report (.jrxml / .jasper) you have. You’ll have some work building it, modeling it and sometimes even “drawing” it, but if you really need to do your and just your jasper, I recommend this one (congratulations for the Brazilians responsible for this plugin).

Otherwise (and covered in this tutorial), if you just need a simple report for your domain classes (an poor-but-effective PDF view of your scaffold listing) like I need in one project here, the DynamicJasper Plugin is gonna let you rock!

It’s a simple, and versatile plugin that generate its output entirely dynamic. This means that you won’t need to open iReport and show us your drawing skills (as a good programmer, you may suck drawing!).

We’ll work only with the Entities Report that Dynamic Jasper offer us, if you need complex queries on the reports, I recommend you reading the “named reports” in the plugins official documentation.

Are you following me and reading my blog’s feed? Be the first to know when I publish some interesting article signing up to my feed and following me on twitter!

Tutorial Info

Groovy Version: 1.6
Grails Version: 1.1
Plugin Version: 0.5
Plugin Documentation: http://grails.org/plugin/dynamic-jasper
Download: source code

Basic setup

Well, our example this time will be a simple agenda, so, let’s create our agenda app, install the dynamic jasper plugin and then create our domain class with some constraints.

grails create-app agenda
grails install-plugin dynamic-jasper
grails create-domain-class Contact

Our initial Contact class will be this one

class Contact {
   String name
   String nickname
   Date bornAt
   String email
   String website
   String phone
   String mobilePhone
   String gender

   static constraints = {
      name(maxLength: 255)
      nickname(nullable: true)
      bornAt(nullable:true)
      email(email:true)
      website(link:true, nullable:true)
      phone()
      mobilePhone()
      gender(inList:["M","F"])
   }
}

Running the application and making it reportable

That’s it, you can run your application and test it if you want. Now we’re going to create our first report, the simplest one we can have. To do this just add this code to your domain class:

static def reportable = [:]

This map notation will tell what fields will be shown in the report and what options of it you’re configuring. As we do not specified any, all propeties will be there and the default report will be generated.

After this you can visit the report generator url at http://localhost:8080/agenda/djReport/index?entity=contact and this will generate a simple report file (no extension, you should add .pdf) of your contacts.

Very very simple, hã!

More options (personalization)

Now let’s configure some basic options of our report. First of all, I’ll not get all this properties in our agenda report, let’s get only the main fields (nick, phone, email):

def static reportable = [
   columns: ['nickname', 'email', 'phone']
]

You can run again the report, it will be similar to this one:

report

Now, there are some other basic options you might want to configure, like the filename, the report title and other stuff:

title: The report’s title, by default if you do not set anything it will be “[entity-name] report”.
fileName: The name that the response file generated will have
columns: the columns shown

def static reportable = [
   title: 'My agenda',
   fileName: 'agenda',
   columns: ['nickname', 'email', 'phone']
]

Second report

This three will help you start in this great plugin. Take a look in the plugin’s page to see all you can do.

It’s a simple but powerful plugin, you can adjust all the page layout, group properties, change the column titles, everything that does not involve the usual iReport drawing process.

Important Note

This note may be valid to other plugins either, but since grails 1.1, the installed plugins is not available in the project’s folder but in your HOME_DIR/.grails/1.1/projects/<project>/plugins

So, if you want to use advanced configuration of this (and others) plugin, you shoud enter its folder in ~/.grails/1.1/projects/agenda/plugins/, and get the configuration file of it, (in our case, DynamicJasperConfig.groovy) in its conf folder and save in our agenda/conf folder.

This file holds all plugin configuration and it can be used to setup the report layout and configure the named reports I said before.

Again, take a look in the plugin page and you’ll find everything you need! This is just an introduction of the plugin!

[]s,

Are you following me and reading my blog’s feed? Be the first to know when I publish some interesting article signing up to my feed and following me on twitter!

4 Making a readeable code, more and more readeable

One thing groovy and grails really rocks, is the legibility of the generated code. It’s amazing how this Java code below

Contact boringObject = new Contact();
boringObject.setNome("Lucas");
boringObject.setIdade(24);
boringObject.setEmail("lucastex@gmail.com");
boringObject.setPhone("55 11 9999.9999");
boringObject.setDateCreated(new java.util.Date());

can turn into this beautiful code:

def c = new Contact()
c.name = "Lucas"
c.age = 24
c.email = "lucastex@gmail.com"
c.phone = "55 11 9999.9999"
c.dateCreated = new Date()

Yeah, I know, this is simple and it’s already done in most frameworks via OGNL and reflection, but groovy bring this feature natively and makes the code human-readeable removing all unnecessary commas, parenthesis and other symbols. Groovy brings the code to the closest natural reading language I ever saw! You can see this by studying the GORM dynamic finders, that is a real good example of what I’m saying; you think, you transcript and that’s the code.

Another useful feature, to make the code more readeable, is the with builder block. Imagine the snippet above, all properties that we’re setting, we’re setting in the same object, so why do we have to mention the object everytime? So, you can use this block to set everything in the same object. Try the code above:

def cb = new Contact()
cb.with {
   name = "Katie"
   age = 24
   email = "katie@xpto.com"
   phone = "55 11 8888.8888"
   dateCreated = new Date()
}

That’s why I like groovy so much, you won’t waste your time on “syntax learning”. All you need to do is read the code! :)

Follow me on twitter and subscribe my blog’s feed!

0 Multi-Tenant plugin. Incredible!

Today I was reading the old mails that I couldn’t read from last week and I read about the Multi-Tenant plugin for grails.

It’s an awesome plugin that solves your biggest problem if you have an simple application that you’ll sell its service for a bunch of people. You always think about having multiple instances of it, one for each user, correct? And you say:

– Ohhh no, I’ll have to have a lot of app servers!

So you start thinking about having a “customerId” for each domain class of your application. You’ll be lucky if you do not forget to use this identifier in some query…

Multi-Tenant plugin does all this boring job for you, you just annotate your domain class and configures a DNS resolver, telling the plugin that any request coming from www.xpto1.com will be the first customer and www.xpto2.com the second one.

Everything will be carefully watched so you’ll not have to worry about.

Don’t forget about this plugin, will save me. Hope it can help you! Read all docs about it here: http://grails.org/plugin/multi-tenant

« Previous PageNext Page »

Web Analytics