001    /**
002     * Copyright (c) 2000-2012 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            @Override
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
056    
057                    try {
058                            if (cmd.equals(Constants.ASSIGN)) {
059                                    assignTask(actionRequest);
060                            }
061                            else if (cmd.equals(Constants.SAVE)) {
062                                    completeTask(actionRequest);
063                            }
064                            else if (cmd.equals(Constants.UPDATE)) {
065                                    updateTask(actionRequest);
066                            }
067    
068                            sendRedirect(actionRequest, actionResponse);
069                    }
070                    catch (Exception e) {
071                            if (e instanceof WorkflowTaskDueDateException) {
072                                    SessionErrors.add(actionRequest, e.getClass().getName());
073                            }
074                            else if (e instanceof PrincipalException ||
075                                             e instanceof WorkflowException) {
076    
077                                    SessionErrors.add(actionRequest, e.getClass().getName());
078    
079                                    setForward(actionRequest, "portlet.workflow_tasks.error");
080                            }
081                            else {
082                                    throw e;
083                            }
084                    }
085            }
086    
087            @Override
088            public ActionForward render(
089                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
090                            RenderRequest renderRequest, RenderResponse renderResponse)
091                    throws Exception {
092    
093                    try {
094                            ActionUtil.getWorkflowTask(renderRequest);
095                    }
096                    catch (Exception e) {
097                            if (e instanceof WorkflowException) {
098    
099                                    SessionErrors.add(renderRequest, e.getClass().getName());
100    
101                                    return mapping.findForward("portlet.workflow_tasks.error");
102                            }
103                            else {
104                                    throw e;
105                            }
106                    }
107    
108                    return mapping.findForward(getForward(
109                            renderRequest, "portlet.workflow_tasks.edit_workflow_task"));
110            }
111    
112            protected void assignTask(ActionRequest actionRequest) throws Exception {
113                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
114                            WebKeys.THEME_DISPLAY);
115    
116                    long workflowTaskId = ParamUtil.getLong(
117                            actionRequest, "workflowTaskId");
118    
119                    long assigneeUserId = ParamUtil.getLong(
120                            actionRequest, "assigneeUserId");
121                    String comment = ParamUtil.getString(actionRequest, "comment");
122    
123                    WorkflowTaskManagerUtil.assignWorkflowTaskToUser(
124                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
125                            workflowTaskId, assigneeUserId, comment, null, null);
126            }
127    
128            protected void completeTask(ActionRequest actionRequest) throws Exception {
129                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
130                            WebKeys.THEME_DISPLAY);
131    
132                    long workflowTaskId = ParamUtil.getLong(
133                            actionRequest, "workflowTaskId");
134    
135                    String transitionName = ParamUtil.getString(
136                            actionRequest, "transitionName");
137                    String comment = ParamUtil.getString(actionRequest, "comment");
138    
139                    WorkflowTaskManagerUtil.completeWorkflowTask(
140                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
141                            workflowTaskId, transitionName, comment, null);
142            }
143    
144            @Override
145            protected boolean isCheckMethodOnProcessAction() {
146                    return _CHECK_METHOD_ON_PROCESS_ACTION;
147            }
148    
149            protected void updateTask(ActionRequest actionRequest) throws Exception {
150                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
151                            WebKeys.THEME_DISPLAY);
152    
153                    long workflowTaskId = ParamUtil.getLong(
154                            actionRequest, "workflowTaskId");
155    
156                    String comment = ParamUtil.getString(actionRequest, "comment");
157    
158                    int dueDateMonth = ParamUtil.getInteger(actionRequest, "dueDateMonth");
159                    int dueDateDay = ParamUtil.getInteger(actionRequest, "dueDateDay");
160                    int dueDateYear = ParamUtil.getInteger(actionRequest, "dueDateYear");
161                    int dueDateHour = ParamUtil.getInteger(actionRequest, "dueDateHour");
162                    int dueDateMinute = ParamUtil.getInteger(
163                            actionRequest, "dueDateMinute");
164                    int dueDateAmPm = ParamUtil.getInteger(actionRequest, "dueDateAmPm");
165    
166                    if (dueDateAmPm == Calendar.PM) {
167                            dueDateHour += 12;
168                    }
169    
170                    Date dueDate = PortalUtil.getDate(
171                            dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
172                            new WorkflowTaskDueDateException());
173    
174                    WorkflowTaskManagerUtil.updateDueDate(
175                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
176                            workflowTaskId, comment, dueDate);
177            }
178    
179            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
180    
181    }