1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditSupportedClientsAction extends EditConfigurationAction {
53  
54      public void processAction(
55              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
56              ActionRequest actionRequest, ActionResponse actionResponse)
57          throws Exception {
58  
59          Portlet portlet = null;
60  
61          try {
62              portlet = getPortlet(actionRequest);
63          }
64          catch (PrincipalException pe) {
65              SessionErrors.add(
66                  actionRequest, PrincipalException.class.getName());
67  
68              setForward(actionRequest, "portlet.portlet_configuration.error");
69          }
70  
71          updateSupportedClients(portlet, actionRequest);
72  
73          sendRedirect(actionRequest, actionResponse);
74      }
75  
76      public ActionForward render(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              RenderRequest renderRequest, RenderResponse renderResponse)
79          throws Exception {
80  
81          Portlet portlet = null;
82  
83          try {
84              portlet = getPortlet(renderRequest);
85          }
86          catch (PrincipalException pe) {
87              SessionErrors.add(
88                  renderRequest, PrincipalException.class.getName());
89  
90              return mapping.findForward("portlet.portlet_configuration.error");
91          }
92  
93          renderResponse.setTitle(getTitle(portlet, renderRequest));
94  
95          return mapping.findForward(getForward(
96              renderRequest,
97              "portlet.portlet_configuration.edit_supported_clients"));
98      }
99  
100     protected void updateSupportedClients(
101             Portlet portlet, ActionRequest actionRequest)
102         throws Exception {
103 
104         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
105             WebKeys.THEME_DISPLAY);
106 
107         Layout layout = themeDisplay.getLayout();
108 
109         PortletPreferences portletSetup =
110             PortletPreferencesFactoryUtil.getLayoutPortletSetup(
111                 layout, portlet.getPortletId());
112 
113         Set<String> allPortletModes = portlet.getAllPortletModes();
114 
115         for (String portletMode : allPortletModes) {
116             String mobileDevicesParam =
117                 "portlet-setup-supported-clients-mobile-devices-" + portletMode;
118 
119             boolean mobileDevices = ParamUtil.getBoolean(
120                 actionRequest, mobileDevicesParam);
121 
122             portletSetup.setValue(
123                 mobileDevicesParam, String.valueOf(mobileDevices));
124         }
125 
126         portletSetup.store();
127     }
128 
129 }