Supporting Workflow at the Service Layer
Step 2 of 3
Now you’ll set the status fields, introduce entries to the workflow framework,
and add the updateStatus
method to GuestbookEntryLocalServiceImpl
. It works
the same as it did for guestbooks.
-
Add the following lines in the
addGuestbookEntry
method, immediately after the existing setter methods (e.g.,entry.setMessage(message)
):entry.setStatus(WorkflowConstants.STATUS_DRAFT); entry.setStatusByUserId(userId); entry.setStatusByUserName(user.getFullName()); entry.setStatusDate(serviceContext.getModifiedDate(null));
-
Still in the
addGuestbookEntry
method, place the following code right before thereturn
statement:WorkflowHandlerRegistryUtil.startWorkflowInstance(entry.getCompanyId(), entry.getGroupId(), entry.getUserId(), GuestbookEntry.class.getName(), entry.getPrimaryKey(), entry, serviceContext);
The
startWorkflowInstance
call eventually directs the workflow processing to yourGuestbookEntryWorkflowHandler
class, which you’ll create later. That class is responsible for making sure the entity is updated in the database (via anupdateStatus
method), but it’s best practice to make persistence calls in the service layer. -
Add a corresponding
updateStatus
method here inGuestbookEntryLocalServiceImpl
. Add this method to the bottom of the class:public GuestbookEntry updateStatus(long userId, long guestbookId, long entryId, int status, ServiceContext serviceContext) throws PortalException, SystemException { User user = userLocalService.getUser(userId); GuestbookEntry entry = getGuestbookEntry(entryId); entry.setStatus(status); entry.setStatusByUserId(userId); entry.setStatusByUserName(user.getFullName()); entry.setStatusDate(new Date()); guestbookEntryPersistence.update(entry); if (status == WorkflowConstants.STATUS_APPROVED) { assetEntryLocalService.updateVisible(GuestbookEntry.class.getName(), entryId, true); } else { assetEntryLocalService.updateVisible(GuestbookEntry.class.getName(), entryId, false); } return entry; }
-
As with Guestbooks, you must add a call to
deleteWorkflowInstanceLinks
in the entry’s delete method to avoid leaving orphaned database entries in theworkflowinstancelinks
table. First add the following<reference>
tag toservice.xml
, this time in theentry
entity section, below the existing reference tags:<reference entity="WorkflowInstanceLink" package-path="com.liferay.portal" />
-
Add the following method call to the
deleteGuestbookEntry
method inGuestbookEntryLocalServiceImpl
, right before thereturn
statement:workflowInstanceLinkLocalService.deleteWorkflowInstanceLinks( entry.getCompanyId(), entry.getGroupId(), GuestbookEntry.class.getName(), entry.getEntryId());
-
Organize imports ([CTRL]+[SHIFT]+O), save your work, run Service Builder, and refresh the Gradle project.
Now both entities support the status of the entity and can handle it as it enters the workflow framework and as it returns from the workflow framework. There’s one more update to make in the local service implementation classes: adding getter methods that take the status as a parameter. Later you’ll use these methods in the view layer so you can display only approved guestbooks and entries.