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.WorkflowTaskManagerUtil;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.ActionResponse;
29  import javax.portlet.PortletConfig;
30  import javax.portlet.RenderRequest;
31  import javax.portlet.RenderResponse;
32  
33  import org.apache.struts.action.ActionForm;
34  import org.apache.struts.action.ActionForward;
35  import org.apache.struts.action.ActionMapping;
36  
37  /**
38   * <a href="EditWorkflowTaskAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Jorge Ferrer
41   * @author Marcellus Tavares
42   */
43  public class EditWorkflowTaskAction extends PortletAction {
44  
45      public void processAction(
46              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
47              ActionRequest actionRequest, ActionResponse actionResponse)
48          throws Exception {
49  
50          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
51  
52          try {
53              if (cmd.equals(Constants.ASSIGN)) {
54                  assignTask(actionRequest);
55              }
56              else if (cmd.equals(Constants.SAVE)) {
57                  updateTask(actionRequest);
58              }
59  
60              sendRedirect(actionRequest, actionResponse);
61          }
62          catch (Exception e) {
63              if (e instanceof PrincipalException ||
64                  e instanceof WorkflowException) {
65  
66                  SessionErrors.add(actionRequest, e.getClass().getName());
67  
68                  setForward(actionRequest, "portlet.workflow_tasks.error");
69              }
70              else {
71                  throw e;
72              }
73          }
74      }
75  
76      public ActionForward render(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              RenderRequest renderRequest, RenderResponse renderResponse)
79          throws Exception {
80  
81          try {
82              ActionUtil.getWorkflowTask(renderRequest);
83          }
84          catch (Exception e) {
85              if (e instanceof WorkflowException) {
86  
87                  SessionErrors.add(renderRequest, e.getClass().getName());
88  
89                  return mapping.findForward("portlet.workflow_tasks.error");
90              }
91              else {
92                  throw e;
93              }
94          }
95  
96          return mapping.findForward(getForward(
97              renderRequest, "portlet.workflow_tasks.edit_workflow_task"));
98      }
99  
100     protected void assignTask(ActionRequest actionRequest) throws Exception {
101         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
102             WebKeys.THEME_DISPLAY);
103 
104         long workflowTaskId = ParamUtil.getLong(
105             actionRequest, "workflowTaskId");
106         long assigneeUserId = ParamUtil.getLong(
107             actionRequest, "assigneeUserId");
108         String comment = ParamUtil.getString(actionRequest, "comment");
109 
110         WorkflowTaskManagerUtil.assignWorkflowTaskToUser(
111             themeDisplay.getCompanyId(), themeDisplay.getUserId(),
112             workflowTaskId, assigneeUserId, comment, null);
113     }
114 
115     protected void updateTask(ActionRequest actionRequest) throws Exception {
116         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
117             WebKeys.THEME_DISPLAY);
118 
119         long workflowTaskId = ParamUtil.getLong(
120             actionRequest, "workflowTaskId");
121         String transitionName = ParamUtil.getString(
122             actionRequest, "transitionName");
123         String comment = ParamUtil.getString(actionRequest, "comment");
124 
125         WorkflowTaskManagerUtil.completeWorkflowTask(
126             themeDisplay.getCompanyId(), themeDisplay.getUserId(),
127             workflowTaskId, transitionName, comment, null);
128     }
129 
130 }