001    /**
002     * Copyright (c) 2000-2010 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.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.Constants;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.WebKeys;
021    import com.liferay.portal.kernel.workflow.WorkflowException;
022    import com.liferay.portal.kernel.workflow.WorkflowTaskDueDateException;
023    import com.liferay.portal.kernel.workflow.WorkflowTaskManagerUtil;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    
029    import java.util.Calendar;
030    import java.util.Date;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Jorge Ferrer
044     * @author Marcellus Tavares
045     * @author Brian Wing Shun Chan
046     */
047    public class EditWorkflowTaskAction extends PortletAction {
048    
049            public void processAction(
050                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
051                            ActionRequest actionRequest, ActionResponse actionResponse)
052                    throws Exception {
053    
054                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055    
056                    try {
057                            if (cmd.equals(Constants.ASSIGN)) {
058                                    assignTask(actionRequest);
059                            }
060                            else if (cmd.equals(Constants.SAVE)) {
061                                    completeTask(actionRequest);
062                            }
063                            else if (cmd.equals(Constants.UPDATE)) {
064                                    updateTask(actionRequest);
065                            }
066    
067                            sendRedirect(actionRequest, actionResponse);
068                    }
069                    catch (Exception e) {
070                            if (e instanceof WorkflowTaskDueDateException) {
071                                    SessionErrors.add(actionRequest, e.getClass().getName());
072                            }
073                            else if (e instanceof PrincipalException ||
074                                             e instanceof WorkflowException) {
075    
076                                    SessionErrors.add(actionRequest, e.getClass().getName());
077    
078                                    setForward(actionRequest, "portlet.workflow_tasks.error");
079                            }
080                            else {
081                                    throw e;
082                            }
083                    }
084            }
085    
086            public ActionForward render(
087                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
088                            RenderRequest renderRequest, RenderResponse renderResponse)
089                    throws Exception {
090    
091                    try {
092                            ActionUtil.getWorkflowTask(renderRequest);
093                    }
094                    catch (Exception e) {
095                            if (e instanceof WorkflowException) {
096    
097                                    SessionErrors.add(renderRequest, e.getClass().getName());
098    
099                                    return mapping.findForward("portlet.workflow_tasks.error");
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105    
106                    return mapping.findForward(getForward(
107                            renderRequest, "portlet.workflow_tasks.edit_workflow_task"));
108            }
109    
110            protected void assignTask(ActionRequest actionRequest) throws Exception {
111                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
112                            WebKeys.THEME_DISPLAY);
113    
114                    long workflowTaskId = ParamUtil.getLong(
115                            actionRequest, "workflowTaskId");
116    
117                    long assigneeUserId = ParamUtil.getLong(
118                            actionRequest, "assigneeUserId");
119                    String comment = ParamUtil.getString(actionRequest, "comment");
120    
121                    WorkflowTaskManagerUtil.assignWorkflowTaskToUser(
122                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
123                            workflowTaskId, assigneeUserId, comment, null, null);
124            }
125    
126            protected void completeTask(ActionRequest actionRequest) throws Exception {
127                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
128                            WebKeys.THEME_DISPLAY);
129    
130                    long workflowTaskId = ParamUtil.getLong(
131                            actionRequest, "workflowTaskId");
132    
133                    String transitionName = ParamUtil.getString(
134                            actionRequest, "transitionName");
135                    String comment = ParamUtil.getString(actionRequest, "comment");
136    
137                    WorkflowTaskManagerUtil.completeWorkflowTask(
138                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
139                            workflowTaskId, transitionName, comment, null);
140            }
141    
142            protected boolean isCheckMethodOnProcessAction() {
143                    return _CHECK_METHOD_ON_PROCESS_ACTION;
144            }
145    
146            protected void updateTask(ActionRequest actionRequest) throws Exception {
147                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
148                            WebKeys.THEME_DISPLAY);
149    
150                    long workflowTaskId = ParamUtil.getLong(
151                            actionRequest, "workflowTaskId");
152    
153                    String comment = ParamUtil.getString(actionRequest, "comment");
154    
155                    int dueDateMonth = ParamUtil.getInteger(actionRequest, "dueDateMonth");
156                    int dueDateDay = ParamUtil.getInteger(actionRequest, "dueDateDay");
157                    int dueDateYear = ParamUtil.getInteger(actionRequest, "dueDateYear");
158                    int dueDateHour = ParamUtil.getInteger(actionRequest, "dueDateHour");
159                    int dueDateMinute = ParamUtil.getInteger(
160                            actionRequest, "dueDateMinute");
161                    int dueDateAmPm = ParamUtil.getInteger(actionRequest, "dueDateAmPm");
162    
163                    if (dueDateAmPm == Calendar.PM) {
164                            dueDateHour += 12;
165                    }
166    
167                    Date dueDate = PortalUtil.getDate(
168                            dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
169                            new WorkflowTaskDueDateException());
170    
171                    WorkflowTaskManagerUtil.updateDueDate(
172                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
173                            workflowTaskId, comment, dueDate);
174            }
175    
176            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
177    
178    }