1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.portletconfiguration.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.model.Layout;
20  import com.liferay.portal.model.Portlet;
21  import com.liferay.portal.security.auth.PrincipalException;
22  import com.liferay.portal.theme.ThemeDisplay;
23  import com.liferay.portal.util.WebKeys;
24  import com.liferay.portlet.PortletPreferencesFactoryUtil;
25  
26  import java.util.Set;
27  
28  import javax.portlet.ActionRequest;
29  import javax.portlet.ActionResponse;
30  import javax.portlet.PortletConfig;
31  import javax.portlet.PortletPreferences;
32  import javax.portlet.RenderRequest;
33  import javax.portlet.RenderResponse;
34  
35  import org.apache.struts.action.ActionForm;
36  import org.apache.struts.action.ActionForward;
37  import org.apache.struts.action.ActionMapping;
38  
39  /**
40   * <a href="EditSupportedClientsAction.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class EditSupportedClientsAction extends EditConfigurationAction {
45  
46      public void processAction(
47              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
48              ActionRequest actionRequest, ActionResponse actionResponse)
49          throws Exception {
50  
51          Portlet portlet = null;
52  
53          try {
54              portlet = getPortlet(actionRequest);
55          }
56          catch (PrincipalException pe) {
57              SessionErrors.add(
58                  actionRequest, PrincipalException.class.getName());
59  
60              setForward(actionRequest, "portlet.portlet_configuration.error");
61          }
62  
63          updateSupportedClients(portlet, actionRequest);
64  
65          sendRedirect(actionRequest, actionResponse);
66      }
67  
68      public ActionForward render(
69              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
70              RenderRequest renderRequest, RenderResponse renderResponse)
71          throws Exception {
72  
73          Portlet portlet = null;
74  
75          try {
76              portlet = getPortlet(renderRequest);
77          }
78          catch (PrincipalException pe) {
79              SessionErrors.add(
80                  renderRequest, PrincipalException.class.getName());
81  
82              return mapping.findForward("portlet.portlet_configuration.error");
83          }
84  
85          renderResponse.setTitle(getTitle(portlet, renderRequest));
86  
87          return mapping.findForward(getForward(
88              renderRequest,
89              "portlet.portlet_configuration.edit_supported_clients"));
90      }
91  
92      protected void updateSupportedClients(
93              Portlet portlet, ActionRequest actionRequest)
94          throws Exception {
95  
96          ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
97              WebKeys.THEME_DISPLAY);
98  
99          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                 "portlet-setup-supported-clients-mobile-devices-" + 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 }