Monday, April 13, 2009

Google App Engine: Guestbook with Groovy Groovlets

google-app-engine-groovy.png The web is soaring, since Google announced Java support on Google App Engine!

People are getting ahead of themselves to try out their favorite piece of the Java ecosystem on the Google platform.

I figured that I also wanted to experience this pre-alpha-geek feeling :-)

I followed the guidelines to run Groovy appliccations on Google App Engine from glaforge and reimplemented the guestbook example from the Google Appengine SDK with Groovy Groovlets.

The result is here. Leave me a message ;-)


The backend classes (Greeting.java, PMF.java) were not changed (however it seems to be possible to datanucleusenhance classes written in Groovy and compiled with groovyc, see here).

I kicked out the two servlets and the jsp and replaced them with the following two Groovlets.

hello.goovy
import com.google.appengine.api.users.User
import com.google.appengine.api.users.UserService
import com.google.appengine.api.users.UserServiceFactory
import javax.jdo.PersistenceManager
import guestbook.PMF
import guestbook.Greeting

UserService userService = UserServiceFactory.getUserService()
User u = userService.getCurrentUser()

PersistenceManager pm = PMF.get().getPersistenceManager()
String query = "select from " + Greeting.class.getName() + " order by date desc range 0,25"
List<Greeting> greetings = (List<Greeting>) pm.newQuery(query).execute()

html.html {
 head {
  title "Hello"
  link(type:"text/css", rel:"stylesheet", href:"/stylesheets/main.css")
 }
 body {
  div(class: "main"){
   div ("Today is: ${new Date()}")

   if (u == null) {
    div(class:"login"){
     a (href: userService.createLoginURL(request.getRequestURI()) , "Log in with your Google Account.")
    }
   }
   else {
    div(class:"login"){
     span("Welcome ${u.nickname}. ")
     a (href: userService.createLogoutURL(request.getRequestURI()) , "Log out")
    }
   }

   p ("Leave me a message:")  

   form(method: "POST", action: "/post.groovy"){
    div(){
     textarea(name: "content", rows: "3", cols: "60", "")
    }
    div(){
     input(type: "submit", value: "Post")
    }
   }

   greetings.each{
    def user = "anonymous"
    if(it.author != null) user = it.author
    div(class:"entry-header", "On  ${it.date} ${user} wrote: ")
    div(class:"entry-body", "${it.content}")
   }
  }
 }
}

post.groovy:
import com.google.appengine.api.users.User
import com.google.appengine.api.users.UserService
import com.google.appengine.api.users.UserServiceFactory
import javax.jdo.PersistenceManager
import guestbook.PMF
import guestbook.Greeting

UserService userService = UserServiceFactory.getUserService()
User user = userService.getCurrentUser()
String content = request.getParameter("content")
Date date = new Date()
Greeting greeting = new Greeting(user, content, date)

PersistenceManager pm = PMF.get().getPersistenceManager()
try {
    pm.makePersistent(greeting)
} finally {
    pm.close()
}

response.sendRedirect("/hello.groovy")

This was my first take at Groovlets, so it might well be that things are not state of the art...

3 comments:

  1. Hi Jonas,

    That's pretty smart. I have spent part of the Easter week-end working my way through the standard Guestbook example and then my first GWT application under the Google App Engine. I'm generally impressed with the high standard of the documentation and implementation, even at this pre-beta stage. So far I have been able to build a dynamic UI with a tabbed dialog and persistence for a self-defined class (postal address, as it happens).

    I found that the Roughian Examples were very helpful in working out what's in the Google Web Toolkit (GWT) widget and panel library.

    Keep up the good work!

    ReplyDelete
  2. I tried to use your groovelets examples to run the guestbook but my pages kept coming up blank. Could you post your project as a zip to see how you configured the java classes and the datanucleus compiling. thanks.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...