001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.MapUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.workflow.WorkflowException;
025    import com.liferay.portal.kernel.workflow.WorkflowTask;
026    import com.liferay.portal.kernel.workflow.WorkflowTaskManagerUtil;
027    import com.liferay.portal.model.UserNotificationEvent;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.UserNotificationEventLocalServiceUtil;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portal.util.PortletKeys;
033    import com.liferay.portlet.PortletURLFactoryUtil;
034    import com.liferay.portlet.workflowtasks.action.WorkflowTaskPermissionChecker;
035    
036    import javax.portlet.PortletRequest;
037    import javax.portlet.WindowState;
038    
039    /**
040     * @author Jonathan Lee
041     */
042    public class WorkflowTasksUserNotificationHandler
043            extends BaseUserNotificationHandler {
044    
045            public WorkflowTasksUserNotificationHandler() {
046                    setPortletId(PortletKeys.MY_WORKFLOW_TASKS);
047            }
048    
049            @Override
050            protected String getBody(
051                            UserNotificationEvent userNotificationEvent,
052                            ServiceContext serviceContext)
053                    throws Exception {
054    
055                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
056                            userNotificationEvent.getPayload());
057    
058                    long workflowTaskId = jsonObject.getLong("workflowTaskId");
059    
060                    WorkflowTask workflowTask = null;
061    
062                    try {
063                            workflowTask = WorkflowTaskManagerUtil.getWorkflowTask(
064                                    serviceContext.getCompanyId(), workflowTaskId);
065                    }
066                    catch (WorkflowException we) {
067                    }
068    
069                    if (!isWorkflowTaskVisible(workflowTask, serviceContext)) {
070                            UserNotificationEventLocalServiceUtil.deleteUserNotificationEvent(
071                                    userNotificationEvent.getUserNotificationEventId());
072    
073                            return null;
074                    }
075    
076                    return HtmlUtil.escape(jsonObject.getString("notificationMessage"));
077            }
078    
079            protected String getKaleoProcessLink(
080                            long workflowTaskId, ServiceContext serviceContext)
081                    throws Exception {
082    
083                    LiferayPortletURL portletURL = PortletURLFactoryUtil.create(
084                            serviceContext.getRequest(), PortletKeys.KALEO_FORMS,
085                            serviceContext.getPlid(), PortletRequest.RENDER_PHASE);
086    
087                    String currentURL = portletURL.toString();
088    
089                    portletURL.setParameter("tabs2", "edit-workflow-task");
090                    portletURL.setParameter("backURL", currentURL);
091                    portletURL.setParameter(
092                            "workflowTaskId", String.valueOf(workflowTaskId));
093                    portletURL.setWindowState(WindowState.NORMAL);
094    
095                    return portletURL.toString();
096            }
097    
098            @Override
099            protected String getLink(
100                            UserNotificationEvent userNotificationEvent,
101                            ServiceContext serviceContext)
102                    throws Exception {
103    
104                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
105                            userNotificationEvent.getPayload());
106    
107                    String entryClassName = jsonObject.getString("entryClassName");
108    
109                    if (Validator.equals(entryClassName, _KALEO_PROCESS_CLASS_NAME)) {
110                            return getKaleoProcessLink(
111                                    jsonObject.getLong("workflowTaskId"), serviceContext);
112                    }
113    
114                    LiferayPortletURL portletURL = PortletURLFactoryUtil.create(
115                            serviceContext.getRequest(), PortletKeys.MY_WORKFLOW_TASKS,
116                            PortalUtil.getControlPanelPlid(serviceContext.getCompanyId()),
117                            PortletRequest.RENDER_PHASE);
118    
119                    portletURL.setControlPanelCategory("my");
120                    portletURL.setParameter(
121                            "struts_action", "/my_workflow_tasks/edit_workflow_task");
122                    portletURL.setParameter(
123                            "workflowTaskId", jsonObject.getString("workflowTaskId"));
124                    portletURL.setWindowState(WindowState.MAXIMIZED);
125    
126                    return portletURL.toString();
127            }
128    
129            protected boolean isWorkflowTaskVisible(
130                    WorkflowTask workflowTask, ServiceContext serviceContext) {
131    
132                    if (workflowTask == null) {
133                            return false;
134                    }
135    
136                    ThemeDisplay themeDisplay = serviceContext.getThemeDisplay();
137    
138                    long groupId = MapUtil.getLong(
139                            workflowTask.getOptionalAttributes(), "groupId",
140                            themeDisplay.getSiteGroupId());
141    
142                    return WorkflowTaskPermissionChecker.hasPermission(
143                            groupId, workflowTask, themeDisplay.getPermissionChecker());
144            }
145    
146            private static final String _KALEO_PROCESS_CLASS_NAME =
147                    "com.liferay.portal.workflow.kaleo.forms.model.KaleoProcess";
148    
149    }