fredag 18. februar 2011

play framework, adding ical

So here's my really quick guide to making ical available in your playapp. This is what you need;

Add ical4j's jars. Or just add the dependency to your pom.xml if you use that
The repo is;

      <name>Modularity Releases Repository</name>

the dependency:


Then install it with play mvn:up if you use the maven plugin. if not, then just download the jar and put it in the lib-directory.
Then you need to write some code to create a calendar instance. here's what I wrote for now;

public static Calendar createCalendar(Event event) throws SocketException {
        TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
        TimeZone timezone = registry.getTimeZone("Europe/Oslo");
        VTimeZone tz = timezone.getVTimeZone();
        // Create the event
        VEvent meeting = new VEvent(new DateTime(, new DateTime(, "javaBin: " + event.title);
        // Add timezone info..
        UidGenerator ug = new UidGenerator(event.title);
        Uid uid = ug.generateUid();

        // Create a calendar
        net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
        icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
        return icsCalendar;

Where as you can see the calendar is based on an event, and it's title.
Don't worry about any of the different types as "VEvent", as they are part of ical4j.

Then you need to render the file to the user, right?

Well, first go to and find a nice icon for your ical-link.
In your html, write a link like this;

<a href="@{Application.ical(event?.id)}"><img src="/public/images/ical.png" alt="calendar"/></a>

then you write the rendering code in your controller;

public static void ical(Long id){
        try {
            Event event = Event.findById(id);
            Calendar calendar = ICalUtil.createCalendar(event);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            CalendarOutputter outputter = new CalendarOutputter();
            outputter.output(calendar, bos);
            response.setHeader("Content-Type", "application/ics");
            InputStream is = new ByteArrayInputStream(bos.toByteArray());
        } catch (IOException e) {
            Logger.error("Io feil ved ical", e);
        } catch (ValidationException e) {
            Logger.error("Feil ved generering av ical", e);

Out of it you get an ical-calendar-item that people can use and enjoy ;) That's it.

3 kommentarer:

Anonym sa...

Have you added any persistence mappings to store events with jpa?

Anonym sa...

I guess the answer is no.

Anonym sa...

witzig bin auch heute hier :D

Copyright © >> /dev/null
Blogger Theme by BloggerThemes Design by