001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.enterpriseadmin.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.model.Plugin;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.PluginSettingServiceUtil;
024    import com.liferay.portal.service.PortletServiceUtil;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.util.Arrays;
029    
030    import javax.portlet.ActionRequest;
031    import javax.portlet.ActionResponse;
032    import javax.portlet.PortletConfig;
033    import javax.portlet.RenderRequest;
034    import javax.portlet.RenderResponse;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionForward;
038    import org.apache.struts.action.ActionMapping;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Jorge Ferrer
043     */
044    public class EditPluginAction extends PortletAction {
045    
046            public void processAction(
047                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
048                            ActionRequest actionRequest, ActionResponse actionResponse)
049                    throws Exception {
050    
051                    try {
052                            updatePluginSetting(actionRequest);
053    
054                            sendRedirect(actionRequest, actionResponse);
055                    }
056                    catch (Exception e) {
057                            if (e instanceof PrincipalException) {
058                                    SessionErrors.add(actionRequest, e.getClass().getName());
059    
060                                    setForward(actionRequest, "portlet.enterprise_admin.error");
061                            }
062                            else {
063                                    throw e;
064                            }
065                    }
066            }
067    
068            public ActionForward render(
069                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
070                            RenderRequest renderRequest, RenderResponse renderResponse)
071                    throws Exception {
072    
073                    return mapping.findForward(
074                            getForward(renderRequest, "portlet.enterprise_admin.edit_plugin"));
075            }
076    
077            protected void updatePluginSetting(ActionRequest actionRequest)
078                    throws Exception {
079    
080                    long companyId = PortalUtil.getCompanyId(actionRequest);
081                    String pluginId = ParamUtil.getString(actionRequest, "pluginId");
082                    String pluginType = ParamUtil.getString(actionRequest, "pluginType");
083    
084                    String[] rolesArray = StringUtil.split(
085                            ParamUtil.getString(actionRequest, "roles"), "\n");
086    
087                    Arrays.sort(rolesArray);
088    
089                    String roles = StringUtil.merge(rolesArray);
090    
091                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
092    
093                    if (pluginType.equals(Plugin.TYPE_PORTLET)) {
094                            String portletId = pluginId;
095    
096                            PortletServiceUtil.updatePortlet(
097                                    companyId, portletId, StringPool.BLANK, active);
098                    }
099                    else {
100                            PluginSettingServiceUtil.updatePluginSetting(
101                                    companyId, pluginId, pluginType, roles, active);
102                    }
103            }
104    
105    }