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.util.GetterUtil;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.kernel.workflow.WorkflowException;
24  import com.liferay.portal.service.WorkflowDefinitionLinkLocalServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.theme.ThemeDisplay;
27  import com.liferay.portal.util.WebKeys;
28  
29  import java.util.Enumeration;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="EditWorkflowDefinitionLinkAction.java.html"><b><i>View Source</i>
43   * </b></a>
44   *
45   * @author Jorge Ferrer
46   * @author Bruno Farache
47   */
48  public class EditWorkflowDefinitionLinkAction extends PortletAction {
49  
50      public void processAction(
51              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws Exception {
54  
55          try {
56              updateWorkflowDefinitionLinks(actionRequest);
57  
58              sendRedirect(actionRequest, actionResponse);
59          }
60          catch (Exception e) {
61              if (e instanceof WorkflowException) {
62                  SessionErrors.add(actionRequest, e.getClass().getName());
63  
64                  setForward(actionRequest, "portlet.workflow_admin.error");
65              }
66              else {
67                  throw e;
68              }
69          }
70      }
71  
72      public ActionForward render(
73              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
74              RenderRequest renderRequest, RenderResponse renderResponse)
75          throws Exception {
76  
77          return mapping.findForward(getForward(
78              renderRequest, "portlet.workflow_admin.view"));
79      }
80  
81      protected void updateWorkflowDefinitionLink(
82              ThemeDisplay themeDisplay, String className, String value)
83          throws Exception {
84  
85          if (Validator.isNull(value)) {
86              WorkflowDefinitionLinkLocalServiceUtil.deleteWorkflowDefinitionLink(
87                  themeDisplay.getUserId(), themeDisplay.getCompanyId(), 0,
88                  className);
89          }
90          else {
91              String[] values = StringUtil.split(value, StringPool.AT);
92  
93              String workflowDefinitionName = values[0];
94              int workflowDefinitionVersion = GetterUtil.getInteger(
95                  values[1]);
96  
97              WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
98                  themeDisplay.getUserId(), themeDisplay.getCompanyId(), 0,
99                  className, workflowDefinitionName, workflowDefinitionVersion);
100         }
101     }
102 
103     protected void updateWorkflowDefinitionLinks(ActionRequest actionRequest)
104         throws Exception {
105 
106         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
107             WebKeys.THEME_DISPLAY);
108 
109         Enumeration<String> enu = actionRequest.getParameterNames();
110 
111         while (enu.hasMoreElements()) {
112             String name = enu.nextElement();
113 
114             if (!name.startsWith(_PREFIX)) {
115                 continue;
116             }
117 
118             String className = name.substring(_PREFIX.length(), name.length());
119             String value = ParamUtil.getString(actionRequest, name);
120 
121             updateWorkflowDefinitionLink(themeDisplay, className, value);
122         }
123     }
124 
125     private static final String _PREFIX = "workflowDefinitionName@";
126 
127 }