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.model.UserNotificationEvent;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PortletKeys;
026    
027    /**
028     * @author Jonathan Lee
029     */
030    public class WorkflowTasksUserNotificationHandler
031            extends BaseUserNotificationHandler {
032    
033            public WorkflowTasksUserNotificationHandler() {
034                    setOpenDialog(true);
035                    setPortletId(PortletKeys.MY_WORKFLOW_TASKS);
036            }
037    
038            @Override
039            protected String getBody(
040                            UserNotificationEvent userNotificationEvent,
041                            ServiceContext serviceContext)
042                    throws Exception {
043    
044                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
045                            userNotificationEvent.getPayload());
046    
047                    return HtmlUtil.escape(jsonObject.getString("notificationMessage"));
048            }
049    
050            @Override
051            protected String getLink(
052                            UserNotificationEvent userNotificationEvent,
053                            ServiceContext serviceContext)
054                    throws Exception {
055    
056                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
057                            userNotificationEvent.getPayload());
058    
059                    String entryClassName = jsonObject.getString("entryClassName");
060    
061                    WorkflowHandler<?> workflowHandler =
062                            WorkflowHandlerRegistryUtil.getWorkflowHandler(entryClassName);
063    
064                    if (workflowHandler == null) {
065                            return null;
066                    }
067    
068                    long workflowTaskId = jsonObject.getLong("workflowTaskId");
069    
070                    return workflowHandler.getURLEditWorkflowTask(
071                            workflowTaskId, serviceContext);
072            }
073    
074    }