Wednesday 19 March 2014

Adding workflow to a custom portlet in Liferay 6.2

In this blog post I am going to give an overview on how you can integrate workflow feature in your portlet using Liferay API's.

Please Note: This blog will not be related to configuring out of the box Kaleo workflow portlet. By configuring I mean adding new workflows. For this please refer to the Liferay documentation.

In this post I will layout the basic steps to get workflow feature integrated in your custom portlet. I used Liferay v 6.2

Things have changed in the new Liferay version, especially the look and feel. We will discuss more about the new features in another article.

Here are the basic steps to get workflow integrated


Create a custom portlet, using any framework of your choice. I used Liferay MVC framework for this example. Also the service layer generated by Liferay makes life easy.

For this Portlet the model object we want to attach workflow to will be CompanyBlog. You can work with any entity. So what happens is whenever an employee adds a blog it needs to pass through a workflow.

Let's create a service.xml file, using which you will generate your service layer. I won't go into the details for using Liferay services.



After this we will modify the CompanyBlogLocalServiceImpl service class file. This file is generated by Liferay.

So we will add a method in this class

addNewCompanyBlog(CompanyBlog newCB, long userId, ServiceContext serviceContext)

So when we will add a new blog post this method will get invoked from our controller.

Prior to this call we need to set the workflow status to draft.


companyBlogObject.setStatus(WorkflowConstants.STATUS_DRAFT);

where companyBlogObject is our object from controller.

This method will first persist CompanyBlog object and after that make a call to
WorkflowHandlerRegistryUtil.startWorkflowInstance()

This method call will register this entity with the Liferay workflow engine. Which can be either Kaleo, the default one, or whichever you want to use. Simple isn't it. Just one call.

Lets see this method signature


startWorkflowInstance(companyId, userId, CompanyBlog.class.getName(),  companyBlogObject. getPrimaryKey(), companyBlogObject, serviceContext)

Its self explanatory. You set most of the values in the companyBlogObject like companyId etc.

So this will trigger the workflow.

Next we need to have a WorkflowHandler which will updateStatus of the workflow. Earlier we had set the status to DRAFT, which needs to be updated as the blog proceeds through workflow.

Your  WorkflowHandler should be named like WorkflowHandler.java
So in our case it will be CompanyBlogWorkflowHandler

Lets see the content of this class file.

I had to refer to the Liferay blog WorkflowHandler class to resolve some exceptions.

public class CompanyBlogWorkflowHandler extends BaseWorkflowHandler{



Next we need to let Liferay know about this handler. Update liferay-portlet.xml

<workflow-handler>com.ui.helper.SloganWorkflowHandler</workflow-handler>

Lastly we need to add a key to Language.properties file to help Liferay find our Entity. How this will help ? Lets take a look. If you go to Liferay Control Panel -> Workflow -> DefaultConfiguration you will see a list of entities with the option to enable workflow. In order to display our CompanyBlog here we will have to add a key-value pair to the global Language.properties file. We reference this in our WorkfloHandler class.


So for this we will need to create a hook. Which is simple. Just add a liferay-hook.xml with following content

<hook>
    <language-properties>
        content-portal/Language.properties
    </language-properties>

</hook>

Next create a content-portal folder in src folder. Then add Language.properties file with following entries

model.resource.com.services.model.CompanyBlog=Company Blog

That's it and you are all set.

Hope this article helps. If you have any questions I will be happy to help. 

1 comment:

  1. Thanks for such a enlightening article. Could you tell where can we configure the levels of approvals. For example if I want , the blog to be approved after specific or dynamic levels of approvals.

    ReplyDelete