Social provides users with a way to share their activity information (also known as Activity Stream) and collaborate in spaces (also known as group work). With the API, you can customize the way to display activities or publish new ones.
To manipulate activities, you need to use the _AtivityManager_ service.

h1. Publish an activity {anchor:id=SOCref.Developer_references.PublishActivity} 

There are two types of activities: activities for a user and activities for a space. The following examples will show you how to publish an activity for each type.

The following examples will show you how to publish an activity for each type.
 
h2. Publish an activity for a user {anchor:id=SOCref.Developer_references.Publish_an_activity_for_a_user}

{code:java}
package org.exoplatform.publish.user;

import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

import org.exoplatform.container.PortalContainer;
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.activity.model.ExoSocialActivity;
import org.exoplatform.social.core.activity.model.ExoSocialActivityImpl;
import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;

public class PublishActivityForUser {
  // Exo Log.
  private final Log LOG = ExoLogger.getLogger(PublishActivityForUser.class);
  
  // Portal container.
  private PortalContainer container;
  
  // identityManager manages identities.
  private IdentityManager identityManager;
  
  // activityManager manages activities.
  private ActivityManager activityManager;
  
  private final static String DEFAULT_USER_NAME = "zun";
  private final static String DEFAULT_ACTIVITY_TITLE = "Hello World!";
  
  /**
   * Constructor.
   */
  public PublishActivityForUser() {
    // Gets the current container.
    container = PortalContainer.getInstance();
    
    // Gets identityManager to handle an identity operation.
    identityManager = (IdentityManager) container.getComponentInstance(IdentityManager.class);

    // Gets activityManager to handle an activity operation.
    activityManager = (ActivityManager) container.getComponentInstanceOfType(ActivityManager.class);
  }
  
  public void createActivityForUser() {
    try {
      // Gets an existing identity or creates a new one.
      Identity userIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, DEFAULT_USER_NAME, false);

      // Creates a new activity for this user.
      ExoSocialActivity activity = new ExoSocialActivityImpl();
      activity.setUserId(userIdentity.getId());
      activity.setTitle(DEFAULT_ACTIVITY_TITLE);
      // Saves an activity into JCR by using ActivityManager.
      activityManager.saveActivity(activity);
    } catch (Exception e) {
      LOG.error("can not save activity.", e);
    }
  }  
}

{code}

h2. Publish an activity for a space {anchor:id=SOCref.Developer_references.Publish_an_activity_for_a_space}

{code:java}
package org.exoplatform.publish.space;

import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;

import org.exoplatform.container.PortalContainer;
import org.exoplatform.social.core.manager.ActivityManager;
import org.exoplatform.social.core.manager.IdentityManager;
import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
import org.exoplatform.social.core.identity.model.Identity;
import org.exoplatform.social.core.activity.model.ExoSocialActivity;
import org.exoplatform.social.core.activity.model.ExoSocialActivityImpl;

import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.SpaceException;
import org.exoplatform.social.core.space.spi.SpaceService;
import org.exoplatform.social.core.identity.provider.SpaceIdentityProvider;

public class PublishActivityForSpace {
  // Exo Log.
  private final Log LOG = ExoLogger.getLogger(PublishActivityForSpace.class);
  
  // Portal container.
  private PortalContainer container;
  
  // identityManager manages identities.
  private IdentityManager identityManager;
  
  // activityManager manages activities.
  private ActivityManager activityManager;
  
  // spaceService manages spaces.
  private SpaceService spaceService;
  
  private final static String DEFAULT_NAME_SPACE = "mySpace";
  private final static String DEFAULT_USER_NAME = "zun";
  private final static String DEFAULT_ACTIVITY_TITLE = "An activity for space";
  
  /**
   * Constructor method.
   */
  public PublishActivityForSpace() {
    // Gets the current container.
    container = PortalContainer.getInstance();

    // Gets identityManager to manage identities.
    identityManager = (IdentityManager) container.getComponentInstance(IdentityManager.class);

    // Gets activityManager to manage activities.
    activityManager = (ActivityManager) container.getComponentInstanceOfType(ActivityManager.class);

    // Gets spaceService to handle the operation of a space.
    spaceService = (SpaceService) container.getComponentInstanceOfType(SpaceService.class);
  }
  
  public void createActivityForSpace() {
    try {
      // make sure that a space with the name "mySpace" is created.
      Space space = spaceService.getSpaceByDisplayName(DEFAULT_NAME_SPACE);
      if (space != null) {
        // Gets spaceIdentity if it already exists. If not, a new one is created.
        Identity spaceIdentity = identityManager.getOrCreateIdentity(SpaceIdentityProvider.NAME, DEFAULT_NAME_SPACE, false);
        // Gets an identity if it already exists. If not, a new one is created.
        Identity userIdentity = identityManager.getOrCreateIdentity(OrganizationIdentityProvider.NAME, DEFAULT_USER_NAME, false);
        // Creates a new activity for this space.
        ExoSocialActivity activity =  new ExoSocialActivityImpl();
        activity.setUserId(userIdentity.getId());
        activity.setTitle(DEFAULT_ACTIVITY_TITLE);
        activityManager.saveActivity(spaceIdentity, activity);
      }
    } catch (SpaceException e) {
      LOG.error("Can not save activity.", e);
    } catch (Exception e) {
      LOG.error("Can not save activity.", e);
    }
  }
}
{code}


h1. Configure an activity processor {anchor:id=SOCref.Developer_references.ConfigureAnActivityProcessor}

An activity processor is used to modify the content of activities before they are returned from an activity manager. For example, to create an activity processor to replace all the texts representing the smile face ":-)" in the activity title by the smiley icons, do as follows:

Firstly, create the _SmileyProcessor_ class by extending the _BaseActivityProcessorPlugin_.

{code:java}
package org.exoplatform.social.core.activitystream;

import org.exoplatform.social.core.BaseActivityProcessorPlugin;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.social.core.activity.model.ExoSocialActivity;

public class SmileyProcessor extends BaseActivityProcessorPlugin {
  private String smiley;
  
  public SmileyProcessor(InitParams params) {
    super(params);
    this.smiley = "<img src=\"http://www.tombraider4u.com/pictures/smiley.gif\"/>";
  }

  @Override
  public void processActivity(ExoSocialActivity activity) {
    String title = activity.getTitle();
    activity.setTitle(title.replaceAll(":-)", this.smiley));
  }
}

{code}

Then, register this processor by editing the _configuration.xml_ file:

{code:xml}
<external-component-plugins>
  <target-component>org.exoplatform.social.core.manager.ActivityManager</target-component>
  <component-plugin>
    <name>SmileyProcessor</name>
    <set-method>addProcessorPlugin</set-method>
    <type>org.exoplatform.social.core.activitystream.SmileyProcessor</type>
    <description></description>
    <init-params>
      <values-param>
        <name>priority</name>
        <value>1</value>
      </values-param>
    </init-params>
  </component-plugin>
</external-component-plugins>
{code}

The "init-params" contains all the key-value data which a processor will use to initialize. In the above configuration, the *priority* value indicates the order in which this processor is executed. If the value is 1, this processor will be used before all the remaining processors with the lower priority.

h1. Publish an RSS feed with feedmash {anchor:id=SOCref.Developer_references.Publish_an_rss_feed_with_feedmash}

It is really easy to publish an RSS feed to a space's activity stream. Social provides _FeedmashJobPlugin_ to publish the RSS feeds. As you can see in the project "_exo.social.extras.feedmash_", there are the _JiraFeedConsumer_ and _HudsonFeedConsumer_ samples to post Social project's feeds (jira and hudson) to a pre-defined space named _exosocial_ in a specific portal container named _socialdemo_ as in the configuration file:

{code:xml}
<external-component-plugins>
  <target-component>org.exoplatform.services.scheduler.JobSchedulerService</target-component>
  <component-plugin>
    <name>RepubSocialJiraActivityJob</name>
    <set-method>addPeriodJob</set-method>
    <type>org.exoplatform.social.extras.feedmash.FeedmashJobPlugin</type>
    <description></description>
    <init-params>
      <properties-param>
        <name>mash.info</name>
        <property name="feedURL" value="http://jira.exoplatform.org/plugins/servlet/streams?key=SOC" />
        <property name="categoryMatch" value="resolved|created" />
        <property name="targetActivityStream" value="space:exosocial" />
        <property name="portalContainer" value="socialdemo" />
      </properties-param>
      <properties-param>
        <name>job.info</name>
        <description>save the monitor data periodically</description>
        <property name="jobName" value="JIRAFeedConsumer" />
        <property name="groupName" value="Feedmash" />
        <property name="job" value="org.exoplatform.social.feedmash.JiraFeedConsumer" />
        <property name="repeatCount" value="0" />
        <property name="period" value="60000" />
        <property name="startTime" value="+45" />
        <property name="endTime" value="" />
      </properties-param>
    </init-params>
  </component-plugin>
  <component-plugin>
    <name>WatchSocialBuildStatus</name>
    <set-method>addPeriodJob</set-method>
    <type>org.exoplatform.social.extras.feedmash.FeedmashJobPlugin</type>
    <description></description>
    <init-params>
      <properties-param>
        <name>mash.info</name>
        <property name="feedURL" value="http://builder.exoplatform.org/hudson/view/social/job/social-trunk-ci/rssAll" />
        <property name="targetActivityStream" value="space:exosocial" />
        <property name="portalContainer" value="socialdemo" />
      </properties-param>
      <properties-param>
        <name>job.info</name>
        <description>save the monitor data periodically</description>
        <property name="jobName" value="HudsonFeedConsumer" />
        <property name="groupName" value="Feedmash" />
        <property name="job" value="org.exoplatform.social.feedmash.HudsonFeedConsumer" />
        <property name="repeatCount" value="0" />
        <property name="period" value="60000" />
        <property name="startTime" value="+100" />
        <property name="endTime" value="" />
      </properties-param>
    </init-params>
  </component-plugin>
</external-component-plugins>
{code}

Run Social with the URL: http://localhost:8080/socialdemo, then log in and create a space named "exosocial". After creating the "exosocial" space, all the feeds of the Social project on Jira and Hudson will be automatically published to the _exosocial_ space.

h1. Sample Code {anchor:id=SOCref.Developer_references.Sample_code}
See the following code snippet to know more details about how to publish an activity and add comments to an activity:

{code:java}
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);
  }
}
{code}