JSF 2 / RichFaces portlet in eXo (Part 3/3) : Social integration

One of the greatest strenghs of eXoPlatform is to integrate a lot of features together, 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 eXoPlatform, we will learn, in this third tutorial, how to leverage eXo Social features in your own business portlets. For 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 :

<dependency>
  <groupId>org.exoplatform.social</groupId>
  <artifactId>exo.social.component.core</artifactId>
  <version>1.2.6</version>
  <scope>provided</scope>
</dependency>

Using eXo Social API

Now let’s create a service which 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 :

@Override
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 post 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}" />
</h:commandButton>

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, eXo Social 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.

  1. Portlet Development in eXo Platform with JSF and RichFaces (Part 3/3) – Open Source Enterprise Social Platform | eXo Blog and News

Leave a comment