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(getForward(
088                            renderRequest,
089                            "portlet.portlet_configuration.edit_supported_clients"));
090            }
091    
092            protected void updateSupportedClients(
093                            Portlet portlet, ActionRequest actionRequest)
094                    throws Exception {
095    
096                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
097                            WebKeys.THEME_DISPLAY);
098    
099                    Layout layout = themeDisplay.getLayout();
100    
101                    PortletPreferences portletSetup =
102                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
103                                    layout, portlet.getPortletId());
104    
105                    Set<String> allPortletModes = portlet.getAllPortletModes();
106    
107                    for (String portletMode : allPortletModes) {
108                            String mobileDevicesParam =
109                                    "portletSetupSupportedClientsMobileDevices_" + portletMode;
110    
111                            boolean mobileDevices = ParamUtil.getBoolean(
112                                    actionRequest, mobileDevicesParam);
113    
114                            portletSetup.setValue(
115                                    mobileDevicesParam, String.valueOf(mobileDevices));
116                    }
117    
118                    portletSetup.store();
119            }
120    
121    }