001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.workflowtasks.notifications;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.notifications.BaseUserNotificationHandler;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.workflow.WorkflowHandler;
022    import com.liferay.portal.kernel.workflow.WorkflowHandlerRegistryUtil;
023    import com.liferay.portal.kernel.workflow.WorkflowTask;
024    import com.liferay.portal.kernel.workflow.WorkflowTaskManagerUtil;
025    import com.liferay.portal.model.UserNotificationEvent;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.UserNotificationEventLocalServiceUtil;
028    import com.liferay.portal.util.PortletKeys;
029    
030    /**
031     * @author Jonathan Lee
032     */
033    public class WorkflowTasksUserNotificationHandler
034            extends BaseUserNotificationHandler {
035    
036            public WorkflowTasksUserNotificationHandler() {
037                    setOpenDialog(true);
038                    setPortletId(PortletKeys.MY_WORKFLOW_TASKS);
039            }
040    
041            @Override
042            protected String getBody(
043                            UserNotificationEvent userNotificationEvent,
044                            ServiceContext serviceContext)
045                    throws Exception {
046    
047                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
048                            userNotificationEvent.getPayload());
049    
050                    long workflowTaskId = jsonObject.getLong("workflowTaskId");
051    
052                    WorkflowTask workflowTask = WorkflowTaskManagerUtil.fetchWorkflowTask(
053                            serviceContext.getCompanyId(), workflowTaskId);
054    
055                    if (workflowTask == null) {
056                            UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
057                                    userNotificationEvent.getUserNotificationEventId());
058    
059                            return null;
060                    }
061    
062                    return HtmlUtil.escape(jsonObject.getString("notificationMessage"));
063            }
064    
065            @Override
066            protected String getLink(
067                            UserNotificationEvent userNotificationEvent,
068                            ServiceContext serviceContext)
069                    throws Exception {
070    
071                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
072                            userNotificationEvent.getPayload());
073    
074                    String entryClassName = jsonObject.getString("entryClassName");
075    
076                    WorkflowHandler<?> workflowHandler =
077                            WorkflowHandlerRegistryUtil.getWorkflowHandler(entryClassName);
078    
079                    if (workflowHandler == null) {
080                            return null;
081                    }
082    
083                    long workflowTaskId = jsonObject.getLong("workflowTaskId");
084    
085                    return workflowHandler.getURLEditWorkflowTask(
086                            workflowTaskId, serviceContext);
087            }
088    
089    }