6.3.2.1. Creating a new activity type

Warning

You are looking at documentation for an older release. Not what you want? See the current release documentation.

The creation of an activity type involves a UI Component which is required for the activity display. In this tutorial, you define an activity type and your own UI Component to display it. You can download all source code of this tutorial here.

  1. Create a Maven project as follows:

  2. Edit the pom.xml file:

    
    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <artifactId>integ-wiki</artifactId>
            <groupId>org.exoplatform.integration</groupId>
            <version>4.0.4</version>
        </parent>
        <artifactId>wiki-activity-type</artifactId>
        <packaging>jar</packaging>
        <version>1.0</version>
        <name>Activity type</name>
        <description>UI Extension - Activity type</description>
        <dependencies>
            <dependency>
                <groupId>org.exoplatform.platform-ui</groupId>
                <artifactId>platform-ui-webui-core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.gatein.portal</groupId>
                <artifactId>exo.portal.webui.framework</artifactId>
            </dependency>
            <dependency>
                <groupId>org.exoplatform.social</groupId>
                <artifactId>social-component-webui</artifactId>
            </dependency>
            <dependency>
                <groupId>org.exoplatform.kernel</groupId>
                <artifactId>exo.kernel.container</artifactId>
            </dependency>
            <dependency>
                <groupId>org.exoplatform.core</groupId>
                <artifactId>exo.core.component.security.core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.exoplatform.social</groupId>
                <artifactId>social-component-core</artifactId>
            </dependency>
            <dependency>
                <groupId>org.exoplatform.wiki</groupId>
                <artifactId>wiki-service</artifactId>
            </dependency>
        </dependencies>
    </project>
  3. Edit the SampleUIActivity.java file:

    package com.acme.samples.activitytype;
    
    
    import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
    import org.exoplatform.webui.config.annotation.ComponentConfig;
    import org.exoplatform.social.webui.activity.BaseUIActivity;
    @ComponentConfig(
      lifecycle = UIFormLifecycle.class,
      template = "classpath:groovy/com/acme/samples/SampleUIActivity.gtmpl"
    )
    public class SampleUIActivity extends BaseUIActivity {
    }
  4. Edit the SampleUIActivity.gtmpl file. You can copy the code of social-extension.war!/groovy/social/webui/activity/UIDefaultActivity.gtmpl.

    Some samples that you can refer:

    • integ-wiki-social-4.x.x.jar!/groovy/wiki/social-integration/plugin/space/WikiUIActivity.gtmpl

    • integ-calendar-social-4.x.x.jar!/groovy/cs/social-integration/plugin/space/CalendarUIActivity.gtmpl

    • integ-ecms-social-4.x.x.jar!/groovy/ecm/social-integration/plugin/space/ContentUIActivity.gtmpl

  5. Edit the SampleUIActivityBuilder.java file:

    package com.acme.samples.activitytype;
    
    
    import org.exoplatform.social.core.activity.model.ExoSocialActivity;
    import org.exoplatform.social.webui.activity.BaseUIActivity;
    import org.exoplatform.social.webui.activity.BaseUIActivityBuilder;
    public class SampleUIActivityBuilder extends BaseUIActivityBuilder {
      @Override
      protected void extendUIActivity(BaseUIActivity uiActivity, ExoSocialActivity activity) {
        //
      }
    }
  6. Edit the configuration.xml file:

    
    <configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd http://www.exoplatform.org/xml/ns/kernel_1_2.xsd"
      xmlns="http://www.exoplatform.org/xml/ns/kernel_1_2.xsd">
        <external-component-plugins>
            <target-component>org.exoplatform.webui.ext.UIExtensionManager</target-component>
            <component-plugin>
                <name>add.action</name>
                <set-method>registerUIExtensionPlugin</set-method>
                <type>org.exoplatform.webui.ext.UIExtensionPlugin</type>
                <init-params>
                    <object-param>
                        <name>Space Activity</name>
                        <object type="org.exoplatform.social.webui.activity.UIActivityExtension">
                            <field name="type"><string>org.exoplatform.social.webui.activity.BaseUIActivity</string></field>
                            <field name="name"><string>test-activity-type</string></field>
                            <field name="component">
                                <string>com.acme.samples.activitytype.SampleUIActivity</string>
                            </field>
                            <field name="activityBuiderClass">
                                <string>com.acme.samples.activitytype.SampleUIActivityBuilder</string>
                            </field>
                        </object>
                    </object-param>
                </init-params>
            </component-plugin>
        </external-component-plugins>

        <external-component-plugins>
            <target-component>org.exoplatform.wiki.service.WikiService</target-component>
            <component-plugin>
                <name>Wiki listener</name>
                <set-method>addComponentPlugin</set-method>
                <type>com.acme.samples.activitytype.GenerateActivity4Testing</type>
                <init-params>
                    <value-param>
                        <name>wikiPortletName</name>
                        <value>wiki</value>
                    </value-param>
                </init-params>
            </component-plugin>
        </external-component-plugins>
        
    </configuration>

    The configuration fulfils two tasks:

    • Register the plugin for UIActivityExtension, using the UI Extension mechanism. Pay attention that the activity type is registered as test-activity-type.

    • Register your GenerateActivity4Testing as a wiki listener.

  7. Edit the GenerateActivity4Testing.java file:

    package com.acme.samples.activitytype;
    
    
    import org.exoplatform.container.PortalContainer;
    import org.exoplatform.services.security.ConversationState;
    import org.exoplatform.social.core.identity.model.Identity;
    import org.exoplatform.social.core.identity.provider.OrganizationIdentityProvider;
    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.wiki.service.listener.PageWikiListener;
    import org.exoplatform.wiki.mow.api.Page;
    public class GenerateActivity4Testing extends PageWikiListener {
        
        public static final String ACTIVITY_TYPE = "test-activity-type";
        
        private void generateActivity() throws Exception {
            // Get current user and assign to ownerStream
            String username = ConversationState.getCurrent().getIdentity().getUserId();
            IdentityManager identityM = 
                (IdentityManager) PortalContainer.getInstance().getComponentInstanceOfType(IdentityManager.class);
            Identity userIdentity = identityM.getOrCreateIdentity(OrganizationIdentityProvider.NAME, username, false);
            Identity ownerStream = userIdentity;
            
            // New activity
            ExoSocialActivityImpl activity = new ExoSocialActivityImpl();
            activity.setUserId(userIdentity.getId());
            activity.setTitle("This is an activity of type <b>" + ACTIVITY_TYPE + "</b>.");
            activity.setBody("This is for testing");
            activity.setType(ACTIVITY_TYPE);
            
            // Save activity
            ActivityManager activityM = 
                (ActivityManager) PortalContainer.getInstance().getComponentInstanceOfType(ActivityManager.class);
            activityM.saveActivityNoReturn(ownerStream, activity);
            
        }
        
        @Override
        public void postAddPage(String wikiType, String wikiOwner, String pageId, Page page) throws Exception {
            
            generateActivity();
            
        }
        
        @Override
        public void postDeletePage(String wikiType, String wikiOwner, String pageId, Page page) throws Exception {
            //
        }
        
        @Override
        public void postUpdatePage(String wikiType, String wikiOwner, String pageId, Page page, String wikiUpdateType) throws Exception {
            //
        }
    }

    This is supposed to create an activity of test-activity-type when a wiki page is added.

  8. Build and deploy the .jar file (target/wiki-activity-type-1.0.jar) into the eXo Platform package.

    • $PLATFORM_TOMCAT_HOME/lib (in Tomcat)

    • $PLATFORM_JBOSS_HOME/standalone/deployments/platform.ear!/lib (in JBoss)

Testing

Start the server, log in and go to Wiki. Here, create a new wiki page, then test the activity in the Intranet homepage:

You have re-used the UIDefaultActivity template. To imagine out what can be done with your own template, let's see the wiki activity that uses WikiUIActivity.gtmpl. Pay attention to the book icon on the leftmost, the link to the relevant wiki page, and the excerpt of its content:

What is ActivityBuilder?

In the above example, you extend BaseUIActivityBuilder and do not write any extra code. To understand what you can do with your ActivityBuilder, let's see the following code of UILinkActivityBuilder:

public class UILinkActivityBuilder extends BaseUIActivityBuilder {

  private static final Log LOG = ExoLogger.getLogger(UILinkActivityBuilder.class);
  @Override
  protected void extendUIActivity(BaseUIActivity uiActivity, ExoSocialActivity activity) {
    UILinkActivity uiLinkActivity = (UILinkActivity) uiActivity;
    Map<String, String> templateParams = activity.getTemplateParams();
    uiLinkActivity.setLinkSource(templateParams.get(UILinkActivityComposer.LINK_PARAM));
    uiLinkActivity.setLinkTitle(templateParams.get(UILinkActivityComposer.TITLE_PARAM));
    uiLinkActivity.setLinkImage(templateParams.get(UILinkActivityComposer.IMAGE_PARAM));
    uiLinkActivity.setLinkDescription(templateParams.get(UILinkActivityComposer.DESCRIPTION_PARAM));
    uiLinkActivity.setLinkComment(templateParams.get(UILinkActivityComposer.COMMENT_PARAM));
  }
}

You can see more complex codes at the eXo Integration project where many activity types are created. The example of this tutorial is very similar to (and simpler than) the ks-wiki:spaces type.

Copyright ©. All rights reserved. eXo Platform SAS
blog comments powered byDisqus