1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.enterpriseadmin.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.model.Plugin;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.PluginSettingServiceUtil;
31  import com.liferay.portal.service.PortletServiceUtil;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.util.PortalUtil;
34  
35  import java.util.Arrays;
36  
37  import javax.portlet.ActionRequest;
38  import javax.portlet.ActionResponse;
39  import javax.portlet.PortletConfig;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditPluginAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   * @author Jorge Ferrer
52   *
53   */
54  public class EditPluginAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          try {
62              updatePluginSetting(actionRequest);
63  
64              sendRedirect(actionRequest, actionResponse);
65          }
66          catch (Exception e) {
67              if (e instanceof PrincipalException) {
68                  SessionErrors.add(actionRequest, e.getClass().getName());
69  
70                  setForward(actionRequest, "portlet.enterprise_admin.error");
71              }
72              else {
73                  throw e;
74              }
75          }
76      }
77  
78      public ActionForward render(
79              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
80              RenderRequest renderRequest, RenderResponse renderResponse)
81          throws Exception {
82  
83          return mapping.findForward(
84              getForward(renderRequest, "portlet.enterprise_admin.edit_plugin"));
85      }
86  
87      protected void updatePluginSetting(ActionRequest actionRequest)
88          throws Exception {
89  
90          long companyId = PortalUtil.getCompanyId(actionRequest);
91          String pluginId = ParamUtil.getString(actionRequest, "pluginId");
92          String pluginType = ParamUtil.getString(actionRequest, "pluginType");
93  
94          String[] rolesArray = StringUtil.split(
95              ParamUtil.getString(actionRequest, "roles"), "\n");
96  
97          Arrays.sort(rolesArray);
98  
99          String roles = StringUtil.merge(rolesArray);
100 
101         boolean active = ParamUtil.getBoolean(actionRequest, "active");
102 
103         if (pluginType.equals(Plugin.TYPE_PORTLET)) {
104             String portletId = pluginId;
105 
106             PortletServiceUtil.updatePortlet(
107                 companyId, portletId, roles, active);
108         }
109         else {
110             PluginSettingServiceUtil.updatePluginSetting(
111                 companyId, pluginId, pluginType, roles, active);
112         }
113     }
114 
115 }