1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.model.Plugin;
26  import com.liferay.portal.security.auth.PrincipalException;
27  import com.liferay.portal.service.PluginSettingServiceUtil;
28  import com.liferay.portal.service.PortletServiceUtil;
29  import com.liferay.portal.struts.PortletAction;
30  import com.liferay.portal.util.PortalUtil;
31  
32  import java.util.Arrays;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="EditPluginAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   * @author Jorge Ferrer
49   *
50   */
51  public class EditPluginAction extends PortletAction {
52  
53      public void processAction(
54              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
55              ActionRequest actionRequest, ActionResponse actionResponse)
56          throws Exception {
57  
58          try {
59              updatePluginSetting(actionRequest);
60  
61              sendRedirect(actionRequest, actionResponse);
62          }
63          catch (Exception e) {
64              if (e instanceof PrincipalException) {
65                  SessionErrors.add(actionRequest, e.getClass().getName());
66  
67                  setForward(actionRequest, "portlet.enterprise_admin.error");
68              }
69              else {
70                  throw e;
71              }
72          }
73      }
74  
75      public ActionForward render(
76              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
77              RenderRequest renderRequest, RenderResponse renderResponse)
78          throws Exception {
79  
80          return mapping.findForward(
81              getForward(renderRequest, "portlet.enterprise_admin.edit_plugin"));
82      }
83  
84      protected void updatePluginSetting(ActionRequest actionRequest)
85          throws Exception {
86  
87          long companyId = PortalUtil.getCompanyId(actionRequest);
88          String pluginId = ParamUtil.getString(actionRequest, "pluginId");
89          String pluginType = ParamUtil.getString(actionRequest, "pluginType");
90  
91          String[] rolesArray = StringUtil.split(
92              ParamUtil.getString(actionRequest, "roles"), "\n");
93  
94          Arrays.sort(rolesArray);
95  
96          String roles = StringUtil.merge(rolesArray);
97  
98          boolean active = ParamUtil.getBoolean(actionRequest, "active");
99  
100         if (pluginType.equals(Plugin.TYPE_PORTLET)) {
101             String portletId = pluginId;
102 
103             PortletServiceUtil.updatePortlet(
104                 companyId, portletId, roles, active);
105         }
106         else {
107             PluginSettingServiceUtil.updatePluginSetting(
108                 companyId, pluginId, pluginType, roles, active);
109         }
110     }
111 
112 }