Alfresco WCM Forms: Unique Identifier
From time to time I’ve seen the need to provide a unique identifier in content created through the Alfresco WCM Forms Service. The reason why has varied from needing an unique identifier across renditions to needing a way to tie the content to User Generated Content (UGC) or some other database managed application. A simple solution is to leverage Java generated [Universally Unique Identifiers][1] (UUID) imported into the [Schema Definition of your form][2] via a [Java Backed Web Script][3]. **Note: **These are not the UUIDs used by Alfresco DM. UUIDs are not available/part of the AVM/WCM model used by Alfresco.
Java Backed Web Scripts provide a powerful way to include complex business process logic, enabling integrations through the [Alfresco Web Script framework][4] using a simple HTTP based API. In this case, we are using a simple Java class to generate a UUID and then returning that UUID wrapped in a XML Scheme Definition imported through a
First let’s tackle the Java portion. This is a simple Java class that extends the Alfresco [DeclarativeWebScript][5] class. A Declarative Web Script allows you to mix Java classes, Freemarker templates (FTL) and Javascript. In this case we a want to leverage a FTL, which generates the XSD for the return. Here is the meat of our class:
public class UUIDWebScript extends DeclarativeWebScript<br />
{
@Override<br />
protected Map executeImpl(WebScriptRequest req,<br />
Status status, Cache cache)<br />
{
Map model = new HashMap();
`</p>
UUID uuid = UUID.randomUUID();
model.put("uuid", uuid.toString());
return model;
}
`
}
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"<br />
xmlns:alfresco="http://www.alfresco.org/alfresco"<br />
elementFormDefault="qualified">
<xs:complexType name="uuid">
<xs:sequence>
<xs:element name="uuid" fixed="${uuid}" type="xs:normalizedString"/>
</xs:sequence>
</xs:complexType>
</xs:schema><br />
<webscript>
<shortname>Generate UUID</shortname><br />
<description>Returns a UUID</description><br />
<url>/util/uuid</url><br />
<authentication>none</authentication><br />
<format default="xml">argument</format>
</webscript>
<?xml version="1.0" encoding="UTF-8"?><br />
<beans xmlns="http://www.springframework.org/schema/beans"<br />
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"<br />
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="webscript.org.alfresco.extension.uuid.uuid.get"
class="org.alfresco.extension.uuid.UUIDWebScript"
parent="webscript">
</bean>
`
</beans>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"<br />
xmlns:alfresco="http://www.alfresco.org/alfresco"<br />
elementFormDefault="qualified">
</xs:sequence> </xs:complexType> `
The important thing here is how we reference the Web Script, and you can do this with any custom Web Script designed to render XML Schema to be included in a form, by referencing the call with the declaration of webscript:
Add this XSD to your Web Project and give it a run! In the form you will see the UUID, as a read-only field.
Now in your XML output you will have a unique key that you can reference in your templates.
</xs:schema>
<?xml version="1.0" encoding="UTF-8"?><br />
<xs:schema<br />
xmlns:alf="http://www.alfresco.org"<br />
xmlns:xs="http://www.w3.org/2001/XMLSchema"<br />
xmlns:sample="http://www.alfresco.org/alfresco/sample"<br />
elementFormDefault="qualified"<br />
targetNamespace="http://www.alfresco.org/alfresco/sample">
<xs:include schemaLocation="webscript://util/uuid" />
<xs:element name="sample">
</p>
<p style="padding-left: 30px"><xs:complexType></p>
<p style="padding-left: 60px"><xs:sequence></p>
<p style="padding-left: 90px"><xs:element name="sample" type="xs:normalizedString" minOccurs="1" maxOccurs="1"/><br />
<xs:element name="key" type="sample:uuid"/></p>
<p style="padding-left: 60px"></xs:sequence></p>
<p style="padding-left: 30px"></xs:complexType></p>
<p style="padding-left: 30px"></xs:element></p>
<p>
</xs:schema>
<xs:include schemaLocation="webscript://util/uuid" />