Liferay Journal Article content through action class
Op 26 juni 2010 door Kristof Verbraeken in categorie Liferay
Getting the dynamic content from a Liferay Journal Article in an action class instead of in a Velocity template
A word about Journal Structures...
A Journal Structure is an XML definition of the dynamic parts of Journal Articles. These parts can be text, a text box, a text area (HTML), an image, an Image Gallery item, a Document Library item, a Boolean flag (true or false), a selection list, multiple selection lists or a link to a page (Layout). Actually, the Journal Structure is a specific XML schema.
Such a Journal Structure could look like this:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<dynamic-element name="Description" type="text_area" repeatable="false" />
<dynamic-element name="Image" type="image_gallery" repeatable="true" />
<dynamic-element name="Document" type="document_library" repeatable="false" />
</root>
If you create a Journal Article based upon this Journal Structure you will be able to:
- fill in a description in a text area (HTML)
- choose multiple images in the Image Gallery
- choose one file in the Document Library
A word about Journal Templates...
Journal Structures are used to define the data structure of a Journal Article.
A Journal Template is actually a Velocity template in which you can use the data defined in the Journal Structure.
You can find more about Journal Structures and Journal Templates on the Liferay Wiki.
Such a Velocity template (for the Journal Structure mentioned above) could look like this:
<div id="wrapper">
<-- some HTML -->
<h1>$htmlUtil.escape($reserved-article-title.data)</h1>
$Description.getData()
<a href="$htmlUtil.escape($Document.getData())">Download</a>
<!-- some more HTML... -->
</div>
The template above will display an article with a heading (using the Liferay Journal Article title), a description (HTML) and a link to download the file you picked in the Document Library.
Liferay provides a very simple mechanism to read the custom fields you defined in the Journal Structure.
You just have to use $fieldName.getData() in the Velocity template and there it is!
But on some occasions it's needed to read the custom fields from the Journal Structure inside the servlet or portlet (action) class.
Liferay does not seem to provide a straightforward method to read those custom fields anywhere else than through the Velocity templates, however...
The portal-kernel package contains a subpackage (com.liferay.portal.kernel.xml) to parse and read XML files.
You can use the SAXReaderUtil in this package to read the custom fields of a Journal Article (which is stored as an XML file in the database).
In the example below I'll show you how to read the custom fields using SAXReaderUtil and XPath:
JournalArticle ja = ...; // Your Journal Article model. You will most likely get this with the JournalArticleLocalService
try {
String name = "Description"; // The name of the custom field
String value = ""; // The value of the custom field: remember that this will always be a java.lang.String
Document document = SAXReaderUtil.read(ja.getDefaultLocale());
Node node = document.selectSingleNode("/root/dynamic-element[@name='" + name + "']/dynamic-content");
value = node.getText();
} catch(NullPointerException nullPointerException) {
// If a NullPointerException is thrown, the element doesn't exist.
} catch(DocumentException documentException) {
// Any other type of XML exception.
}
With this short piece of code you can easily read all custom fields from a Journal Article.
All this functionality is implemented in the Dynamic Content Portlet which you can download below.
In the package be.ekon.portlet.dc.util you'll find the class DynamicContentUtil with methods to get single or multiple nodes from a dynamic Journal Article using a Journal Structure.



Wacht op zijn afspraak... 