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.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            public void processAction(
045                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
046                            ActionRequest actionRequest, ActionResponse actionResponse)
047                    throws Exception {
048    
049                    Portlet portlet = null;
050    
051                    try {
052                            portlet = getPortlet(actionRequest);
053                    }
054                    catch (PrincipalException pe) {
055                            SessionErrors.add(
056                                    actionRequest, PrincipalException.class.getName());
057    
058                            setForward(actionRequest, "portlet.portlet_configuration.error");
059                    }
060    
061                    updateSupportedClients(portlet, actionRequest);
062    
063                    sendRedirect(actionRequest, actionResponse);
064            }
065    
066            public ActionForward render(
067                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
068                            RenderRequest renderRequest, RenderResponse renderResponse)
069                    throws Exception {
070    
071                    Portlet portlet = null;
072    
073                    try {
074                            portlet = getPortlet(renderRequest);
075                    }
076                    catch (PrincipalException pe) {
077                            SessionErrors.add(
078                                    renderRequest, PrincipalException.class.getName());
079    
080                            return mapping.findForward("portlet.portlet_configuration.error");
081                    }
082    
083                    renderResponse.setTitle(getTitle(portlet, renderRequest));
084    
085                    return mapping.findForward(getForward(
086                            renderRequest,
087                            "portlet.portlet_configuration.edit_supported_clients"));
088            }
089    
090            protected void updateSupportedClients(
091                            Portlet portlet, ActionRequest actionRequest)
092                    throws Exception {
093    
094                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
095                            WebKeys.THEME_DISPLAY);
096    
097                    Layout layout = themeDisplay.getLayout();
098    
099                    PortletPreferences portletSetup =
100                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
101                                    layout, portlet.getPortletId());
102    
103                    Set<String> allPortletModes = portlet.getAllPortletModes();
104    
105                    for (String portletMode : allPortletModes) {
106                            String mobileDevicesParam =
107                                    "portlet-setup-supported-clients-mobile-devices-" + portletMode;
108    
109                            boolean mobileDevices = ParamUtil.getBoolean(
110                                    actionRequest, mobileDevicesParam);
111    
112                            portletSetup.setValue(
113                                    mobileDevicesParam, String.valueOf(mobileDevices));
114                    }
115    
116                    portletSetup.store();
117            }
118    
119    }