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