001    /**
002     * Copyright (c) 2000-2012 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.portletconfiguration.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.model.Layout;
020    import com.liferay.portal.model.Portlet;
021    import com.liferay.portal.security.auth.PrincipalException;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portal.util.WebKeys;
024    import com.liferay.portlet.PortletPreferencesFactoryUtil;
025    
026    import java.util.Set;
027    
028    import javax.portlet.ActionRequest;
029    import javax.portlet.ActionResponse;
030    import javax.portlet.PortletConfig;
031    import javax.portlet.PortletPreferences;
032    import javax.portlet.RenderRequest;
033    import javax.portlet.RenderResponse;
034    
035    import org.apache.struts.action.ActionForm;
036    import org.apache.struts.action.ActionForward;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     */
042    public class EditSupportedClientsAction extends EditConfigurationAction {
043    
044            @Override
045            public void processAction(
046                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
047                            ActionRequest actionRequest, ActionResponse actionResponse)
048                    throws Exception {
049    
050                    Portlet portlet = null;
051    
052                    try {
053                            portlet = getPortlet(actionRequest);
054                    }
055                    catch (PrincipalException pe) {
056                            SessionErrors.add(
057                                    actionRequest, PrincipalException.class.getName());
058    
059                            setForward(actionRequest, "portlet.portlet_configuration.error");
060                    }
061    
062                    updateSupportedClients(portlet, actionRequest);
063    
064                    sendRedirect(actionRequest, actionResponse);
065            }
066    
067            @Override
068            public ActionForward render(
069                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
070                            RenderRequest renderRequest, RenderResponse renderResponse)
071                    throws Exception {
072    
073                    Portlet portlet = null;
074    
075                    try {
076                            portlet = getPortlet(renderRequest);
077                    }
078                    catch (PrincipalException pe) {
079                            SessionErrors.add(
080                                    renderRequest, PrincipalException.class.getName());
081    
082                            return mapping.findForward("portlet.portlet_configuration.error");
083                    }
084    
085                    renderResponse.setTitle(getTitle(portlet, renderRequest));
086    
087                    return mapping.findForward(
088                            getForward(
089                                    renderRequest,
090                                    "portlet.portlet_configuration.edit_supported_clients"));
091            }
092    
093            protected void updateSupportedClients(
094                            Portlet portlet, ActionRequest actionRequest)
095                    throws Exception {
096    
097                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
098                            WebKeys.THEME_DISPLAY);
099    
100                    Layout layout = themeDisplay.getLayout();
101    
102                    PortletPreferences portletSetup =
103                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
104                                    layout, portlet.getPortletId());
105    
106                    Set<String> allPortletModes = portlet.getAllPortletModes();
107    
108                    for (String portletMode : allPortletModes) {
109                            String mobileDevicesParam =
110                                    "portletSetupSupportedClientsMobileDevices_" + portletMode;
111    
112                            boolean mobileDevices = ParamUtil.getBoolean(
113                                    actionRequest, mobileDevicesParam);
114    
115                            portletSetup.setValue(
116                                    mobileDevicesParam, String.valueOf(mobileDevices));
117                    }
118    
119                    portletSetup.store();
120            }
121    
122    }