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