{"id":4487,"date":"2013-02-14T08:55:06","date_gmt":"2013-02-14T16:55:06","guid":{"rendered":"http:\/\/localhost\/exoblog\/?p=4487"},"modified":"2013-02-14T08:55:06","modified_gmt":"2013-02-14T16:55:06","slug":"portlet-development-in-exo-platform-with-jsf-and-richfaces-part-33","status":"publish","type":"post","link":"https:\/\/www.exoplatform.com\/blog\/portlet-development-in-exo-platform-with-jsf-and-richfaces-part-33\/","title":{"rendered":"Portlet Development in eXo Platform with JSF and RichFaces (Part 3\/3)"},"content":{"rendered":"<p>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.<\/p>\n<p>After <a href=\"https:\/\/www.exoplatform.com\/blog\/2013\/02\/12\/portlet-development-in-exo-platform-with-jsf-and-richfaces-part-13\/\">the development of a JSF 2 \/ RichFaces portlet<\/a>, and <a href=\"https:\/\/www.exoplatform.com\/blog\/2013\/02\/13\/portlet-development-in-exo-platform-with-jsf-and-richfaces-part-23\/\">the integration of the Content Management capabilities of the eXo Platform<\/a>, 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 <a href=\"https:\/\/www.exoplatform.com\/blog\/2013\/02\/13\/portlet-development-in-exo-platform-with-jsf-and-richfaces-part-23\/\">the previous tutorial<\/a> and post an activity in the user\u2019s activity stream when he buys a product.<\/p>\n<p><!--more--><\/p>\n<p>The source code of this tutorial is available <a href=\"https:\/\/github.com\/thomasdelhomenie\/exo-jsfportletbridge-ecms-social-portlet\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p>\n<h2>Adding eXo dependencies<\/h2>\n<p>The first step is to add the eXo dependencies that will be used in our portlet. Edit your pom.xml and add these lines:<\/p>\n<pre class=\"wrap:true lang:default decode:true \">&lt;dependency&gt;\n  &lt;groupId&gt;org.exoplatform.social&lt;\/groupId&gt;\n  &lt;artifactId&gt;exo.social.component.core&lt;\/artifactId&gt;\n  &lt;version&gt;1.2.6&lt;\/version&gt;\n  &lt;scope&gt;provided&lt;\/scope&gt;\n&lt;\/dependency&gt;<\/pre>\n<h2>Using the eXo Social API<\/h2>\n<p>Now let\u2019s create a service that will use the eXo Social API to post an activity in the user\u2019s activity stream. For this, we will create a new class with the following method:<\/p>\n<pre class=\"wrap:true lang:default decode:true  \">@Override\npublic void postActivity(String activityText) {\n\n    \/\/ Gets the current container.\n    PortalContainer container = PortalContainer.getInstance();\n\n    \/\/ Gets the current user id\n    ConversationState conversationState = ConversationState.getCurrent();\n    org.exoplatform.services.security.Identity identity = conversationState.getIdentity();\n    String userId = identity.getUserId();\n\n    \/\/ Gets identityManager to handle an identity operation.\n    IdentityManager identityManager = (IdentityManager) container.getComponentInstanceOfType(IdentityManager.class);\n\n    \/\/ Gets an existing social identity or creates a new one.\n    Identity userIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, userId, false);\n\n    \/\/ Gets activityManager to handle an activity operation.\n    ActivityManager activityManager = (ActivityManager) container.getComponentInstanceOfType(ActivityManager.class);\n\n    \/\/ Saves an activity by using ActivityManager.\n    activityManager.saveActivity(userIdentity, null, activityText);\n}<\/pre>\n<p>This service retrieves the current user\u2019s identity and then posts an activity for this identity with the given text.<\/p>\n<p>We now use this service in our JSF view. The \u2018Buy\u2019 button of each product calls an action with the product\u2019s data as parameter:<\/p>\n<pre class=\"wrap:true lang:default decode:true \">&lt;h:commandButton value=\"Buy\" styleClass=\"buyButton\" action=\"#{comicsStoreBean.buy}\"&gt;\n  &lt;f:param name=\"productId\" value=\"#{product.id}\" \/&gt;\n  &lt;f:param name=\"productName\" value=\"#{product.name}\" \/&gt;\n&lt;\/h:commandButton&gt;<\/pre>\n<p>And this action, added in the ComicsStoreBean managed bean, uses our previously created service to post the activity:<\/p>\n<pre class=\"wrap:true lang:default decode:true \">public void buy() {\n    FacesContext context = FacesContext.getCurrentInstance();\n    ActionRequest request = (ActionRequest) context.getExternalContext().getRequest();\n    String productName = request.getParameter(\"productName\");\n    socialService.postActivity(\"I have just bought &lt;b&gt;\" + productName + \"&lt;\/b&gt; !\");\n}<\/pre>\n<p>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 \u2018intranet\u2019 demo site (<span class=\"navCode\">My Sites &gt; intranet<\/span>):<\/p>\n<p><a href=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2013\/02\/social-activity.png\"><img decoding=\"async\" class=\"aligncenter size-full\" title=\"social-activity\" src=\"https:\/\/www.exoplatform.com\/blog\/wp-content\/uploads\/2013\/02\/social-activity.png\" alt=\"social-activity\" width=\"600\" \/><\/a><\/p>\n<p>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 &#8220;iron-man-2.jpg&#8221; file), adding a comment to an activity, getting the relations of an user, getting the users of a space,&#8230;<\/p>\n<p>Besides this Java API, the eXo social feature offers a REST API and the OpenSocial API.<\/p>\n<p>In this tutorial, we learnt how to use the eXo Social API to post an activity from your own business portlet.<\/p>\n<p>Don\u2019t hesitate to take a look at the documentation to discover all the available APIs.<\/p>\n<p><span style=\"font-size: 10px;\">(<a href=\"http:\/\/thomasdelhomenie.wordpress.com\/2012\/05\/20\/jsf-2-richfaces-portlet-in-exo-part-33-social-integration\/\" target=\"_blank\" rel=\"noopener\">original article from Thomas blog<\/a>)<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"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, [&hellip;]","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[699],"tags":[],"lang":"en","translations":{"en":4487},"pll_sync_post":[],"_links":{"self":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts\/4487"}],"collection":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/comments?post=4487"}],"version-history":[{"count":0,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/posts\/4487\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/media?parent=4487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/categories?post=4487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.exoplatform.com\/blog\/wp-json\/wp\/v2\/tags?post=4487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}