1   /**
2    * Copyright (c) 2000-2007 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.security.permission.ActionKeys;
26  import com.liferay.portal.kernel.security.permission.PermissionChecker;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.model.Portlet;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.PortletLocalServiceUtil;
31  import com.liferay.portal.service.permission.PortletPermissionUtil;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.PortletPreferencesFactoryUtil;
36  import com.liferay.util.servlet.SessionErrors;
37  
38  import java.util.Iterator;
39  import java.util.Set;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.PortletPreferences;
45  import javax.portlet.PortletRequest;
46  import javax.portlet.RenderRequest;
47  import javax.portlet.RenderResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="EditSupportedClientsAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Brian Wing Shun Chan
57   *
58   */
59  public class EditSupportedClientsAction extends PortletAction {
60  
61      public void processAction(
62              ActionMapping mapping, ActionForm form, PortletConfig config,
63              ActionRequest req, ActionResponse res)
64          throws Exception {
65  
66          try {
67              checkPermissions(req);
68          }
69          catch (PrincipalException pe) {
70              return;
71          }
72  
73          updateSupportedClients(req);
74  
75          String redirect = ParamUtil.getString(req, "supportedClientsRedirect");
76  
77          sendRedirect(req, res, redirect);
78      }
79  
80      public ActionForward render(
81              ActionMapping mapping, ActionForm form, PortletConfig config,
82              RenderRequest req, RenderResponse res)
83          throws Exception {
84  
85          try {
86              checkPermissions(req);
87          }
88          catch (PrincipalException pe) {
89              SessionErrors.add(req, PrincipalException.class.getName());
90  
91              return mapping.findForward("portlet.portlet_configuration.error");
92          }
93  
94          return mapping.findForward(
95              "portlet.portlet_configuration.edit_supported_clients");
96      }
97  
98      protected void checkPermissions(PortletRequest req) throws Exception {
99          ThemeDisplay themeDisplay =
100             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
101 
102         PermissionChecker permissionChecker =
103             themeDisplay.getPermissionChecker();
104 
105         String portletId = ParamUtil.getString(req, "portletResource");
106 
107         if (!PortletPermissionUtil.contains(
108                 permissionChecker, themeDisplay.getPlid(), portletId,
109                 ActionKeys.CONFIGURATION)) {
110 
111             throw new PrincipalException();
112         }
113     }
114 
115     protected void updateSupportedClients(ActionRequest req)
116         throws Exception {
117 
118         ThemeDisplay themeDisplay =
119             (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
120 
121         String portletResource = ParamUtil.getString(req, "portletResource");
122 
123         PortletPreferences portletSetup =
124             PortletPreferencesFactoryUtil.getPortletSetup(
125                 req, portletResource, true, true);
126 
127         Portlet portlet = PortletLocalServiceUtil.getPortletById(
128             themeDisplay.getCompanyId(), portletResource);
129 
130         Set allPortletModes = portlet.getAllPortletModes();
131 
132         Iterator itr = allPortletModes.iterator();
133 
134         while (itr.hasNext()) {
135             String portletMode = (String)itr.next();
136 
137             String mobileDevicesParam =
138                 "portlet-setup-supported-clients-mobile-devices-" + portletMode;
139 
140             boolean mobileDevices = ParamUtil.getBoolean(
141                 req, mobileDevicesParam);
142 
143             portletSetup.setValue(
144                 mobileDevicesParam, String.valueOf(mobileDevices));
145         }
146 
147         portletSetup.store();
148     }
149 
150 }