Tuesday, October 27, 2009

RSS Builder in Grails in a Few minutes

As I planned to include RSS feeds into www.spenddaywithkids.com I needed possibility to create kind of RSS Builder. The good example of what I needed is IceRocket RSS Builder. So you are not generating RSS feeds directly from the data on your site but you are writing them manually. This way RSS feeds become kind of communication channel between the web site and users.

I would stay with IceRocket but I couldn’t find possibility to do even simple formatting for RSS items. So my thought was let see how it is complicated to do it in Grails. And it turned out that it takes only few minutes to get very similar functionality. So here is short example done almost step by step.

Note: As my site is currently running on Grails 1.0.3 maybe not everything will be compatible with 1.1 version out of the box.

So let go with implementation.

First create grails application and named it rssbuilder.

grails create-app rssbuilder
Then create two domain classes:
class RssChannel {
String name
String channelLink
String description

static constraints = {
name(blank:false, nullable:false, unique:true)
channelLink(blank:false, nullable:false, unique:false, url:true)
description(blank:true, nullable:true, maxSize:30000)

class RssItem {
String title
String itemLink
String summary
RssChannel rssChannel
Date dateCreated
Date lastUpdated

static constraints = {
title(blank:false, nullable:false, unique:false)
itemLink(blank:false, nullable:false, unique:false, url:true)
summary(blank:false, nullable:false, unique:false, maxSize:50000)
rssChannel(blank:false, nullable:false, unique:false)
Ah, don’t forget to install two plugins: Feeds Plugin and RichUI plugin.

Now we can generate controllers and views for two domain classes:
grails generate-all RssChannel
grails generate-all RssItem
Well now if you start application you are able to specify RSS channel and items for RSS channel. To provide possibility to specify better formatting for RssItem we can reuse <richui:richTextEditor tag from the RichUI plugin. In the create and edit view of the RssItem change summary and bind it to the richTextEditor. More or less you should input tag with the following:

<richui:richTextEditor name="summary" value="${rssItem?.summary}" width="525" height="525" />
Now to generate RSS feed we can reuse functionality of the Feed plugin. So create FeedController class with command grails create-controller Feed.

The code of the feed controller should be:

    def rss = {
if (!params.id) {
render "missing id in params"

RssChannel rssChannel = RssChannel.get(new Long(params.id))

if(!rssChannel) {
render "rss channel not found"

render(feedType:"rss", feedVersion:"2.0") {
title = rssChannel.name
link = rssChannel.channelLink
description = rssChannel.description
RssItem.findAllByRssChannel(rssChannel, [sort:"dateCreated", order:"desc"]).each() { item ->
entry(item.title) {
So now if you start application and create RSS channel and some items in that channel all you need to know is ID of that channel.

If the Id of the channel is e.g. 1 in the browser write http://localhost:8080/rssbuilder/feed/rss/1 and you will see your feed.

I believe this was really easy :)

As soon as I switch fun places to go with kids to the Grails 1.1 I will create plugin from this.

No comments: