1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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.StringUtil;
20  import com.liferay.portal.model.Plugin;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.service.PluginSettingServiceUtil;
23  import com.liferay.portal.service.PortletServiceUtil;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.portal.util.PortalUtil;
26  
27  import java.util.Arrays;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  
39  /**
40   * <a href="EditPluginAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Jorge Ferrer
44   */
45  public class EditPluginAction extends PortletAction {
46  
47      public void processAction(
48              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
49              ActionRequest actionRequest, ActionResponse actionResponse)
50          throws Exception {
51  
52          try {
53              updatePluginSetting(actionRequest);
54  
55              sendRedirect(actionRequest, actionResponse);
56          }
57          catch (Exception e) {
58              if (e instanceof PrincipalException) {
59                  SessionErrors.add(actionRequest, e.getClass().getName());
60  
61                  setForward(actionRequest, "portlet.enterprise_admin.error");
62              }
63              else {
64                  throw e;
65              }
66          }
67      }
68  
69      public ActionForward render(
70              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
71              RenderRequest renderRequest, RenderResponse renderResponse)
72          throws Exception {
73  
74          return mapping.findForward(
75              getForward(renderRequest, "portlet.enterprise_admin.edit_plugin"));
76      }
77  
78      protected void updatePluginSetting(ActionRequest actionRequest)
79          throws Exception {
80  
81          long companyId = PortalUtil.getCompanyId(actionRequest);
82          String pluginId = ParamUtil.getString(actionRequest, "pluginId");
83          String pluginType = ParamUtil.getString(actionRequest, "pluginType");
84  
85          String[] rolesArray = StringUtil.split(
86              ParamUtil.getString(actionRequest, "roles"), "\n");
87  
88          Arrays.sort(rolesArray);
89  
90          String roles = StringUtil.merge(rolesArray);
91  
92          boolean active = ParamUtil.getBoolean(actionRequest, "active");
93  
94          if (pluginType.equals(Plugin.TYPE_PORTLET)) {
95              String portletId = pluginId;
96  
97              PortletServiceUtil.updatePortlet(
98                  companyId, portletId, roles, active);
99          }
100         else {
101             PluginSettingServiceUtil.updatePluginSetting(
102                 companyId, pluginId, pluginType, roles, active);
103         }
104     }
105 
106 }