Archive for the ‘WCM’ tag
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 (UUID) imported into the Schema Definition of your form via a Java Backed Web Script. 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 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 <xs:include> into a Schema Definition used to generate a form used to capture content in Alfresco’s WCM service.
First let’s tackle the Java portion. This is a simple Java class that extends the Alfresco DeclarativeWebScript 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
{
@Override
protected Map executeImpl(WebScriptRequest req,
Status status, Cache cache)
{
Map model = new HashMap();
UUID uuid = UUID.randomUUID();
model.put("uuid", uuid.toString());
return model;
}
}
Basically,
- Create a
mapof properties you want to return (by coding conventions this is called a model). - Perform your business logic, in our case generate a UUID.
- Add the values you want to return to the template with a unique name to identify it in the template
- Return the map of properties
Next we want to format our return. This is done using a Freemarker Template . (Freemarker is a Java Based markup language.) Here is ours:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:alfresco="http://www.alfresco.org/alfresco"
elementFormDefault="qualified">
<xs:complexType name="uuid">
<xs:sequence>
<xs:element name="uuid" fixed="${uuid}" type="xs:normalizedString"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
The FTL, as you can see , is a simple XSD. The value returned from our Java class, in the model, named uuid, is declared as ${uuid}.
Now let’s declare the web script so that it can be registered in Alfresco:
<webscript>
<shortname>Generate UUID</shortname>
<description>Returns a UUID</description>
<url>/util/uuid</url>
<authentication>none</authentication>
<format default="xml">argument</format>
</webscript>
And since we are using a Java class in our Web Script, we also need to declare it so that the Web Script framework can use it:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
It is important to note here that the bean id for a Java backed Web Script requires a very specific format:
- Start by declaring this bean a webscript with: webscript.
- Next add the full Java package declaration: org.alfresco.extension.uuid
- Followed by the name used in your FTL and XML declaration with the method used to access the web script: uuid.get
- When packaged, the FTL and XML declaration must be in a folder structure that matches the Java package declaration.
This puts together all of the pieces on the Web Script side. It is easiest to manage this through an AMP file, which we won’t discuss here how to create or install, but code and amp file are available at the project page: http://code.google.com/p/alfresco-uuid-webscript/
Once installed we can verify that it works by going to http://localhost:8080/alfresco/service/util/uuid in your browser (replace localhost and port as needed for your install). What you should see returned is an XSD that looks something like this:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:alfresco="http://www.alfresco.org/alfresco"
elementFormDefault="qualified">
<xs:complexType name="uuid">
<xs:sequence>
<xs:element name="uuid" fixed="9c2fce39-3351-47c9-bb05-4b002967a00d" type="xs:normalizedString"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
It is our template but with are declaration of ${uuid} replaced with aUUID: 9c2fce39-3351-47c9-bb05-4b002967a00d. With each call to this Web Script you will see a uniquely generated ID.
Now let’s integrate this with a simple XSD:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:alf="http://www.alfresco.org"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sample="http://www.alfresco.org/alfresco/sample"
elementFormDefault="qualified"
targetNamespace="http://www.alfresco.org/alfresco/sample">
<xs:include schemaLocation="webscript://util/uuid" />
<xs:element name="sample">
<xs:complexType>
<xs:sequence>
<xs:element name="sample" type="xs:normalizedString" minOccurs="1" maxOccurs="1"/>
<xs:element name="key" type="sample:uuid"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
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:
<xs:include schemaLocation="webscript://util/uuid" />
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.
Alfresco Sysadmin and WCM Training
We are having Sysadmin and WCM training for Alfresco in Santa Clara, CA, at the end of the month. If you are interested, register soon. Seats will go quickly. (This is probably the last chance for training on the west coast for the year.)
Sysadmin training is being taught by Luis Sala, Senior Director of Solution Engineering. WCM training will be taught the Peter Monks, Director of Services.
System Administration
This is an introductory course for customers and partners who need to deploy a production-ready system. It is a pre-requisite to most other Alfresco Training courses.
Full course information »
| Date | Location | |
|---|---|---|
| Nov 26-28, 2007 | Santa Clara | Details & Register |
Web Content Management for Developers
This developer-oriented course focuses around an installation of Alfresco 2.1E with lab exercises for form development, template development, and deployment.
Full course information »
| Date | Location | |
|---|---|---|
| Nov 29-30, 2007 | Santa Clara | Details & Register |