Posted 10/17/2010 - 03:19 by nzblue_fish
Hi Mashers,
I've been meaning to blog on this topic for a long time, but being pulled off on others projects meant a bit of time away from doing more on my favourite technology, mashups and apps.
With the release of Presto 3.0 I knew I had to make a start on some new mashups. We've recently started using Yammer internally, and I thought Presto would provide a great way to tap in to the Yammer API so that I could create an App to show a live running feed on the homepage of our intranet.
In doing so, I was reminded of a hidden gem in Presto that I wanted to make sure everyone knew about, and that is the ability to use Groovy when you are coding EMML. Any of my fellow mashers who have done any work with Java will probably already know about Groovy. I came upon it two years ago, even before I started creating mashups. I've blogged on this and the reason why I fell in love with this scripting language that is based on Java but without all the attendent complexity.
EMML includes a <script> tag that allows you to embed and run scripts within your mashups as they execute. Officially, Presto only supports Javascript and JRuby according to the documentation (correct me if I'm wrong - Raj has let me know it'supported in 3.0 ), but I've been using Groovy scripts within both Presto 2.7 and 3.0 and haven't had any compatability issues. Since Presto/EMML is built on a Java foundation and Groovy is fully compatible with Java, it's unlikely you'll run into many issues.
So why would you use Groovy any way? Well, EMML provides some very powerful functions especially when you combine these with the XPath functions, but there are times when it's just not enough or it just gets too hard.
While writing the Yammer API mashups, I needed to generate a timestamp as part of the authorisation sequence. I looked at the XPath functions for DateTime conversions, but in the end it was just easier to create a macro that executed a Groovy script that generated the timestamp in the required format.
<macro name="genTimestamp" inputvariable="timestamp" outputvariable="timestamp"> <output name="timestamp" type="string" /> <script type="groovy" outputvariable="timestamp"> <![CDATA[ import java.util.Date def date = new Date() timestamp = date.time.toString() ]]> </script> </macro>
One of the best solutions that Groovy gave me was the ability to dynamically create and populate a variable within a mashup. While writing mashups to execute SQL code from a set of supplied parameters, I was unable to find a way to create a named variable from a supplied parameter and then populate that variable with a supplied value. In Presto 2.7 this simply wasn't possible. Groovy came to the rescue.
<!--
Using a bit of Groovy trickery we now instantiate a
mashup variable containing the supplied value. It's scope
is only within the macro though.
-->
<script inputvariables="varName, varContent" type="groovy">
<![CDATA[evaluate("${varName} = varContent")]]>
</script>
Groovy is an incredibly powerful scripting language that provides full access to the underlying Java language and classes and is a handy addition to your advanced mashup tool kit. It is well worth taking a look at if you have time. The power and accessibility of the Java environment and classes has come in handy on more than one occassion when I have hit a major coding roadblock.
I've spent a lot of time learning Groovy and as mentioned in my blog on the subject can thoroughly recommend it. There are some great texts around on the subject, and my favourite three are: Groovy In Action, Programming Groovy, and Groovy Recipes.
Happy mashing everyone,
Innes
- nzblue_fish's blog
- Login or register to post comments
- Email this page

Thanks! It's a pity that you couldn't post it earlier. I was busy working on my project dedicated to sudoku game as well so I didn't browse this blog for qute a while as well. There is so much nice stuff for me to read now!
Hello! I want to launch business of mu owm and search for the ideas which have prospect. Developing apps seems to be a profitable activity. I am thinking of creating some application to help to convert mov to avi. The idea is not original but I willk add some new features to this project.
Thanks Innes for a great writeup. Just a clarification - Groovy is officially supported in presto 3.0 - we will update docs to reflect that. Thanks again.
raj.
Thanks Raj, that's great news and I've updated my blog.
The project that took me away for so long involved using Grails & Groovy. All the way through I kept wondering how I could add some Presto magic to a Grails project ... and I'm sure there is something very cool possible by combining all 3 blinds together, especially using Presto as a datasource within Grails. Might make a cool Grails plugin!!!! Maybe even a Grails taglib to add Presto Apps!!!
Cheers, Innes