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.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.servlet.SessionMessages;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.MapUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.util.WebKeys;
025    import com.liferay.portal.kernel.workflow.WorkflowException;
026    import com.liferay.portal.kernel.workflow.WorkflowTask;
027    import com.liferay.portal.kernel.workflow.WorkflowTaskDueDateException;
028    import com.liferay.portal.kernel.workflow.WorkflowTaskManagerUtil;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.struts.PortletAction;
031    import com.liferay.portal.theme.ThemeDisplay;
032    import com.liferay.portal.util.PortalUtil;
033    
034    import java.util.Calendar;
035    import java.util.Date;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.RenderRequest;
041    import javax.portlet.RenderResponse;
042    
043    import org.apache.struts.action.ActionForm;
044    import org.apache.struts.action.ActionForward;
045    import org.apache.struts.action.ActionMapping;
046    
047    /**
048     * @author Jorge Ferrer
049     * @author Marcellus Tavares
050     * @author Brian Wing Shun Chan
051     */
052    public class EditWorkflowTaskAction extends PortletAction {
053    
054            @Override
055            public void processAction(
056                            ActionMapping actionMapping, ActionForm actionForm,
057                            PortletConfig portletConfig, ActionRequest actionRequest,
058                            ActionResponse actionResponse)
059                    throws Exception {
060    
061                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
062    
063                    try {
064                            if (cmd.equals(Constants.ASSIGN)) {
065                                    assignTask(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.SAVE)) {
068                                    completeTask(actionRequest);
069                            }
070                            else if (cmd.equals(Constants.UPDATE)) {
071                                    updateTask(actionRequest);
072                            }
073    
074                            String redirect = ParamUtil.getString(actionRequest, "redirect");
075                            String closeRedirect = ParamUtil.getString(
076                                    actionRequest, "closeRedirect");
077    
078                            if (Validator.isNotNull(closeRedirect)) {
079                                    redirect = HttpUtil.setParameter(
080                                            redirect, "closeRedirect", closeRedirect);
081    
082                                    SessionMessages.add(
083                                            actionRequest,
084                                            PortalUtil.getPortletId(actionRequest) +
085                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
086                                            closeRedirect);
087                            }
088    
089                            sendRedirect(actionRequest, actionResponse, redirect);
090                    }
091                    catch (Exception e) {
092                            if (e instanceof WorkflowTaskDueDateException) {
093                                    SessionErrors.add(actionRequest, e.getClass());
094                            }
095                            else if (e instanceof PrincipalException ||
096                                             e instanceof WorkflowException) {
097    
098                                    SessionErrors.add(actionRequest, e.getClass());
099    
100                                    setForward(actionRequest, "portlet.workflow_tasks.error");
101                            }
102                            else {
103                                    throw e;
104                            }
105                    }
106            }
107    
108            @Override
109            public ActionForward render(
110                            ActionMapping actionMapping, ActionForm actionForm,
111                            PortletConfig portletConfig, RenderRequest renderRequest,
112                            RenderResponse renderResponse)
113                    throws Exception {
114    
115                    try {
116                            ActionUtil.getWorkflowTask(renderRequest);
117                    }
118                    catch (Exception e) {
119                            if (e instanceof PrincipalException ||
120                                    e instanceof WorkflowException) {
121    
122                                    SessionErrors.add(renderRequest, e.getClass());
123    
124                                    return actionMapping.findForward(
125                                            "portlet.workflow_tasks.error");
126                            }
127                            else {
128                                    throw e;
129                            }
130                    }
131    
132                    return actionMapping.findForward(
133                            getForward(
134                                    renderRequest, "portlet.workflow_tasks.edit_workflow_task"));
135            }
136    
137            protected void assignTask(ActionRequest actionRequest) throws Exception {
138                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
139                            WebKeys.THEME_DISPLAY);
140    
141                    long workflowTaskId = ParamUtil.getLong(
142                            actionRequest, "workflowTaskId");
143    
144                    long assigneeUserId = ParamUtil.getLong(
145                            actionRequest, "assigneeUserId");
146                    String comment = ParamUtil.getString(actionRequest, "comment");
147    
148                    checkWorkflowTaskAssignmentPermission(workflowTaskId, themeDisplay);
149    
150                    WorkflowTaskManagerUtil.assignWorkflowTaskToUser(
151                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
152                            workflowTaskId, assigneeUserId, comment, null, null);
153            }
154    
155            protected void checkWorkflowTaskAssignmentPermission(
156                            long workflowTaskId, ThemeDisplay themeDisplay)
157                    throws Exception {
158    
159                    WorkflowTask workflowTask = WorkflowTaskManagerUtil.getWorkflowTask(
160                            themeDisplay.getCompanyId(), workflowTaskId);
161    
162                    long groupId = MapUtil.getLong(
163                            workflowTask.getOptionalAttributes(), "groupId",
164                            themeDisplay.getSiteGroupId());
165    
166                    if (!WorkflowTaskPermissionChecker.hasPermission(
167                                    groupId, workflowTask, themeDisplay.getPermissionChecker())) {
168    
169                            throw new PrincipalException(
170                                    String.format(
171                                            "User %d does not have permission to assign task %d",
172                                            themeDisplay.getUserId(), workflowTaskId));
173                    }
174            }
175    
176            protected void completeTask(ActionRequest actionRequest) throws Exception {
177                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
178                            WebKeys.THEME_DISPLAY);
179    
180                    long workflowTaskId = ParamUtil.getLong(
181                            actionRequest, "workflowTaskId");
182    
183                    String transitionName = ParamUtil.getString(
184                            actionRequest, "transitionName");
185                    String comment = ParamUtil.getString(actionRequest, "comment");
186    
187                    WorkflowTaskManagerUtil.completeWorkflowTask(
188                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
189                            workflowTaskId, transitionName, comment, null);
190            }
191    
192            @Override
193            protected boolean isCheckMethodOnProcessAction() {
194                    return _CHECK_METHOD_ON_PROCESS_ACTION;
195            }
196    
197            protected void updateTask(ActionRequest actionRequest) throws Exception {
198                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
199                            WebKeys.THEME_DISPLAY);
200    
201                    long workflowTaskId = ParamUtil.getLong(
202                            actionRequest, "workflowTaskId");
203    
204                    String comment = ParamUtil.getString(actionRequest, "comment");
205    
206                    int dueDateMonth = ParamUtil.getInteger(actionRequest, "dueDateMonth");
207                    int dueDateDay = ParamUtil.getInteger(actionRequest, "dueDateDay");
208                    int dueDateYear = ParamUtil.getInteger(actionRequest, "dueDateYear");
209                    int dueDateHour = ParamUtil.getInteger(actionRequest, "dueDateHour");
210                    int dueDateMinute = ParamUtil.getInteger(
211                            actionRequest, "dueDateMinute");
212                    int dueDateAmPm = ParamUtil.getInteger(actionRequest, "dueDateAmPm");
213    
214                    if (dueDateAmPm == Calendar.PM) {
215                            dueDateHour += 12;
216                    }
217    
218                    Date dueDate = PortalUtil.getDate(
219                            dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
220                            WorkflowTaskDueDateException.class);
221    
222                    WorkflowTaskManagerUtil.updateDueDate(
223                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
224                            workflowTaskId, comment, dueDate);
225            }
226    
227            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
228    
229    }