001    /**
002     * Copyright (c) 2000-2013 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.portlet.LiferayPortletURL;
021    import com.liferay.portal.kernel.util.HtmlUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.kernel.workflow.WorkflowException;
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.PortalUtil;
029    import com.liferay.portal.util.PortletKeys;
030    import com.liferay.portlet.PortletURLFactoryUtil;
031    
032    import javax.portlet.PortletRequest;
033    import javax.portlet.WindowState;
034    
035    /**
036     * @author Jonathan Lee
037     */
038    public class WorkflowTasksUserNotificationHandler
039            extends BaseUserNotificationHandler {
040    
041            public WorkflowTasksUserNotificationHandler() {
042                    setPortletId(PortletKeys.MY_WORKFLOW_TASKS);
043            }
044    
045            @Override
046            protected String getBody(
047                            UserNotificationEvent userNotificationEvent,
048                            ServiceContext serviceContext)
049                    throws Exception {
050    
051                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
052                            userNotificationEvent.getPayload());
053    
054                    long workflowTaskId = jsonObject.getLong("workflowTaskId");
055    
056                    try {
057                            WorkflowTaskManagerUtil.getWorkflowTask(
058                                    serviceContext.getCompanyId(), workflowTaskId);
059                    }
060                    catch (WorkflowException we) {
061                            UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
062                                    userNotificationEvent.getUserNotificationEventId());
063    
064                            return null;
065                    }
066    
067                    return HtmlUtil.escape(jsonObject.getString("notificationMessage"));
068            }
069    
070            protected String getKaleoProcessLink(
071                            long workflowTaskId, ServiceContext serviceContext)
072                    throws Exception {
073    
074                    LiferayPortletURL portletURL = PortletURLFactoryUtil.create(
075                            serviceContext.getRequest(), PortletKeys.KALEO_FORMS,
076                            serviceContext.getPlid(), PortletRequest.RENDER_PHASE);
077    
078                    String currentURL = portletURL.toString();
079    
080                    portletURL.setParameter("tabs2", "edit-workflow-task");
081                    portletURL.setParameter("backURL", currentURL);
082                    portletURL.setParameter(
083                            "workflowTaskId", String.valueOf(workflowTaskId));
084                    portletURL.setWindowState(WindowState.NORMAL);
085    
086                    return portletURL.toString();
087            }
088    
089            @Override
090            protected String getLink(
091                            UserNotificationEvent userNotificationEvent,
092                            ServiceContext serviceContext)
093                    throws Exception {
094    
095                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
096                            userNotificationEvent.getPayload());
097    
098                    String entryClassName = jsonObject.getString("entryClassName");
099    
100                    if (Validator.equals(entryClassName, _KALEO_PROCESS_CLASS_NAME)) {
101                            return getKaleoProcessLink(
102                                    jsonObject.getLong("workflowTaskId"), serviceContext);
103                    }
104    
105                    LiferayPortletURL portletURL = PortletURLFactoryUtil.create(
106                            serviceContext.getRequest(), PortletKeys.MY_WORKFLOW_TASKS,
107                            PortalUtil.getControlPanelPlid(serviceContext.getCompanyId()),
108                            PortletRequest.RENDER_PHASE);
109    
110                    portletURL.setControlPanelCategory("my");
111                    portletURL.setParameter(
112                            "struts_action", "/my_workflow_tasks/edit_workflow_task");
113                    portletURL.setParameter(
114                            "workflowTaskId", jsonObject.getString("workflowTaskId"));
115                    portletURL.setWindowState(WindowState.MAXIMIZED);
116    
117                    return portletURL.toString();
118            }
119    
120            private static final String _KALEO_PROCESS_CLASS_NAME =
121                    "com.liferay.portal.workflow.kaleo.forms.model.KaleoProcess";
122    
123    }