1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.portletconfiguration.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.model.Layout;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.theme.ThemeDisplay;
31  import com.liferay.portal.util.WebKeys;
32  import com.liferay.portlet.PortletPreferencesFactoryUtil;
33  
34  import java.util.Set;
35  
36  import javax.portlet.ActionRequest;
37  import javax.portlet.ActionResponse;
38  import javax.portlet.PortletConfig;
39  import javax.portlet.PortletPreferences;
40  import javax.portlet.RenderRequest;
41  import javax.portlet.RenderResponse;
42  
43  import org.apache.struts.action.ActionForm;
44  import org.apache.struts.action.ActionForward;
45  import org.apache.struts.action.ActionMapping;
46  
47  /**
48   * <a href="EditSupportedClientsAction.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class EditSupportedClientsAction extends EditConfigurationAction {
54  
55      public void processAction(
56              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
57              ActionRequest actionRequest, ActionResponse actionResponse)
58          throws Exception {
59  
60          Portlet portlet = null;
61  
62          try {
63              portlet = getPortlet(actionRequest);
64          }
65          catch (PrincipalException pe) {
66              SessionErrors.add(
67                  actionRequest, PrincipalException.class.getName());
68  
69              setForward(actionRequest, "portlet.portlet_configuration.error");
70          }
71  
72          updateSupportedClients(portlet, actionRequest);
73  
74          sendRedirect(actionRequest, actionResponse);
75      }
76  
77      public ActionForward render(
78              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
79              RenderRequest renderRequest, RenderResponse renderResponse)
80          throws Exception {
81  
82          Portlet portlet = null;
83  
84          try {
85              portlet = getPortlet(renderRequest);
86          }
87          catch (PrincipalException pe) {
88              SessionErrors.add(
89                  renderRequest, PrincipalException.class.getName());
90  
91              return mapping.findForward("portlet.portlet_configuration.error");
92          }
93  
94          renderResponse.setTitle(getTitle(portlet, renderRequest));
95  
96          return mapping.findForward(getForward(
97              renderRequest,
98              "portlet.portlet_configuration.edit_supported_clients"));
99      }
100 
101     protected void updateSupportedClients(
102             Portlet portlet, ActionRequest actionRequest)
103         throws Exception {
104 
105         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
106             WebKeys.THEME_DISPLAY);
107 
108         Layout layout = themeDisplay.getLayout();
109 
110         PortletPreferences portletSetup =
111             PortletPreferencesFactoryUtil.getLayoutPortletSetup(
112                 layout, portlet.getPortletId());
113 
114         Set<String> allPortletModes = portlet.getAllPortletModes();
115 
116         for (String portletMode : allPortletModes) {
117             String mobileDevicesParam =
118                 "portlet-setup-supported-clients-mobile-devices-" + portletMode;
119 
120             boolean mobileDevices = ParamUtil.getBoolean(
121                 actionRequest, mobileDevicesParam);
122 
123             portletSetup.setValue(
124                 mobileDevicesParam, String.valueOf(mobileDevices));
125         }
126 
127         portletSetup.store();
128     }
129 
130 }