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