Pagine

Wednesday, September 21, 2011

DHTMLX Tags on Gaelyk

About a month ago the final release of Gaelyk 1.0, the lightweight Groovy toolkit for Google App Engine, has been released. I'm a fan of the Groovy programming language, even if it is challenging to involve customers inthis paradigm shift. This post illustrates a simple CRUD application, (available here), written with Gaelyk and theDHTMLX Tag library. There is a school of thought that considers tag libraries to be evil and another one which does not. Between the two schools there are those who believe that tag libraries can be useful, if you don't turn them into a monster. In a discussion about Gaelyk and tag libraries support,Guillaume Laforge, made a very smart point about this subject (see here):

Gaelyk templates are really just one view option. You can still continue using Groovlets, but delegate to JSP views, using JSP taglibs. Gaelyk doesn't try to reinvent the wheel here.

Gaelyk provides templates (GTPL) similar to JSPs, which are pages containing scriptlets of code. In the same way in which you can mix Java and Groovy code for classes, Gaelyk let you use a combination of GTPL and JSP pages. In this sample application there are two pages, the first one based on a dhtmlxGrid, which presents the list of records, and second uses a dhtmlxForm to display and edit the data.See screenshot hereBoth pages have been created using the DHTMLX Tag Designer, which makes it pretty simple to prototype the layout.

To take the advantage of Gaelyk templates the JSPs are included within the GTPL:

war/WEB-INF/pages/index.gtpl

<% include '/WEB-INF/includes/header.gtpl' %>

<% include '/WEB-INF/jsp/list.jsp' %>

<% include '/WEB-INF/includes/footer.gtpl' %>

war/WEB-INF/pages/edit.gtpl

<% include '/WEB-INF/includes/header.gtpl' %>

<% include '/WEB-INF/jsp/form.jsp' %>

<% include '/WEB-INF/includes/footer.gtpl' %>

The section about the grid page is missing, because it is very similar to a previous post, and the focus here is on the form based edit page. The source code is below:

The initialization attaches a handler to the onButtonClick form button event, which updates the message on top of the form, and posts the save operation to the application. The second step of the code sets up the validation, binding validators to the object form fields, and provides the onBeforeValidate and onValidateError event handlers to update the text message box.

The final step invokes the load method on the form, it triggers an Ajax call to the book Groovlet to retrieve the data and fill the form.

The routes.groovy defines the URL mapping :

war/WEB-INF/routes.groovy

get"/", forward: "/WEB-INF/pages/index.gtpl"

get"/book/@task/@id", forward: "/book.groovy?id=@id&task=@task"

post "/book/processor", forward: "/processor.groovy"

In the case above, path variables are translated in order to route different task to a single Groovlet. The book.groovyGroovlet performs the list, show and edit operations.

war/WEB-INF/groovy/book.groovy

importcom.google.appengine.api.datastore.Entity

importcom.google.appengine.api.datastore.Key

importcom.google.appengine.api.datastore.KeyFactory

switch(params."task"){

case"list":

log.info"list : getting book list "

int entityCount = datastore.execute{ select count from books }

params.offset= params.posStart?Integer.parseInt( params.posStart) : 0

params.max= params.count?Integer.parseInt( params.count) : 20

params.sort

= params.orderby? params.orderby : "sales"

params.order= params.dir?(params.dir=="des"?"desc" : "asc") : "asc"

def books = datastore.execute{

from books

limit params.max offset params.offset

sort params.order by params.sort

}

response.setContentType("text/xml")

html.rows(total_count: entityCount , pos: params.offset){

books.each{ book ->

html.row(id: book.key.id){

html.cell( book.sales)

html.cell( book.title)

html.cell( book.author)

html.cell( book.price)

}

}

}

break

case"edit":

log.info"edit: editing a book"

request.setAttribute'id', params.id

forward '/WEB-INF/pages/edit.gtpl'

break

case"show":

log.info"show: getting book data"

def id =Long.parseLong(params.id)

Key key =KeyFactory.createKey("books", id)

def book = datastore.get(key)

response.setContentType("text/xml")

html.data{

author {

mkp.yieldUnescaped("<![CDATA["+ book.author+"]]>")

}

price {

mkp.yieldUnescaped("<![CDATA["+ book.price+"]]>")

}

sales {

mkp.yieldUnescaped("<![CDATA["+ book.sales+"]]>")

}

title {

mkp.yieldUnescaped("<![CDATA["+ book.title+"]]>")

}

}

break

default:

break

}

The data processor POST requests are routed to the Groovlet named processor.groovy, which handles the insert, update, delete operations, and persist changes to the entity. Gaelyk’s abstractions for the datastore make them quite straight forward to implement.

1 comment:

to me all what i read was gibberish as it all went above my zooming in each and every direction possible but i guess thats just me and my opinions. To that tech geek who is always up his game and always up to date with wach and new software is in his heaven.