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.workflowdefinitionlinks.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   * @author Juan Fernández
48   */
49  public class EditWorkflowDefinitionLinkAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          try {
57              updateWorkflowDefinitionLinks(actionRequest);
58  
59              sendRedirect(actionRequest, actionResponse);
60          }
61          catch (Exception e) {
62              if (e instanceof WorkflowException) {
63                  SessionErrors.add(actionRequest, e.getClass().getName());
64  
65                  setForward(
66                      actionRequest, "portlet.workflow_definition_links.error");
67              }
68              else {
69                  throw e;
70              }
71          }
72      }
73  
74      public ActionForward render(
75              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
76              RenderRequest renderRequest, RenderResponse renderResponse)
77          throws Exception {
78  
79          return mapping.findForward(getForward(
80              renderRequest, "portlet.workflow_definition_links.view"));
81      }
82  
83      protected void updateWorkflowDefinitionLink(
84              ActionRequest actionRequest, String className, String value)
85          throws Exception {
86  
87          long groupId = ParamUtil.getLong(actionRequest, "groupId");
88  
89          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
90              WebKeys.THEME_DISPLAY);
91  
92          if (Validator.isNull(value)) {
93              WorkflowDefinitionLinkLocalServiceUtil.deleteWorkflowDefinitionLink(
94                  themeDisplay.getCompanyId(), groupId, className);
95          }
96          else {
97              String[] values = StringUtil.split(value, StringPool.AT);
98  
99              String workflowDefinitionName = values[0];
100             int workflowDefinitionVersion = GetterUtil.getInteger(
101                 values[1]);
102 
103             WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
104                 themeDisplay.getUserId(), themeDisplay.getCompanyId(),
105                 groupId, className, workflowDefinitionName,
106                 workflowDefinitionVersion);
107         }
108     }
109 
110     protected void updateWorkflowDefinitionLinks(ActionRequest actionRequest)
111         throws Exception {
112 
113         Enumeration<String> enu = actionRequest.getParameterNames();
114 
115         while (enu.hasMoreElements()) {
116             String name = enu.nextElement();
117 
118             if (!name.startsWith(_PREFIX)) {
119                 continue;
120             }
121 
122             String className = name.substring(_PREFIX.length(), name.length());
123             String value = ParamUtil.getString(actionRequest, name);
124 
125             updateWorkflowDefinitionLink(actionRequest, className, value);
126         }
127     }
128 
129     private static final String _PREFIX = "workflowDefinitionName@";
130 
131 }