Friday, November 29, 2013

Kaleo Workflow Configuration for Custom Portlet in Liferay 6.1

1. Make sure you have deployed kaleo workflow war in your liferay portal.

2. Create the service.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_1_0.dtd">
<service-builder package-path="com.sample.harish">
<author>harish.kumar</author>
<namespace>harish</namespace>
<entity name="Feedback" uuid="true" local-service="true" remote-service="false">
<column name="feedbackId" type="long" primary="true" />
<column name="feedbackDate" type="Date" />
<column name="feedbackText" type="String" />
<column name="status" type="int" />
<column name="statusByUserId" type="long" />
<column name="statusByUserName" type="String" />
<column name="statusDate" type="Date" />
<column name="companyId" type="long" />
<column name="groupId" type="long" />
<column name="userId" type="long" />
<order>
<order-column name="feedbackId" order-by="asc" />
<order-column name="feedbackDate" order-by="desc" />
</order>
<finder name="GroupId" return-type="Collection">
<finder-column name="groupId" />
</finder>
<finder name="CompanyId" return-type="Collection">
<finder-column name="companyId" />
</finder>
<finder name="feedbackText" return-type="Collection">
<finder-column name="feedbackText" />
</finder>
<finder name="G_S" return-type="Collection">
<finder-column name="groupId" />
<finder-column name="status" />
</finder>
<reference package-path="com.liferay.portal" entity="User" />
<reference package-path="com.liferay.portlet.asset" entity="AssetEntry" />
<reference package-path="com.liferay.portlet.ratings" entity="RatingsStats" />
</entity>
</service-builder>
view raw service.xml hosted with ❤ by GitHub

3. Build the service

4. Edit the FeedbackLocalServiceImpl class and add the following methods:


public Feedback addFeedback(Feedback newFeedback, long userId,
ServiceContext serviceContext) throws SystemException,
PortalException {
Feedback feedback = feedbackPersistence.create(counterLocalService
.increment(Feedback.class.getName()));
feedback.setCompanyId(newFeedback.getCompanyId());
feedback.setGroupId(newFeedback.getGroupId());
feedback.setUserId(serviceContext.getUserId());
feedback.setFeedbackDate(newFeedback.getFeedbackDate());
feedback.setFeedbackText(newFeedback.getFeedbackText());
feedback.setStatus(WorkflowConstants.STATUS_DRAFT);
feedbackPersistence.update(feedback, false);
assetEntryLocalService.updateEntry(userId, feedback.getGroupId(),
Feedback.class.getName(), feedback.getFeedbackId(),
serviceContext.getAssetCategoryIds(),
serviceContext.getAssetTagNames());
WorkflowHandlerRegistryUtil.startWorkflowInstance(
feedback.getCompanyId(), feedback.getGroupId(), userId,
Feedback.class.getName(), feedback.getPrimaryKey(), feedback,
serviceContext);
return feedback;
}
public void deleteFeedbacks(Feedback feedback) throws SystemException,
PortalException {
assetEntryLocalService.deleteEntry(Feedback.class.getName(),
feedback.getFeedbackId());
feedbackPersistence.remove(feedback);
}
public Feedback updateStatus(long userId, long resourcePrimKey, int status,
ServiceContext serviceContext) throws PortalException,
SystemException {
User user = userLocalService.getUser(userId);
Feedback feedback = getFeedback(resourcePrimKey);
feedback.setStatus(status);
feedback.setStatusByUserId(userId);
feedback.setStatusByUserName(user.getFullName());
feedback.setStatusDate(serviceContext.getModifiedDate());
feedbackPersistence.update(feedback, false);
if (status == WorkflowConstants.STATUS_APPROVED) {
assetEntryLocalService.updateVisible(Feedback.class.getName(),
resourcePrimKey, true);
} else {
assetEntryLocalService.updateVisible(Feedback.class.getName(),
resourcePrimKey, false);
}
return feedback;
}

5. Make the following entries in liferay-portlet.xml file


<asset-renderer-factory>com.sample.harish.portlet.asset.FeedbackAssetRendererFactory</asset-renderer-factory>
<workflow-handler>com.sample.harish.portlet.workflow.FeedbackWorkflowHandler</workflow-handler>

6. create the FeedbackAssetRendererFactory class


public class FeedbackAssetRendererFactory extends BaseAssetRendererFactory {
@Override
public AssetRenderer getAssetRenderer(long classPK, int type)
throws PortalException, SystemException {
Feedback feedback = FeedbackLocalServiceUtil.getFeedback(classPK);
return new FeedbackAssetRenderer(feedback);
}
@Override
public String getClassName() {
return Feedback.class.getName();
}
@Override
public String getType() {
return "article";
}
}

7. create the FeedbackAssetRenderer class-


public class FeedbackAssetRenderer extends BaseAssetRenderer{
private Feedback _feedback;
public FeedbackAssetRenderer(Feedback feedback) {
_feedback = feedback;
}
public long getClassPK() {
return _feedback.getFeedbackId();
}
public long getGroupId() {
return _feedback.getGroupId();
}
public String getSummary(Locale arg0) {
return _feedback.getFeedbackText();
}
public String getTitle(Locale arg0) {
return "Feedback Contest Entry";
}
public long getUserId() {
return _feedback.getUserId();
}
public String getUuid() {
return _feedback.getUuid();
}
public String render(RenderRequest request, RenderResponse response, String template)
throws Exception
{
if (template.equals(TEMPLATE_FULL_CONTENT)) {
return "/html/feedback.jsp";
}
else
{
return null;
}
}
@Override
public String getUserName() {
// TODO Auto-generated method stub
return null;
}
}

8. create the /html/feedback.jsp file.


9. create the FeedbackWorkflowHandler class -


public class FeedbackWorkflowHandler extends BaseWorkflowHandler{
public String getClassName() {
return CLASS_NAME;
}
public String getType(Locale locale) {
return LanguageUtil.get(locale, "model.resource." + CLASS_NAME);
}
public Object updateStatus(int status,
Map<String, Serializable> workflowContext) throws PortalException,
SystemException {
long userId = GetterUtil.getLong(workflowContext.get(WorkflowConstants.CONTEXT_USER_ID));
long resourcePrimKey = GetterUtil.getLong(workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));
ServiceContext serviceContext = (ServiceContext) workflowContext.get("serviceContext");
return FeedbackLocalServiceUtil.updateStatus(userId, resourcePrimKey,status, serviceContext);
}
public static final String CLASS_NAME = Feedback.class.getName();
}

10. We are done with all the changes now, to see our portlet in action go to control panel → Workflow Configuration and set the workflow for your custom portlet.

3 comments:

  1. As per above example
    i am able to configure workflow for custom porlet. but unable to get aproove status while add new record to database..
    help me ....thanks advance...!!

    ReplyDelete
  2. Hi Ranjit,

    What issue are you facing? Are you getting any exception?

    After adding the new record using FeedbackLocalServiceUtil class, you need to check the My Workflow Tasks portlet in the control panel. Are you able to see the entry there?

    Regards,
    Harish

    ReplyDelete
  3. how to use the kaleo workflow in the portlet via xml extension but not in single approver of liferay ?

    ReplyDelete