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