001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(getForward(
126                            renderRequest, "portlet.workflow_tasks.edit_workflow_task"));
127            }
128    
129            protected void assignTask(ActionRequest actionRequest) throws Exception {
130                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
131                            WebKeys.THEME_DISPLAY);
132    
133                    long workflowTaskId = ParamUtil.getLong(
134                            actionRequest, "workflowTaskId");
135    
136                    long assigneeUserId = ParamUtil.getLong(
137                            actionRequest, "assigneeUserId");
138                    String comment = ParamUtil.getString(actionRequest, "comment");
139    
140                    WorkflowTaskManagerUtil.assignWorkflowTaskToUser(
141                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
142                            workflowTaskId, assigneeUserId, comment, null, null);
143            }
144    
145            protected void completeTask(ActionRequest actionRequest) throws Exception {
146                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
147                            WebKeys.THEME_DISPLAY);
148    
149                    long workflowTaskId = ParamUtil.getLong(
150                            actionRequest, "workflowTaskId");
151    
152                    String transitionName = ParamUtil.getString(
153                            actionRequest, "transitionName");
154                    String comment = ParamUtil.getString(actionRequest, "comment");
155    
156                    WorkflowTaskManagerUtil.completeWorkflowTask(
157                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
158                            workflowTaskId, transitionName, comment, null);
159            }
160    
161            @Override
162            protected boolean isCheckMethodOnProcessAction() {
163                    return _CHECK_METHOD_ON_PROCESS_ACTION;
164            }
165    
166            protected void updateTask(ActionRequest actionRequest) throws Exception {
167                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
168                            WebKeys.THEME_DISPLAY);
169    
170                    long workflowTaskId = ParamUtil.getLong(
171                            actionRequest, "workflowTaskId");
172    
173                    String comment = ParamUtil.getString(actionRequest, "comment");
174    
175                    int dueDateMonth = ParamUtil.getInteger(actionRequest, "dueDateMonth");
176                    int dueDateDay = ParamUtil.getInteger(actionRequest, "dueDateDay");
177                    int dueDateYear = ParamUtil.getInteger(actionRequest, "dueDateYear");
178                    int dueDateHour = ParamUtil.getInteger(actionRequest, "dueDateHour");
179                    int dueDateMinute = ParamUtil.getInteger(
180                            actionRequest, "dueDateMinute");
181                    int dueDateAmPm = ParamUtil.getInteger(actionRequest, "dueDateAmPm");
182    
183                    if (dueDateAmPm == Calendar.PM) {
184                            dueDateHour += 12;
185                    }
186    
187                    Date dueDate = PortalUtil.getDate(
188                            dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
189                            WorkflowTaskDueDateException.class);
190    
191                    WorkflowTaskManagerUtil.updateDueDate(
192                            themeDisplay.getCompanyId(), themeDisplay.getUserId(),
193                            workflowTaskId, comment, dueDate);
194            }
195    
196            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
197    
198    }