eXo Platform comes with powerful content management features and a large set of portlets to use these features. However, you may want to use these content management capabilities in your own portlets.

This second tutorial will teach you how to use the eXo Content Management API and portlets to create a sample store application. The first part, about JSF/RichFaces integration, is available here.

Our sample application will display a list of products with related pictures. Each product will be composed of a name, a description, a price, a thumbnail, and some pictures. The thumbnail and the related pictures will be retrieved from the eXo Content Management system.

The page will display the product’s thumbnail and the product’s characteristics. The related pictures will be available as links. A Content Detail portlet will be used to display each related picture.

The source code for this tutorial is available here.

Adding contents

In order for us to retrieve the contents easily, they should be organized correctly. Within the root folder (let’s say repository:collaboration:/sites content/live/acme/documents/products), a folder will be created for each product, named with the product’s ID. All product-related contents will be stored in the corresponding folder. The thumbnail will also be stored in the product’s folder, but with the special name ‘thumbnail’ in order to distinguish it from the other pictures.

So, the content tree will look like this:

Instead of creating all the contents by hand, you can download this JCR export. To use it:

  • Go to the Content Explorer;
  • Select the Sites Management drive;
  • Go to acme > documents;
  • Click on the System tab;
  • Click on the Import Node button;
  • Select the export file for the Upload File field;
  • Click on Import.

Important: To ensure that all contents are in published state, you may need to republish them.

Adding eXo dependencies

The first step is to add the eXo dependencies that will be used in our portlet. Edit your pom.xml file and add these lines:

Using the eXo Content Management API

In order to manage data displayed on our page, we need a managed bean:

(See the code here)

This bean instantiates two services: one for the products (ProductService) and one for the contents (ContentService), which is initialized with the root folder in the content management system that houses the contents used in the store (retrieved from a portlet preference).

The bean exposes only one kind of data: the products, via the getProducts method. This method calls the ProductService object to retrieve all the products. The ProductService class simply returns sample products:

Then the managed bean calls the ContentService object to retrieve the contents relating to the products. This is the most interesting part as it deals with eXo Platform’s content management features.

eXo Platform provides an API to interact with all content management capabilities (contents, taxonomy, links, publication, SEO, …). In our sample application we will use the WCMComposer API, which allows us to work with contents.

eXo Platform’s Content Management feature provides a utility method to easily instantiate a service:

This service is used in the two methods of ContentService: getProductThumbnailPath and getProductPictures.

The getProductThumbnailPath method returns the path of the thumbnail of the product if it exists:

The WCMComposer API’s getContent method is used here. It allows us to retrieve content based on its path. Some filters can be added to select the right content base (for instance, based on its language, its version or its publication state).

The getProductPictures method returns all the contents related to a product:

We now use the getContents method, which is equivalent to getContent but for multiple contents.

One really interesting point is that, by using this API, you can benefit from all the eXo Platform content management features. For example, by calling the getContents method with the filter WCMComposer.FILTER_VERSION set to WCMComposer.BASE_VERSION and the filter WCMComposer.FILTER_MODE set to the current mode (live or edit), only the link of the published pictures will be visible in live mode, whereas the link of the pictures in draft state will be visible in edit mode.

As you can see, the node’s type is checked to distinguish the frozenNode case:

In fact, a frozenNode is a published version of an original node. A frozenNode references the original node (in our case, the picture node) through its jcr:frozenUuid property. So we need to get this property to retrieve the original node and get its path.

The last part is to edit our JSF pages to display all the products. For this, a RichFaces data grid (rich:dataGrid) is used:

The ui:repeat property lists all links to the product’s pictures. The link simply goes to the same page (because a Content Detail portlet will be added to this page) and passes the content-id parameter with the path of the content to display. The Content Detail will just look at this parameter and display the targeted content.

Adding Content Detail in the page

In order to display the products’ pictures we need to add a Content Detail portlet to the page:

  • Edit the page (Edit > Page > Layout in the top bar);
  • In the portlet catalog, open the Contents category;
  • Drag and drop a Content Detail portlet onto the page, under the first portlet;
  • Edit the portlet;
  • Select the default content for the Content Path field (products/Comics);
  • Scroll down the preferences screen and click on the Advanced link;
  • Choose Enabled for the Contextual Content field;
  • Leave the other field as default (by content-id);
  • Click on Save;
  • Click on Close;
  • Click on the Save icon (upper right) to save the modifications made to the page.

Our store is ready!

Let’s play with it!

Click on a picture link (for instance, the Photo 1 of Wolverine):

The picture is displayed below.

Let’s now add a new picture. To do so:

  • Go to the Content Explorer;
  • Go to the Iron Man folder (acme > documents > products > 1);
  • Click on the Upload Files button;
  • Select your file;
  • Click on Save.

The picture is now uploaded. Leave it in draft state and go back to the Comics store page. As you can see, your new picture does not appear in the Iron Man’s pictures yet.

Now switch to edit mode by clicking on Edit > Content.

The picture link now appears and you can click it to display the new picture. It’s up to you now to publish it to make it available in live mode.

This tutorial has shown you how to use the eXo Content Management API to benefit from all the eXo Content Management features inside your own portlets.

Don’t hesitate to take a look at the documentation to discover all the available APIs. Enjoy!

(original article from Thomas blog)

Keep in touch with the author


Be part of the discussion, share your comments

comments