001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
056                            PortletConfig portletConfig, ActionRequest actionRequest,
057                            ActionResponse actionResponse)
058                    throws Exception {
059    
060                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
061    
062                    try {
063                            if (cmd.equals(Constants.ASSIGN)) {
064                                    assignTask(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.SAVE)) {
067                                    completeTask(actionRequest);
068                            }
069                            else if (cmd.equals(Constants.UPDATE)) {
070                                    updateTask(actionRequest);
071                            }
072    
073                            String redirect = ParamUtil.getString(actionRequest, "redirect");
074                            String closeRedirect = ParamUtil.getString(
075                                    actionRequest, "closeRedirect");
076    
077                            if (Validator.isNotNull(closeRedirect)) {
078                                    redirect = HttpUtil.setParameter(
079                                            redirect, "closeRedirect", closeRedirect);
080    
081                                    LiferayPortletConfig liferayPortletConfig =
082                                            (LiferayPortletConfig)portletConfig;
083    
084                                    SessionMessages.add(
085                                            actionRequest,
086                                            liferayPortletConfig.getPortletId() +
087                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
088                                            closeRedirect);
089                            }
090    
091                            sendRedirect(actionRequest, actionResponse, redirect);
092                    }
093                    catch (Exception e) {
094                            if (e instanceof WorkflowTaskDueDateException) {
095                                    SessionErrors.add(actionRequest, e.getClass());
096                            }
097                            else if (e instanceof PrincipalException ||
098                                             e instanceof WorkflowException) {
099    
100                                    SessionErrors.add(actionRequest, e.getClass());
101    
102                                    setForward(actionRequest, "portlet.workflow_tasks.error");
103                            }
104                            else {
105                                    throw e;
106                            }
107                    }
108            }
109    
110            @Override
111            public ActionForward render(
112                            ActionMapping actionMapping, ActionForm actionForm,
113                            PortletConfig portletConfig, RenderRequest renderRequest,
114                            RenderResponse renderResponse)
115                    throws Exception {
116    
117                    try {
118                            ActionUtil.getWorkflowTask(renderRequest);
119                    }
120                    catch (Exception e) {
121                            if (e instanceof WorkflowException) {
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                    WorkflowTaskManagerUtil.assignWorkflowTaskToUser(
149                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
150                            workflowTaskId, assigneeUserId, comment, null, null);
151            }
152    
153            protected void completeTask(ActionRequest actionRequest) throws Exception {
154                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
155                            WebKeys.THEME_DISPLAY);
156    
157                    long workflowTaskId = ParamUtil.getLong(
158                            actionRequest, "workflowTaskId");
159    
160                    String transitionName = ParamUtil.getString(
161                            actionRequest, "transitionName");
162                    String comment = ParamUtil.getString(actionRequest, "comment");
163    
164                    WorkflowTaskManagerUtil.completeWorkflowTask(
165                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
166                            workflowTaskId, transitionName, comment, null);
167            }
168    
169            @Override
170            protected boolean isCheckMethodOnProcessAction() {
171                    return _CHECK_METHOD_ON_PROCESS_ACTION;
172            }
173    
174            protected void updateTask(ActionRequest actionRequest) throws Exception {
175                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
176                            WebKeys.THEME_DISPLAY);
177    
178                    long workflowTaskId = ParamUtil.getLong(
179                            actionRequest, "workflowTaskId");
180    
181                    String comment = ParamUtil.getString(actionRequest, "comment");
182    
183                    int dueDateMonth = ParamUtil.getInteger(actionRequest, "dueDateMonth");
184                    int dueDateDay = ParamUtil.getInteger(actionRequest, "dueDateDay");
185                    int dueDateYear = ParamUtil.getInteger(actionRequest, "dueDateYear");
186                    int dueDateHour = ParamUtil.getInteger(actionRequest, "dueDateHour");
187                    int dueDateMinute = ParamUtil.getInteger(
188                            actionRequest, "dueDateMinute");
189                    int dueDateAmPm = ParamUtil.getInteger(actionRequest, "dueDateAmPm");
190    
191                    if (dueDateAmPm == Calendar.PM) {
192                            dueDateHour += 12;
193                    }
194    
195                    Date dueDate = PortalUtil.getDate(
196                            dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
197                            WorkflowTaskDueDateException.class);
198    
199                    WorkflowTaskManagerUtil.updateDueDate(
200                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
201                            workflowTaskId, comment, dueDate);
202            }
203    
204            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
205    
206    }