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