1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.workflowtasks.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.Constants;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.WebKeys;
21  import com.liferay.portal.kernel.workflow.WorkflowException;
22  import com.liferay.portal.kernel.workflow.WorkflowTaskDueDateException;
23  import com.liferay.portal.kernel.workflow.WorkflowTaskManagerUtil;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.PortalUtil;
28  
29  import java.util.Calendar;
30  import java.util.Date;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="EditWorkflowTaskAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Jorge Ferrer
46   * @author Marcellus Tavares
47   * @author Brian Wing Shun Chan
48   */
49  public class EditWorkflowTaskAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
57  
58          try {
59              if (cmd.equals(Constants.ASSIGN)) {
60                  assignTask(actionRequest);
61              }
62              else if (cmd.equals(Constants.SAVE)) {
63                  completeTask(actionRequest);
64              }
65              else if (cmd.equals(Constants.UPDATE)) {
66                  updateTask(actionRequest);
67              }
68  
69              sendRedirect(actionRequest, actionResponse);
70          }
71          catch (Exception e) {
72              if (e instanceof WorkflowTaskDueDateException) {
73                  SessionErrors.add(actionRequest, e.getClass().getName());
74              }
75              else if (e instanceof PrincipalException ||
76                       e instanceof WorkflowException) {
77  
78                  SessionErrors.add(actionRequest, e.getClass().getName());
79  
80                  setForward(actionRequest, "portlet.workflow_tasks.error");
81              }
82              else {
83                  throw e;
84              }
85          }
86      }
87  
88      public ActionForward render(
89              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
90              RenderRequest renderRequest, RenderResponse renderResponse)
91          throws Exception {
92  
93          try {
94              ActionUtil.getWorkflowTask(renderRequest);
95          }
96          catch (Exception e) {
97              if (e instanceof WorkflowException) {
98  
99                  SessionErrors.add(renderRequest, e.getClass().getName());
100 
101                 return mapping.findForward("portlet.workflow_tasks.error");
102             }
103             else {
104                 throw e;
105             }
106         }
107 
108         return mapping.findForward(getForward(
109             renderRequest, "portlet.workflow_tasks.edit_workflow_task"));
110     }
111 
112     protected void assignTask(ActionRequest actionRequest) throws Exception {
113         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
114             WebKeys.THEME_DISPLAY);
115 
116         long workflowTaskId = ParamUtil.getLong(
117             actionRequest, "workflowTaskId");
118 
119         long assigneeUserId = ParamUtil.getLong(
120             actionRequest, "assigneeUserId");
121         String comment = ParamUtil.getString(actionRequest, "comment");
122 
123         WorkflowTaskManagerUtil.assignWorkflowTaskToUser(
124             themeDisplay.getCompanyId(), themeDisplay.getUserId(),
125             workflowTaskId, assigneeUserId, comment, null, null);
126     }
127 
128     protected void completeTask(ActionRequest actionRequest) throws Exception {
129         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
130             WebKeys.THEME_DISPLAY);
131 
132         long workflowTaskId = ParamUtil.getLong(
133             actionRequest, "workflowTaskId");
134 
135         String transitionName = ParamUtil.getString(
136             actionRequest, "transitionName");
137         String comment = ParamUtil.getString(actionRequest, "comment");
138 
139         WorkflowTaskManagerUtil.completeWorkflowTask(
140             themeDisplay.getCompanyId(), themeDisplay.getUserId(),
141             workflowTaskId, transitionName, comment, null);
142     }
143 
144     protected boolean isCheckMethodOnProcessAction() {
145         return _CHECK_METHOD_ON_PROCESS_ACTION;
146     }
147 
148     protected void updateTask(ActionRequest actionRequest) throws Exception {
149         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
150             WebKeys.THEME_DISPLAY);
151 
152         long workflowTaskId = ParamUtil.getLong(
153             actionRequest, "workflowTaskId");
154 
155         String comment = ParamUtil.getString(actionRequest, "comment");
156 
157         int dueDateMonth = ParamUtil.getInteger(actionRequest, "dueDateMonth");
158         int dueDateDay = ParamUtil.getInteger(actionRequest, "dueDateDay");
159         int dueDateYear = ParamUtil.getInteger(actionRequest, "dueDateYear");
160         int dueDateHour = ParamUtil.getInteger(actionRequest, "dueDateHour");
161         int dueDateMinute = ParamUtil.getInteger(
162             actionRequest, "dueDateMinute");
163         int dueDateAmPm = ParamUtil.getInteger(actionRequest, "dueDateAmPm");
164 
165         if (dueDateAmPm == Calendar.PM) {
166             dueDateHour += 12;
167         }
168 
169         Date dueDate = PortalUtil.getDate(
170             dueDateMonth, dueDateDay, dueDateYear, dueDateHour, dueDateMinute,
171             new WorkflowTaskDueDateException());
172 
173         WorkflowTaskManagerUtil.updateDueDate(
174             themeDisplay.getCompanyId(), themeDisplay.getUserId(),
175             workflowTaskId, comment, dueDate);
176     }
177 
178     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
179 
180 }