JBoss.orgCommunity Documentation

Sample Code

See the following code snippet to know more details about how to publish an activity and add comments to an activity:



package org.exoplatform.social.introduction.activitystreamandexosocialactivity;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.container.PortalContainer;
import org.exoplatform.social.core.activity.model.ActivityStream;
import org.exoplatform.social.core.activity.model.ExoSocialActivity;
import org.exoplatform.social.core.activity.model.ExoSocialActivityImpl;
import org.exoplatform.social.core.manager.ActivityManager;
import org.exoplatform.social.core.manager.IdentityManager;
import org.exoplatform.social.core.identity.model.Identity;
import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
public class IntroduceActivityStreamAndExoSocialActivity {
  // Exo Log.
  private final Log LOG = ExoLogger.getLogger(IntroduceActivityStreamAndExoSocialActivity.class);
  
  // Demo identity.
  private Identity demoIdentity;
  
  // John identity.
  private Identity johnIdentity;
  
  // identityManager manages identities.
  private IdentityManager identityManager;
  
  // activityManager manages activities.
  private ActivityManager activityManager;
  
  // Portal container.
  private PortalContainer container;
  
  private final static String DEMO_NAME = "demo";
  private final static String JOHN_NAME = "john";
  private final static String DEFAULT_ACTIVITY_TITLE = "blabla";
  private final static String DEFAULT_COMMENT_TITLE = "comment blah blah";
  
  /** 
   * Constructor.
   */
  public IntroduceActivityStreamAndExoSocialActivity() {
    // Gets the current container.
    container = PortalContainer.getInstance();
    
    // Gets IdentityManager to handle an identity operation.
    identityManager = (IdentityManager) container.getComponentInstanceOfType(IdentityManager.class);
    
    // Gets ActivityManager to handle activity operation.
    ActivityManager activityManager = (ActivityManager) container.getComponentInstanceOfType(ActivityManager.class);
    
   // Gets or create demo's identity
   demoIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, DEMO_NAME, false);
    
    // Gets or creates the identity "john".
    johnIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, JOHN_NAME, false);
  }
 
   /**
   * Posts activity to activity stream
   */
   public void introduceActivityStreamAndExoSocialActivity {
    
    // Sets a string that specifies the primary text of an activity. This field is REQUIRED by ActivityManager. The title field may only have the following HTML tags: <b> <i>, <a>, <span>.
    activity.setTitle(DEFAULT_ACTIVITY_TITLE);
    
    // Sets this activity for demo.
   activity.setUserId(demoIdentity.getId());
    // Saves the activity.
    activityManager.saveActivity(johnIdentity, activity);
    
    // Gets activity stream.
    ActivityStream activityStream = activity.getActivityStream();
    
    // Type of the activity stream. It can be organization or space.
    LOG.info("activity stream type: " + activityStream.getType());
    
    LOG.info("activity stream id: " + activityStream.getId());
    LOG.info("activity stream pretty id: " + activityStream.getPrettyId());
    LOG.info("activity stream perma link: " + activityStream.getPermaLink());
    
    LOG.info("activity stream id: " + activity.getStreamId());
    LOG.info("activity stream owner: " + activity.getStreamOwner());
    
    // Comment in Social
    ExoSocialActivity demoActivity = new ExoSocialActivityImpl();;
    activity.setTitle(DEFAULT_ACTIVITY_TITLE);
    activityManager.saveActivity(demoIdentity, demoActivity);
    ExoSocialActivity comment = new ExoSocialActivityImpl();;
    comment.setTitle(DEFAULT_COMMENT_TITLE);
    
    //Sets comment of demo
   comment.setUserId(demoIdentity.getId());
  
    //Saves a comment.
    activityManager.saveComment(activity, comment);
  }
}