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.workflowadmin.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.upload.UploadPortletRequest;
19  import com.liferay.portal.kernel.util.Constants;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.kernel.workflow.WorkflowDefinition;
22  import com.liferay.portal.kernel.workflow.WorkflowDefinitionManagerUtil;
23  import com.liferay.portal.kernel.workflow.WorkflowException;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.PortalUtil;
27  import com.liferay.portal.util.WebKeys;
28  
29  import java.io.File;
30  import java.io.FileInputStream;
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="EditWorkflowDefinitionAction.java.html"><b><i>View Source</i></b>
44   * </a>
45   *
46   * @author Bruno Farache
47   */
48  public class EditWorkflowDefinitionAction extends PortletAction {
49  
50      public void processAction(
51              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws Exception {
54  
55          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
56  
57          try {
58              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
59                  updateWorkflowDefinition(actionRequest);
60              }
61              else if (cmd.equals(Constants.DELETE)) {
62                  deleteWorkflowDefinition(actionRequest);
63              }
64  
65              sendRedirect(actionRequest, actionResponse);
66          }
67          catch (Exception e) {
68              if (e instanceof WorkflowException) {
69                  SessionErrors.add(actionRequest, e.getClass().getName());
70  
71                  setForward(actionRequest, "portlet.workflow_admin.error");
72              }
73              else {
74                  throw e;
75              }
76          }
77      }
78  
79      public ActionForward render(
80              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
81              RenderRequest renderRequest, RenderResponse renderResponse)
82          throws Exception {
83  
84          try {
85              ActionUtil.getWorkflowDefinition(renderRequest);
86          }
87          catch (Exception e) {
88              if (e instanceof WorkflowException) {
89                  SessionErrors.add(renderRequest, e.getClass().getName());
90  
91                  return mapping.findForward("portlet.workflow_admin.error");
92              }
93              else {
94                  throw e;
95              }
96          }
97  
98          return mapping.findForward(getForward(
99              renderRequest, "portlet.workflow_admin.edit_workflow_definition"));
100     }
101 
102     protected void deleteWorkflowDefinition(ActionRequest actionRequest)
103         throws Exception {
104 
105         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
106             WebKeys.THEME_DISPLAY);
107 
108         String name = ParamUtil.getString(actionRequest, "name");
109         int version = ParamUtil.getInteger(actionRequest, "version");
110 
111         WorkflowDefinitionManagerUtil.undeployWorkflowDefinition(
112             themeDisplay.getCompanyId(), themeDisplay.getUserId(),
113             name, version);
114     }
115 
116     protected void updateWorkflowDefinition(ActionRequest actionRequest)
117         throws Exception {
118 
119         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
120             actionRequest);
121 
122         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
123             WebKeys.THEME_DISPLAY);
124 
125         String name = ParamUtil.getString(actionRequest, "name");
126         File file = uploadRequest.getFile("file");
127 
128         WorkflowDefinition workflowDefinition =
129             WorkflowDefinitionManagerUtil.deployWorkflowDefinition(
130                 themeDisplay.getCompanyId(), themeDisplay.getUserId(),
131                 name, new FileInputStream(file));
132 
133         actionRequest.setAttribute(
134             WebKeys.WORKFLOW_DEFINITION, workflowDefinition);
135     }
136 
137 }