Thursday, February 9, 2012

Simple Reporting with Alfresco

Basic static reports can be generated in Alfresco by using XSLT (or FreeMarker) to build the static pages. As an example, I have created a simple list of all items generated by a web form. One approach would be to create a separate web form for this task. In this case a list can be generated an any time by using this form to create content. The approach I used requires slightly less work (no new web form is created), but also slightly less flexibility (an index is created only when a new item is created). For a start, I created a web form that adds simple items, using the following XSL file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
exclude-result-prefixes="xhtml">

<xsl:output method="html" encoding="UTF-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />

<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<div id="main_content">
<h1>Item ID: <xsl:value-of select="/item/id"/></h1>
<p>Item Name: <xsl:value-of select="/item/name"/></p>
<p>Expiry Date:<xsl:value-of select="/item/expiryDate"/></p>
</div>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

Next step is to create an XSL template to redner an HTML list of items. This is the simplest I could come up with:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
exclude-result-prefixes="xhtml">

<xsl:output method="html" encoding="UTF-8" indent="yes" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" />

<xsl:template match="/">

<xsl:variable name="itemList" select="alf:parseXMLDocuments('item', '/items')"/>

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<ul>
<xsl:for-each select="$itemList">
<xsl:variable name="selectedVar">
<xsl:choose>
<xsl:when test="position() = 1">selected</xsl:when>
<xsl:otherwise>leaf</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<li class="{$selectedVar}">
<xsl:variable name="fileNameFixed">
<xsl:call-template name="fixFileName">
<xsl:with-param name="fileName">
<xsl:value-of select="@alf:file_name" />
</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<a href="{$fileNameFixed}"><xsl:value-of select="id" /><span> <xsl:value-of select="name" /></span></a>
</li>
</xsl:for-each>
</ul>
</body>
</html>
</xsl:template>

<xsl:template name="fixFileName">
<xsl:param name="fileName" />
<xsl:value-of select="concat(substring-before($fileName, '.xml'), '.html')" />
</xsl:template>
</xsl:stylesheet>

The template calls parseXMLDocuments function of AVMRemote API to get a list of items. Note the parameters. First parameter is the name of the web form that was used to generate items. If the parameter does not match to the web form name exactly, it is likely that an empty list will be generated. I spent some time struggling with that! The second parameter is just the location where to get items from. As I understand, it is optional and the whole repository may be searched if the parameter is omitted. Next the template goes through each item and generates a link to it. The fixFileName function only replaces the file extension.

When the template is complete, navigate to Data Dictionary > Web Forms > Edit Web Form (for the desired web form). Select "Next" to get to the Configure Templates dialog. Locate the saved template and set the correct output path, i.e. “/${webapp}/items/index.html”. Select "Add to list", then "Next" and "Finish". As the result, the web form will create an "Item" each time it is used. Also, it will recreate the "index.html" page, which will now contain the newly added item.

This is the sample HTML generated by the web form.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xml:lang="en" lang="en">
<head />
<body>
<ul>
<li class="selected"><a href="Item7.html">7<span>amazing achievement</span></a></li>
<li class="leaf"><a href="Item6.html">6<span>a real item</span></a></li>
<li class="leaf"><a href="item8.html">8<span>Improvement</span></a></li>
</ul>
</body>
</html>

This is the simples example I managed to come up with. Another reporting option, which is more advanced, is to writh web scripts to define a REST API for the Alfresco database content. Then the API is used to create, read and delete data in the backend repository and return responses in HTML, XML or JSON.

Reference:

Alfresco Developer Guide

Web Content Management Roles

by . Also posted on my website

No comments: