Tuesday, December 2, 2008

Grails Integration Testing - Some Tips and Tricks

As stated in the Grails documentation "Integration tests differ from unit tests in that you have full access to the Grails environment within the test". This actually means that you have access to database, you can inject necessary services into the test class.

All integration tests have to extend GroovyTestCase class.

Database

By default, integration tests use in memory HSQLDB. You can change this by updating DataSource.groovy.

Next important feature to remember is that integration tests run within transaction that is rollbacked after the test. So if you want to have data within database after the testing, in the test specify boolean transactional = false.

Services

To use default injection of specified service classes within test all that is necessary is to specify service as a field in the test class. For the rest magic of the Grails will take care. E.g. example would be FeedService feedService

Domain Classes

Within integration test you can use domain classes same as you would in the regular code. The main difference is that in the most cases methods on the domain classes should be called with the flush:true parameter.

Knowing all these it seems that integration testing with grails is really easy. And it is the case but there are some additional features to take care (some of them are valid also for the production code).

Some Tips

Tip: Saving domain object can fail without any error

Yes this can happen and happens often when you forget about some validation rule. And when you are not aware that your object was not saved your test results cannot be correct.

So for example following code snippet will not save the object but also no exception will be thrown:

FeedSource feed = new FeedSource(feedLink:"http://abc.com", title:"abc")
feed.save(flush:true)
Article article = new Article(title:"afaf", articleLink:"http://abc.com", source:feed)
article = article.save(flush:true)

Reason is that there is 'description' field that is mandatory for the article. To ensure that you don't get such surprises I use following code:

private Object save(Object object) {
validateAndPrintErrors(object)
Object result = object.save(flush:true)
assertNotNull("Object not created: " + object, result)
return result
}

private void validateAndPrintErrors(Object object) {
if (!object.validate()) {
object.errors.allErrors.each {error ->
println error
}
fail("failed to save object ${object}")
}
}

To check that there are no errors in the domain object you need to call method validate(). If there are validation errors you can get them through errors property of the domain object.

Next thing to notice is that method save() of the domain object returns instance of created object if it is correctly saved. So you can check if returned object is different than null.

Code for saving and validating can also be implemented as extension to domain classes using some nice groovy magic. Further on, this methods also works only thanks to groovy magic.

So initial code snippet should be changed to following.

FeedSource feed = new FeedSource(feedLink:"http://abc.com", title:"abc")
feed = save(feed)

Article article = new Article(title:"afaf", articleLink:"http://abc.com", source:feed, description:"asbfasfd")
article = save(article)

In this case there will be no surprise that domain object was not created within database without any exception thrown.

Tip: In some cases you need to reload domain object from database

As base for the GORM magic is hibernate, and we know that hibernate uses cache, there are cases when you need to reload object from database to ensure that it is in correct state. So for example if the rule is that when feed is deleted source of the article should be set to null, following code may fail.

feedService.deleteFeed(feed)
// we are sure that this article exists
article = Article.findByTitle("afaf")
assertNull("Article source should be null but is ${article.source}", article.source)

Reason is that deleting feed will not update source field of the article in the hibernate cache and the last line will fail. To ensure that you really have latest object from the database you should call refresh method on the article.

feedService.deleteFeed(feed)
// we are sure that this article exists
article = Article.findByTitle("afaf")

article.refresh()

assertNull("Article source should be null but is ${article.source}", article.source)

Tuesday, November 25, 2008

Even Agile Projects Need Vision and Concept

So you have heard about this strange word - 'Agile' . After some googling, reading some blogs and maybe some books you hear only promises how you will implement majority of your projects easily, in time and within budget. All you need is to put some team together, write some user stories, estimate them and start doing iterations. You track your velocity, write unit tests and more or less follow best agile practices while coding. And somewhere in the middle you find out that you are lost and not sure what is happening. Then you start to ask your self what had happen?

This is happening probably more often than we would expect. And the reasons are different. I will not try to find those reasons as lot of them are already described and you can find out how to prevent them.

I will concentrate on one that is for unknown reasons rarely mentioned in blogs and even agile books.

Even when you are doing agile software development (XP, Scrum) what you need is concept or vision for the project. But not only in the head but also on the paper. This needs not to be hundred pages document but document that clearly states what are the goals of the project.

In the number of agile posts, your project is starting with writing epics, stories and/or roles. But it is little bit confusing how to get those user stories. Advice you find the most often is to make user stories writing session where moderator guides meeting and the goal is to write as many user stories as possible. But how you can write user stories if you are not aware what is the main goal of the project? To be able to write good user stories (even epics) you must have clear idea what is the vision of the project.

To be able to write good user stories you must have vision document.

Some of the attributes of the good vision document are:

  • It is short
  • It clearly states goals of the project
  • It is read and understood by the software development team (and all other interested parties)
  • It is always up to date (changing with the project)

I believe these attributes state that vision document is agile

So if you are planning new project and want to do it right way I believe correct order of steps would be (note that very often the first step is left out and I believe it is the first mistake in the project):

  • Write vision document and communicate it to all interested parties
  • Write epics and user stories based on the vision document
  • Create your product backlog
  • Start with iterations
    • After few iterations review the project
    • If necessary change user stories
    • If necessary change vision document
    • Communicate changes in the vision to all team members so they are aware of the new course of the project

So vision document must reflect all the time your current goal of the project and this goal has to be clearly communicated to the team.

Be careful, doing agile project without clean vision and without adapting during the path is impossible.

Wednesday, November 12, 2008

Public Methods Should be Like Stories

I believe that most of the developers try to write code to be readable as much as possible. I will try to explain how I try to achieve this. From my point of view one of the most important things is to write public methods as stories. This means when somebody is reading your public method its implementation should tell with the sentence like methods what it tries to achieve. This way one can concentrate on the business logic that the method tries to achieve and not how that is achieved.

So I try to follow few short rules:

  • Instead of code within public method call number of private methods
  • Avoid loops
  • Try to minimize if statements
  • If private method is hard to read apply this rules to private method

Following this rules your public methods are easy to understand. It is easier to test if you are doing black-box testing. What may happen to your code is that you will have number of private methods if you implement mentioned rules also on private methods. But I don't consider this to be bad because in that case even private methods are easy to understand so if one should change your code it will be easier.

As opinion is easier to understand through example, I will show one method from my http://www.flexiblefeeds.com site.

Although example is in groovy I believe it will be easily understandable for all developers. Currently method looks like this:

def currentUserVote(Long articleId, boolean upVoting) {
boolean canVote = canCurrentUserVote(articleId)

if (!canVote) {
return
}

vote(articleId, upVoting)

registerVoting(articleId)
}

I believe that is is easy to understand what the method does from the code itself. But to be sure, first it is checked if current user can vote. If user cannot vote method returns. If user can vote voting is done and then voting is registered.

Now let us see how this method can look like if the code would be embedded into this public method.

def currentUserVote(Long articleId, boolean upVoting) {
// decide if user can vote
if (!loggedInUserIsAdministrator()) {
return
}

// logged in user can vote if he didn't voted
if (userIsLoggedIn()) {
return !voted(loggedInUser().id, articleId)
}

// not logged in user can vote if he didn't voted and data is stored in session
if(votedInSession(articleId)) {
return
}

// perform voting
try {
String sql
if (upVoting) {
sql = "SQL_FOR_UP_VOTING"
} else {
sql = "SQL_FOR_DOWN_VOTING"
}
Article.executeUpdate(sql, [id:articleId])
} catch (Exception ex) {
log.error("Failed to vote up for article ${articleId}", ex)
throw ex;
}

// register voting
if (loggedInUser()) {
def a = Article.get(articleId)

try {
ArticleVoting voting = new ArticleVoting(user:loggedInUser(), article:a)
voting.save(flush:true)
} catch (Exception ex) {
log.error(ex)
throw ex;
}
} else {
if (!session().votedIds) {
def votedIds = [] as Set
session().votedIds = votedIds
}
session().votedIds.add(articleId)
}
}
Having look at this method you can notice it is possible to understand what is method doing. But beside understanding what is method doing you are reading code. It means you are doing two things at the same time. Trying to understand business logic and trying to understand how this business logic is achieved.

Therefore in all cases I would recommend to refactor such code and to extract parts of the public methods into private methods.

Tuesday, November 11, 2008

It is very expensive to work with Grails

Just to make disclaimer immediately at the beginning. I don't really thing that working with Grails is really expensive, I would display it as expensive from the funny point of view.

This post is more advertisement then real post. So if you don't want just don't read it.

Small ad. One of my latest expenses, please have a look at http://www.flexiblefeeds.com.

So lets go to the point. Some time ago I discovered Grails. I tried to write few simple applications. Then I tried to write more complex applications. Then I was investigating advance features.
And very soon, less than a month after discovery, I realized that one is able to write real web applications with Grails more or less within minutes.

But if writing web applications is so easy, why not create real web application and put it on line. And there came the first idea. I wrote http://www.grailstutorials.com, payed subscription 20$/month and put it online. Visitors are coming, site is I believe well known within Grails/Groovy community so it stays online and I am still paying 20$/month. But these money can not be compared with satisfaction having real, visited web site online.

But writing web applications with Grails is still easy. So why not try another application. I implemented (actually still implementing) http://www.flexiblefeeds.com, payed subscription 20$/month and put it online.

Now I have two Grails based applications online, still trying to improve both of them. More or less I have idea about two or three more projects that I want to implement with Grails. What I am missing is time.

So why is it expensive to work with Grails:
  • You develop web applications very fast so you will need lot of money to pay web hosting
  • You develop web applications very fast so you will need most of your free time just to try all ideas you get
  • Grails is growing so fast that you will need lot of free time just to read all the blogs and nice things you can do with groovy/grails
So if you don't want to write web applications fast and easy just ignore Grails. If you want to write web applications fast and easy than it would be wise to try Grails.

Sunday, November 9, 2008

Flexible Feeds Launched

New grails powered web site is launched. You can have a look at www.flexiblefeeds.com.

On this site you can register RSS feed of your blog or feeds of your favorite blogs or web sites. Each feed is registered in different category so you can navigate articles by categories. To help others to decide if it is worth to read article you can vote for articles.

You should give a chance to this site and you will see it can be excellent source of news in the blogging sphere.

Why this site? If you are at least a little bit like me your RSS reader is full of feeds. Feeds are probably added to different folders but you need lot of time only to navigate through all new articles. What I am missing in readers is possibility to have kind of unique view and social aspects like voting on articles and tags on articles.

All that I miss in classic readers is supported in the first release of flexible feeds.

But I don't plan to stop here. I would like to have flexibility with feeds. Maybe I want to have different views for the same category. Or I want to combine different feeds. All this and much more is planned for the next version.

And for the end, I would be glad if you register RSS feed of your blog or feeds of your favorite sites.

Sunday, November 2, 2008

Grails Conditional Tags as Method Calls Trap - Part 2

In my previous post I was discussing how grails conditional tags should be used from the code. More or less post was about how to use conditional tags within if blog. Although I thought that my approach is good comment from Graeme Rocher opened my eyes. And therefore I decided to write short post because maybe this comment wasn't noticed by others.

I planned to write this post immediately next day but as I was busy with releasing first version of Flexible Feeds I didn't have enough time. By the way you can also visit www.flexiblefeeds.com :)

But let's go to the point. My approach with conditional tags was that if I want to execute something that depends on condition that is already implemented as tag you should use structure like this one:

if (g.conditionalTag() {true}) {
// execute some business logic
}

{true} part is actually closure that will get executed in the case that conditional tag satisfy condition. I must admit that this structure looks little bit strange but if I need not to copy/paste already existing code I am happy with that.

But as Graeme correctly noticed better approach in such situations would be:

g.conditionalTag() {
// execute some business logic
}

I agree that this is more readable and easier to understand.

In the case you need if...else approach then you still need version with the {true}.

Wednesday, October 22, 2008

Grails Conditional Tags as Method Calls Trap

As you probably know grails tags can also be used within controllers as method calls. In this post I will describe the trap that caught me when I tried to use conditional tag within if statement.

Default usage of grails tag as method call can be described with the following example. g.render tag within gsp page will look like this:

<g:render template="someTemplate" model:[name:"peter"]/>

While within controller you can use this tag like this:

g.render(template:"someTemplate", name:"peter")
I must admit that this is excellent feature of the grails and I use it often. But you can get confused if you try to use conditional tags within if statements. Lets try one example.

Imagine that we have following conditional tag userHasGoldMembership that displays its content only when logged in user has gold membership. Usage of this tag within gsp page may look like this:

...
<g:userHasGoldMembership>
<g:link controller="abc" action="xyz">Perform this action</g:link>
</g:userHasGoldMembership>
...

What example does is that link 'Perform this action' will be displayed only if logged in user has gold membership.

Now let us try to use this tag as method call within controller as condition within if.

def price = 100
if (g.userHasGoldMembership()) {
price = price - discount
}
chargeCreditCard()

Well to my surprise what happens here is that g.userHasGoldMembership() always resolves to false. And after some thinking it becomes clear why this happens. Let us analyze how in the most cases conditional tags work. For example, for mentioned conditional tag implementation may look like this (and I believe this is the case for the majority of conditional tags):

class MembershipTagLib {
def userHasGoldMembership = {attrs, body ->
if (session.user.isGold()) {
out << body()
}
}
}

What we see in this code is that in the case that user has gold membership returned value from the tag will be body of the tag. In all other cases nothing is returned. But what is body within construct if(g.userHasGoldMembership())?

Well there is no body. This means that in both cases, user has and user has not gold membership, result of the tag used as method call will be null.

Does it mean that we cannot use conditional tags? Of course not. It is enough to provide body. Version of the if statement that works is:

def price = 100
if (g.userHasGoldMembership() {true}) {
price = price - discount
}
chargeCreditCard()

Did you notice the {true} part? This is actually body of the tag. It means that when user has gold membership, tag will return result of body execution and that is value true.

And of course example from the real conditional tag that is probably widely used is from acegi. Acegi plugin provides tag <g:ifAllGranted role="LIST_OF_ROLES">content to display</g:ifAllGranted>. And if you want to reuse this plugin within your controller then you have to use it in the form of

if (g.ifAllGranter(role="ROLE_ADMIN"){true}) {
do something
}
otherwise you will never get into the if part of the condition.

Thursday, October 9, 2008

javax.faces.el.PropertyNotFoundException

This is another interesting bug we had in just two days. Again one of those "this is not happening to me" bugs :)

We have JSF project and on JSF projects you are creating managed beans and setting values to those beans within faces-config.xml file.

Well for one of the new managed beans we got javax.faces.el.PropertyNotFoundException. In the first moment it seemed like one of standard typo errors but after verifing the code everything seemed ok but JSF was just refusing to set value of the managed been. Again, after some googling we found solution in this post.

Well shortly, this problem occurs if you have variable in the managed bean that starts with lower-case character after which immediately follows upper-case character. So variables with the names like: xCor, xStreamConverter or similar cannot be found by JSF and finish with the
javax.faces.el.PropertyNotFoundException exception.

java.util.MissingResourceException

These days we had two very strange errors during build of project. Our build configuration is CruiseControl with Maven.
When building project locally everything worked fine while on CruiseControl we got this error:
java.util.MissingResourceException: Can't find resource for bundle com.sun.tools.doclets
.formats.html.resources.standard, key doclet.malformed_html_link_tag
First surprise was why everything works fine locally but fails on the build server. But answer we found quickly. On build server project was build with mvn clean site but locally we are mainly using mvn clean package. After using mvn site locally we got the same error. And then we started with investigation. After hours of investigation we found solution and the problem is described with this Jira issue.

Shortly, with some build versions of Java 5, generation of javadoc will fail if there is <a somewhere in java doc.

So when writing java doc avoid <a characters sequence.

Friday, September 26, 2008

Wednesday, September 24, 2008

Grails - Ajax Presentation

Next presentation I had is about Grails and Ajax. The ppt is posted on ITslaves.

Tuesday, September 23, 2008

Grails and Acegi Presentation

Next presentation I had is about Acegi security plugin for Grails. It is shortly described what will Acegi plugin generate for you and how to setup basic security. Presentation is posted at ITslaves.

Tuesday, September 16, 2008

Grails - CRUD

Recently I had internal presentation about Grails in my company. If you are interested you can find presentation on ITslaves.com

Three more presentations are planned (acegi, ajax, templates).

Sunday, August 17, 2008

svn: inconsistent line ending style

Today I was adding grails tutorials into SVN repository. Yes I know it should be in the repository long time ago :). And something that should be simple operation finished with the svn:inconsistent line ending style. For those who didn't still hit this problem, SVN finishes with this error if you have different line ending styles in the same file. And it will refuse to add such files into repository till it is not fixed. As there was more than one file with such problem (few hundreds of them) manual intervention was not an option. But to my surprise (after googling) I was not able to find how to fix it automatically for all the files. So I decided to write a groovy script that will fix it for me.

And without too much waiting groovy script is here:

if (!args) {
println "Usage: <path_to_directory>"
println "And: <existance of extensions.txt comma separated values file with file extensions to convert>"
return
}

Convert c = new Convert()
c.convert(args[0])


class Convert {
Set extensions = new HashSet()

public void convert(String path) {
extensions()

println "extensions to convert: ${extensions}"

File f = new File(path)
convertDir(f)
}

private void convertDir(File f) {
def sum = 0
def filesFound = 0
def filesVisited = 0
f.eachFileRecurse{File file ->
filesVisited = filesVisited + 1
if (shouldConvert(file)) {
filesFound = filesFound + 1
if (filesFound % 100 == 0) {
println "Files checked: ${filesFound}"
}
if (replaceLines(file)) {
sum = sum + 1
if (sum % 10 == 0)
println "Replaced eol in files: " + sum
}
}
}

println "Files converted: ${sum}"
println "Files checked: ${filesFound}"
println "Files visited: ${filesVisited}"
}

private boolean shouldConvert(File f) {
return extensions.contains(extension(f))
}

private String extension(File f) {
int idx = f.getName().lastIndexOf('.')
if (idx != -1) {
String result = f.getName().substring(idx + 1)
return result
} else {
return null
}
}

def replaceLines = {File f ->
String text = f.text

if (text.contains('\r\n') || text.contains('\r')) {
text = text.replaceAll('\r\n', '\n')
text = text.replaceAll('\r', '\n')
f.write(text)
return true
}
return false
}

private void extensions() {
File f = new File("extensions.txt")
String content = f.text
String[] str = content.split(",")
Set extensions = new HashSet(Arrays.asList(str))
extensions.each{
it = it.replaceAll('\r\n','')
this.extensions.add(it)
}
}
}

To be able to use this script you have to install groovy. Then in the same directory where you have your file you need to create extensions.txt file that contains list of file extensions that should be checked. Extensions should be comma separated without spaces in between.

Then run the script with groovy convert.groovy <path_to_directory>.

Now what is visible in this simple script is how groovy extensions to the java.io.File help us to work with files and directories. Actually if you go through code you will see that following methods have been used:


  • f.eachFileRecurse - will recursively traverse of files in the directory structure

  • f.text - will return you content of the whole file as String

  • f.write - will write string as a context to the file

Well you can use this script if you have the same problem but you know that script is provided as is and in the case of damage I will not feel responsible any way.

Thursday, August 7, 2008

Grails, Groovy, XML

Last few days, while working on grails tutorials, I needed to translate some Grails domain objects to XML. I thought it should not be so hard as there is lot of articles about Groovy and XML. But what I found out is that most of the articles present only the simple usage of Groovy XmlSlurper. One of the examples would be:

def xml = new groovy.xml.MarkupBuilder()
xml.person(id:99){
firstname("John" )
lastname("Smith" )
}

And what I need is to traverse recursive structure of domain object. I know it is not so hard but anyway I decided to describe it here. At the end, as you will see it is very simple. But, by the way I will also explain how Grails supports XML transformations.

Let start with Grails support. If you don't request special structure of XML but it is enough to directly transform domain object to XML then Grails is excellent solution for you.

Domain object to translate is:

class LearningArea {
String title
String description
Date dateCreated
Date lastUpdated
List tutorials
List subAreas

static hasMany = [tutorials: TutorialLink, subAreas: LearningArea]

static constraints = {
title(blank:false, minSize:3, maxSize:500)
description(blank:false, maxSize:3000)
}

public String toString() {
return title
}
}

In Grails you can translate object to XML as in following example:

LearningArea learningArea = LearningArea.get(1)
String xml = render learningArea as XML

But interesting thing to notice is that render can be imported from two packages:

grails.converters.XML and grails.converters.deep.XML

And of course there is difference which package you use.

If you use deep package complete domain objects tree (with all sub areas's, sub areas and so on...) will be generated with all the properties. So in the case of LearningArea complete tree of all subAreas and all tutorials will be translated to XML including all properties.

If you use just converters.XML package, top level object is fully translated but referenced objects are translated only to the level of ids. This means that XML generated will be much smaller but to obtain additional information you need to retrieve additional data later.

Unfortunately for me, I couldn't use this Grails XML magic because I didn't want tutorials in the XML but I wanted full tree of subAreas only with titles and without description properties. So I had to use Groovy XmlSlurper. Important thing to notice in the above example of XmlSlurper is that when using XML builder, you can also provide closures. So all I needed was to recursively traverse the tree of the subAreas. And the code to do that is very simple:

    def generateXML= {
def writer = new StringWriter()
MarkupBuilder xml = new MarkupBuilder(writer)

LearningArea lr = LearningArea.findByTitle("Groovy")

toXml(lr, xml)

String str = writer.toString()
}

private void toXml(LearningArea la, def xml) {
xml.learningArea(name: la.title, id: la.id) {
la.subAreas.each {
toXml(it, xml)
}
}
}

As we can see all the logic is in the toXml method. This method accepts instance of LearningArea and MarkupBuilder, writes element learningArea to XML with attributes name and id, and the as subelements recursively writes all sub learning areas. This way the complete XML tree is generated in few lines of code and I got example that is little bit more complex than most of the examples on the web.

And if you didn't visited grails tutorials yes, you can do it now :)

Saturday, July 12, 2008

Memory leak when using external configuration with Grails?

Reported as Jira issue: http://jira.codehaus.org/browse/GRAILS-3252


Few days ago I added some changes to grails tutorials and suddenly it started to fail due the out of memory error. After some investigation this is my conclusion.

It seems that there is a memory leak when using external configuration file to handle certain parts of your grails application. This happened to me with Grails version 1.0 and I am able to repeat it on Axis delivered with Grails and Tomcat 5. I didn't tried with other jsp containers.

Follows short description how to create simple application to reproduce this bug. I use tag lib and didn't try if the same thing happens without tag lib.

Create new grails application: grails create-app memory-leak.

Then create new tag lib MemoryLeakTagLib.groovy. Content of this tag lib is:

class MemoryLeakTagLib {
def displayExternal = {attrs, body ->
if (grailsApplication.config.t1.value) {
out << body()
}
if (grailsApplication.config.t2.value) {
out << body()
}
if (grailsApplication.config.t3.value) {
out << body()
}
if (grailsApplication.config.t4.value) {
out << body()
}
if (grailsApplication.config.t5.value) {
out << body()
}
if (grailsApplication.config.t6.value) {
out << body()
}
if (grailsApplication.config.t7.value) {
out << body()
}
if (grailsApplication.config.t8.value) {
out << body()
}
}
}

Then create controller: grails create-controller home.

Content of the controller is:

class HomeController {
def index = {
render (view:'home')
}
}

Now create home.gsp file within folder views/home/.

Content of the home.gsp is:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="layout" content="main" />
<title>Memory leak</title>
</head>
<body>
Will same situation repeat?
<g:displayExternal>
a
</g:displayExternal>
<g:displayExternal>
a
</g:displayExternal>
<g:displayExternal>
a
</g:displayExternal>
<g:displayExternal>
a
</g:displayExternal>

</body>
</html>

Now in your HOME/.grails directory create file memory-leak-config.groovy file with the following content:

t1 {
value=true
}

t2 {
value=true
}

t3 {
value=true
}

t4 {
value=true
}

t5 {
value=true
}
t6 {
value=true
}
t7 {
value=true
}
t8 {
value=true
}

Now update Config.groovy file to also read external configuration files. Snippet how it should look like is:

 grails.config.locations = ["file:${userHome}/.grails/${appName}-config.groovy"]


if(System.properties["${appName}.config.location"]) {
grails.config.locations << "file:" + System.properties["${appName}.config.location"]
}

Now if I didn't forget some file change you should be able to start application. Open browser and point it to: http://localhost:8080/memory-leak/home First you will notice is that page is loading very long. Then while monitoring memory consumption of Tomcat or Axis refresh page few times. You will see that memory consumption is increasing. And it is never returned to the initial state. If you refresh this page constantly for 10 minutes you should run out of memory.

Well, if you didn't visit grails tutorials yet, you can do it now. It already can stand more than 80 hits (it was running out of memory after 80 hits).

Wednesday, July 9, 2008

Be careful when developing on Windows but hosting on Linux

I know this is kind of old and obvious but any way I had the problem with paths that are not case sensitive on Windows but case sensitive on Linux. I hit this problem while I used grails controller with the name LearningCenterController and had my gsp pages in folder named learningcenter.

Simple code from controller like this one:

def index = {
render (view:'about')
}

worked fine on Windows. Then I deployed changes to my VPS hosting provider and there I had a problem with error reported: file not exists. And of course, this happened because folder names are case sensitive on Linux.

So nothing special but only good well known story: be careful when developing on Windows but deploying on Linux.

And by the way did you already visited grails tutorials? ;)

Monday, July 7, 2008

Experience with Act as Taggable Plugin

While working on grails tutorials site I have also used Act as Taggable plugin. In this post I will try to describe my experience while using this plugin. Although plugin is in alpha phase it gives you solid ground to implement tag cloud. At least you need not to start from the scratch.

First step is of course to install plugin. Download latest version from google code svn repository of Act as Taggable Plugin. Then in the console type:

grails install-plugin grails-acts-as-taggable-0.1.zip 
That line will install plugin. To mark some domain class as taggable add implements Taggable to that domain class. Other basics you can find in act as taggable tutorial on the grails home page.

Let us have a deeper look at what this plugin offers.

If you inspect the code you will see that there are two domain classes Tag and Tagging. This means that this plugin will add two tables into your database. Class Tag represents tag and has only one property: name. Class Tagging is used as connection between Tag and any Taggable class. Interesting property here is String taggableType. This property represents magic how to connect any tag with any taggable domain class. This property equals to the class name of the taggable domain class. This way you are able to connect same or different set of tags to different taggable classes.

Beside those two domain classes there are two more groovy classes: Taggable (already mentioned in previous section) interface and TaggableMixin. Taggable interface is just interface with no methods but it is heavily used within TaggableMixin to add additional methods to all classes that implements this interface. It is really good to have a look at the TaggableMixin class.

TaggableMixin defines four closures: addTag, getTags, setTags, removeTags, and removeTag. Method getTags will return list of all tags for the calling Taggable instance. But as it uses join(" ") to separate tag names, if you have tags with the spaces those tags will be separated into multiple tags. This was not behavior I needed so I added additional closure getTagsAsList. This closure does the same thing as getTags but will not split your tag if it has space. The code of the closure is:

Taggable.metaClass.getTagsAsList = { ->
def taggings = Tagging.findAllWhere( taggableId: delegate.id, taggableType: delegate.class.toString())

def result = taggings.inject([]) {list, tagging ->
list.add(tagging.tag.name)
return list
}

return result
}

Next thing you will probably need is list of tags in this form: [tagName:count, tagName:count,...]. Taggable plugin currently does not offer method that will do it for you so I have created one for me. I didn't put it into source of taggable plugin because I want to minimize merging problems when new version of plugin is released. Code looks like this:

def tagGrouping = Tagging.executeQuery("select distinct t.tag.name, count(t.tag.id) from Tagging t group by t.tag.id")

def values = tagGrouping.inject([:]) {val, obj ->
val.put(obj[0], obj[1])
return val
}

Note that I didn't add control where taggalbeType = 'class of taggable instance'. If you need it just add that line. I think that this method should be part of the TaggableMixin too.

Further on what you will probably need is to get all instances (or subset of them) tagged with provided tag and total number of those instances. So for example you want to get all tutorials tagged with GORM tag. As such query is currently not supported within plugin I have created my methods. Lines of code you can use are:

def tls = Tagging.executeQuery
("select distinct t from TutorialLink t, Tagging tgg, Tag tg
where tg.name = ? and tgg.tag = tg and tgg.taggableType = ? and tgg.taggableId=t.id
order by t.dateCreated desc"
, [params.selectedTag, TutorialLink.class.toString()], [max:max,offset:offset])

def total = Tagging.executeQuery
("select count(t) from TutorialLink t, Tagging tgg, Tag tg
where tg.name = ? and tgg.tag = tg and tgg.taggableType = ? and tgg.taggableId=t.id
order by t.dateCreated desc"
, [params.selectedTag, TutorialLink.class.toString()])[0]

I must admit that these lines are more specialized for grails tutorials but they can be generalized with the minimum effort.

As you can see I took Act as Pluggable plugin and made it more user friendly by adding few simple methods. Hopefully these methods will be soon available in the official plugin. Usage of plugin is simple and gives you excellent base to increase your productivity.

Friday, July 4, 2008

Is development of CRUD apps faster with Grails than with Swing?

To make my point clear immediately at the beginning. I am not claiming that creation of fat rich client is easier in some web framework than with Java or .NET. I am talking about simple CRUD application with three domain objects, and two features that cannot be directly connected with CRUD.

So problem is following. There are three domain objects that should be stored into local database. Domain objects are Calculation, Shop and Item. Relationship between domain is displayed on the following picture:

calculation

So each calculation is made for exactly one shop and can contain number of items. Pretty straightforward domain model. Additional requirements are that ordinal number of Calculation is increased per Shop instance and you are able to print report for each Calculation.

So what are the tools necessary to create simple CRUD application for mentioned domain model. I decided for Swing, Hibernate, HSQLDB, Jasper reports. So how the work goes:

In Java:

As first step you create domain classes and then using some examples on the Internet create hibernate mapping files and hibernate configuration file. Next step is to reuse some your local component that handles SessionFactory initialization or you just find example on the Internet and copy/paste that example. More or less even these simple steps already can bring some headache (e.g. if you mistype property name of your domain object in XML file). And yes I know, probably there is number of tools that can help me with this configuration and I heard about Hibernate annotations too :)

After your domain object is created and database is automatically generated for you real fun begins. You need to create Swing application. You need main frame, dialogs and panels (even reused) to CRUD Shop domain objects and the same thing for Calculation and Items that are part of Calculation. As you need tables to display list of domain objects you have to create corresponding table models, connect listeners to buttons and so on. Everybody that have some experience with Java Swing know that although this is more or less simple and straightforward process it does not mean it will work from the first try. And the most often it will not.

When set of your screens is ready you still need to wire it up with domain and Hibernate (DB). You have to cope with hibernate sessions, lazy loading...

When your application supports all CRUD operations the end is near. You just need to create Jasper report and it is easy with the help of iReport.

Now when you see all steps necessary what would be your estimation for such application. I would say 2-3 days (well I did it in my free time).

In Grails:

Now let us see how all these steps are handled in Grails. Thanks to the GORM, scaffolding and plugins (Jasper plugin particularly) we will see that it is significantly faster with the Grails.

To start first you need to create new Grails application. For this it is enough to type grails create-app.

When your application is created using grails create-domain-class you can generate necessary domain classes. When domain classes are generated you add code for properties and relationships between domain objects. Having this you don't need to worry about Hibernate configuration because Grails will do that automatically for you.

Now when domain objects are ready we need to create user interface of application.

Well creating web user interface can really take time. You need to type generate-all for each domain object or if you are lazy you can reuse UberGenerateAll script. And your user interface is mainly finished (actually you can keep it that way if you want and still you will have nice fully functional web application). If you want you can update generated pages (e.g. remove ids from tables, maybe copy/paste few buttons). More or less easy work. You need to add little bit of behavior to the controllers or domain classes but this work can be only faster with Groovy than it was with Java.

To create report you can reuse Jasper Reports plugin for Grails. This means even all necessary dependencies will be resolved for you :)

And actually simple CRUD application is finished.

Conclusion

While I worked on Java implementation of described application I was thinking why it cannot be easy as with Grails. Anyway I was just repeating steps that I did number of times. Domain classes, Hibernate, Swing, JTables, dialogs. When I finished Java application I generated new Grails application and had basic behavior done in less than hour. It is just great and unbelievable. I didn't do final touches to the Grails application because anyway I didn't need this application in Grails.

But I was thinking, if I use embedded Jetty, embedded browser in Java frame, wouldn't it be easier to finish this application :) Maybe next time I will try.

But another idea that came to my mind is what if we could reuse GORM in the standalone applications, why not to implement scaffolding for Swing front end...

More or less I believe that Grails is excellent web framework that boost your speed and agility. The main problem with Grails is that you are going so fast forward that very often you are forgetting to write tests :) Believe me, I have experienced this on grails tutorials.

Thursday, July 3, 2008

Jasper Reports and JDK vs JRE

Although it was clear to me what happened immediately as I saw stacktrace, I was surprised that it didn't came to my mind during development. One of the tasks I had was to create report in the very simple application. As I wasn't sure about structure of the report and all the fields that should appear in the report I have decided to use jrxml file. jrxml file is XML definition of report and to display it you have to compile it during runtime. The code to do this is actually very simple:

JasperReport report = JasperCompileManager.compileReport("report.jrxml");
When you have instance of the report it is enough to use JasperFillManager to fill it with data.Another possibility is to compile jrxml file to jasper report. This will generate jasper, binary version of the report. When you have compiled report of course you don't have to compile it during runtime.Well, more or less I had XML version of the report, created installation using Null Soft installer, installed it on my computer and everything worked fine. I could create domain objects and display and print reports. So I was happy and delivered the installation to customer (in this case my mom). She installed application without problems, entered necessary data and tried to open report. And then to my surprise report didn't opened but there was message about unexpected error. As soon as I saw the stacktrace it was clear that actually she does not have java installed with JDK but as JRE.And of course JRE is not delivered with compiler and therefore application was not able to compile report. As soon as I changed application to read compiled report everything worked fine. So if you plan to deliver Java application to standard user computers, don't forget to test application on computers where only JRE is installed.

Saturday, June 28, 2008

Book Review: Groovy Recipes

I just finished reading Groovy Recipes book by Scott Davis so I would like to share my opinion about the book. Book is excellent "I'am in a Hurry" guide and you can get initial, but good enough knowledge about Groovy in a really short time. For sure it is not comprehensive guide to Groovy.

If you are Java developer and want to jump on Groovy wagon fast this book is excellent start. You will get introduction into Groovy and be able to use Groovy immediately as you finish with the book or related chapter of the book.

For those who have no knowledge about Java or Groovy I would not recommend this as a first book about Groovy. Even it is not stated that way, I believe that book expects at least some knowledge of Java.

The main minus for the book are poorly described closures. I believe that closures are really important part of the Groovy language and would expect better explanation. If you have no knowledge about Groovy closures, read some articles on the web to gain more knowledge about them.

Book is divided into 12 chapters.

Chapters 1 and 2 are introductory chapters describing in general what is Groovy, how to install it and how to integrated with the most popular IDEs.

Chapter 3 is dedicated to "special" (no Java like) constructs of language where you will learn about autoboxing, operator overloading...

Chapter 4 describes how to integrate Java and Groovy and vice versa. You will learn how to compile Groovy code to Java classes and how to solve some possible tricky dependencies issues.

In Chapter 5 you will see how you can use Groovy from the command line.

Chapter 6 is very interesting and you will see how easy it is to work with files within Groovy. This chapter is very useful for Java developers because they would ask themselves why Java file handling cannot be as easy. In Groovy you can read content of the file, you can list content of the file literally with the line of code.

Chapter 7 and 8 are even bigger "wows" for the Java developers. These chapters describe how Groovy handles XML. You will learn how to read XML file with XmlSlurper and XmlParser and how to create XML with MarkupBuilder and StreamingMarkupBuilder. For those that are used to to work with XML in Java this chapter will be proof that working with the XMLs need not to be painful.

Chapter 9 is devoted to web services and there is very nice introduction to different types of requests like: http get, post, SOAP request, XML-RPC request and others.

Chapter 10 is about metaprogramming. Metaprogramming is dynamic part of the Groovy language and the Groovy language option that make Grails so good web development framework. This is the chapter I liked the most.

Chapters 11 and 12 introduce Grails, web development framework based on Groovy.




Monday, June 16, 2008

Tag Cloud Added to Grails Tutorials

I have added tag cloud to http://www.grailstutorials.com. Now each tutorial can be tagged so navigation is possible also by tags.

The main difference between tag cloud on grails tutorials compared to tag cloud on other sites is that it is enough to be logged in to change (add/remove) tags on any tutorial.

So far there is no possibility to add new tags, you have to reuse precreated set, but if there will be interest I plan to add this possibility too.

For tag cloud implementation I used Act As Taggable Plugin
This plugin give you boost at the beginning but from my point of view still misses lot of support functionality that I had to implement. I hope that in next days I will be able to organize code I implemented for tag cloud features and provide it to 'act as taggable plugin'. This way everybody can reuse what I had implemented and don't have to reimplement the same thing again.

If time permits me for sure I will write at least one post about tag cloud implementation on grails tutorials.

If you have any ideas or proposals for grails tutorials please let me know. I would be happy to have more responses from those who visit grailstutorials.com

Tuesday, June 10, 2008

Tweaking Star Rating component of RichUI Grails plugin

RichUI plugin for Grails is easy to use with number of interesting Ajax enabled components. One of the components is Star Rating component. Core behavior of this component is good but I wanted to get a little bit more than this core behavior. This component works fine out of the box. You click on the star, go to server, update rating and update number of stars assigned. And everything is executed without reloading the whole page. But what I wanted to achieve is also to update number of votes within the same Ajax request. And this is not supported by component. I checked the code of this component and found out that this can be achieved with really only few lines of code. Actually solution is so simple that maybe it would be good if it is included into the official plugin code. And all described in this post you can of course see live on the grails tutorials site.

So to make more clear what I am talking about have a look on the picture.

star_rating

Idea is that when you click on the star, number of votes is increased also without necessity to reload the whole page. And this is not possible with the current star rating component.

Right place to start searching what to change is RatingRenderer.groovy file. This class is responsible for rendering the star rating component.

Signature of method is

protected void renderTagContent(Map attrs, GroovyPageTagBody body, MarkupBuilder builder)
where attrs are attributes sent to the component from the gsp page. And after some code inspection it is clear that update is implemented using remoteFunction grails tag. To see more about remoteFunction read this post. One of the parameters to remoteFunction is also name of the element in the gsp page to update. Line of the code in RatingRenderer.groovy that accomplish this is
String link = attrs.link.replace(":class:", "r${i}-unit rater").replace(":title:", "$i").replace("update", "${id}").replace("number", "$i").replace("%3Arating%3A", "${i}")

and for us interesting part is replace("update",${id}). And id to update is automatically generated in the first line of method. So actually, all we need to be able to update any page element, not exactly only stars, is to provide alternate id instead of one automatically generated.

So all I changed is following:

String updateId = id
if (attrs.updateId) {
updateId = attrs.updateId
}
String link = attrs.link.replace(":class:", "r${i}-unit rater").replace(":title:", "$i").replace("update", "${updateId}").replace("number", "$i").replace("%3Arating%3A", "${i}")

This way if I provide updateId attribute to the star rating component, remote function generated by this component will update page element with provided id, otherwise it will keep original behavior.

And now, all I have to do is to use construct like this one in my gsp pages:

<div id="${tutorialid}tutorial">
<richui:rating dynamic="true" updateId="${tutorialid}tutorial" id="${tutorialid}" units="5" rating="${rating}" showCurrent="true" controller="rating" action="rate" />
${rate?.votes} votes; ${clicks} clicks
</div>

And of course previous code snippet is part of the grails template so during rendition necessary values are read from the provided model.

So only with three lines of code added to existing plugin code and one change to existing plugin code, star rating component is more powerful than the original one.

Wasn't that easy? ;)

Thursday, June 5, 2008

GrailsTutorials.com now supports RSS, Star Rating and Click Count

After few days of other responsibilities I had found some free time and additionally to star rating and click count I have added also support for RSS feeds. To see and try the latest features visit www.grailstutorials.com.

During development of RSS feeds and star rating I have hit some not too complicated but interesting problems and challenges. I will write separate posts about those challenges.

And as usually, just to remind you, register and post interesting links.

In the case of any feature requests, bugs or just comments do not hesitate to comment to this post.

Sunday, June 1, 2008

Make gsp readable with grails templates and taglibs

In one of my previous post I already explained how grails templates and taglibs can simplify your gsp files and make them more readable. Now I believe I made even step further. One of the reasons I like grails is that front end technology is solved very good and is easy to understand even for java back-end developers like me. My gsp files does not look like html files but more like collection of components. The reason is simple. With grails it is very easy to create templates and tags. Example in this post is taken from www.grailstutorials.com, portal of grails and groovy community on which I work in my free time. Let me try to explain mind flow. I wanted to display components like one on the picture in common and easy to read way.

stats

The first solution that came to my mind was really simple. Thing like this one you can solve easily in this way:

<div class="messageBox">
<div class="msg_box_title">
<label>TITLE_HERE</label>
</div>
<div class="msg_box_body">
CONTENT_HERE
</div>
</div>

But in the sea of the divs it is really hard to orientate and the gsp looks more complicated than it really is. So I start thinking how it would be nicer. And I realized that what I want to achieve so my code is easier to read is following:

<g:messageBox title="TITLE_HERE">
CONTENT_HERE
</g:messageBox>

So as the first thing I created message box template.

<div class="messageBox">
<div class="msg_box_title">
<label>${title}</label>
</div>
<div class="msg_box_body">
${body}
</div>
</div>



This code is almost the same as the first code. Important things to notice are model variables ${title} and ${body}. Now when I had a template I needed possibility to display it with the messageBox tag. So the next thing to create was to create tag. Just create simple tag file, if you don't know how read the post mentioned at the begging. And the code of the tag is:



def messageBox = {attrs, body ->
out << render(template:"/templates/common/messageBoxTemplate", model:[title:attrs.title, body:body()])
}



The only good thing to notice in the code above is usage of body() as a value of body model parameter instead of body that came as a input value into messageBox. Reason for this is that we don't want to send body closure to the template but content of the body closure.



Having messageBox tag available, the first code example in this post can be specified in the gsp file as:



<g:messageBox title="TITLE_HERE">
CONTENT_HERE
</g:messageBox>



exactly as we wanted.



Now let us think about CONTET_HERE part. If we put complex gsp code there we will not get too much in readability. But solution is very simple. Don't use complex gsp code there, create corresponding template and tag for that part and you gsp code will be easy for reading. Just to give you example of what I have in mind, this is code snippet I use in grailstutorials.com to display statistics (you can see the picture on the top of post):

<g:messageBox title="Stats">
<g:dataInfo/>
</g:messageBox>

I believe that the final result is really easy to understand.

Tuesday, May 27, 2008

Grails Tutorials

I have just released first draft version of grails tutorials powered by Grails. You can find it at www.grailstutorials.com.

For me it is only alpha version but I believe it can be useful already. You are able to register, to post link and to search among posted articles. I hope that in the near future there will be much more features like tags, rating, hierarchical search...

As you find interesting article, just paste link to that article here so the all community of Grails and Groovy practicioners can benefit from it. This way our knowledge will be focused and the Grails and Groovy community will be able to grow even faster.

To understand how I see future development of www.grailstutorials.com just visit vision section.

And don't be lazy and post some interesting links :)

Wednesday, May 21, 2008

What to do when Grails does not generate table for your domain class

Situation I hit was that while running on HSQL file based database everything was running perfectly. Then I have decided to change database to MySQL. Well, in grails this is really easy. Just add MySql jdbc driver, change DataSource.groovy file, manually create empty database and start application. You see no errors in console so you access you application through browser and here comes surprise. You get stacktrace claiming that table is missing. You check the database and table is really missing. How is it possible? There is no error reported?

After some googling and reading this post I found solution or better solution how to find solution :)

Go to Config.groovy and log4j hibernate level change to the debug. Start the application and hibernate will display all actions it performs. In the big stacktrace you should be able to find description what happend. In my case it was:

[1704] hbm2ddl.SchemaUpdate Unsuccessful: create table tutorial_link (id bigint ...
[1704] hbm2ddl.SchemaUpdate BLOB/TEXT column 'link' used in key specification ...

So it turned out its not a problem of Grails, nor problem of Hibernate but different handling of columns by different databases.

However it took me some time to find out and understand what is happening so I decided to post it so maybe I will save somebody's time.

Tuesday, May 13, 2008

Why is it so Easy to Work with Grails

Last few days I am working on grailstutorials and this is the first time that I am doing some bigger project than experimenting with Grails. While so far I had that feeling now it is even more clear to me while development with Grails is so easy and fast. I will try to share my understanding how high efficiency is achieved when working with grails. Let us see what more or less common web application requires to be implemented: (This list is not full list and I can add number of points into this list)
  • front end for end users
  • front end for administrators and other special users
  • Easy creation of templates and tag libraries
  • business logic persisted to database
  • security
  • searching
  • (java back end - this can be discussable)

Let see each of these points.

Front End for End Users

Regarding front end for end users different requirements may be important. Maybe you want fancy front end with lot of Ajax. If this is the case you have different plugins available like (GWT, Flex). This means you can easily integrate advance Ajax concepts into front end. If you need only simple Ajax you can achieve this with the core grails functionality. You may want to have a look on one of my previous posts Grails and Simple Ajax. At the final end, as with all frameworks to have really nice user interface you have to work hardly and cannot achieve this in minutes. With Grails it may be faster but need not. But the real velocity comes with further features.

Front End for Administrators

Creation of front end for administrators is mainly out of the box. Just try to think what are administrators responsibilities. They have to create something (usually business objects), they have to approve something - usually on business objects, they need to have overview of the system status - usually on business objects. Mainly they do not need fancy user interface. No fancy user interface means that you can use one of the grails out of the box features - scaffolding. For each domain object grails can for you generate CRUD screens. For administrators to be able to work with these screens minor if any updates are necessary.

Easy Creation of Templates and Tag Libraries

With Grails creation of templates and tags is so easy that very soon you will realize that all gsp files are created from templates and tags. Code that influence readability of gsp files, at least for me, are conditions, loops and similar programmatic constructs. All these constructs are easily replaceable with template or tags. Actually, when I create template, I immediately create tag for that template and after that work only with tag. To see example of template and tag lib you may want to have a look at my post about templates and tag.

Business Logic Persisted to Database

Here comes full power of the Grails. Just create domain object and database tables are generated for you. Even database is automatically updated when you update domain objects. For each domain object or relationship between domain objects you can create basic CRUD screens. Learning how to work with domain objects does not take a long time and if you have experience with Hibernate it can take you less than a day.

Security

Generated front end for administrators and working with domain object on the low level will be useless if there is no security for application. For sure in the most of the cases we cannot allow end users to see domain objects in CRUD screens. It means we have to secure CRUD screens. This is actually not a problem. There are security plugins for grails. I have used acegi security plugin. It will generate all necessary classes and screens for you. All you need to do is to specify what is visible by which role. And this configuration is really very simple. So in matter of minutes you integrate security into application. I am not sure about advance features as I didn't have used them but probably it should not be complex either. How much time it usually takes you to secure web applications developed with other technologies?

Searching

These days searching is popular. And of course it is useful. All data on you web site will be useless if users cannot find them. Grails has solution for this too. You can use Searchable plugin. You add line of the code to the domain class(es) and out of the box you have searching integrated into your application.

Java

I must agree that this is really discussable if we need Java or no. But in the case you want to have backend implemented with Java, important thing is that Java is integrated with Grails applications seamlessly.

I have just made short overview of features that was most useful for me so far while working on grails application. Features mentioned are not full set as grails frameworks contains lot of other important capabilities. Just to mention few of them: easy unit, functional and integration testing, excellent plugin system, work with XML, web services...

What about your experience with Grails?