iCal export implementation for Alfresco


For my vcf planning calendar, which is partially described in my Bachelor's thesis, I made function, which does export to iCal calendar format. I used Jav-backed WebScript at Alfresco side and iCal4j library for implementation of RFC 2445 standard – iCalendar.

The aim was to create service with parameter of node with planned sessions (ArchivaceSummary aspect) which exports iCal file with all sessions (ArchivaceSession aspect) in it. Simple data model scheme of aspects is at following picture. Those aspects are just flags of type of nodes, only ArchivaceSession aspect associates some properties – a date of begin and end of action. More informations are in my Bachelor's thesis.

Image Hosted by ImageShack.us

Due to use iCal4j library I needed to use Java-backed web script, because i can't see a simple way to create iCal file just with Freemarker, which is used as a rendering machine in "normal" web scripts. Java-backed web scripts are clearly described at Alfresco wiki pages, but when you try to write your own you discover that there is no library in SDK. This problem is reported as a bug, but there is still no solution. It's needed to download library (and source) alfresco-web-framework and include it to project, then it's all ok and it's possible to build Java-backed web scripts.

So, every webscript needs a description file, i put this one into WEB-INF/classes/alfresco/templates/webscripts/org/alfresco/archivace/ical.get.desc.xml

<webscript>
    <shortname>iCal Export</shortname>
    <description>Export kalendare z Archivacniho prostoru</description>
    <url>/archivace/ical?nodeRef={noderef}</url>
    <authentication>user</authentication>
    <transaction>required</transaction>
</webscript>

You can see from its name (ical.get.desc.xml) that this webscript implements get http method. You also need to specify that fact in bean identificator – as a postix, as you can see on next listing of part of web-scripts-application-context.xml.

 <bean id="webscript.org.alfresco.archivace.ical.get"
    class="cz.shmoula.webscripts.ICal"
    parent="webscript">
  </bean>

This postfix is picked up by the Web Script engine, also 'webscript.' prefix is picked up. Webscript prefix identifies that bean you are specifying is a webscript implementation.  Notice that url parameter in desc.xml and suffix of bean identificator  are corresponding: alfresco.archivace.ical.get   —  /archivace/ical.Second parameter, class, specifies class with implementation. My class implementation is based on AbstractWebScript class, which provides simplier development of WebScripts. Main method of this class is method public void execute(WebScriptRequest req, WebScriptResponse res), which is described on next listing.

public void execute(WebScriptRequest req, WebScriptResponse res)
            throws IOException {
        // getting request data
        HttpServletRequest httpReq = req.getHttpServletRequest();
        // opening specified node
        Node noda = new Node(NodeRef(httpReq.getParameter("noderef")));
        // creating new calendar object
        net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();

        // cycling through node and reading data
        for (Iterator i = noda.getChildAssociations().values().iterator(); i
                .hasNext();) {
            . . .
            if (n.hasAspect(ArchivaceModel.archSession)) {
                // if type of child is what I want, just create data from provided info and add to calendar
                this.addEvent(n.getProperties());
            }
            . . .
        }
        // setting response and printing out a calendar
        res.setContentType("text/calendar");
        res.getWriter().write(this.calendar.toString());
    }

Pressty simple, huh? And the last task – calling this webscript. Either by specifying an url with required parameter (node reference), or in code. I'm using an ActionLink from Alfresco Tag Library called this way:

 <a:actionLink value="iCal export"
    image="/images/icons/calendar-16.gif"
    href="/service/archivace/ical?noderef=#{NavigationBean.currentNode.nodeRefAsString}"
/>

And that is it. I have a calendar export button on my page as you can see on following image.

Image and video hosting by TinyPic

 


Napsat komentář

Vaše e-mailová adresa nebude zveřejněna. Vyžadované informace jsou označeny *