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