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.enterpriseadmin.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.kernel.util.StringPool;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.model.Plugin;
22  import com.liferay.portal.security.auth.PrincipalException;
23  import com.liferay.portal.service.PluginSettingServiceUtil;
24  import com.liferay.portal.service.PortletServiceUtil;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portal.util.PortalUtil;
27  
28  import java.util.Arrays;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="EditPluginAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Jorge Ferrer
45   */
46  public class EditPluginAction extends PortletAction {
47  
48      public void processAction(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              ActionRequest actionRequest, ActionResponse actionResponse)
51          throws Exception {
52  
53          try {
54              updatePluginSetting(actionRequest);
55  
56              sendRedirect(actionRequest, actionResponse);
57          }
58          catch (Exception e) {
59              if (e instanceof PrincipalException) {
60                  SessionErrors.add(actionRequest, e.getClass().getName());
61  
62                  setForward(actionRequest, "portlet.enterprise_admin.error");
63              }
64              else {
65                  throw e;
66              }
67          }
68      }
69  
70      public ActionForward render(
71              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
72              RenderRequest renderRequest, RenderResponse renderResponse)
73          throws Exception {
74  
75          return mapping.findForward(
76              getForward(renderRequest, "portlet.enterprise_admin.edit_plugin"));
77      }
78  
79      protected void updatePluginSetting(ActionRequest actionRequest)
80          throws Exception {
81  
82          long companyId = PortalUtil.getCompanyId(actionRequest);
83          String pluginId = ParamUtil.getString(actionRequest, "pluginId");
84          String pluginType = ParamUtil.getString(actionRequest, "pluginType");
85  
86          String[] rolesArray = StringUtil.split(
87              ParamUtil.getString(actionRequest, "roles"), "\n");
88  
89          Arrays.sort(rolesArray);
90  
91          String roles = StringUtil.merge(rolesArray);
92  
93          boolean active = ParamUtil.getBoolean(actionRequest, "active");
94  
95          if (pluginType.equals(Plugin.TYPE_PORTLET)) {
96              String portletId = pluginId;
97  
98              PortletServiceUtil.updatePortlet(
99                  companyId, portletId, StringPool.BLANK, active);
100         }
101         else {
102             PluginSettingServiceUtil.updatePluginSetting(
103                 companyId, pluginId, pluginType, roles, active);
104         }
105     }
106 
107 }