001    /**
002     * Copyright (c) 2000-2013 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.pluginsadmin.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            @Override
047            public void processAction(
048                            ActionMapping actionMapping, ActionForm actionForm,
049                            PortletConfig portletConfig, ActionRequest actionRequest,
050                            ActionResponse actionResponse)
051                    throws Exception {
052    
053                    try {
054                            updatePluginSetting(actionRequest);
055    
056                            sendRedirect(actionRequest, actionResponse);
057                    }
058                    catch (Exception e) {
059                            if (e instanceof PrincipalException) {
060                                    SessionErrors.add(actionRequest, e.getClass());
061    
062                                    setForward(actionRequest, "portlet.plugins_admin.error");
063                            }
064                            else {
065                                    throw e;
066                            }
067                    }
068            }
069    
070            @Override
071            public ActionForward render(
072                            ActionMapping actionMapping, ActionForm actionForm,
073                            PortletConfig portletConfig, RenderRequest renderRequest,
074                            RenderResponse renderResponse)
075                    throws Exception {
076    
077                    return actionMapping.findForward(
078                            getForward(renderRequest, "portlet.plugins_admin.edit_plugin"));
079            }
080    
081            protected void updatePluginSetting(ActionRequest actionRequest)
082                    throws Exception {
083    
084                    long companyId = PortalUtil.getCompanyId(actionRequest);
085                    String pluginId = ParamUtil.getString(actionRequest, "pluginId");
086                    String pluginType = ParamUtil.getString(actionRequest, "pluginType");
087    
088                    String[] rolesArray = StringUtil.split(
089                            ParamUtil.getString(actionRequest, "roles"), '\n');
090    
091                    Arrays.sort(rolesArray);
092    
093                    String roles = StringUtil.merge(rolesArray);
094    
095                    boolean active = ParamUtil.getBoolean(actionRequest, "active");
096    
097                    if (pluginType.equals(Plugin.TYPE_PORTLET)) {
098                            String portletId = pluginId;
099    
100                            PortletServiceUtil.updatePortlet(
101                                    companyId, portletId, StringPool.BLANK, active);
102                    }
103                    else {
104                            PluginSettingServiceUtil.updatePluginSetting(
105                                    companyId, pluginId, pluginType, roles, active);
106                    }
107            }
108    
109    }