Portlet Development in eXo Platform with JSF and RichFaces (Part 3/3)

eXo Platform Blog

One of the greatest strengths of the eXo Platform is that it integrates a lot of features, such as content management and collaboration or social capabilities.

After the development of a JSF 2 / RichFaces portlet, and the integration of the Content Management capabilities of the eXo Platform, we will learn, in this third tutorial, how to leverage the eXo Platform social features in your own business portlets. To do so, we will start from the portlet created in the previous tutorial and post an activity in the user’s activity stream when he buys a product.

The source code of this tutorial is available here.

Adding eXo dependencies

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


Using the eXo Social API

Now let’s create a service that will use the eXo Social API to post an activity in the user’s activity stream. For this, we will create a new class with the following method:

public void postActivity(String activityText) {

    // Gets the current container.
    PortalContainer container = PortalContainer.getInstance();

    // Gets the current user id
    ConversationState conversationState = ConversationState.getCurrent();
    org.exoplatform.services.security.Identity identity = conversationState.getIdentity();
    String userId = identity.getUserId();

    // Gets identityManager to handle an identity operation.
    IdentityManager identityManager = (IdentityManager) container.getComponentInstanceOfType(IdentityManager.class);

    // Gets an existing social identity or creates a new one.
    Identity userIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, userId, false);

    // Gets activityManager to handle an activity operation.
    ActivityManager activityManager = (ActivityManager) container.getComponentInstanceOfType(ActivityManager.class);

    // Saves an activity by using ActivityManager.
    activityManager.saveActivity(userIdentity, null, activityText);

This service retrieves the current user’s identity and then posts an activity for this identity with the given text.

We now use this service in our JSF view. The ‘Buy’ button of each product calls an action with the product’s data as parameter:

<h:commandButton value="Buy" styleClass="buyButton" action="#{comicsStoreBean.buy}">
  <f:param name="productId" value="#{product.id}" />
  <f:param name="productName" value="#{product.name}" />

And this action, added in the ComicsStoreBean managed bean, uses our previously created service to post the activity:

public void buy() {
    FacesContext context = FacesContext.getCurrentInstance();
    ActionRequest request = (ActionRequest) context.getExternalContext().getRequest();
    String productName = request.getParameter("productName");
    socialService.postActivity("I have just bought <b>" + productName + "</b> !");

You can now see the result in your activity stream after clicking on the Buy button of one of the products by going to the ‘intranet’ demo site (My Sites > intranet):


eXo Social offers you a lot of others possibilities, such as posting an activity in a space, posting different kinds of activity (for instance content activity, such as the one in the screenshot for the “iron-man-2.jpg” file), adding a comment to an activity, getting the relations of an user, getting the users of a space,…

Besides this Java API, the eXo social feature offers a REST API and the OpenSocial API.

In this tutorial, we learnt how to use the eXo Social API to post an activity from your own business portlet.

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

(original article from Thomas blog)

Related Posts

I am a software architect at eXo. With the developers, I (try to) convert technically what the product management people have in their minds. I also assist the support team in answering customers’ questions. In this blog, I share what we develop in the platform and provide tips to help customers to use or customise it.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>