Sunday 31 May 2009

Enter the Glassfish

Okay I didn't want to admit it but the whole Grails/JDO thing turned out to be a pain. The Google hosting plan sounded awesome but what I found was that trying to recode my all ready underdone project was an exercise in futility. Therefore I had to think about other hosting solutions. I thought briefly about Grails Podcast sponsor Morph Labs only to find out that free developer accounts had just been discontinued. Really the only feasible solution was to host it myself.

I had previously set up dynamic dns server as my ISP doesn't have static ip's (unless you pay more of course), so only had to think about an App Server. Based on a friend's experiences and recommendation I decided to give Glassfish a try. Given the early adopter issues I had with Google App Engine I chose V2.1 over the V3 prelude (what's a prelude anyway?).

Set up was relatively easy as there are plenty of guides on the web. After grails war I was away. There were a couple of things that needed addressing though:
  • url contained port number
  • only using hsql file based db
The first issue was addressed with the virtual server in my router by changing all incoming port 80 requests to 8080 for the app server. The second was a little trickier. If I changed the db to mysql or postgresql or whatever, I didn't want to store the db username and password in the datasources config file of my project because being open sources any one could access it. Using advice from the great grails sage I've used a jndi datasource and all is good. Setting this up was also fairly straight forward and all is working well.

Whilst I would like to go back to GAE at some stage, I have definitely learned quite a bit from having my own app server and think that until the project is completely finished I'll keep with Glassfish. After that maybe I look at porting it over again. Next big thing for this app will be security. At this point I'm thinking of using the JSecurity plugin so will let you know how it all goes.

Wednesday 20 May 2009

My first google app-engine grails app

Free hosting appeals and you can't beat Google's hardware firepower so Google's App Engine sounds like a great hosting solution. The thing is, now that app-engine supports Java would it do the job for my grails app?
This question was basically answered with the release of grails 1.1.1 which includes support for an app-engine plugin. I decided to give it a try with the squash ladder app as it currently stands.
The first issue I found was that my current project name had already been taken or was not suitable. I probably could have just changed the config file in my current project but I decided to create a new app and if all turned out to be easy, I'd be able to paste in my current code. However all was not easy. I tried following Graeme Rocher's screencast but got errors when trying to generate controllers and views.

This bug is described in the comments on the screencast page and in a nutshell, you need to reinstall the hibernate plugin, generate-all, then uninstall the hibernate plug in again. Its a only a minor hiccup and once you're done it pretty much works as described.

So now I've got a shell of an app with CRUD, hosted and using JDO instead of hibernate. Life is looking pretty sweet. Graeme's comments indicate that GORM is not supported so far, so it will be interesting to see how the rest of my hibernate-luvin code goes. The app is here and I'll post the progress of translating the current code to Google also. Exciting times ahead!

Tuesday 5 May 2009

Solution for transient problem

After much head scratching and reference searching I've come up with a solution to the previously mentioned transient problem. Basically I've changed the list action in the Player controller to:
- check to see if the user is trying to sort by percentage,
- if so a closure sorts based on the getPercentage() method.
- then if the order was supposed to be descending, we call reverse on the list.

Here's the list action code:

def list = { params.max = Math.min( params.max ? params.max.toInteger() : 10, 100)

if(params.sort=="percentage"){
def players = Player.list().sort {player ->
player.getPercentage()
}
if(params.order=="desc"){
println params.order
players = players.reverse()
}
return [ playerInstanceList: players, playerInstanceTotal: Player.count() ]
}
else{
return [ playerInstanceList: Player.list(params), playerInstanceTotal: Player.count() ]
}
}


Quick and dirty tests look good so far. I'm sure there's probably a more elegant solution out there, if so let me know.

Sunday 3 May 2009

Tricky transient problem

Following on with squash ladder project, I've come across a particularly patience ending problem. I need to display a view that contains a sortable column based on a transient property. My ladder needs to be sorted by winning percentage and this value is transient (calculated and not persisted). A google search shows me a few different posts but nothing that I could get working. Ideally I want the user to be able to customise their view by sorting the columns so in the interests of getting it working I've persisted the winning percentage value.

For this project the additional database isn't going to be particularly taxed but in the interest of learning there must be a grails solution to this. I'll post any solution if I find one, but if you've got a solution let me know. I'm off to cruise Nabble ...